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
public instance
Constants
METRICS | = | [:rt, :sc, :sr, :fb, :lb, :rscs, :rshs, :rqcs, :rqhs] |
TALLIES | = | [:n, :errors] |
Public class methods
# File lib/stella/core_ext.rb, line 360 def self.from_csv(str) unpack str end
# File lib/stella/core_ext.rb, line 369 def self.from_json(str) unpack(str) end
# File lib/stella/core_ext.rb, line 372 def self.metric?(guess) METRICS.member?(guess.to_s.to_sym) end
# 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
# 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
@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)
# File lib/stella/core_ext.rb, line 341 def quantize_stamp(quantum) @stamp - (@stamp % quantum) end
# 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
should be in the same order as the fields are defined (i.e. MetricsPack.field_names)
# 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