lib/health-data-standards/models/cqm/measure.rb in health-data-standards-3.4.5 vs lib/health-data-standards/models/cqm/measure.rb in health-data-standards-3.4.6
- old
+ new
@@ -27,10 +27,13 @@
field :measure_period, type: Hash
field :measure_attributes, type: Hash
field :populations, type: Array
field :preconditions, type: Hash
field :hqmf_document, type: Hash
+
+ embeds_many :prefilters
+
scope :top_level_by_type , ->(type){where({"type"=> type}).any_of({"sub_id" => nil}, {"sub_id" => "a"})}
scope :top_level , any_of({"sub_id" => nil}, {"sub_id" => "a"})
scope :order_by_id_sub_id, order_by([["id", :asc],["sub_id", :asc]])
index oids: 1
@@ -133,8 +136,81 @@
@crit << HQMF::DataCriteria.from_json(k,v)
end
end
@crit
end
+
+ # Builds the query hash to pass to MongoDB
+ # Calling this method will create Prefilters if they do not exist on the
+ # measure
+ def prefilter_query!(effective_time)
+ self.build_pre_filters! if self.prefilters.empty?
+
+ if self.prefilters.count == 1
+ self.prefilters.first.build_query_hash(effective_time)
+ else
+ self.prefilters.inject({}) do |query, pf|
+ query.merge(pf.build_query_hash(effective_time)) do |key, new_val, old_val|
+ new_val.merge(old_val)
+ end
+ end
+ end
+ end
+
+ # For submeasures, this will return something like IPP_1
+ def ipp_id
+ ipp_hqmf_id = self.population_ids['IPP']
+ pop_id, pop_criteria = hqmf_document['population_criteria'].find do |population_id, population_criteria|
+ population_criteria['hqmf_id'] == ipp_hqmf_id
+ end
+ pop_id
+ end
+
+ def build_pre_filters!
+ dc = self.data_criteria.inject({}) do |all_dc, single_dc|
+ key = single_dc.keys.first
+ value = single_dc.values.first
+ all_dc[key] = value
+ all_dc
+ end
+ dc.each_pair do |criteria_name, data_criteria|
+ if data_criteria['definition'] == 'patient_characteristic_birthdate'
+ if data_criteria_in_population?(self.ipp_id, criteria_name)
+ prefilter = Prefilter.new(record_field: 'birthdate',
+ effective_time_based: true)
+ if data_criteria['temporal_references']
+ data_criteria['temporal_references'].each do |tr|
+ if tr['type'] == 'SBS' && tr['reference'] == 'MeasurePeriod'
+ years = nil
+ if tr['range']['high']
+ prefilter.comparison = '$gte'
+ years = tr['range']['high']['value'].to_i
+ elsif tr['range']['low']
+ prefilter.comparison = '$lte'
+ years = tr['range']['low']['value'].to_i
+ end
+
+ prefilter.effective_time_offset = 1 + years
+ self.prefilters << prefilter
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+
+ private
+
+ def data_criteria_in_population?(population_id, criteria_name)
+ criteria_in_precondition?(self.hqmf_document['population_criteria'][population_id]['preconditions'], criteria_name)
+ end
+
+ def criteria_in_precondition?(preconditions, criteria_name)
+ preconditions.any? do |precondition|
+ (precondition['reference'] == criteria_name) ||
+ (precondition['preconditions'] && criteria_in_precondition?(precondition['preconditions'], criteria_name))
+ end
+ end
end
end
-end
\ No newline at end of file
+end