Skip to content

Commit

Permalink
Display facilities by district - Analytics Cache Jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
timmyjose committed May 17, 2019
1 parent 9f8eefe commit 16b7c0b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/jobs/warm_up_analytics_cache_job.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class WarmUpAnalyticsCacheJob < ApplicationJob
queue_as :default
queue_as :analytics_warmup
self.queue_adapter = :sidekiq

def perform(record_type, record_id, from_time_string, to_time_string)
record = record_type.constantize.find(record_id)
record.patient_set_analytics(from_time_string.to_time, to_time_string.to_time)
end
end
end
11 changes: 11 additions & 0 deletions app/jobs/warm_up_district_analytics_cache_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class WarmUpDistrictAnalyticsCacheJob < ApplicationJob
queue_as :analytics_warmup
self.queue_adapter = :sidekiq

def perform(district_name, organization_id, from_time_string, to_time_string)
organization_district = OrganizationDistrict.new(district_name, Organization.find(organization_id))
from_time = from_time_string.to_time
to_time = to_time_string.to_time
organization_district.patient_set_analytics(from_time, to_time)
end
end
1 change: 1 addition & 0 deletions config/sidekiq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

:queues:
- default
- [analytics_warmup, 2]
48 changes: 34 additions & 14 deletions lib/tasks/analytics.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,47 @@ def enqueue_cache_warmup(record, from_time, to_time)
Rails.logger.info("Enqueued job to warm up for #{record_class} - #{record.id}")
end

def warmup_analytics_for_organization_districts(from_time_string, to_time_string)
Organization.all.each do |organization|
organization.districts.each do |district_name|
WarmUpDistrictAnalyticsCacheJob.perform_later(
district_name,
organization.id,
from_time_string,
to_time_string)
Rails.logger.info("Enqueued job to warm up for organization district (#{organization.name}, #{district_name})")
end
end
end

def fan_out_cache_warmup(from_time, to_time)
def warmup_analytics_for_facility_groups(from_time_string, to_time_string)
FacilityGroup.all.each do |facility_group|
enqueue_cache_warmup(facility_group, from_time, to_time)
facility_group.facilities.each do |facility|
enqueue_cache_warmup(facility, from_time, to_time)
end
enqueue_cache_warmup(facility_group, from_time_string, to_time_string)
end
end

def warmup_analytics_for_facilities(from_time_string, to_time_string)
Facility.all.each do |facility|
enqueue_cache_warmup(facility, from_time_string, to_time_string)
end
end


namespace :analytics do
desc 'Warm up analytics for last 90 days'
task warm_up_last_ninety_days: :environment do
Rails.logger = Logger.new(STDOUT)
timezone = ENV.fetch('ANALYTICS_TIME_ZONE')

to_time = Time.now.in_time_zone(timezone)
from_time = to_time - 90.days.in_time_zone(timezone)
from_time = (to_time - 90.days).in_time_zone(timezone)

fan_out_cache_warmup(
from_time.strftime('%Y-%m-%d'),
to_time.strftime('%Y-%m-%d')
)
from_time_string = from_time.strftime('%Y-%m-%d')
to_time_string = to_time.strftime('%Y-%m-%d')

warmup_analytics_for_facilities(from_time_string, to_time_string)
warmup_analytics_for_facility_groups(from_time_string, to_time_string)
warmup_analytics_for_organization_districts(from_time_string, to_time_string)
end

desc 'Warm up analytics for last four quarters'
Expand All @@ -41,10 +59,12 @@ namespace :analytics do

(1..4).each do |n|
range = ApplicationController.helpers.range_for_quarter(-1 * n)
fan_out_cache_warmup(
range[:from_time].in_time_zone(timezone).strftime('%Y-%m-%d'),
range[:to_time].in_time_zone(timezone).strftime('%Y-%m-%d')
)
from_time_string = range[:from_time].in_time_zone(timezone).strftime('%Y-%m-%d')
to_time_string = range[:to_time].in_time_zone(timezone).strftime('%Y-%m-%d')

warmup_analytics_for_facilities(from_time_string, to_time_string)
warmup_analytics_for_facility_groups(from_time_string, to_time_string)
warmup_analytics_for_organization_districts(from_time_string, to_time_string)
end
end
end
2 changes: 1 addition & 1 deletion spec/jobs/warm_up_analytics_cache_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
let(:job) { WarmUpAnalyticsCacheJob.perform_later('FacilityGroup', facility_groups.first.id, from_time, to_time) }

it 'queues the job on the default queue' do
expect(job.queue_name).to eq('default')
expect(job.queue_name).to eq('analytics_warmup')
end

it 'queues the job' do
Expand Down
35 changes: 35 additions & 0 deletions spec/jobs/warm_up_district_analytics_cache_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'rails_helper'

RSpec.describe WarmUpDistrictAnalyticsCacheJob, type: :job do
include ActiveJob::TestHelper

let(:district) { 'Bathinda' }
let(:organization) { create(:organization) }
let(:facility_group) { create(:facility_group, organization: organization) }
let!(:facilities) { create_list(:facility, 2, facility_group: facility_group, district: district) }

let(:organization_district) { OrganizationDistrict.new(district, organization) }

let(:to_time) { Time.new(2019, 3, 31).strftime('%Y-%m-%d') }
let(:from_time) { (to_time.to_time - 90.days).strftime('%Y-%m-%d') }

describe '.perform_later' do
let(:job) { WarmUpDistrictAnalyticsCacheJob.perform_later(district, organization.id, from_time, to_time) }

it 'queues the job on the default queue' do
expect(job.queue_name).to eq('analytics_warmup')
end

it 'queues the job' do
assert_enqueued_jobs 1 do
job
end
end

it 'updates the cache for organization district with analytics for the given time' do
perform_enqueued_jobs { job }
expect(Rails.cache.exist?(organization_district.analytics_cache_key(from_time.to_time, to_time.to_time)))
.to be_truthy
end
end
end

0 comments on commit 16b7c0b

Please sign in to comment.