savonarola/pulse_meter_core

View on GitHub
lib/pulse_meter/sensor/hashed_counter.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'json'

# Static hashed counter to count values by multiple keys
module PulseMeter
  module Sensor
    class HashedCounter < Counter

      # Increments counter value by 1 for given key
      # @param key [String] key to be incremented
      def incr(key)
        event({key => 1})
      end

      # Returs data stored in counter
      # @return [Hash]
      def value
        redis.
          hgetall(value_key).
          inject(Hash.new(0)) {|h, (k, v)| h[k] = v.to_i; h}
      end

      private

      # Processes events for multiple keys
      # @param data [Hash] hash where keys represent counter keys
      #   and values are increments for their keys
      def process_event(data)
        data.each_pair do |k, v|
          command_aggregator.hincrby(value_key, k, v.to_i)
          command_aggregator.hincrby(value_key, :total, v.to_i)
        end
      end

    end
  end
end