music session ratingsl

This commit is contained in:
Seth Call 2021-01-09 16:15:56 -06:00
parent 270f28be9e
commit 3e953b1b4c
7 changed files with 111 additions and 12 deletions

View File

@ -62,7 +62,7 @@ gem 'stripe'
gem 'zip-codes'
gem 'icalendar'
gem 'email_validator'
gem 'typhoeus'
gem 'elasticsearch'
group :test do

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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}"

View File

@ -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'