Class: GraphiteMetric::Raw
- Inherits:
-
Object
- Object
- GraphiteMetric::Raw
- Includes:
- Util
- Defined in:
- lib/graphite-metric/raw.rb
Instance Attribute Summary collapse
-
#metrics ⇒ Object
readonly
Returns the value of attribute metrics.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
-
#grouped_metrics ⇒ Object
Cannot be chained unlike the previous methods because the other methods might modify @metrics.
-
#initialize(raw) ⇒ Raw
constructor
A new instance of Raw.
- #populate_from_raw ⇒ Object
- #round ⇒ Object
-
#summary_metrics ⇒ Object
It’s most efficient to use the raw metrics directly.
- #timeshift(offset) ⇒ Object
Methods included from Util
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
#metrics ⇒ Object (readonly)
Returns the value of attribute metrics.
6 7 8 |
# File 'lib/graphite-metric/raw.rb', line 6 def metrics @metrics end |
#raw ⇒ Object (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_metrics ⇒ Object
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_raw ⇒ Object
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 |
#round ⇒ Object
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_metrics ⇒ Object
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 |