jam-cloud/ruby/lib/jam_ruby/elasticsearch.rb

70 lines
2.1 KiB
Ruby
Raw Normal View History

2021-01-09 22:15:56 +00:00
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
my_data = original_body.delete('**me')
2021-01-18 16:55:52 +00:00
my_data["rating_type"] = original_body["rating_type"]
my_data["comment"] = original_body["comment"]
2021-01-09 22:15:56 +00:00
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)
2021-01-11 18:15:15 +00:00
other_connection = Connection.find_by_client_id(k)
if other_connection
other_user = other_connection.user
2021-01-09 22:15:56 +00:00
2021-01-11 18:15:15 +00:00
body = {me: my_data, other: other_data}
2021-01-09 22:15:56 +00:00
2021-01-11 18:15:15 +00:00
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)
end
2021-01-09 22:15:56 +00:00
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}")
2021-01-11 18:15:15 +00:00
puts("unable to parse session ratings #{e}")
2021-01-09 22:15:56 +00:00
return false
end
end
end
end