Sha256: f3678d0e3f97ea284b756a36befd936153d40ab14de01dc90010190c304027a7

Contents?: true

Size: 977 Bytes

Versions: 24

Compression:

Stored size: 977 Bytes

Contents

module Harness
  class Meter
    def initialize(name)
      @name = name

      raise Harness::NoCounter, "#{@name} is not being metered" unless redis.exists key
    end

    def per_second
      per 1.second
    end

    def per_minute
      per 1.minute
    end

    def per_hour
      per 1.hour
    end

    def per(rate, base = Time.now)
      gauge = Gauge.new :value => redis.zcount(key, base.to_f - rate, base.to_f)

      if words = rate_in_words(rate)
        gauge.name = "#{@name} per #{words}"
        gauge.id = "#{@name}-per-#{words}"
      else
        gauge.id = "#{@name} gauge"
      end

      gauge.time = Time.now

      gauge
    end

    private
    def key
      "meters/#{@name}"
    end

    def redis
      Harness.redis
    end

    def rate_in_words(rate)
      if rate < 1.minute
        "second"
      elsif rate >= 1.minute && rate < 1.hour
        "minute"
      elsif rate >= 1.hour && rate < 1.day
        "hour"
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
harness-0.2.3 lib/harness/meter.rb
harness-0.2.2 lib/harness/meter.rb
harness-0.2.1 lib/harness/meter.rb
harness-0.2.0 lib/harness/meter.rb