Class MetricsPack

  1. lib/stella/core_ext.rb
Parent: Storable

A simple class for really fast serialized timing data. NOTE: We’re storing the serialized data directly to a redis sorted set so it’s important that each chunk of data in unique. Also, it’s possible to grab the stamp from the zrange using :with_scores. NOTE2: We bypass Storable’s to_csv and from_csv for speed. TODO: test speed difference.

Methods

public class

  1. from_csv
  2. from_json
  3. metric?
  4. new
  5. unpack

public instance

  1. kind
  2. pack
  3. quantize_stamp
  4. to_a
  5. to_csv
  6. to_s
  7. update

Constants

METRICS = [:rt, :sc, :sr, :fb, :lb, :rscs, :rshs, :rqcs, :rqhs]
TALLIES = [:n, :errors]

Public class methods

from_csv (str)
[show source]
# File lib/stella/core_ext.rb, line 360
  def self.from_csv(str)
    unpack str
  end
from_json (str)
[show source]
# File lib/stella/core_ext.rb, line 369
  def self.from_json(str)
    unpack(str)
  end
metric? (guess)
[show source]
# File lib/stella/core_ext.rb, line 372
  def self.metric?(guess)
    METRICS.member?(guess.to_s.to_sym)
  end
new (stamp=nil, uid=nil, n=nil)
[show source]
# File lib/stella/core_ext.rb, line 303
  def initialize(stamp=nil, uid=nil, n=nil)
    @stamp, @uid, @n = stamp, uid, n
    @stamp &&= @stamp.utc.to_i if Time === @stamp      
    self.class.field_names.each do |fname|
      self.send("#{fname}=", 0) unless fname == :id || self.send(fname)
    end                                                
    @score ||= 1.0
    @errors ||= 0
  end
unpack (str)
[show source]
# File lib/stella/core_ext.rb, line 363
  def self.unpack(str)
    return ArgumentError, "Cannot unpack nil" if str.nil?
    a = str.split(',')
    me = new
    me.update *a
  end

Public instance methods

kind ()
[show source]
# File lib/stella/core_ext.rb, line 312
  def kind
    :metric
  end
pack ()
[show source]
# File lib/stella/core_ext.rb, line 333
  def pack
    to_s
  end
quantize_stamp (quantum)

@stamp => 1281355304 (2010-08-09-12-01-44) quantize_stamp(1.day) => 1281312000 (2010-08-09) quantize_stamp(1.hour) => 1281355200 (2010-08-09-12) quantize_stamp(1.minute) => 1281355260 (2010-08-09-12-01)

[show source]
# File lib/stella/core_ext.rb, line 341
  def quantize_stamp(quantum)
    @stamp - (@stamp % quantum)
  end
to_a ()
[show source]
# File lib/stella/core_ext.rb, line 344
  def to_a
    field_names.collect { |field| 
      v = send(field) 
      if v.nil?
        field_types[field] == String ? 'unknown' : 0.0
      else
        field_types[field] == Float ? v.fineround : v
      end
    }
  end
to_csv ()
[show source]
# File lib/stella/core_ext.rb, line 357
  def to_csv
    to_s
  end
to_s ()
[show source]
# File lib/stella/core_ext.rb, line 354
  def to_s
    to_a.join(',')
  end
update (*args)

should be in the same order as the fields are defined (i.e. MetricsPack.field_names)

[show source]
# File lib/stella/core_ext.rb, line 317
  def update(*args)
    field_names.each_with_index do |field,idx| 
      val = args[idx]
      val = case field_types[field].to_s
      when 'Float' 
        val.to_f
      when 'Integer'
        val.to_i
      else
        val
      end
      send("#{field}=", val)
    end
    self
  end