-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_controller.rb
More file actions
309 lines (281 loc) · 9.67 KB
/
Copy pathweb_controller.rb
File metadata and controls
309 lines (281 loc) · 9.67 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
# frozen_string_literal: true
require 'bundler/setup'
require 'sinatra'
require 'cqm-reports'
require 'cqm/models'
require 'rest-client'
require 'rack'
require 'rack/contrib' # Includes the JSONBodyParser middleware
require 'jwt'
require 'newrelic_rpm'
puts "Loading QRDA Export Service"
# Override the as_json method to ensure the _id is displayed as
# just the _id value as a string in the QRDA XML, "<_id>".
# Without this override it will be serialized as extended
# BSON::JSON, "{$oid => "<_id>"}"
module BSON
class ObjectId
def as_json(*args)
to_s.as_json
end
end
end
Mongoid.load!("config/mongoid.yml")
use Rack::JSONBodyParser
POPULATION_ABBR = {
"initialPopulation" => "IPP",
"measurePopulation" => "MSRPOPL",
"measurePopulationExclusion" => "MSRPOPLEX",
"denominator" => "DENOM",
"numerator" => "NUMER",
"numeratorExclusion" => "NUMEX",
"denominatorException" => "DENEXCEP",
"denominatorExclusion" => "DENEX",
"stratification" => "STRAT",
"measureObservation" => "OBSERV",
"measurePopulationObservation" => "OBSERV"
}
# Implementation pre-reqs
# 1. Parsing JSON input ✅
# 2. Porting over html generation from bonnie ✅
# 3. JWT verification
# 4. Data requirements: ✅ what inputs do we need and where do we get that data from?
# Looks like we'll only need the madie Measure object. It contains the array of test cases
# and each testcase already contains an instance of QDM::Patient
# Service stand-up tasks
# 0. github repo ✅
# 1. Bundler setup ✅
# 2. Mongoid config ✅
# 3. Unit testing scaffolding ✅
# 4. RDoc scaffolding
# 5. Containerization ✅
# 6. Clean up require statements ✅
# 7. Clean up \class name ✅
# 8. README, including how to run locally instructions
# 9. Log formatting
put "/api/qrda" do
# Set return type
content_type 'application/json'
# TODO probably don't need access token here, will remove after SME confirmation
access_token = request.env["HTTP_Authorization"]
# Parse request params
measure_dto = request.params # Uses the Rack::JSONBodyParser middleware
# Prepare CQM Measure
if measure_dto["measure"].nil?
return [400, "Measure is empty."]
end
madie_measure = JSON.parse(measure_dto["measure"], max_nesting: 512)
measure = CQM::Measure.new(madie_measure) unless measure_dto["measure"].nil?
if measure.nil?
return [400, "Measure is empty."]
end
measure.source_data_criteria = build_source_data_criteria(measure_dto["sourceDataCriteria"])
measure.cms_id = measure.cms_id.nil? ? 'CMS0v0' : measure.cms_id
measure.hqmf_id = madie_measure["id"]
test_cases = measure_dto["testCases"]
qrda_errors = {}
html_errors = {}
patients = Array.new
generated_reports = Array.new # Array of each Patient's QRDA and HTML summary
# Generate QRDA XMLs and HTML patient summaries
test_cases.each_with_index do | test_case, idx |
patient = build_cqm_patient(idx, test_case)
patients.push patient # For the summary HTML
filename = "#{idx+1}_#{patient[:familyName]}_#{patient[:givenNames][0]}"
# generate QRDA
begin
qrda = Qrda1R5.new(patient, measure, measure_dto["options"].symbolize_keys).render
rescue Exception => e
qrda_errors[patient.id] = e
end
# attach the HTML export, or the error
begin
report = QdmPatient.new(patient, true).render
rescue Exception => e
html_errors[patient.id] = e
end
generated_reports.push << {filename:, qrda:, report:}
end
summary_report = summary_report(patients,
qrda_errors,
html_errors,
measure,
measure_dto["groupDTOs"])
return { summaryReport: summary_report, individualReports: generated_reports }.to_json
end
# Helper methods for rendering the Summary Report
helpers do
def strat_fail(test_case)
if test_case["stratifications"].nil?
false
end
test_case["stratifications"].any? { |strats| strats["stratificationDtos"].find { |strat| strat["pass"] == false }}
end
def pop_fail(test_case)
test_case["populations"].any? { |pop| pop["pass"] == false }
end
end
def summary_report(patients, qrda_errors, html_errors, measure, population_results)
erb "top_level_summary".to_sym, {}, {
measure: ,
records: patients,
html_errors: ,
qrda_errors: ,
population_crit_results: population_results,
population_abbr: POPULATION_ABBR
}
end
get "/api/health" do
puts "QRDA Export Service is up"
"QRDA Export Service is up"
end
def build_source_data_criteria(source_data_criteria)
data_criteria = Array.new
source_data_criteria.each do | criteria |
data_criteria.push map_source_data_criteria(criteria)
end
data_criteria
end
def map_source_data_criteria(criteria)
data_criteria = instantiate_model(criteria["type"])
data_criteria.codeListId = criteria["oid"]
data_criteria.description = criteria["description"]
data_criteria
end
def build_cqm_patient(idx, test_case)
qdm_patient = QDM::Patient.new(JSON.parse(test_case["json"]))
patient = CQM::Patient.new
patient[:id] = idx
patient.qdmPatient = qdm_patient
patient[:givenNames] = [test_case["title"]]
patient[:familyName] = test_case["series"]
patient[:pass] = true
if patient.qdmPatient.get_data_elements('patient_characteristic', 'payer').empty?
payer_codes = [{ 'code' => '1', 'system' => '2.16.840.1.113883.3.221.5', 'codeSystem' => 'SOP' }]
patient.qdmPatient.dataElements.push QDM::PatientCharacteristicPayer.new(dataElementCodes: payer_codes,
relevantPeriod: QDM::Interval.new(patient.qdmPatient.birthDatetime, nil))
end
patient
end
def instantiate_model(model_name)
case model_name
when "PatientEntity"
return QDM::PatientEntity.new
when "CarePartner"
return QDM::CarePartner.new
when "RelatedPerson"
return QDM::RelatedPerson.new
when "Practitioner"
return QDM::Practitioner.new
when "Organization"
return QDM::Organization.new
when "Location"
return QDM::Location.new
when "PhysicalExamOrder"
return QDM::PhysicalExamOrder.new
when "Participation"
return QDM::Participation.new
when "PatientCharacteristicSex"
return QDM::PatientCharacteristicSex.new
when "CareGoal"
return QDM::CareGoal.new
when "PatientCharacteristic"
return QDM::PatientCharacteristic.new
when "PatientCharacteristicEthnicity"
return QDM::PatientCharacteristicEthnicity.new
when "PatientCharacteristicRace"
return QDM::PatientCharacteristicRace.new
when "LaboratoryTestPerformed"
return QDM::LaboratoryTestPerformed.new
when "Symptom"
return QDM::Symptom.new
when "MedicationAdministered"
return QDM::MedicationAdministered.new
when "ProcedureRecommended"
return QDM::ProcedureRecommended.new
when "Diagnosis"
return QDM::Diagnosis.new
when "CommunicationPerformed"
return QDM::CommunicationPerformed.new
when "AssessmentPerformed"
return QDM::AssessmentPerformed.new
when "PatientCharacteristicClinicalTrialParticipant"
return QDM::PatientCharacteristicClinicalTrialParticipant.new
when "DeviceOrder"
return QDM::DeviceOrder.new
when "DiagnosticStudyPerformed"
return QDM::DiagnosticStudyPerformed.new
when "InterventionOrder"
return QDM::InterventionOrder.new
when "FamilyHistory"
return QDM::FamilyHistory.new
when "MedicationActive"
return QDM::MedicationActive.new
when "LaboratoryTestOrder"
return QDM::LaboratoryTestOrder.new
when "DiagnosticStudyOrder"
return QDM::DiagnosticStudyOrder.new
when "SubstanceOrder"
return QDM::SubstanceOrder.new
when "PatientCharacteristicPayer"
return QDM::PatientCharacteristicPayer.new
when "PatientCharacteristicExpired"
return QDM::PatientCharacteristicExpired.new
when "AssessmentOrder"
return QDM::AssessmentOrder.new
when "AssessmentRecommended"
return QDM::AssessmentRecommended.new
when "ImmunizationAdministered"
return QDM::ImmunizationAdministered.new
when "SubstanceAdministered"
return QDM::SubstanceAdministered.new
when "EncounterPerformed"
return QDM::EncounterPerformed.new
when "EncounterOrder"
return QDM::EncounterOrder.new
when "EncounterRecommended"
return QDM::EncounterRecommended.new
when "ProcedurePerformed"
return QDM::ProcedurePerformed.new
when "Allergy/Intolerance"
return QDM::AllergyIntolerance.new
when "PhysicalExamRecommended"
return QDM::PhysicalExamRecommended.new
when "PatientCharacteristicBirthdate"
return QDM::PatientCharacteristicBirthdate.new
when "AdverseEvent"
return QDM::AdverseEvent.new
when "DeviceRecommended"
return QDM::DeviceRecommended.new
when "MedicationDischarge"
return QDM::MedicationDischarge.new
when "InterventionPerformed"
return QDM::InterventionPerformed.new
when "LaboratoryTestRecommended"
return QDM::LaboratoryTestRecommended.new
when "MedicationDispensed"
return QDM::MedicationDispensed.new
when "DiagnosticStudyRecommended"
return QDM::DiagnosticStudyRecommended.new
when "ImmunizationOrder"
return QDM::ImmunizationOrder.new
when "PatientCareExperience"
return QDM::PatientCareExperience.new
when "ProviderCareExperience"
return QDM::ProviderCareExperience.new
when "ProcedureOrder"
return QDM::ProcedureOrder.new
when "MedicationOrder"
return QDM::MedicationOrder.new
when "SubstanceRecommended"
return QDM::SubstanceRecommended.new
when "InterventionRecommended"
return QDM::InterventionRecommended.new
when "PhysicalExamPerformed"
return QDM::PhysicalExamPerformed.new
when "CommunicationNotPerformed"
return QDM::CommunicationPerformed.new
else
raise "Unsupported data type: #{model_name}"
end
end