Sha256: c08066004f2cfa3218e508fe2f1f37131b0221c555616e79369fc9f549674d78

Contents?: true

Size: 1.22 KB

Versions: 18

Compression:

Stored size: 1.22 KB

Contents

module Concen
  module Visit
    class Referral
      include Mongoid::Document

      store_in self.name.underscore.gsub("/", ".").pluralize

      field :hour, :type => Time
      field :url, :type => String
      field :domain, :type => String
      field :count, :type => Integer, :default => 1

      index :hour, :background => true
      index :url, :background => true
      index :domain, :background => true

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
concen-0.2.9 app/models/concen/visit/referral.rb
concen-0.2.8 app/models/concen/visit/referral.rb
concen-0.2.7 app/models/concen/visit/referral.rb
concen-0.2.6 app/models/concen/visit/referral.rb
concen-0.2.5 app/models/concen/visit/referral.rb
concen-0.2.4 app/models/concen/visit/referral.rb
concen-0.2.3 app/models/concen/visit/referral.rb
concen-0.2.2 app/models/concen/visit/referral.rb
concen-0.2.1 app/models/concen/visit/referral.rb
concen-0.2.0 app/models/concen/visit/referral.rb
concen-0.1.7 app/models/concen/visit/referral.rb
concen-0.1.6 app/models/concen/visit/referral.rb
concen-0.1.5 app/models/concen/visit/referral.rb
concen-0.1.4 app/models/concen/visit/referral.rb
concen-0.1.3 app/models/concen/visit/referral.rb
concen-0.1.2 app/models/concen/visit/referral.rb
concen-0.1.1 app/models/concen/visit/referral.rb
concen-0.1 app/models/concen/visit/referral.rb