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

create pssg download job #3438

Merged
merged 12 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ gem 'rails_semantic_logger', '~> 4.4'
gem 'redis'
gem 'redis-namespace'
gem 'restforce'
gem 'rgeo-geojson'
gem 'ruby-saml'
gem 'rubyzip', '>= 1.3.0'
gem 'savon'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ GEM
rgeo-activerecord (6.1.0)
activerecord (~> 5.0)
rgeo (>= 1.0.0)
rgeo-geojson (2.1.1)
rgeo (>= 1.0.0)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
Expand Down Expand Up @@ -776,6 +778,7 @@ DEPENDENCIES
redis
redis-namespace
restforce
rgeo-geojson
rspec-rails (~> 3.5)
rspec_junit_formatter
rubocop
Expand Down
58 changes: 58 additions & 0 deletions app/workers/facilities/pssg_download.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'rgeo/geo_json'

module Facilities
class PSSGDownload
include Sidekiq::Worker
include SentryLogging

def perform
@drivetime_band_client = Facilities::DrivetimeBandClient.new
download_data
end

private

def create_and_save_drive_time_data(drive_time_data)
attributes = drive_time_data.dig('attributes')

vha_id = extract_vha_id(attributes)
facility = BaseFacility.find_facility_by_id(vha_id)
return if facility.nil?

drive_time_band = facility.drivetime_bands.find_or_initialize_by(vha_facility_id: extract_name(attributes))
drive_time_band.min = attributes.dig('FromBreak')
drive_time_band.max = attributes.dig('ToBreak')
drive_time_band.name = attributes.dig('Name')
drive_time_band.polygon = extract_polygon(drive_time_data)
facility.save
end

def extract_name(attributes)
name = attributes.dig('Name')
name.partition(':').first.strip!
end

def extract_vha_id(attributes)
'vha_' + extract_name(attributes)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

You could get rid of def extract_vha_id and reduce the calls to extract_name from 2 down to 1 with something like this:

name = extract_name(attributes)
vha_id = 'vha_' + name
#...
drive_time_band = facility.drivetime_bands.find_or_initialize_by(vha_facility_id: name)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I added some exception handling but not sure if it's pragmatically correct in ruby


def extract_polygon(drive_time_data)
rings = drive_time_data.dig('geometry', 'rings')
geojson = "{\"type\":\"Polygon\",\"coordinates\":#{rings}}"
RGeo::GeoJSON.decode(geojson)
end

def download_data
offset = 0
loop do
response = @drivetime_band_client.get_drivetime_bands(offset, 1)
break if response.nil?

response.each(&method(:create_and_save_drive_time_data))
offset += 1
end
end
end
end
6 changes: 6 additions & 0 deletions config/sidekiq_scheduler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ FacilityLocationBulkUpdateVHA:
args: ['vha']
description: "Download and store facility location data"

FacilityLocationVHADriveTime:
cron: "55 4 * * * America/New_York"
class: Facilities::PSSGDownload
args: ['vha']
description: "Download and store facility location data"
edmkitty marked this conversation as resolved.
Show resolved Hide resolved

MaintenanceWindowRefresh:
cron: "*/3 * * * * America/New_York"
class: PagerDuty::PollMaintenanceWindows
Expand Down
8 changes: 7 additions & 1 deletion rakelib/jobs.rake
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ namespace :jobs do
end

desc 'Populate/refresh All facility location types to db cache'
task pull_all_facility_location_data: %i[pull_nca_data pull_vba_data pull_vc_data pull_vha_data] do
task pull_all_facility_location_data:
%i[pull_nca_data pull_vba_data pull_vc_data pull_vha_data pull_drive_time_bands] do
# run all dependencies
end

desc 'Populate/refresh Drive time bands'
task pull_drive_time_bands: :environment do
Facilities::PSSGDownload.perform_async
end
end
Loading