Class: QME::MapReduce::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/qme/map/map_reduce_builder.rb

Overview

Builds Map and Reduce functions for a particular measure

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, measure_def, params) ⇒ Builder

Create a new Builder

Parameters:

  • measure_def (Hash)

    a JSON hash of the measure, field values may contain Erb directives to inject the values of supplied parameters into the map function

  • params (Hash)

    a hash of parameter names (String or Symbol) and their values



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
# 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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/qme/map/map_reduce_builder.rb', line 9

def id
  @id
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/qme/map/map_reduce_builder.rb', line 9

def params
  @params
end

Instance Method Details

#finalize_functionString

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

Returns:

  • (String)

    the reduce function



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
# 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_functionString

Get the map function for the measure

Returns:

  • (String)

    the map function



90
91
92
# File 'lib/qme/map/map_reduce_builder.rb', line 90

def map_function
  @measure_def.map_fn
end