Sha256: 1c2ddab255a79b99bf8b85047ca4d91cf4222d074e525190c9a708f92883c552

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'em-hiredis'
require_relative './helpers'

module Rstatsd
  class Collector < EventMachine::Connection
    include Rstatsd::Helpers

    def initialize
      super
      @redis = EM::Hiredis.connect
    end

    def post_init
    end

    def receive_data(data)
      bits = data.split(':')
      key = format_key(bits.first)

      fields = bits.last.split("|")
      case fields[1]
      when 'c'
        if fields[0] == '1'
          @redis.incr(key).callback do |value|
            @redis.rpush(counter_key_name(key), "#{value}:#{Time.now.to_i}")
          end
        elsif fields[0] == '-1'
          @redis.decr(key).callback do |value|
            @redis.rpush(counter_key_name(key), "#{value}:#{Time.now.to_i}")
          end
        end
        @redis.ltrim(counter_key_name(key), 10000)
      when 'ms'
        #update timer
        @redis.rpush(timer_key_name(key), "#{fields[0]}:#{Time.now.to_i}")
        @redis.ltrim(timer_key_name(key), 10000)
      else
        # invalid update
      end
    end

    def unbind
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rstatsd-0.2 lib/rstatsd/collector.rb