This repository was archived by the owner on Apr 22, 2020. It is now read-only.
forked from pophealth/quality-measure-engine
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmeasure_calculation_job.rb
More file actions
106 lines (95 loc) · 3.91 KB
/
Copy pathmeasure_calculation_job.rb
File metadata and controls
106 lines (95 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
module QME
module MapReduce
# A delayed_job that allows for measure calculation by a delayed_job worker. Can be created as follows:
#
# Delayed::Job.enqueue QME::MapRedude::MeasureCalculationJob.new(quality_report, :effective_date => 1291352400, :test_id => xyzzy)
#
# MeasureCalculationJob will check to see if a measure has been calculated before running the calculation. It will do this by
# checking the status of the quality report that this calculation job was created with.
#
# When a measure needs calculation, the job will create a QME::MapReduce::Executor and interact with it to calculate
# the report.
class MeasureCalculationJob
attr_accessor :quality_report
def initialize(options)
@quality_report = QME::QualityReport.find(options["quality_report_id"])
@options = options
@options.merge! @quality_report.attributes
end
def perform
if !@quality_report.calculated? || @options['recalculate']
map = QME::MapReduce::Executor.new(@quality_report.measure_id,@quality_report.sub_id, @options.merge('start_time' => Time.now.to_i))
if !@quality_report.patients_cached? || @options['recalculate']
@quality_report.expire_patient_results
tick('Starting MapReduce')
map.map_records_into_measure_groups(@options['prefilter'])
tick('MapReduce complete')
end
tick('Calculating group totals')
result = map.count_records_in_measure_groups
@quality_report.result=result
# backwards compatibility with previous q cahce users. Should be reomved going foward
# and provide a means to update existing results to the newer format
result.attributes.each_pair do |k,v|
unless k.to_s == "_id"
@quality_report[k]=v
end
end
@quality_report.save
completed("#{@measure_id}#{@sub_id}: p#{result[QME::QualityReport::POPULATION]}, d#{result[QME::QualityReport::DENOMINATOR]}, n#{result[QME::QualityReport::NUMERATOR]}, excl#{result[QME::QualityReport::EXCLUSIONS]}, excep#{result[QME::QualityReport::EXCEPTIONS]}")
QME::QualityReport.queue_staged_rollups(@quality_report.measure_id,@quality_report.sub_id,@quality_report.effective_date)
end
@quality_report
end
def completed(message)
@quality_report.status["state"] = "completed"
@quality_report.status["log"] << message
@quality_report.calculation_time = Time.now
@quality_report.save
end
def tick(message)
@quality_report.status["state"] = "calculating"
@quality_report.status["log"] << message
@quality_report.save
end
def enqueue(job)
@quality_report.status = {"state" => "queued", "log" => ["Queued at #{Time.now}"], "job_id" => job.id}
@quality_report.save
end
def error(job, exception)
@quality_report.status["state"] = "error"
@quality_report.status["log"] << exception.to_s
@quality_report.save
end
def failure(job)
@quality_report.status["state"] = "failed"
@quality_report.status["log"] << "Failed at #{Time.now}"
@quality_report.save
end
def after(job)
@quality_report.status.delete("job_id")
@quality_report.save
end
# Returns the status of a measure calculation job
# @param job_id the id of the job to check on
# @return [Symbol] Will return the status: :complete, :queued, :running, :failed
def self.status(job_id)
job = Delayed::Job.where(_id: job_id).first
if job.nil?
# If we can't find the job, we assume that it is complete
:complete
else
if job.locked_at.nil?
:queued
else
if job.failed?
:failed
else
:running
end
end
end
end
end
end
end