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_users_controller.rb b/web/app/controllers/api_users_controller.rb index a71815375..2e78eb826 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 @@ -935,6 +935,38 @@ 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.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 = "error" + report.add_tab(:latency, { + user_id: current_user.id, + name: current_user.name, + user_ids: params[:user_ids], + url: latency_url, + body: response.body + }) + end + render json: {}, status: 422 + end + rescue + Bugsnag.notify("Latency server returned code: #{response.code}") + render json: {}, status: 500 + end + end + def udp_reachable Connection.transaction do @connection = Connection.find_by_client_id!(params[:client_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..478424b43 100644 --- a/web/config/environments/development.rb +++ b/web/config/environments/development.rb @@ -1,4 +1,3 @@ - def audiomixer_workspace_path if ENV['WORKSPACE'] dev_path = ENV['WORKSPACE'] @@ -115,4 +114,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