Sha256: 602a7071367961be6f494a67ecc08fe2452ed03fcabf49ad1e82d443fc3b8f3f

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module Tabs
  module Metrics
    class Counter
      include Storage
      include Helpers

      attr_reader :key

      def initialize(key)
        @key = key
      end

      def increment
        timestamp = Time.now.utc
        Tabs::RESOLUTIONS.each do |resolution|
          increment_resolution(resolution, timestamp)
        end
        increment_total
        true
      end

      def stats(period, resolution)
        period = normalize_period(period, resolution)
        keys = smembers("stat:counter:#{key}:keys:#{resolution}")
        dates = keys.map { |k| extract_date_from_key(k, resolution) }
        values = mget(*keys).map(&:to_i)
        pairs = dates.zip(values)
        filtered_pairs = pairs.find_all { |p| period.cover?(p[0]) }
        filtered_pairs = fill_missing_dates(period, filtered_pairs, resolution)
        filtered_pairs.map { |p| Hash[[p]] }
      end

      def total
        get("stat:counter:#{key}:total").to_i
      end

      private

      def increment_resolution(resolution, timestamp)
        formatted_time = Tabs::Resolution.serialize(resolution, timestamp)
        stat_key = "stat:counter:#{key}:count:#{formatted_time}"
        sadd("stat:counter:#{key}:keys:#{resolution}", stat_key)
        incr(stat_key)
      end

      def increment_total
        incr("stat:counter:#{key}:total")
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tabs-0.6.1 lib/tabs/metrics/counter.rb