Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Census auto update new map #20575

Merged
merged 2 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions bin/cron/update_census_map
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env ruby

# The map on code.org/yourschool is run off of a Google Fusion Table
# (documentation: https://developers.google.com/maps/documentation/javascript/fusiontableslayer).
# This script updates the fusion table using the REST API
# (documentation: https://developers.google.com/fusiontables/docs/v2/using),
# populating it with all rows from the census_summaries table for the appropriate school year.

# After setting up authentication, provide the following CDO.* attributes:
# CDO.hoc_map_account: (JSON of a credential which can edit the FusionTables table)
# CDO.census_map_table_id: (the id of the fusion table)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can still set these via locals.yml?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That is how I'm testing.

require_relative '../../dashboard/config/environment'
require 'cdo/properties'

require 'google/apis/fusiontables_v2'
require 'googleauth'

TABLE_ID = CDO.census_map_table_id.freeze

def make_location(lat, long)
return nil if lat.nil? || long.nil?

"#{format('%.6f', lat)}, #{format('%.6f', long)}"
end

# The map should show the data from the school year that began
# in the previous calendar year. So, Aug-Dec we show the previous school
# year's data and then switch over to the current school year in Jan.
year = Date.today.year - 1

file = Tempfile.new(['data', '.csv'])
begin
CSV.open(file.path, 'w') do |csv|
Census::CensusSummary.where(school_year: year).find_each do |summary|
school = summary.school
location = make_location(school.latitude, school.longitude)
csv << [school.id, year, school.name.titleize, school.city.titleize, school.state, location, summary.teaches_cs]
end
end
ensure
file.close
end

tables = Google::Apis::FusiontablesV2::FusiontablesService.new

tables.authorization = Google::Auth::ServiceAccountCredentials.make_creds(
scope: 'https://www.googleapis.com/auth/fusiontables',
json_key_io: StringIO.new(CDO.hoc_map_account.to_json)
)

tables.replace_table_rows(TABLE_ID, content_type: 'application/octet-stream', upload_source: file.path)
1 change: 1 addition & 0 deletions cookbooks/cdo-apps/templates/default/crontab.erb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
cronjob at:'*/1 * * * *', do:deploy_dir('bin', 'cron', 'index_users_in_solr')
cronjob at:'25 7 * * *', do:deploy_dir('bin', 'cron', 'update_hoc_map')
cronjob at:'12 3 * * *', do:deploy_dir('bin', 'cron', 'update_pledge_map')
cronjob at:'19 4 * * *', do:deploy_dir('bin', 'cron', 'update_census_map')
cronjob at:'*/10 * * * *', do:deploy_dir('bin', 'cron', 'delete_twilio_data')
cronjob at:'* * * * *', do:deploy_dir('bin', 'cron', 'confirm_usage')
cronjob at:'5 16 * * *', do:deploy_dir('bin', 'cron', 'calculate_workshop_survey_results')
Expand Down
1 change: 1 addition & 0 deletions deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def load_configuration
'build_apps' => false,
'build_dashboard' => true,
'build_pegasus' => true,
'census_map_table_id' => rack_env == :production ? '1AUZYRjLMI5NiQsDeDBGFsOIFpL_rLGsnxNpSyR13' : nil,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any downside to this being in the repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. You can see the value if you view source on the page.

'chef_local_mode' => rack_env == :adhoc,
'dcdo_table_name' => "dcdo_#{rack_env}",
'dashboard_assets_dir' => "#{root_dir}/dashboard/public/assets",
Expand Down