class QME::MapReduce::Builder

Builds Map and Reduce functions for a particular measure

Attributes

id[R]
params[R]

Public Class Methods

new(db, measure_def, params) click to toggle source

Create a new Builder @param [Hash] measure_def a JSON hash of the measure, field values may contain Erb directives to inject the values of supplied parameters into the map function @param [Hash] params a hash of parameter names (String or Symbol) and their values

# File lib/qme/map/map_reduce_builder.rb, line 62
def initialize(db, measure_def, params)
  @id = measure_def['id']
  @params = {}
  @db = db

  # normalize parameters hash to accept either symbol or string keys
  params.each do |name, value|
    @params[name.to_s] = value
  end
  @measure_def = measure_def
  @measure_def.parameters ||= {}
  @measure_def.parameters.each do |parameter, value|
    if !@params.has_key?(parameter)
      raise "No value supplied for measure parameter: #{parameter}"
    end
  end
  # if the map function is specified then replace any erb templates with their values
  # taken from the supplied params
  # always true for actual measures, not always true for unit tests
  if (@measure_def.map_fn)
    template = ERB.new(@measure_def.map_fn)
    context = Context.new(@db, @params)
    @measure_def.map_fn = template.result(context.get_binding)
  end
end

Public Instance Methods

finalize_function() click to toggle source

Get the reduce function for the measure, this is a simple wrapper for the reduce utility function specified in map-reduce-utils.js @return [String] the reduce function

# File lib/qme/map/map_reduce_builder.rb, line 98
def finalize_function
  reporting_period_start = Time.at(@params['effective_date']).prev_year.to_i
  reduce =
  "function (key, value) {
    var patient = value;
    patient.measure_id = \"#{@measure_def['id']}\";\n"
  if @params['test_id'] && @params['test_id'].class==BSON::ObjectId
    reduce += "  patient.test_id = new ObjectId(\"#{@params['test_id']}\");\n"
  end
  if @measure_def.sub_id
    reduce += "  patient.sub_id = \"#{@measure_def.sub_id}\";\n"
  end
  if @measure_def.nqf_id
    reduce += "  patient.nqf_id = \"#{@measure_def.nqf_id}\";\n"
  end

  reduce += "patient.effective_date = #{@params['effective_date']};
             if (patient.provider_performances) {
               var tmp = [];
               for(var i=0; i<patient.provider_performances.length; i++) {
                 var value = patient.provider_performances[i];
                 if (
                  // Early Overlap
                  ((value['start_date'] <= #{reporting_period_start} || value['start_date'] == null) && (value['end_date'] > #{reporting_period_start})) ||
                  // Late Overlap
                  ((value['start_date'] < #{@params['effective_date']}) && (value['end_date'] >= #{@params['effective_date']} || value['end_date'] == null)) ||
                  // Full Overlap
                  ((value['start_date'] <= #{reporting_period_start} || value['start_date'] == null) && (value['end_date'] >= #{@params['effective_date']} || value['end_date'] == null)) ||
                  // Full Containment
                  (value['start_date'] > #{reporting_period_start} && value['end_date'] < #{@params['effective_date']})
                 )
                 tmp.push(value);
               }
               if (tmp.length > 0) {
                  patient.provider_performances = tmp;
               } else {
                  sortedProviders = _.sortBy(patient.provider_performances, function(performance){return performance['end_date']});
                  patient.provider_performances = [_.last(sortedProviders)];
               }
             }
             return patient;}"

  reduce
end
map_function() click to toggle source

Get the map function for the measure @return [String] the map function

# File lib/qme/map/map_reduce_builder.rb, line 90
def map_function
  @measure_def.map_fn
end