From 3e953b1b4c974c45ffdf76e6e6a7361f05005a44 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Sat, 9 Jan 2021 16:15:56 -0600 Subject: [PATCH] music session ratingsl --- ruby/Gemfile | 2 +- ruby/lib/jam_ruby.rb | 1 + ruby/lib/jam_ruby/elasticsearch.rb | 65 +++++++++++++++++++ ruby/spec/jam_ruby/lib/elasticsearch_spec.rb | 47 ++++++++++---- ruby/spec/support/utilities.rb | 3 + .../api_music_sessions_controller.rb | 3 + web/config/application.rb | 2 + 7 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 ruby/lib/jam_ruby/elasticsearch.rb diff --git a/ruby/Gemfile b/ruby/Gemfile index 495e27c22..fe842048a 100644 --- a/ruby/Gemfile +++ b/ruby/Gemfile @@ -62,7 +62,7 @@ gem 'stripe' gem 'zip-codes' gem 'icalendar' gem 'email_validator' -gem 'typhoeus' + gem 'elasticsearch' group :test do diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 02dd9f955..7d38d57f9 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -87,6 +87,7 @@ require "jam_ruby/resque/google_analytics_event" require "jam_ruby/resque/batch_email_job" require "jam_ruby/resque/long_running" require "jam_ruby/mq_router" +require "jam_ruby/elasticsearch" require "jam_ruby/recurly_client" require "jam_ruby/base_manager" require "jam_ruby/connection_manager" diff --git a/ruby/lib/jam_ruby/elasticsearch.rb b/ruby/lib/jam_ruby/elasticsearch.rb new file mode 100644 index 000000000..94e325918 --- /dev/null +++ b/ruby/lib/jam_ruby/elasticsearch.rb @@ -0,0 +1,65 @@ +require 'elasticsearch' +module JamRuby + class ElasticSearch + + # index names + MUSIC_SESSION_RATINGS = "music-session-ratings" + + def initialize() + @log = Logging.logger[self] + + @client = Elasticsearch::Client.new( + url: APP_CONFIG.elasticsearch_host, + retry_on_failure: 2, + request_timeout: 15, + log: Rails.env.development?, + ) + end + + def smash_indices + + index_settings = { number_of_shards: 1, number_of_replicas: 0 } + settings = { settings: { index: index_settings } } + + @client.indices.delete(index: MUSIC_SESSION_RATINGS, body: settings) + @client.indices.create(index: MUSIC_SESSION_RATINGS, body: settings) + end + + def session_ratings(music_session, current_user, original_body) + + begin + time = Time.now.utc.iso8601 + + my_data = original_body.delete('**me') + + original_body.each do |k, other_data| + begin + # we init passwordfield as a UUID, which is a bogus hash; so if we see UUId, we know retailer has no password yet + UUIDTools::UUID.parse(k) + + other_user = User.find(k) + + body = {me: my_data, other: other_data} + + body["@timestamp"] = Time.now.utc.iso8601 + body["my_email"] = current_user.email + body["my_user_id"] = current_user.id + body["other_email"] = other_user.email + body["other_user_id"] = other_user.id + body["music_session_id"] = music_session.id + @client.index(id: "#{music_session.id}-#{current_user.id}-#{other_user.id}", index: MUSIC_SESSION_RATINGS, body: body) + rescue ArgumentError + # that's ok ; there are non-uuid keys in here; we ignore those + end + end + + return true + rescue => e + @log.error("unable to parse session ratings #{e}") + return false + end + + end + + end +end diff --git a/ruby/spec/jam_ruby/lib/elasticsearch_spec.rb b/ruby/spec/jam_ruby/lib/elasticsearch_spec.rb index 121ec58a1..a4ff4d57c 100644 --- a/ruby/spec/jam_ruby/lib/elasticsearch_spec.rb +++ b/ruby/spec/jam_ruby/lib/elasticsearch_spec.rb @@ -474,22 +474,47 @@ string = %{ } describe "Elasticsearch"do + let(:me) { FactoryGirl.create(:user, email: 'estest@jamkazam.com') } + let(:user1) { FactoryGirl.create(:user) } + let(:user2) { FactoryGirl.create(:user) } + let(:user3) { FactoryGirl.create(:user) } + let(:music_session) { FactoryGirl.create(:music_session) } + it "should save to index" do - client = Elasticsearch::Client.new( - url: "http://support.jamkazam.com:9200", - retry_on_failure: 2, - request_timeout: 15, - adapter: :typhoeus, - log: Rails.env.development?, - ) - - index_settings = { number_of_shards: 1, number_of_replicas: 0 } settings = { settings: { index: index_settings } } - client.indices.create(index: "session_ratings", body: settings) + + # 06952d1b-1ba0-4d13-8e82-f5438e030d07 + # 65c57483-7605-4ee0-a754-4acb60e29d0b + # 8a7ceb38-6cdf-447d-bee3-89bc08644104 + + # fix the canned body above + body = JSON.parse(string) + + # overrwite real user IDs with test IDs + body[user1.id] = body.delete("06952d1b-1ba0-4d13-8e82-f5438e030d07") + body[user2.id] = body.delete("65c57483-7605-4ee0-a754-4acb60e29d0b") + body[user3.id] = body.delete("8a7ceb38-6cdf-447d-bee3-89bc08644104") + + client = JamRuby::ElasticSearch.new + + #client.smash_indices + succeeded = JamRuby::ElasticSearch.new.session_ratings(music_session, me, body) #SearchClient.index(id: tag.id, index: "tags_development", body: string) - client.index(id: "25b883be-5670-4767-894a-81e574b2a7b4", index: "session_ratings", body: string) + succeeded.should be_true + end + + + it "should fail" do + + # do NOT fix the canned body above + body = JSON.parse(string) + + client = JamRuby::ElasticSearch.new + + succeeded = client.session_ratings(music_session, me, body) + succeeded.should be_false end end diff --git a/ruby/spec/support/utilities.rb b/ruby/spec/support/utilities.rb index e6a75f494..5bb9343a6 100644 --- a/ruby/spec/support/utilities.rb +++ b/ruby/spec/support/utilities.rb @@ -7,6 +7,9 @@ def app_config 'http://localhost:3333' end + def elasticsearch_host + "http://support.jamkazam.com:9200" + end def email_partners_alias 'partners@jamkazam.com' end diff --git a/web/app/controllers/api_music_sessions_controller.rb b/web/app/controllers/api_music_sessions_controller.rb index c0e8f0e1a..860799ded 100644 --- a/web/app/controllers/api_music_sessions_controller.rb +++ b/web/app/controllers/api_music_sessions_controller.rb @@ -438,6 +438,9 @@ class ApiMusicSessionsController < ApiController body << "Backend Detail: NONE in POST!!\n" end + succeeded = JamRuby::ElasticSearch.new.session_ratings(@history.music_session, current_user, backend_details) + + body << succeeded ? "Stored in ElasticSearch" : "Unable to store in ElasticSearch!" AdminMailer.jamclass_alerts({subject: subject, body: body}).deliver_now rescue Exception => e puts "Exception sending out ratings email. Boo #{e}" diff --git a/web/config/application.rb b/web/config/application.rb index b71232015..be5e7d2f9 100644 --- a/web/config/application.rb +++ b/web/config/application.rb @@ -294,6 +294,8 @@ if defined?(Bundler) config.session_cookie_domain = nil + config.elasticsearch_host = "http://support.jamkazam.com:9200" + # these are production values. we should have a test server, but would require us to set one up # we do have some 'fake pages' in the vanilla_forums_controller.rb to get close config.vanilla_client_id = 'www'