class Dasht::Metric

Attributes

checkpoints[R]
data[R]

Public Class Methods

new() click to toggle source
# File lib/dasht/metric.rb, line 17
def initialize
  @checkpoints = List.new
  @data = List.new
  @last_item = nil
  @last_ts = nil
end

Public Instance Methods

append(data, ts) { |last_item, data| ... } click to toggle source
# File lib/dasht/metric.rb, line 28
def append(data, ts, &block)
  # Maybe checkpoint the time.
  if @last_ts == ts
    @last_item = yield(@last_item, data)
  else
    if @last_ts
      pointer = @data.append(@last_item)
      @checkpoints.append([@last_ts, pointer])
    end
    @last_ts = ts
    @last_item = nil
    @last_item = yield(@last_item, data)
  end
  return
end
enum(start_ts, end_ts = nil) click to toggle source
# File lib/dasht/metric.rb, line 54
def enum(start_ts, end_ts = nil)
  # Get a pointer to our location in the data.
  start_pointer = nil
  end_pointer = nil
  prev_p = nil
  @checkpoints.enum.each do |s, p|
    start_pointer ||= p if start_ts <= (s || 0)
    end_pointer   ||= prev_p if end_ts && end_ts <= (s || 0)
    break if start_pointer && (end_ts.nil? || end_pointer)
    prev_p = p
  end
  start_pointer ||= @data.tail_pointer
  end_pointer ||= @data.tail_pointer

  # Enumerate through the data, then tack on the last item.
  return Enumerator.new do |yielder|
    @data.enum(start_pointer, end_pointer).each do |data|
      yielder << data
    end
    # Maybe include the last item.
    if @last_item &&
       (start_ts <= @last_ts) &&
       (end_ts.nil? || (@last_ts < end_ts))
      yielder << @last_item
    end
  end
end
to_s() click to toggle source
# File lib/dasht/metric.rb, line 24
def to_s
  return @data.to_s + " (last: #{@last_item})"
end
trim_to(ts) click to toggle source
# File lib/dasht/metric.rb, line 44
def trim_to(ts)
  pointer = nil
  @checkpoints.trim_while do |s, p|
    pointer = p
    (s || 0) < ts
  end
  @data.trim_to(pointer)
  return
end