Class: Cubicle::Aggregation::MapReduceHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/cubicle/aggregation/map_reduce_helper.rb

Class Method Summary collapse

Class Method Details

.generate_finalize_function(query) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cubicle/aggregation/map_reduce_helper.rb', line 55

def generate_finalize_function(query)
  <<FINALIZE
    function(key,value)
    {

  #{(query.measures.select{|m|m.aggregation_method == :average}).map do |m|
    "value.#{m.name}=value.#{m.name}/value.#{m.name}_count;"
  end.join("\n")}




    return value;
    }
FINALIZE
end

.generate_keys_string(query) ⇒ Object



6
7
8
# File 'lib/cubicle/aggregation/map_reduce_helper.rb', line 6

def generate_keys_string(query)
  "{#{query.dimensions.map{|dim|dim.to_js_keys}.flatten.join(", ")}}"
end

.generate_map_function(query) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/cubicle/aggregation/map_reduce_helper.rb', line 14

def generate_map_function(query)
  map="emit(#{generate_keys_string(query)},#{generate_values_string(query)});"
  if query.respond_to?(:expansions)
    query.expansions.reverse.each do |expansion|
      map = "for (#{expansion.index_variable} in #{expansion.expression}){ var #{expansion.value_variable} = #{expansion.expression}[#{expansion.index_variable}];#{map}}"                
    end if query.expansions
  end
  "function(){#{map}}"
end

.generate_reduce_functionObject



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
# File 'lib/cubicle/aggregation/map_reduce_helper.rb', line 24

def generate_reduce_function()
  <<REDUCE
  function(key,values){
	var output = {};
	values.forEach(function(doc){
for(var measure_name in doc){
    var measure_value = doc[measure_name];
    if (measure_value || measure_value == 0){
        if (typeof(measure_value) == "number") {
          output[measure_name] = output[measure_name] || 0;
          output[measure_name] += measure_value;
        }
        else if (measure_value) {
          output[measure_name] = output[measure_name] || {}
          if (typeof(measure_value)=="string")
            output[measure_name][measure_value] = true;
          if (typeof(measure_value)=="object") {
            for (var unique in measure_value){
              if (typeof(unique)=="string"){output[measure_name][unique]=true;}
            }
          }

        }
			}
		}
	  });     
	  return output;
  }
REDUCE
end

.generate_values_string(query) ⇒ Object



10
11
12
# File 'lib/cubicle/aggregation/map_reduce_helper.rb', line 10

def generate_values_string(query)
  "{#{query.measures.map{|measure|measure.to_js_keys}.flatten.join(", ")}}"
end