Class: Concen::Response

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/concen/response.rb

Class Method Summary collapse

Class Method Details

.aggregate_average_runtime(*args) ⇒ Object



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
# File 'app/models/concen/response.rb', line 8

def self.aggregate_average_runtime(*args)
  options = args.extract_options!

  if options[:type]
    runtime_type = "#{options[:type]}_runtime"
  else
    runtime_type = "total_runtime"
  end

  map = "    function() {\n      emit(this.controller + \"#\" + this.action, this.\#{runtime_type});\n    }\n  EOF\n\n  reduce = <<-EOF\n    function(controller_action, runtimes) {\n      var runtime = 0;\n      for (index in runtimes) { runtime += runtimes[index]; };\n      return runtime/runtimes.length;\n    }\n  EOF\n\n  begin\n    results = self.collection.map_reduce(map, reduce, :out => {:inline => 1}, :raw => true)[\"results\"]\n    results = results.sort {|x,y| y[\"value\"] <=> x[\"value\"]}\n    results = results[0..options[:limit]-1] if options[:limit]\n    results = results.map do |result|\n      [result[\"_id\"], result[\"value\"]]\n    end\n  rescue\n    results = []\n  end\n\n  return results\nend\n"