From 2421a4bc0376c7c87a1d73b43917848592868eed Mon Sep 17 00:00:00 2001 From: Nuwan Date: Wed, 12 May 2021 16:07:23 +0530 Subject: [PATCH 1/5] add api endpoint to fetch latency data this api method fetches latency data for the current user from the latency graph database service --- web/Gemfile | 1 + web/Gemfile.lock | 9 +++++ web/app/controllers/api_latency_controller.rb | 7 ++++ web/app/controllers/api_users_controller.rb | 24 ++++++++++++ web/config/application.rb | 3 +- web/config/environments/development.rb | 4 +- web/config/environments/production.rb | 2 + web/config/environments/test.rb | 2 + web/config/routes.rb | 2 + .../controllers/api_users_controller_spec.rb | 16 ++++++++ web/spec/fixtures/latency_reponse.json | 38 +++++++++++++++++++ 11 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 web/app/controllers/api_latency_controller.rb create mode 100644 web/spec/fixtures/latency_reponse.json diff --git a/web/Gemfile b/web/Gemfile index 60ab6b80b..54770d81e 100644 --- a/web/Gemfile +++ b/web/Gemfile @@ -227,6 +227,7 @@ group :test, :cucumber do # gem 'growl', '1.0.3' gem 'resque_spec' gem 'timecop' + gem 'webmock', '~> 3.11', '>= 3.11.2' # gem 'thin' end diff --git a/web/Gemfile.lock b/web/Gemfile.lock index 535d86036..1a150ba57 100644 --- a/web/Gemfile.lock +++ b/web/Gemfile.lock @@ -136,6 +136,8 @@ GEM coffee-script-source (1.12.2) concurrent-ruby (1.1.8) connection_pool (2.2.3) + crack (0.4.5) + rexml crass (1.0.6) database_cleaner (1.3.0) devise (3.3.0) @@ -386,6 +388,7 @@ GEM haml (>= 4.0.6, < 5.0) html2haml (>= 1.0.1) railties (>= 4.0.1) + hashdiff (1.0.1) hashie (4.1.0) html2haml (2.2.0) erubis (~> 2.7.0) @@ -653,6 +656,7 @@ GEM mime-types (>= 1.16, < 4.0) netrc (~> 0.8) retriable (3.0.1) + rexml (3.2.5) rspec-collection_matchers (1.2.0) rspec-expectations (>= 2.99.0.beta1) rspec-core (3.10.1) @@ -780,6 +784,10 @@ GEM nokogiri (~> 1.6) rubyzip (~> 1.0) selenium-webdriver (>= 3.0, < 4.0) + webmock (3.12.2) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) will_paginate (3.3.0) xml-simple (1.1.8) xmlrpc (0.3.1) @@ -918,6 +926,7 @@ DEPENDENCIES unicorn uuidtools (= 2.1.2) webdrivers (~> 4.0) + webmock (~> 3.11, >= 3.11.2) will_paginate zip-codes diff --git a/web/app/controllers/api_latency_controller.rb b/web/app/controllers/api_latency_controller.rb new file mode 100644 index 000000000..85347be36 --- /dev/null +++ b/web/app/controllers/api_latency_controller.rb @@ -0,0 +1,7 @@ +class ApiLatencyController < ApiController + before_filter :api_signed_in_user + + def index + + end +end \ No newline at end of file diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index a71815375..3dbb09d27 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -935,6 +935,27 @@ class ApiUsersController < ApiController end end + #fetch latency information from latency-graph serverless + def get_latencies + query_str = params[:user_ids].split(',').inject(""){|q, id| q.concat("id=#{id}&")} + latency_url = "#{Rails.application.config.latency_data_host}/dev/user_latencies/#{current_user.id}?#{query_str}" + url = URI(latency_url) + begin + http = Net::HTTP.new(url.host, url.port) + http.use_ssl = true if Rails.env.production? + request = Net::HTTP::Get.new(url) + request["Authorization"] = "Basic #{Rails.application.config.latency_data_host_auth_code}" + response = http.request(request) + if response.is_a?(Net::HTTPOK) || response.is_a?(Net::HTTPSuccess) + render json: response.body, status: 200 + else + render json: {}, status: 422 + end + rescue + render json: {}, status: 500 + end + end + def udp_reachable Connection.transaction do @connection = Connection.find_by_client_id!(params[:client_id]) @@ -1133,6 +1154,9 @@ class ApiUsersController < ApiController @onboarding = User.find(params[:id]) end + def latency + end + ###################### RECORDINGS ####################### # def recording_index # @recordings = User.recording_index(current_user, params[:id]) diff --git a/web/config/application.rb b/web/config/application.rb index c6e9bcb50..66d305676 100644 --- a/web/config/application.rb +++ b/web/config/application.rb @@ -505,6 +505,7 @@ if defined?(Bundler) config.braintree_token = 'sandbox_pgjp8dvs_5v5rwm94m2vrfbms' config.paypal_admin_only = false config.video_conferencing_host = "https://webrtc-demo.jamkazam.com" - + config.latency_data_host = "http://localhost:4001" + config.latency_data_host_auth_code = "c2VydmVyOnBhc3N3b3Jk" end end diff --git a/web/config/environments/development.rb b/web/config/environments/development.rb index c36b3badf..03cdf4e60 100644 --- a/web/config/environments/development.rb +++ b/web/config/environments/development.rb @@ -1,4 +1,4 @@ - +gcp_stats_host def audiomixer_workspace_path if ENV['WORKSPACE'] dev_path = ENV['WORKSPACE'] @@ -115,4 +115,6 @@ SampleApp::Application.configure do config.video_conferencing_host = "https://webrtc-demo.jamkazam.com" config.use_video_conferencing_server = true + config.latency_data_host = "http://localhost:4001" + config.latency_data_host_auth_code = "c2VydmVyOnBhc3N3b3Jk" end diff --git a/web/config/environments/production.rb b/web/config/environments/production.rb index d27fae9f3..a0d761e5f 100644 --- a/web/config/environments/production.rb +++ b/web/config/environments/production.rb @@ -96,4 +96,6 @@ SampleApp::Application.configure do config.video_conferencing_host = "" config.use_video_conferencing_server = false + config.latency_data_host = "" + config.latency_data_host_auth_code = "" end diff --git a/web/config/environments/test.rb b/web/config/environments/test.rb index 2acd9605f..e2627130e 100644 --- a/web/config/environments/test.rb +++ b/web/config/environments/test.rb @@ -133,5 +133,7 @@ SampleApp::Application.configure do config.video_conferencing_host = "https://webrtc-demo.jamkazam.com" config.use_video_conferencing_server = false + config.latency_data_host = "http://localhost:4001" + config.latency_data_host_auth_code = "c2VydmVyOnBhc3N3b3Jk" end diff --git a/web/config/routes.rb b/web/config/routes.rb index 37fe089c1..6440ee582 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -510,6 +510,8 @@ Rails.application.routes.draw do # audio latency match '/users/:id/audio_latency' => 'api_users#audio_latency', :via => :post + match '/users/:id/latencies' => 'api_users#get_latencies', via: :get + # udp reachable (can stun?) match '/users/:id/udp_reachable' => 'api_users#udp_reachable', :via => :post match '/users/:id/is_network_testing' => 'api_users#is_network_testing', :via => :post diff --git a/web/spec/controllers/api_users_controller_spec.rb b/web/spec/controllers/api_users_controller_spec.rb index edee44cff..f019ea6c3 100644 --- a/web/spec/controllers/api_users_controller_spec.rb +++ b/web/spec/controllers/api_users_controller_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'webmock/rspec' describe ApiUsersController, type: :controller do render_views @@ -310,6 +311,21 @@ describe ApiUsersController, type: :controller do end end + describe "get_latencies" do + let(:user1) { FactoryGirl.create(:user) } + let(:user2) { FactoryGirl.create(:user) } + let(:latency_data_uri) { /\/dev\/user_latencies\// } + let(:response_body) { File.open('./spec/fixtures/latency_reponse.json') } + + it "fetch latency graph data" do + stub_request(:get, latency_data_uri) + .to_return( body: response_body, status: 200) + get :get_latencies, id: user.id, user_ids: [user1, user2].map(&:id).join(',') + response.should be_success + JSON.parse(response.body)['users'].size.should eq(2) + end + end + describe "crash_dump" do include UsesTempFiles CRASH_TEMP_FILE='crash.txt' diff --git a/web/spec/fixtures/latency_reponse.json b/web/spec/fixtures/latency_reponse.json new file mode 100644 index 000000000..502a3ef17 --- /dev/null +++ b/web/spec/fixtures/latency_reponse.json @@ -0,0 +1,38 @@ +{ + "users": [ + { + "user_id": "b9461515-2540-46a6-b973-ff3060806952", + "first_name": "David", + "last_name": "Wilson", + "audio_latency": 4.172335624694824, + "audio_latency_unknown": false, + "ars": { + "internet_latency": 41.301548024602965, + "total_latency": 45.47388364929779 + }, + "p2p": { + "internet_latency": 10.31944465637207, + "total_latency": 14.491780281066895 + }, + "wifi": false + }, + { + "user_id": "40a88b89-ec4b-43c7-a738-3d88e12c8e8d", + "first_name": "Peter", + "last_name": "Walker", + "audio_latency": 4.172335624694824, + "audio_latency_unknown": false, + "ars": { + "internet_latency": null, + "total_latency": null + }, + "p2p": { + "internet_latency": null, + "total_latency": null + }, + "wifi": false + } + ], + "my_audio_latency": 4.172335624694824, + "my_audio_latency_unknown": false +} \ No newline at end of file From 93b848c2d8703743582ed194deb44c55ebdbd6d4 Mon Sep 17 00:00:00 2001 From: Nuwan Date: Wed, 12 May 2021 19:09:21 +0530 Subject: [PATCH 2/5] remove unused code bits --- web/app/controllers/api_latency_controller.rb | 7 ------- web/app/controllers/api_users_controller.rb | 5 +---- 2 files changed, 1 insertion(+), 11 deletions(-) delete mode 100644 web/app/controllers/api_latency_controller.rb diff --git a/web/app/controllers/api_latency_controller.rb b/web/app/controllers/api_latency_controller.rb deleted file mode 100644 index 85347be36..000000000 --- a/web/app/controllers/api_latency_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -class ApiLatencyController < ApiController - before_filter :api_signed_in_user - - def index - - end -end \ No newline at end of file diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index 3dbb09d27..26fce0bd7 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -13,7 +13,7 @@ class ApiUsersController < ApiController :band_invitation_index, :band_invitation_show, :band_invitation_update, # band invitations :set_password, :begin_update_email, :update_avatar, :delete_avatar, :generate_filepicker_policy, :share_session, :share_recording, - :affiliate_report, :audio_latency, :broadcast_notification, :redeem_giftcard] + :affiliate_report, :audio_latency, :get_latencies, :broadcast_notification, :redeem_giftcard] before_filter :ip_blacklist, :only => [:create, :redeem_giftcard] respond_to :json, :except => :calendar @@ -1154,9 +1154,6 @@ class ApiUsersController < ApiController @onboarding = User.find(params[:id]) end - def latency - end - ###################### RECORDINGS ####################### # def recording_index # @recordings = User.recording_index(current_user, params[:id]) From 9dac3fdb56c95f7e73d22e7ac6c31ee9d9431ea1 Mon Sep 17 00:00:00 2001 From: Nuwan Date: Wed, 12 May 2021 19:51:29 +0530 Subject: [PATCH 3/5] on error in get_latencies send details to bugsnag --- web/app/controllers/api_users_controller.rb | 12 +++++++++++- web/config/environments/development.rb | 1 - 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index 26fce0bd7..93c0d9ef1 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -942,16 +942,26 @@ class ApiUsersController < ApiController url = URI(latency_url) begin http = Net::HTTP.new(url.host, url.port) - http.use_ssl = true if Rails.env.production? + http.use_ssl = true if Rails.application.config.latency_data_host.start_with?("https://") request = Net::HTTP::Get.new(url) request["Authorization"] = "Basic #{Rails.application.config.latency_data_host_auth_code}" response = http.request(request) if response.is_a?(Net::HTTPOK) || response.is_a?(Net::HTTPSuccess) render json: response.body, status: 200 else + Bugsnag.notify(exception) do |report| + report.severity = "warning" + report.add_tab(:latency, { + user_id: current_user.id, + name: current_user.name, + user_ids: params[:user_ids], + url: latency_url + }) + end render json: {}, status: 422 end rescue + Bugsnag.notify("Latency server returned code: #{response.code}") render json: {}, status: 500 end end diff --git a/web/config/environments/development.rb b/web/config/environments/development.rb index 03cdf4e60..478424b43 100644 --- a/web/config/environments/development.rb +++ b/web/config/environments/development.rb @@ -1,4 +1,3 @@ -gcp_stats_host def audiomixer_workspace_path if ENV['WORKSPACE'] dev_path = ENV['WORKSPACE'] From 2567292580fe95e89d0fdf74080392e649fa2cfc Mon Sep 17 00:00:00 2001 From: Nuwan Date: Wed, 12 May 2021 19:56:21 +0530 Subject: [PATCH 4/5] change bugsnag severity for failed latency requests --- web/app/controllers/api_users_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index 93c0d9ef1..9382fd396 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -950,7 +950,7 @@ class ApiUsersController < ApiController render json: response.body, status: 200 else Bugsnag.notify(exception) do |report| - report.severity = "warning" + report.severity = "error" report.add_tab(:latency, { user_id: current_user.id, name: current_user.name, From 34bf811453ba1d3e50ce5f57043fd5c0e984f692 Mon Sep 17 00:00:00 2001 From: Nuwan Date: Wed, 12 May 2021 20:03:28 +0530 Subject: [PATCH 5/5] add response.body to bugsnag call of failed latency requests --- web/app/controllers/api_users_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index 9382fd396..2e78eb826 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -955,7 +955,8 @@ class ApiUsersController < ApiController user_id: current_user.id, name: current_user.name, user_ids: params[:user_ids], - url: latency_url + url: latency_url, + body: response.body }) end render json: {}, status: 422