Class: GraphiteMetric::Raw

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#extract_headers, #float

Constructor Details

#initialize(raw) ⇒ Raw

Returns a new instance of Raw.



8
9
10
11
# File 'lib/graphite-metric/raw.rb', line 8

def initialize(raw)
  @raw = raw
  populate_from_raw
end

Instance Attribute Details

#metricsObject (readonly)

Returns the value of attribute metrics.



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

def metrics
  @metrics
end

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

Instance Method Details

#grouped_metricsObject

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



26
27
28
29
30
31
32
33
34
# File 'lib/graphite-metric/raw.rb', line 26

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

#populate_from_rawObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/graphite-metric/raw.rb', line 61

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

    metric_name, from, to, step  = extract_headers(raw_headers)

    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



18
19
20
21
# File 'lib/graphite-metric/raw.rb', line 18

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

#summary_metricsObject

It’s most efficient to use the raw metrics directly. As of graphite 0.9.10, these values are available in the headers when using cactiStyle function: graphite.readthedocs.org/en/0.9.10/functions.html#graphite.render.functions.cactiStyle



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/graphite-metric/raw.rb', line 40

def summary_metrics
  return @summary if @summary

  @summary = {}

  @raw.split("\n").each do |raw|
    raw_headers, raw_metrics = raw.split("|")

    metric_name, from, to, step  = extract_headers(raw_headers)
    metrics = raw_metrics.split(",").map { |v| float(v) }

    @summary[metric_name] = {
      :min => metrics.min,
      :max => metrics.max,
      :avg => float(metrics.reduce(:+) / metrics.size).round(3)
    }
  end

  @summary
end

#timeshift(offset) ⇒ Object



13
14
15
16
# File 'lib/graphite-metric/raw.rb', line 13

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