2021-08-26 17:23:24 +00:00
|
|
|
module LatencyHelper
|
2021-08-31 18:25:15 +00:00
|
|
|
|
2021-11-30 13:58:09 +00:00
|
|
|
#GOOD = <40ms, FAIR = 40-60ms, and POOR = >60ms
|
|
|
|
|
|
|
|
|
|
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 }
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-26 17:14:20 +00:00
|
|
|
def users_latency_data(latency_good, latency_fair, latency_high, filter_opts, offset, limit)
|
2022-03-18 13:57:48 +00:00
|
|
|
|
2021-08-26 17:23:24 +00:00
|
|
|
latency_data = []
|
2021-12-13 14:16:07 +00:00
|
|
|
|
|
|
|
|
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"
|
2022-02-08 15:25:00 +00:00
|
|
|
|
|
|
|
|
req_params = {
|
2021-12-13 14:16:07 +00:00
|
|
|
my_user_id: current_user.id,
|
|
|
|
|
my_public_ip: request.remote_ip,
|
|
|
|
|
my_device_id: nil,
|
2022-02-08 15:25:00 +00:00
|
|
|
my_client_id: nil,
|
|
|
|
|
from_location: filter_opts[:from_location] || "0",
|
2022-03-18 13:57:48 +00:00
|
|
|
offset: offset,
|
2022-02-08 15:25:00 +00:00
|
|
|
limit: limit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req_params.merge!(instruments: filter_opts[:instruments]) if filter_opts[:instruments]
|
|
|
|
|
req_params.merge!(genres: filter_opts[:genres]) if filter_opts[:genres]
|
2022-02-17 14:07:23 +00:00
|
|
|
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]
|
2022-02-08 15:25:00 +00:00
|
|
|
|
|
|
|
|
req.body = req_params.to_json
|
2021-08-26 17:23:24 +00:00
|
|
|
|
2021-12-13 14:16:07 +00:00
|
|
|
response = http.request(req)
|
2022-03-18 13:57:48 +00:00
|
|
|
|
|
|
|
|
#debugger
|
2021-12-13 14:16:07 +00:00
|
|
|
|
|
|
|
|
if response.is_a?(Net::HTTPOK) || response.is_a?(Net::HTTPSuccess)
|
2022-03-18 13:57:48 +00:00
|
|
|
json_body = JSON.parse(response.body)
|
|
|
|
|
graph_db_users = json_body['users']
|
2022-10-27 06:23:47 +00:00
|
|
|
nextOffset = json_body['next']
|
2021-12-13 14:16:07 +00:00
|
|
|
|
|
|
|
|
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
|
2021-08-26 17:23:24 +00:00
|
|
|
|
2021-12-13 14:16:07 +00:00
|
|
|
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
|
|
|
|
|
|
2022-10-27 06:46:02 +00:00
|
|
|
return {data: latency_data, next: nextOffset}
|
2021-12-13 14:16:07 +00:00
|
|
|
else
|
|
|
|
|
logger.debug("Latency response failed: #{response}")
|
|
|
|
|
Bugsnag.notify("LatencyResponseFailed") do |report|
|
|
|
|
|
report.severity = "faliure"
|
|
|
|
|
report.add_tab(:latency, {
|
|
|
|
|
user_id: current_user.id,
|
|
|
|
|
name: current_user.name,
|
|
|
|
|
params: params,
|
|
|
|
|
url: filter_latency_url,
|
|
|
|
|
code: response.code,
|
|
|
|
|
body: response.body,
|
|
|
|
|
})
|
2021-08-26 17:23:24 +00:00
|
|
|
end
|
|
|
|
|
end
|
2021-12-13 14:16:07 +00:00
|
|
|
rescue => exception
|
|
|
|
|
raise exception
|
|
|
|
|
end
|
|
|
|
|
|
2021-08-26 17:23:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|