graphite-metric 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -70,7 +70,7 @@ gmps.map(&:to_s)
70
70
  ### GraphiteMetric::Raw
71
71
 
72
72
  It supports single as well as multiple raw metrics (multiple targets).
73
- For simplicity's, all examples will use single metrics:
73
+ For simplicity, all examples will use single metrics:
74
74
 
75
75
  ```ruby
76
76
  gmr = GraphiteMetric::Raw.new("my.metric,1336559725,1336559845,60|34.999,35.10,33.0")
@@ -33,6 +33,31 @@ module GraphiteMetric
33
33
  end
34
34
  end
35
35
 
36
+ # It's most efficient to use the raw metrics directly.
37
+ # As of graphite 0.9.10, these values are available in the headers
38
+ # when using cactiStyle function:
39
+ # http://graphite.readthedocs.org/en/0.9.10/functions.html#graphite.render.functions.cactiStyle
40
+ def summary_metrics
41
+ return @summary if @summary
42
+
43
+ @summary = {}
44
+
45
+ @raw.split("\n").each do |raw|
46
+ raw_headers, raw_metrics = raw.split("|")
47
+
48
+ metric_name, from, to, step = extract_headers(raw_headers)
49
+ metrics = raw_metrics.split(",").map { |v| float(v) }
50
+
51
+ @summary[metric_name] = {
52
+ :min => metrics.min,
53
+ :max => metrics.max,
54
+ :avg => float(metrics.reduce(:+) / metrics.size).round(3)
55
+ }
56
+ end
57
+
58
+ @summary
59
+ end
60
+
36
61
  def populate_from_raw
37
62
  @metrics = []
38
63
  @raw.split("\n").each do |raw|
@@ -5,7 +5,7 @@ module GraphiteMetric
5
5
  def float(raw_metric)
6
6
  Float(raw_metric)
7
7
  rescue
8
- 0
8
+ 0.0
9
9
  end
10
10
 
11
11
  def extract_headers(raw_headers)
@@ -1,3 +1,3 @@
1
1
  module GraphiteMetric
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -39,5 +39,17 @@ module GraphiteMetric
39
39
  gmr = GraphiteMetric::Raw.new(metric_with_functions_raw)
40
40
  gmr.metrics.must_equal metric_with_functions_converted
41
41
  end
42
+
43
+ describe "summarises" do
44
+ it "single metrics" do
45
+ gmr = GraphiteMetric::Raw.new(single_metric_raw)
46
+ gmr.summary_metrics.must_equal single_metric_summarised
47
+ end
48
+
49
+ it "multiple metrics" do
50
+ gmr = GraphiteMetric::Raw.new(multiple_metrics_raw)
51
+ gmr.summary_metrics.must_equal multiple_metrics_summarised
52
+ end
53
+ end
42
54
  end
43
55
  end
@@ -5,6 +5,24 @@ include TestHelpers
5
5
 
6
6
  module GraphiteMetric
7
7
  describe Util do
8
+ describe "float" do
9
+ it "converts string" do
10
+ Util.float("1.8008138").must_equal 1.8008138
11
+ end
12
+
13
+ it "converts integer" do
14
+ converted = Util.float(1)
15
+ converted.must_be_instance_of Float
16
+ converted.to_s.must_equal "1.0"
17
+ end
18
+
19
+ it "invalid value becomes 0.0" do
20
+ converted = Util.float("None")
21
+ converted.must_be_instance_of Float
22
+ converted.to_s.must_equal "0.0"
23
+ end
24
+ end
25
+
8
26
  describe "extract_raw_headers" do
9
27
  it "simple metrics" do
10
28
  Util.extract_headers("my.metric,1336559725,1336559845,60").must_equal(
@@ -32,6 +32,16 @@ module TestHelpers
32
32
  ]
33
33
  end
34
34
 
35
+ def single_metric_summarised
36
+ {
37
+ "my.metric" => {
38
+ :min => 0.000,
39
+ :max => 35.100,
40
+ :avg => 23.366
41
+ }
42
+ }
43
+ end
44
+
35
45
  def multiple_metrics_raw
36
46
  @multiple_metrics_raw ||= File.read("#{BASE_PATH}/test/fixtures/multiple_metrics.txt")
37
47
  end
@@ -71,6 +81,21 @@ module TestHelpers
71
81
  ]
72
82
  end
73
83
 
84
+ def multiple_metrics_summarised
85
+ {
86
+ "my.first.metric" => {
87
+ :min => 0.000,
88
+ :max => 9.210,
89
+ :avg => 5.848
90
+ },
91
+ "my.second.metric" => {
92
+ :min => 1.100,
93
+ :max => 3.500,
94
+ :avg => 2.533
95
+ }
96
+ }
97
+ end
98
+
74
99
  def multiple_metrics_grouped
75
100
  {
76
101
  "my.first.metric" => [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphite-metric
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-25 00:00:00.000000000 Z
12
+ date: 2012-07-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: guard-minitest