Class: GraphiteMetric::Raw
- Inherits:
-
Object
- Object
- GraphiteMetric::Raw
- 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
- #timeshift(offset) ⇒ Object
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
#metrics ⇒ Object (readonly)
Returns the value of attribute metrics.
3 4 5 |
# File 'lib/graphite-metric/raw.rb', line 3 def metrics @metrics end |
#raw ⇒ Object (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_metrics ⇒ Object
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_raw ⇒ Object
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 |
#round ⇒ Object
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 |