Class: GraphiteMetric::Raw

Inherits:
Object
  • Object
show all
Defined in:
lib/graphite-metric/raw.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Raw

Returns a new instance of Raw.



5
6
7
8
# File 'lib/graphite-metric/raw.rb', line 5

def initialize(raw)
  @raw = raw
  populate_from_raw
end

Instance Attribute Details

#metricsObject (readonly)

Returns the value of attribute metrics.



3
4
5
# File 'lib/graphite-metric/raw.rb', line 3

def metrics
  @metrics
end

#rawObject (readonly)

Returns the value of attribute raw.



3
4
5
# File 'lib/graphite-metric/raw.rb', line 3

def raw
  @raw
end

Instance Method Details

#grouped_metricsObject

Cannot be chained unlike the previous methods because the other methods might modify @metrics.



23
24
25
26
27
28
29
30
31
# File 'lib/graphite-metric/raw.rb', line 23

def grouped_metrics
  @metrics.inject({}) do |result, metric|
    (result[metric[:key]] ||= []) << {
      :timestamp => metric[:timestamp],
      :value     => metric[:value]
    }
    result
  end
end

#populate_from_rawObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/graphite-metric/raw.rb', line 33

def populate_from_raw
  @metrics = []
  @raw.split("\n").each do |raw|
    raw_headers, raw_metrics = raw.split("|")

    metric_name, from, to, step  = raw_headers.split(",")
    
    current_step = 0
    raw_metrics.split(",").each do |raw_metric|
      @metrics << {
        :key       => metric_name,
        :timestamp => from.to_i + current_step,
        :value     => Float(raw_metric)
      }
      current_step += step.to_i
    end
  end
  self
end

#roundObject



15
16
17
18
# File 'lib/graphite-metric/raw.rb', line 15

def round
  @metrics.each { |metric| metric[:value] = metric[:value].round }
  self
end

#timeshift(offset) ⇒ Object



10
11
12
13
# File 'lib/graphite-metric/raw.rb', line 10

def timeshift(offset)
  @metrics.each { |metric| metric[:timestamp] += offset }
  self
end