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



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
44
45
46
47
48
49
50
51
52
53
# File 'app/models/concen/response.rb', line 18

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 = <<-EOF
    function() {
      emit(this.controller + "#" + this.action, this.#{runtime_type});
    }
  EOF

  reduce = <<-EOF
    function(controller_action, runtimes) {
      var runtime = 0;
      for (index in runtimes) { runtime += runtimes[index]; };
      return runtime/runtimes.length;
    }
  EOF

  begin
    results = self.collection.map_reduce(map, reduce, :out => {:inline => 1}, :raw => true)["results"]
    results = results.sort {|x,y| y["value"] <=> x["value"]}
    results = results[0..options[:limit]-1] if options[:limit]
    results = results.map do |result|
      [result["_id"], result["value"]]
    end
  rescue
    results = []
  end

  return results
end