Class: Dasht::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/dasht/metric.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetric

Returns a new instance of Metric.



17
18
19
20
21
22
# File 'lib/dasht/metric.rb', line 17

def initialize
  @checkpoints = List.new
  @data = List.new
  @last_item = nil
  @last_ts = nil
end

Instance Attribute Details

#checkpointsObject (readonly)

Returns the value of attribute checkpoints.



15
16
17
# File 'lib/dasht/metric.rb', line 15

def checkpoints
  @checkpoints
end

#dataObject (readonly)

Returns the value of attribute data.



15
16
17
# File 'lib/dasht/metric.rb', line 15

def data
  @data
end

Instance Method Details

#append(data, ts, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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_sObject



24
25
26
# File 'lib/dasht/metric.rb', line 24

def to_s
  return @data.to_s + " (last: #{@last_item})"
end

#trim_to(ts) ⇒ Object



44
45
46
47
48
49
50
51
52
# 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