Class: Concen::Visit::Referral

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/concen/visit/referral.rb

Class Method Summary collapse

Class Method Details

.aggregate_count_by_domain(*args) ⇒ Object



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
# File 'app/models/concen/visit/referral.rb', line 17

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

  map = <<-EOF
    function() {
      if (this.domain != null) {
        emit(this.domain, this.count);
      };
    }
  EOF

  reduce = <<-EOF
    function(time, counts) {
      var count = 0;
      for (index in counts) { count += counts[index]; };
      return count;
    }
  EOF

  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"].to_i]
  end
end