153 lines
6.1 KiB
Ruby
153 lines
6.1 KiB
Ruby
|
|
module LatencyHelper
|
||
|
|
LATENCY_SCORES = {
|
||
|
|
good: { label: 'GOOD', min: 0, max: 40 },
|
||
|
|
fair: { label: 'FAIR', min: 40, max: 80 },
|
||
|
|
high: { label: 'HIGH', min: 80, max: 10000000 },
|
||
|
|
me: { label: 'ME', min: -1, max: -1 },
|
||
|
|
unknown: { label: 'UNKNOWN', min: -2, max: -2 }
|
||
|
|
}
|
||
|
|
# def users_latency_data(latency_good, latency_fair, latency_high)
|
||
|
|
# latency_data = []
|
||
|
|
# if latency_good || latency_fair || latency_high
|
||
|
|
# 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.body = {
|
||
|
|
# my_user_id: current_user.id,
|
||
|
|
# my_public_ip: request.remote_ip,
|
||
|
|
# my_device_id: nil,
|
||
|
|
# my_client_id: nil
|
||
|
|
# }.to_json
|
||
|
|
|
||
|
|
# response = http.request(req)
|
||
|
|
|
||
|
|
# if response.is_a?(Net::HTTPOK) || response.is_a?(Net::HTTPSuccess)
|
||
|
|
# graph_db_users = JSON.parse(response.body)["users"]
|
||
|
|
# if latency_good || latency_fair || latency_high
|
||
|
|
# 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 |
|
||
|
|
# total = user["ars"]["total_latency"].to_f
|
||
|
|
# label = if total >= LATENCY_SCORES[:good][:min] && total <= LATENCY_SCORES[:good][:max]
|
||
|
|
# LATENCY_SCORES[:good][:label]
|
||
|
|
# elsif total > LATENCY_SCORES[:fair][:min] && total <= LATENCY_SCORES[:fair][:max]
|
||
|
|
# LATENCY_SCORES[:fair][:label]
|
||
|
|
# elsif total > LATENCY_SCORES[:high][:min]
|
||
|
|
# LATENCY_SCORES[:high][:label]
|
||
|
|
# else
|
||
|
|
# LATENCY_SCORES[:unknown][:label]
|
||
|
|
# end
|
||
|
|
# {
|
||
|
|
# 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,
|
||
|
|
# label: label
|
||
|
|
# }
|
||
|
|
# }.uniq
|
||
|
|
# #debugger
|
||
|
|
# return latency_data
|
||
|
|
# 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,
|
||
|
|
# })
|
||
|
|
# end
|
||
|
|
# end
|
||
|
|
# rescue => exception
|
||
|
|
# raise exception
|
||
|
|
# end
|
||
|
|
# end
|
||
|
|
# latency_data
|
||
|
|
# end
|
||
|
|
|
||
|
|
|
||
|
|
def users_latency_data(latency_good, latency_fair, latency_high)
|
||
|
|
latency_data = []
|
||
|
|
if latency_good || latency_fair || latency_high
|
||
|
|
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.body = {
|
||
|
|
my_user_id: current_user.id,
|
||
|
|
my_public_ip: request.remote_ip,
|
||
|
|
my_device_id: nil,
|
||
|
|
my_client_id: nil
|
||
|
|
}.to_json
|
||
|
|
|
||
|
|
response = http.request(req)
|
||
|
|
|
||
|
|
if response.is_a?(Net::HTTPOK) || response.is_a?(Net::HTTPSuccess)
|
||
|
|
graph_db_users = JSON.parse(response.body)["users"]
|
||
|
|
if latency_good || latency_fair || latency_high
|
||
|
|
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 |
|
||
|
|
total = user["ars"]["total_latency"].to_f
|
||
|
|
label = if total >= LATENCY_SCORES[:good][:min] && total <= LATENCY_SCORES[:good][:max]
|
||
|
|
LATENCY_SCORES[:good][:label]
|
||
|
|
elsif total > LATENCY_SCORES[:fair][:min] && total <= LATENCY_SCORES[:fair][:max]
|
||
|
|
LATENCY_SCORES[:fair][:label]
|
||
|
|
elsif total > LATENCY_SCORES[:high][:min]
|
||
|
|
LATENCY_SCORES[:high][:label]
|
||
|
|
else
|
||
|
|
LATENCY_SCORES[:unknown][:label]
|
||
|
|
end
|
||
|
|
{
|
||
|
|
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,
|
||
|
|
label: label
|
||
|
|
}
|
||
|
|
}.uniq
|
||
|
|
return latency_data
|
||
|
|
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,
|
||
|
|
})
|
||
|
|
end
|
||
|
|
end
|
||
|
|
rescue => exception
|
||
|
|
raise exception
|
||
|
|
end
|
||
|
|
end
|
||
|
|
latency_data
|
||
|
|
end
|
||
|
|
|
||
|
|
end
|