From 34cd8490e731c3113a7a686966b6b897cfe21688 Mon Sep 17 00:00:00 2001 From: "Chris Baudouin, Jr" Date: Sat, 23 May 2020 18:51:00 -0400 Subject: [PATCH 1/4] fix(dashboard): Switches map to use different geocoder, reenables map --- app/controllers/manage/dashboard_controller.rb | 2 +- app/views/manage/dashboard/index.html.haml | 11 +++++------ app/views/manage/dashboard/map_data.tsv.erb | 15 +++++++++------ .../manage/dashboard_controller_test.rb | 4 ++-- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/controllers/manage/dashboard_controller.rb b/app/controllers/manage/dashboard_controller.rb index 6a415b11f..033bad1eb 100644 --- a/app/controllers/manage/dashboard_controller.rb +++ b/app/controllers/manage/dashboard_controller.rb @@ -6,7 +6,7 @@ def index end def map_data - @schools = School.where("questionnaire_count", 1..Float::INFINITY).select([:city, :state, :questionnaire_count]) + @schools = School.where("questionnaire_count", 1..Float::INFINITY).select([:name, :address, :city, :state, :questionnaire_count]) end def todays_activity_data diff --git a/app/views/manage/dashboard/index.html.haml b/app/views/manage/dashboard/index.html.haml index c0a070828..7291650c8 100644 --- a/app/views/manage/dashboard/index.html.haml +++ b/app/views/manage/dashboard/index.html.haml @@ -11,12 +11,11 @@ = render "layouts/manage/page_title", title: "Dashboard" --# - .row - .col - #map - :javascript - $('#map').initMap(); +.row + .col + #map + :javascript + $('#map').initMap(); .row .col-7 diff --git a/app/views/manage/dashboard/map_data.tsv.erb b/app/views/manage/dashboard/map_data.tsv.erb index 8723d5df5..147f637e2 100644 --- a/app/views/manage/dashboard/map_data.tsv.erb +++ b/app/views/manage/dashboard/map_data.tsv.erb @@ -9,19 +9,22 @@ redo_limit = 10 if school.fips_code.blank? next if school.city.blank? || school.state.blank? - resp = HTTParty.get("https://maps.googleapis.com/maps/api/geocode/json?address=#{CGI.escape(school.city)}+#{CGI.escape(school.state)}&sensor=true") - results = resp.parsed_response["results"][0] - if results.blank? + resp = HTTParty.get("https://geocoding.geo.census.gov/geocoder/locations/address?street=#{CGI.escape(school.address)}&city=#{CGI.escape(school.city)}&state=#{CGI.escape(school.state)}&benchmark=Public_AR_Current&format=json") + result = resp.parsed_response["result"] + if result.blank? if redo_count >= redo_limit - raise 'Exceeded maximum number of retries: No results from Google Maps API.' + raise 'Exceeded maximum number of retries: No results from Census.gov.' end redo_count += 1 redo end redo_count = 0 - lat = results["geometry"]["location"]["lat"] - lng = results["geometry"]["location"]["lng"] + addressMatches = result["addressMatches"] + next if addressMatches.blank? + + lat = result["addressMatches"][0]["coordinates"]["x"] + lng = result["addressMatches"][0]["coordinates"]["y"] next if lat.blank? || lng.blank? resp = HTTParty.get("https://geo.fcc.gov/api/census/area?lat=#{lat}&lon=#{lng}&format=json") diff --git a/test/controllers/manage/dashboard_controller_test.rb b/test/controllers/manage/dashboard_controller_test.rb index 021def962..25fdccdd8 100644 --- a/test/controllers/manage/dashboard_controller_test.rb +++ b/test/controllers/manage/dashboard_controller_test.rb @@ -45,8 +45,8 @@ class Manage::DashboardControllerTest < ActionController::TestCase FactoryBot.create_list(:questionnaire, 1, school_id: school2.id, acc_status: status) end - stub_request(:get, "https://maps.googleapis.com/maps/api/geocode/json?address=Rochester%20NY&sensor=true"). - to_return(status: 200, body: '{"results":[{"geometry":{"location":{"lat": 100, "lng": 100}}}]}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) + stub_request(:get, "https://geocoding.geo.census.gov/geocoder/locations/address?street=1+Lomb+Memorial+Dr&city=Rochester&state=New+York&benchmark=Public_AR_Current&format=json"). + to_return(status: 200, body: '{"result":{"addressMatches":[{"coordinates":{"x": -77.67469, "y": 43.09212}}]}}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) stub_request(:get, "https://geo.fcc.gov/api/census/area?format=json&lat=100&lon=100"). to_return(status: 200, body: '{"results":[{"country_fips":1234}]}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) From 16f01a77100c80b75b3250cdefff34948ecc67a9 Mon Sep 17 00:00:00 2001 From: "Chris Baudouin, Jr" Date: Tue, 26 May 2020 17:35:11 -0400 Subject: [PATCH 2/4] fix(dashboard): Fixes map test --- test/controllers/manage/dashboard_controller_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/controllers/manage/dashboard_controller_test.rb b/test/controllers/manage/dashboard_controller_test.rb index 25fdccdd8..045f50852 100644 --- a/test/controllers/manage/dashboard_controller_test.rb +++ b/test/controllers/manage/dashboard_controller_test.rb @@ -45,8 +45,8 @@ class Manage::DashboardControllerTest < ActionController::TestCase FactoryBot.create_list(:questionnaire, 1, school_id: school2.id, acc_status: status) end - stub_request(:get, "https://geocoding.geo.census.gov/geocoder/locations/address?street=1+Lomb+Memorial+Dr&city=Rochester&state=New+York&benchmark=Public_AR_Current&format=json"). - to_return(status: 200, body: '{"result":{"addressMatches":[{"coordinates":{"x": -77.67469, "y": 43.09212}}]}}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) + stub_request(:get, "https://geocoding.geo.census.gov/geocoder/locations/address?street=123+Fake+Street&city=Rochester&state=New+York&benchmark=Public_AR_Current&format=json"). + to_return(status: 200, body: '{"result":{"addressMatches":[{"coordinates":{"x": 100, "y": 100}}]}}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) stub_request(:get, "https://geo.fcc.gov/api/census/area?format=json&lat=100&lon=100"). to_return(status: 200, body: '{"results":[{"country_fips":1234}]}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) From b8b3912d029bef33fce64a8722bc399cab15cbe2 Mon Sep 17 00:00:00 2001 From: "Chris Baudouin, Jr" Date: Tue, 26 May 2020 17:44:07 -0400 Subject: [PATCH 3/4] fix(dashboard): Simplifies New York for map test --- test/controllers/manage/dashboard_controller_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/controllers/manage/dashboard_controller_test.rb b/test/controllers/manage/dashboard_controller_test.rb index 045f50852..b8f33bc7a 100644 --- a/test/controllers/manage/dashboard_controller_test.rb +++ b/test/controllers/manage/dashboard_controller_test.rb @@ -45,10 +45,10 @@ class Manage::DashboardControllerTest < ActionController::TestCase FactoryBot.create_list(:questionnaire, 1, school_id: school2.id, acc_status: status) end - stub_request(:get, "https://geocoding.geo.census.gov/geocoder/locations/address?street=123+Fake+Street&city=Rochester&state=New+York&benchmark=Public_AR_Current&format=json"). - to_return(status: 200, body: '{"result":{"addressMatches":[{"coordinates":{"x": 100, "y": 100}}]}}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) - stub_request(:get, "https://geo.fcc.gov/api/census/area?format=json&lat=100&lon=100"). - to_return(status: 200, body: '{"results":[{"country_fips":1234}]}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) + stub_request(:get, "https://geocoding.geo.census.gov/geocoder/locations/address?street=123+Fake+Street&city=Rochester&state=NY&benchmark=Public_AR_Current&format=json") + .to_return(status: 200, body: '{"result":{"addressMatches":[{"coordinates":{"x": 100, "y": 100}}]}}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) + stub_request(:get, "https://geo.fcc.gov/api/census/area?format=json&lat=100&lon=100") + .to_return(status: 200, body: '{"results":[{"country_fips":1234}]}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) paths = [ :todays_activity_data, From b6429a8e270429a2d5f149be779315ef421e86a9 Mon Sep 17 00:00:00 2001 From: "Chris Baudouin, Jr" Date: Tue, 26 May 2020 23:17:48 -0400 Subject: [PATCH 4/4] fix(dashboard): Fixes Hound issues --- test/controllers/manage/dashboard_controller_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/controllers/manage/dashboard_controller_test.rb b/test/controllers/manage/dashboard_controller_test.rb index b8f33bc7a..f1dad23c1 100644 --- a/test/controllers/manage/dashboard_controller_test.rb +++ b/test/controllers/manage/dashboard_controller_test.rb @@ -46,9 +46,9 @@ class Manage::DashboardControllerTest < ActionController::TestCase end stub_request(:get, "https://geocoding.geo.census.gov/geocoder/locations/address?street=123+Fake+Street&city=Rochester&state=NY&benchmark=Public_AR_Current&format=json") - .to_return(status: 200, body: '{"result":{"addressMatches":[{"coordinates":{"x": 100, "y": 100}}]}}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) + .to_return(status: 200, body: '{ "result":{ "addressMatches":[{ "coordinates":{ "x": 100, "y": 100 } }] } }', headers: { 'Content-Type' => 'application/json; charset=UTF-8' }) stub_request(:get, "https://geo.fcc.gov/api/census/area?format=json&lat=100&lon=100") - .to_return(status: 200, body: '{"results":[{"country_fips":1234}]}', headers: {'Content-Type' => 'application/json; charset=UTF-8'}) + .to_return(status: 200, body: '{ "results":[{ "country_fips":1234 }] }', headers: { 'Content-Type' => 'application/json; charset=UTF-8' }) paths = [ :todays_activity_data,