-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathproduct_test.rb
More file actions
524 lines (443 loc) · 18.6 KB
/
Copy pathproduct_test.rb
File metadata and controls
524 lines (443 loc) · 18.6 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# frozen_string_literal: true
# rubocop:disable Metrics/ClassLength
class ProductTest
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Attributes::Dynamic
include GlobalID::Identification
scope :by_updated_at, -> { order(updated_at: :desc) }
# TODO: Use real attributes?
scope :measure_tests, -> { where(_type: 'MeasureTest') }
scope :checklist_tests, -> { where(_type: 'ChecklistTest') }
scope :filtering_tests, -> { where(_type: 'FilteringTest') }
scope :multi_measure_tests, -> { where(_type: 'MultiMeasureTest') }
scope :cms_program_tests, -> { where(_type: 'CMSProgramTest') }
belongs_to :product, index: true, touch: true
has_many :tasks, dependent: :destroy, inverse_of: :product_test
# TODO: R2P: fix foreign key descriptor?
has_many :patients, dependent: :destroy, foreign_key: 'correlation_id', class_name: 'CQM::ProductTestPatient'
field :augmented_patients, type: Array, default: []
field :expected_results, type: Hash
# this the hqmf id of the measure
field :measure_ids, type: Array
# Test Details
field :name, type: String
field :cms_id, type: String
field :description, type: String
field :state, type: Symbol, default: :pending
field :rand_seed, type: String
field :most_recent_product_test_status, type: Hash, default: {}
field :most_recent_product_test_status_updated_at, type: Hash, default: {}
field :backtrace, type: String
field :status_message, type: String
validates :name, presence: true
validates :product, presence: true
validates :measure_ids, presence: true
mount_uploader :patient_archive, PatientArchiveUploader
mount_uploader :html_archive, PatientArchiveUploader
delegate :cures_update, to: :product
delegate :name, :version, to: :product, prefix: true
delegate :bundle, to: :product
delegate :slim_test_deck?, to: :product
delegate :test_deck_max, to: :product
delegate :c1_test, :c2_test, :c3_test, to: :product
before_create :generate_random_seed
def self.inherited(child)
child.instance_eval do
def model_name
ProductTest.model_name
end
end
super
end
# This is a helper method for the vendor and product cleanup code. It takes a collection of
# product test ID's and deletes those ProducTests along with all associated data.
def self.destroy_by_ids(product_test_ids)
tasks = Task.where(:product_test_id.in => product_test_ids)
task_ids = tasks.pluck(:_id)
patients = ProductTestPatient.where(:correlation_id.in => product_test_ids)
patient_ids = patients.pluck(:_id)
test_executions = TestExecution.where(:task_id.in => task_ids)
test_execution_ids = test_executions.pluck(:_id)
patients.delete
tasks.delete
test_executions.delete
# Artifact runs a destroy instead of a delete in order to invoke the file callbacks from
# carrierwave. Without this the system would be left with a lot of uploaded files on it
# long after the parent data was destroyed.
Artifact.where(:test_execution_id.in => test_execution_ids).destroy
CQM::IndividualResult.where(:patient_id.in => patient_ids).delete
test_execution_ids.each do |test_execution_id|
CQM::IndividualResult.where(correlation_id: test_execution_id).delete
CQM::TestExecutionPatient.where(correlation_id: test_execution_id).delete
end
ProductTest.in(id: product_test_ids).delete
end
def generate_patients(job_id = nil)
if product.randomize_patients
# If we're using a "slim test deck", don't pass in any random IDs
random_ids = slim_test_deck? ? [] : gather_patient_ids
Cypress::PopulationCloneJob.new('test_id' => id, 'patient_ids' => master_patient_ids,
'scoop_and_filter' => true, 'randomization_ids' => random_ids,
'include_virtual' => (measure_ids & APP_CONSTANTS['telehealth_ineligible_measures']),
'randomize_demographics' => true, 'generate_provider' => product.c4_test, 'job_id' => job_id).perform
else
Cypress::PopulationCloneJob.new('test_id' => id, 'patient_ids' => master_patient_ids,
'scoop_and_filter' => true, 'disable_randomization' => true).perform
end
end
def archive_patients
file = Tempfile.new("product_test-#{id}.zip")
pat_arr = patients.to_a
if product.duplicate_patients && _type != 'FilteringTest'
prng = Random.new(rand_seed.to_i)
# ids of all patients in IPP
ids = results.where('IPP' => { '$gt' => 0 }).collect(&:patient_id)
pat_arr = sample_and_duplicate_patients(pat_arr, ids, random: prng) if ids.present?
end
Cypress::PatientZipper.zip(file, pat_arr, :qrda)
patient_archive.store!(file)
file = Tempfile.new("product_test-html-#{id}.zip")
Cypress::PatientZipper.zip(file, pat_arr, :html)
html_archive.store!(file)
save
end
def sample_and_duplicate_patients(pat_arr, ids, random: Random.new)
car = ::Validators::CalculatingAugmentedRecords.new(measures, [], id)
dups = patients.find(ids)
# Don't randomize clinical_data for hybrid measures
pat_arr, dups = randomize_clinical_data(pat_arr, dups, random) unless hybrid_measures?
# choose up to 3 duplicate patients
dups.sample(random.rand(1..3), random:).each do |pat|
prng_repeat = Random.new(rand_seed.to_i)
dup_pat, pat_augments, old_pat = pat.duplicate_randomization(augmented_patients, random: prng_repeat)
# only add if augmented patient validates
if car.validate_calculated_results(dup_pat, effective_date:, orig_product_patient: old_pat)
augmented_patients << pat_augments
pat_arr << dup_pat
else
augmented_patients << { original_patient_id: old_pat.id,
first: [old_pat.first_names, old_pat.first_names], last: [old_pat.familyName, old_pat.familyName] }
pat_arr << old_pat
end
end
pat_arr
end
def randomize_clinical_data(pat_arr, dups, random)
# Pick a patient to clinically randomize, then delete it from dups (so it doesn't get duplicated also)
# And delete it from pat_arr so we don't return the whole patient too
# TODO: check... why not randomize if there is one duplicate?
return [pat_arr, dups] if dups.count < 1
clinical_pat = dups.sample(random:)
dups.delete(clinical_pat)
pat_arr.delete(clinical_pat)
# Re-add clinically randomized patients (patient split in two across date or data element type)
# use end and start dates for correct comparison format
[pat_arr.concat(Cypress::ClinicalRandomizer.randomize(clinical_pat, end_date, start_date, random:)), dups]
end
def calculate
MeasureEvaluationJob.perform_later(self, {})
end
def measures
bundle.measures.in(hqmf_id: measure_ids)
end
def execute(_params)
raise NotImplementedError
end
def results
CQM::IndividualResult.where(correlation_id: id.to_s)
end
%i[ready queued building errored].each do |test_state|
define_method test_state do
self.state = test_state
save
end
end
def status
Rails.cache.fetch("#{cache_key}/status") do
grouped_tasks = tasks.group_by(&:status)
if grouped_tasks.key?('failing') && grouped_tasks['failing'].count.positive?
'failing'
elsif grouped_tasks.key?('passing') && grouped_tasks['passing'].count == tasks.size
'passing'
elsif grouped_tasks.key?('errored') && grouped_tasks['errored'].count.positive?
'errored'
else
'incomplete'
end
end
end
def most_recent_task_status(task_type)
most_recent_product_test_status&.[](task_type.to_s)
end
def most_recent_task_status_updated_at(task_type)
most_recent_product_test_status_updated_at&.[](task_type.to_s)
end
def task_status(task_type)
cached_status = most_recent_task_status(task_type)
return cached_status if cached_status
task = tasks.find_by(_type: task_type.to_s)
task&.computed_status || 'incomplete'
rescue StandardError
'incomplete'
end
def refresh_most_recent_task_status!(task)
return unless task&._type
self.most_recent_product_test_status ||= {}
self.most_recent_product_test_status_updated_at ||= {}
recent_execution = task.most_recent_execution
status = task.status_for_execution(recent_execution)
updated_at = recent_execution&.updated_at
most_recent_product_test_status[task._type] = status
most_recent_product_test_status_updated_at[task._type] = updated_at
self.class.where(id:).set(
"most_recent_product_test_status.#{task._type}" => status,
"most_recent_product_test_status_updated_at.#{task._type}" => updated_at
)
end
def remove_most_recent_task_status!(task)
return unless task&._type && most_recent_product_test_status
most_recent_product_test_status.delete(task._type)
most_recent_product_test_status_updated_at&.delete(task._type)
self.class.where(id:).unset(
"most_recent_product_test_status.#{task._type}",
"most_recent_product_test_status_updated_at.#{task._type}"
)
end
def rebuild_most_recent_product_test_status!
self.most_recent_product_test_status = {}
self.most_recent_product_test_status_updated_at = {}
tasks.each do |task|
recent_execution = task.most_recent_execution
most_recent_product_test_status[task._type] = task.status_for_execution(recent_execution)
most_recent_product_test_status_updated_at[task._type] = recent_execution&.updated_at
end
save!
end
# The name to display on a button to view this test (e.g., Previous/Next Test)
def button_short_name
cms_id.nil? ? name : cms_id
end
def effective_date
end_date.to_i
end
def measure_period_start
start_date.to_i
end
def start_date
if timing_constraint? && product.shift_patients
Date.parse(APP_CONSTANTS['timing_constraints'].detect { |tc| measure_ids.include? tc['hqmf_id'] }.start_time).in_time_zone
else
Time.at(product.measure_period_start).in_time_zone
end
end
def end_date
if timing_constraint? && product.shift_patients
Date.parse(APP_CONSTANTS['timing_constraints'].detect { |tc| measure_ids.include? tc['hqmf_id'] }.end_time).in_time_zone
else
Time.at(product.effective_date).in_time_zone
end
end
def timing_constraint?
APP_CONSTANTS['timing_constraints'].any? { |tc| measure_ids.include? tc['hqmf_id'] }
end
def additional_shift
shifted_start = Date.parse(APP_CONSTANTS['timing_constraints'].detect { |tc| measure_ids.include? tc['hqmf_id'] }.start_time).in_time_zone
product_start = Time.at(product.measure_period_start).in_time_zone
shifted_start - product_start
end
def update_with_checklist_tests(checklist_test_params)
update(checklist_test_params)
# reverse_each as criteria may be deleted as iterating
checked_criteria.reverse_each(&:change_criteria)
checked_criteria.each(&:validate_criteria)
save!
end
def self.most_recent
by_updated_at.first
end
def most_recent_task_execution_incomplete?
tasks.any? && tasks[0].most_recent_execution&.incomplete?
end
def most_recent_task_execution
tasks[0].most_recent_execution
end
# Are any of the measures in this test a Hybrid measure
def hybrid_measures?
!!measures.map(&:hqmf_id).intersect?(APP_CONSTANTS['result_measures'].map(&:hqmf_id))
end
# Are any of the measures in this test EH
def eh_measures?
measures.pluck(:reporting_program_type).include?('eh')
end
# Are any of the measures in this test EH
def ep_measures?
measures.pluck(:reporting_program_type).include?('ep')
end
def submission_program
if eh_measures?
return APP_CONSTANTS['oqr_measures'].intersection(measure_ids).blank? ? 'HQR_IQR' : 'HQR_OQR'
elsif ep_measures?
return 'MIPS_INDIV'
end
false
end
# A measure test has a C1 Task if the product has C1 criteria, or C3 criteria with EH measures
def c1_task?
product.c1_test || (product.c3_test && eh_measures?)
end
# A measure test has a C2 Task if the product has C2 criteria, or C3 criteria with EP measures
def c2_task?
product.c2_test || (product.c3_test && ep_measures?)
end
# A measure test has a C3 Cat I Task if the product has C3 and EH measures
def c3_cat1_task?
product.c3_test && eh_measures?
end
# A measure test has a C3 Cat III Task if the product has C3 and EP measures
def c3_cat3_task?
product.c3_test && ep_measures?
end
private
def gather_patient_ids
aggregate_id_list = []
# if it's a cvu_plus product
if product.cvuplus
# then check if vendor and/or patients are included in product
if product.vendor_patients
# If so, add appropriate vendor patient ids
aggregate_id_list.concat product.vendor_patient_ids
end
if product.bundle_patients
# If so, add appropriate bundle patient ids
aggregate_id_list.concat bundle.patients.pluck(:id)
end
# Check for neither as an edge case
if aggregate_id_list == []
# In theory, this should never happen.
Rails.logger.error 'User was able to select an option for which there were no template patients.'
end
# For cert products (the only option to cvu, hence the else) just grab bundle patients
else
aggregate_id_list.concat bundle.patients.pluck(:id)
end
aggregate_id_list
end
# Returns a listing of all ids for patients in the IPP
def patients_in_ipp_and_greater
Patient.find(gather_patient_ids).keep_if do |p|
p.patient_relevant?(measures.pluck(:_id), ['IPP'])
end.pluck(:_id)
end
def patients_in_ippex
Patient.find(gather_patient_ids).keep_if do |p|
p.patient_relevant?(measures.pluck(:_id), ['IPPEX'])
end.pluck(:_id)
end
# Returns a listing of all ids for patients in the Numerator
def patient_in_numerator
Patient.find(gather_patient_ids).keep_if do |p|
p.patient_relevant?(measures.pluck(:_id), ['NUMER'])
end.pluck(:_id)
end
# Returns a listing of all ids for patients in the Denominator
def patients_in_denominator_and_greater
Patient.find(gather_patient_ids).keep_if do |p|
p.patient_relevant?(measures.pluck(:_id), ['DENOM'])
end.pluck(:_id)
end
# Returns a listing of all ids for patients in the Measure Population
def patients_in_measure_population_and_greater
Patient.find(gather_patient_ids).keep_if do |p|
p.patient_relevant?(measures.pluck(:_id), ['MSRPOPL'])
end.pluck(:_id)
end
# Returns a listing of all ids for patients in the Measure Population
def patients_in_measure_population_observation
Patient.find(gather_patient_ids).keep_if do |p|
p.patient_relevant?(measures.pluck(:_id), ['OBSERV'])
end.pluck(:_id)
end
# Returns a listing of all ids for patients in the Measure Population
def patients_in_high_value_populations
Patient.find(gather_patient_ids).keep_if do |p|
p.patient_relevant?(measures.pluck(:_id), %w[NUMER NUMEX DENEXCEP DENEX])
end.pluck(:_id)
end
def master_patient_ids
mpl_ids = patients_in_ipp_and_greater
return randomize_master_patient_ids(mpl_ids) if product.randomize_patients
mpl_ids.compact
end
def randomize_master_patient_ids(mpl_ids)
denom_ids = pick_denom_ids
msrpopl_ids = pick_msrpopl_ids
ipp_ids = (mpl_ids - denom_ids - msrpopl_ids)
ippex_id = patients_in_ippex.sample(1)
# Pick 3 IDs from the IPP unless test includes hybrid measures.
ipp_count = hybrid_measures? ? test_deck_max : 3
(ipp_ids.sample(ipp_count) + ippex_id + denom_ids + msrpopl_ids).compact
end
def pick_denom_ids
# numer_id ensures we get at least one patient who is in the Numerator, no matter what
numer_id = patient_in_numerator.sample
denom_ids = patients_in_denominator_and_greater
# If there are a lot of patients in denom_ids (usually when the IPP and denominator are the same thing),
# pull out the numerator/Denex/Denexcep patients as high value (this is numer_ids), then sample from the rest to get to TEST_DECK_MAX
# NOTE: "a lot" is defined by the relation to "TEST_DECK_MAX" on the product,
if denom_ids.count > (test_deck_max - 1)
high_value_ids = patients_in_high_value_populations
high_value_ids = high_value_ids.sample(test_deck_max - 1)
denom_ids = high_value_ids + denom_ids.sample(3)
end
patients_in_denominator_and_greater.empty? ? [numer_id] : (denom_ids << numer_id).uniq
end
def pick_msrpopl_ids
# Look for patients with observations first
observ_ids = patients_in_measure_population_observation.sample(test_deck_max)
# If there are patient with observations, limit MSRPOPL to 3
msrpopl_count = observ_ids.empty? ? test_deck_max : 3
msrpopl_ids = patients_in_measure_population_and_greater.sample(msrpopl_count)
(observ_ids + msrpopl_ids).compact
end
def generate_random_seed
# create and store a new random seed for debugging repeatability
self.rand_seed = Random.new_seed.to_s unless rand_seed
end
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/PerceivedComplexity
# Adds 0's for all missing populations (e.g., if DENEX is 0) or missing demographics (e.g., no Payer 349 in the DENEX)
def expected_results_with_all_supplemental_codes
# Since this is a CMS IG requirement, only do this for CVU+ or C3 tests
return expected_results unless product.cvuplus? || product.c3_test?
gender_codes = bundle.randomization['genders']&.map(&:code)
required_codes = { 'PAYER' => %w[1 2 6 9 349], 'SEX' => gender_codes, 'RACE' => %w[2106-3 2076-8 2054-5 2028-9 1002-5 2131-1],
'ETHNICITY' => %w[2135-2 2186-5] }.freeze
equivalent_codes = { '9' => '349', '349' => '9', 'M' => '248153007', '248153007' => 'M', 'F' => '248152002', '248152002' => 'F' }
new_hash = expected_results
new_hash.each_value do |pop_set_hash|
pop_set_hash.each_value do |pop_set|
sup_data = pop_set['supplemental_data']
%w[IPP DENOM NUMER NUMEX DENEX DENEXCEP MSRPOPL MSRPOPLEX].each do |pop_key|
next unless pop_set[pop_key]
sup_data[pop_key] = { 'RACE' => {}, 'ETHNICITY' => {}, 'SEX' => {}, 'PAYER' => {} } unless sup_data[pop_key]
required_codes.each do |sup_data_type, codes|
codes.each do |code|
unless sup_data[pop_key][sup_data_type][code] || sup_data[pop_key][sup_data_type][equivalent_codes[code]]
sup_data[pop_key][sup_data_type][code] = 0
end
end
end
end
end
end
new_hash
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/PerceivedComplexity
end
# rubocop:enable Metrics/ClassLength