From bbab6cdb9f82a383bcf1f027913325bba1fe8563 Mon Sep 17 00:00:00 2001 From: Nuwan Date: Mon, 2 Jan 2023 16:39:05 +0530 Subject: [PATCH] user recom email wip --- .bundle/config | 2 + ruby/lib/jam_ruby/lib/musicians_filter.rb | 178 ++++++++++++++++++ .../models/email_new_musician_match.rb | 9 + .../scheduled/new_musician_match_emailer.rb | 15 ++ web/Gemfile.alt | 2 + web/Gemfile.lock | 7 +- 6 files changed, 211 insertions(+), 2 deletions(-) create mode 100644 .bundle/config create mode 100644 ruby/lib/jam_ruby/lib/musicians_filter.rb create mode 100644 ruby/lib/jam_ruby/models/email_new_musician_match.rb create mode 100644 ruby/lib/jam_ruby/resque/scheduled/new_musician_match_emailer.rb diff --git a/.bundle/config b/.bundle/config new file mode 100644 index 000000000..173bf61e4 --- /dev/null +++ b/.bundle/config @@ -0,0 +1,2 @@ +BUNDLE_GEMFILE=Gemfile.alt +BUNDLE_RUBY diff --git a/ruby/lib/jam_ruby/lib/musicians_filter.rb b/ruby/lib/jam_ruby/lib/musicians_filter.rb new file mode 100644 index 000000000..eccf6cafe --- /dev/null +++ b/ruby/lib/jam_ruby/lib/musicians_filter.rb @@ -0,0 +1,178 @@ +module JamRuby + class MusiciansFilter + + LATENCY_SCORES = { + good: { label: 'GOOD', min: 0, max: 40 }, + fair: { label: 'FAIR', min: 40, max: 60 }, + high: { label: 'HIGH', min: 60, max: 10000000 }, + me: { label: 'ME', min: -1, max: -1 }, + unknown: { label: 'UNKNOWN', min: -2, max: -2 } + }; + + #ATTN: Rails.application.config is out side to the JamRuby module. Is it a good decision to use + #application confis here? + def self.users_latency_data(user_obj, remote_ip, latency_good, latency_fair, latency_high, filter_opts, offset, limit) + + latency_data = [] + + filter_latency_url = "#{Rails.application.config.latency_data_host}/search_users" + + uri = URI(filter_latency_url) + begin + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true if Rails.application.config.latency_data_host.start_with?("https://") + req = Net::HTTP::Post.new(uri) + req["Authorization"] = "Basic #{Rails.application.config.latency_data_host_auth_code}" + req["Content-Type"] = "application/json" + + req_params = { + my_user_id: user_obj.id, + my_public_ip: remote_ip, + my_device_id: nil, + my_client_id: nil, + from_location: filter_opts[:from_location] || "0", + offset: offset, + limit: limit + } + + req_params.merge!(instruments: filter_opts[:instruments]) if filter_opts[:instruments] + req_params.merge!(genres: filter_opts[:genres]) if filter_opts[:genres] + req_params.merge!(joined_within_days: filter_opts[:joined_within_days]) if filter_opts[:joined_within_days] + req_params.merge!(active_within_days: filter_opts[:active_within_days]) if filter_opts[:active_within_days] + + req.body = req_params.to_json + + response = http.request(req) + + #debugger + + if response.is_a?(Net::HTTPOK) || response.is_a?(Net::HTTPSuccess) + json_body = JSON.parse(response.body) + graph_db_users = json_body['users'] + nextOffset = json_body['next'] + + if latency_good || latency_fair || latency_high + #fiter by latency params + graph_db_users.select! do |user| + total_latency = user["ars"]["total_latency"].to_f + (total_latency >= LATENCY_SCORES[:good][:min] && total_latency <= LATENCY_SCORES[:good][:max] && latency_good) || + (total_latency > LATENCY_SCORES[:fair][:min] && total_latency <= LATENCY_SCORES[:fair][:max] && latency_fair) || + (total_latency > LATENCY_SCORES[:high][:min] && latency_high) + end + end + + latency_data = graph_db_users.map { | user | + { + user_id: user["user_id"], + audio_latency: user["audio_latency"].to_f, + ars_total_latency: user["ars"]["total_latency"].to_f, + ars_internet_latency: user["ars"]["internet_latency"].to_f + } + }.uniq + + return {data: latency_data, next: nextOffset} + else + logger.debug("Latency response failed: #{response}") + Bugsnag.notify("LatencyResponseFailed") do |report| + report.severity = "faliure" + report.add_tab(:latency, { + user_id: user_obj.id, + name: user_obj.name, + params: params, + url: filter_latency_url, + code: response.code, + body: response.body, + }) + end + end + rescue => exception + raise exception + end + + latency_data + end + + def self.filter(params) + user_id = params[:user_id] + remote_ip = params[:remote_ip] + raise Exception("This query request should contain user_id and remote_ip") if user_id.blank? || remote_ip.blank? + + user = User.find(user_id) + + latency_good = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_good]) + latency_fair = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_fair]) + latency_high = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_high]) + offset = [params[:offset].to_i, 0].max + limit = [params[:limit].to_i, 20].max + filter_params = {} + + filter_params.merge!(from_location: params[:from_location] ? '1' : '0') + + genres = params[:genres] + filter_params.merge!(genres: genres) if genres + + beginner = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:proficiency_beginner]) + intermediate = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:proficiency_intermediate]) + expert = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:proficiency_expert]) + + proficiency_levels = [] + proficiency_levels.push(1) if beginner + proficiency_levels.push(2) if intermediate + proficiency_levels.push(3) if expert + + instruments = params[:instruments] + + #debugger + + if instruments && instruments.any? && proficiency_levels.any? + inst = [] + instruments.each do |ii| + proficiency_levels.each do |pl| + inst << { id: ii[:value], proficiency: pl} + end + end + filter_params.merge!(instruments: inst) + end + + filter_params.merge!(joined_within_days: params[:joined_within_days]) unless params[:joined_within_days].blank? + filter_params.merge!(active_within_days: params[:active_within_days]) unless params[:active_within_days].blank? + + @latency_data = [] + begin + + #bm = Benchmark.measure do + result = users_latency_data(user, remote_ip, latency_good, latency_fair, latency_high, filter_params, offset, limit) + @latency_data = result[:data] + @nextOffset = result[:next] + + user_ids = @latency_data.map{ |l_data| l_data[:user_id] } + #end + + # Bugsnag.notify("search_users_benchmark") do |report| + # report.severity = "info" + # report.add_tab(:benchmark, benchmark: bm.to_s) + # end if Rails.env.production? + + sobj = MusicianSearch.user_search_filter(user) + #@search = sobj.search_results_page(filter_params, page, user_ids) + #debugger + @search = sobj.user_search_results(user_ids) + + respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/filter' + + rescue => exception + logger.debug("Latency exception: #{exception.message}") + Bugsnag.notify(exception) do |report| + report.severity = "error" + report.add_tab(:latency, { + params: params, + user_id: user.id, + name: user.name, + url: filter_latency_url, + }) + end + render json: {}, status: 500 + end + end + end +end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/email_new_musician_match.rb b/ruby/lib/jam_ruby/models/email_new_musician_match.rb new file mode 100644 index 000000000..33530a3cf --- /dev/null +++ b/ruby/lib/jam_ruby/models/email_new_musician_match.rb @@ -0,0 +1,9 @@ +module JamRuby + class EmailNewMusicianMatch + + def self.send_new_musician + + end + + end +end diff --git a/ruby/lib/jam_ruby/resque/scheduled/new_musician_match_emailer.rb b/ruby/lib/jam_ruby/resque/scheduled/new_musician_match_emailer.rb new file mode 100644 index 000000000..d7d2c5fef --- /dev/null +++ b/ruby/lib/jam_ruby/resque/scheduled/new_musician_match_emailer.rb @@ -0,0 +1,15 @@ +module JamRuby + class NewMusicianMatchEmailer + extend Resque::Plugins::JamLonelyJob + + @queue = :scheduled_new_musician_match_emailer + @@log = Logging.logger[NewMusicianMatchEmailer] + + def self.perform + @@log.debug("waking up") + EmailNewMusicianMatch.send_new_musician + @@log.debug("done") + end + + end +end diff --git a/web/Gemfile.alt b/web/Gemfile.alt index 79046da10..9b573ea5b 100644 --- a/web/Gemfile.alt +++ b/web/Gemfile.alt @@ -182,6 +182,8 @@ end # gem 'rack-timeout' #end +gem 'ffi', '1.12.0' + group :development, :test do gem 'rspec-rails' #, require: "rspec/rails" #, '2.14.2' gem 'rspec-collection_matchers' diff --git a/web/Gemfile.lock b/web/Gemfile.lock index 1a150ba57..6dec7519e 100644 --- a/web/Gemfile.lock +++ b/web/Gemfile.lock @@ -517,7 +517,7 @@ GEM paypal-sdk-merchant-jk (1.118.1) paypal-sdk-core (~> 0.3.0) pdf-core (0.7.0) - pg (0.17.1) + pg (0.21.0) pg_array_parser (0.0.9) pleaserun (0.0.31) cabin (> 0) @@ -554,6 +554,8 @@ GEM rabl (0.13.1) activesupport (>= 2.3.14) rack (1.6.13) + rack-cors (1.0.6) + rack (>= 1.6.0) rack-oauth2 (1.12.0) activesupport attr_required @@ -864,7 +866,7 @@ DEPENDENCIES omniauth-stripe-connect omniauth-twitter paypal-sdk-merchant-jk (= 1.118.1) - pg (= 0.17.1) + pg (= 0.21.0) postgres-copy postgres_ext prawn-table @@ -873,6 +875,7 @@ DEPENDENCIES puma quiet_assets rabl (= 0.13.1) + rack-cors (~> 1.0, >= 1.0.6) rack-test rails (= 4.2.8) rails-assets-bluebird!