simple_metrics 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,5 +38,33 @@ module SimpleMetrics
38
38
  end
39
39
  end
40
40
 
41
+ def aggregate_values(dps)
42
+ raise SimpleMetrics::DataPoint::NonMatchingTypesError if has_non_matching_types?(dps)
43
+
44
+ dp = dps.first.dup
45
+ dp.value = if dp.counter?
46
+ sum(dps)
47
+ elsif dp.gauge?
48
+ sum(dps) / dps.size
49
+ elsif dp.event?
50
+ raise "Implement me!"
51
+ elsif dp.timing?
52
+ raise "Implement me!"
53
+ else
54
+ raise ArgumentError("Unknown data point type: #{dp}")
55
+ end
56
+ dp
57
+ end
58
+
59
+ private
60
+
61
+ def sum(dps)
62
+ dps.map { |dp| dp.value }.inject(0) { |result, value| result += value }
63
+ end
64
+
65
+ def has_non_matching_types?(dps)
66
+ dps.group_by { |dp| dp.type }.size != 1
67
+ end
68
+
41
69
  end
42
70
  end
@@ -34,7 +34,7 @@ module SimpleMetrics
34
34
  def query(bucket, from, to, target)
35
35
  if wild_card_query?(target)
36
36
  result = bucket.find_all_in_ts_range_by_wildcard(from, to, target)
37
- result = ArrayAggregation.aggregate(result, target)
37
+ result = aggregate(result, target)
38
38
  bucket.fill_gaps(from, to, result)
39
39
  elsif target.is_a?(String)
40
40
  result = bucket.find_all_in_ts_range_by_name(from, to, target)
@@ -46,6 +46,31 @@ module SimpleMetrics
46
46
 
47
47
  private
48
48
 
49
+ def aggregate(dps, target)
50
+ raise SimpleMetrics::DataPoint::NonMatchingTypesError if has_non_matching_types?(dps)
51
+
52
+ tmp = {}
53
+ dps.each do |dp|
54
+ dp.name = target
55
+ if tmp.key?(dp.ts)
56
+ tmp[dp.ts] << dp
57
+ else
58
+ tmp[dp.ts] = [dp]
59
+ end
60
+ end
61
+ tmp
62
+
63
+ result = []
64
+ tmp.each_pair do |ts, dps|
65
+ result << DataPoint.aggregate_values(dps)
66
+ end
67
+ result
68
+ end
69
+
70
+ def has_non_matching_types?(dps)
71
+ dps.group_by { |dp| dp.type }.size != 1
72
+ end
73
+
49
74
  def one_minute
50
75
  60
51
76
  end
@@ -22,7 +22,7 @@ module SimpleMetrics
22
22
  bucket = Bucket.first
23
23
 
24
24
  group_by_name(data_points) do |name, dps|
25
- dp = aggregate_values(dps)
25
+ dp = DataPoint.aggregate_values(dps)
26
26
  bucket.save(dp, ts)
27
27
  update_metric(dp, dps.size)
28
28
  aggregate_coarse_buckets(dp)
@@ -47,32 +47,6 @@ module SimpleMetrics
47
47
  end
48
48
  end
49
49
 
50
- def aggregate_values(dps)
51
- raise SimpleMetrics::DataPoint::NonMatchingTypesError if has_non_matching_types?(dps)
52
-
53
- dp = dps.first.dup
54
- dp.value = if dp.counter?
55
- sum(dps) / dps.size
56
- elsif dp.gauge?
57
- sum(dps)
58
- elsif dp.event?
59
- raise "Implement me!"
60
- elsif dp.timing?
61
- raise "Implement me!"
62
- else
63
- raise ArgumentError("Unknown data point type: #{dp}")
64
- end
65
- dp
66
- end
67
-
68
- def sum(dps)
69
- dps.map { |dp| dp.value }.inject(0) { |result, value| result += value }
70
- end
71
-
72
- def has_non_matching_types?(dps)
73
- dps.group_by { |dp| dp.type }.size != 1
74
- end
75
-
76
50
  def update_metric(dp, total)
77
51
  if metric = MetricRepository.find_one_by_name(dp.name)
78
52
  metric.total += total
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module SimpleMetrics
3
- VERSION = "0.3.3"
3
+ VERSION = "0.3.4"
4
4
  end
@@ -12,7 +12,6 @@ require "simple_metrics/data_point/counter"
12
12
  require "simple_metrics/data_point/event"
13
13
  require "simple_metrics/data_point/gauge"
14
14
  require "simple_metrics/data_point/timing"
15
- require "simple_metrics/array_aggregation"
16
15
  require "simple_metrics/importer"
17
16
  require "simple_metrics/bucket"
18
17
  require "simple_metrics/graph"
@@ -56,7 +56,26 @@ module SimpleMetrics
56
56
  it "parses increment timing with sample rate" do
57
57
  # TODO
58
58
  end
59
- end
59
+ end # describe
60
+
61
+ describe "#aggregate" do
62
+
63
+ it "aggregate counters" do
64
+ key1 = DataPoint::Counter.new(:name => "key1", :value => 1)
65
+ key2 = DataPoint::Counter.new(:name => "key1", :value => 3)
66
+ result = DataPoint.aggregate_values([key1, key2])
67
+ result.name.should == "key1"
68
+ result.value.should == 4
69
+ end
70
+
71
+ it "aggregate gauges" do
72
+ key1 = DataPoint::Gauge.new(:name => "key1", :value => 1)
73
+ key2 = DataPoint::Gauge.new(:name => "key1", :value => 3)
74
+ result = DataPoint.aggregate_values([key1, key2])
75
+ result.name.should == "key1"
76
+ result.value.should == 2
77
+ end
60
78
 
79
+ end # describe
61
80
  end
62
81
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70191513973620 !ruby/object:Gem::Requirement
16
+ requirement: &70274892543080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70191513973620
24
+ version_requirements: *70274892543080
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70191513972880 !ruby/object:Gem::Requirement
27
+ requirement: &70274892542460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70191513972880
35
+ version_requirements: *70274892542460
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rr
38
- requirement: &70191513972300 !ruby/object:Gem::Requirement
38
+ requirement: &70274892541780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70191513972300
46
+ version_requirements: *70274892541780
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: shotgun
49
- requirement: &70191513971780 !ruby/object:Gem::Requirement
49
+ requirement: &70274892541240 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70191513971780
57
+ version_requirements: *70274892541240
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: eventmachine
60
- requirement: &70191513971140 !ruby/object:Gem::Requirement
60
+ requirement: &70274892540640 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70191513971140
68
+ version_requirements: *70274892540640
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: daemons
71
- requirement: &70191513970580 !ruby/object:Gem::Requirement
71
+ requirement: &70274892540080 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70191513970580
79
+ version_requirements: *70274892540080
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: mongo
82
- requirement: &70191513969960 !ruby/object:Gem::Requirement
82
+ requirement: &70274892539440 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '1.6'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70191513969960
90
+ version_requirements: *70274892539440
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: bson
93
- requirement: &70191513985680 !ruby/object:Gem::Requirement
93
+ requirement: &70274892555180 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '1.6'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *70191513985680
101
+ version_requirements: *70274892555180
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: bson_ext
104
- requirement: &70191513985040 !ruby/object:Gem::Requirement
104
+ requirement: &70274892554540 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ~>
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '1.6'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *70191513985040
112
+ version_requirements: *70274892554540
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: sinatra
115
- requirement: &70191513984480 !ruby/object:Gem::Requirement
115
+ requirement: &70274892553960 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :runtime
122
122
  prerelease: false
123
- version_requirements: *70191513984480
123
+ version_requirements: *70274892553960
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: erubis
126
- requirement: &70191513983800 !ruby/object:Gem::Requirement
126
+ requirement: &70274892553260 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :runtime
133
133
  prerelease: false
134
- version_requirements: *70191513983800
134
+ version_requirements: *70274892553260
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: vegas
137
- requirement: &70191513983000 !ruby/object:Gem::Requirement
137
+ requirement: &70274892552580 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ~>
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: 0.1.2
143
143
  type: :runtime
144
144
  prerelease: false
145
- version_requirements: *70191513983000
145
+ version_requirements: *70274892552580
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: json
148
- requirement: &70191513982480 !ruby/object:Gem::Requirement
148
+ requirement: &70274892551940 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,7 +153,7 @@ dependencies:
153
153
  version: '0'
154
154
  type: :runtime
155
155
  prerelease: false
156
- version_requirements: *70191513982480
156
+ version_requirements: *70274892551940
157
157
  description: SimpleMetrics
158
158
  email:
159
159
  - fdietz@gmail.com
@@ -178,7 +178,6 @@ files:
178
178
  - example/increment.rb
179
179
  - lib/simple_metrics.rb
180
180
  - lib/simple_metrics/app.rb
181
- - lib/simple_metrics/array_aggregation.rb
182
181
  - lib/simple_metrics/bucket.rb
183
182
  - lib/simple_metrics/configuration.rb
184
183
  - lib/simple_metrics/data_point.rb
@@ -215,7 +214,6 @@ files:
215
214
  - lib/simple_metrics/views/layout.erb
216
215
  - lib/simple_metrics/views/show.erb
217
216
  - simple_metrics.gemspec
218
- - spec/array_aggregation_spec.rb
219
217
  - spec/bucket_spec.rb
220
218
  - spec/data_point_repository_spec.rb
221
219
  - spec/data_point_spec.rb
@@ -237,7 +235,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
237
235
  version: '0'
238
236
  segments:
239
237
  - 0
240
- hash: 878091864515966970
238
+ hash: -3260041322875483001
241
239
  required_rubygems_version: !ruby/object:Gem::Requirement
242
240
  none: false
243
241
  requirements:
@@ -246,7 +244,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
246
244
  version: '0'
247
245
  segments:
248
246
  - 0
249
- hash: 878091864515966970
247
+ hash: -3260041322875483001
250
248
  requirements: []
251
249
  rubyforge_project:
252
250
  rubygems_version: 1.8.15
@@ -254,7 +252,6 @@ signing_key:
254
252
  specification_version: 3
255
253
  summary: SimpleMetrics
256
254
  test_files:
257
- - spec/array_aggregation_spec.rb
258
255
  - spec/bucket_spec.rb
259
256
  - spec/data_point_repository_spec.rb
260
257
  - spec/data_point_spec.rb
@@ -1,84 +0,0 @@
1
- module SimpleMetrics
2
- module ArrayAggregation
3
- extend self
4
-
5
- class Base
6
- def aggregate(dps, name = nil)
7
- raise "subclass must implement me!"
8
- end
9
-
10
- private
11
-
12
- def ts_hash_aggregated(data_points, &block)
13
- tmp = {}
14
- data_points.each do |dp|
15
- if tmp.key?(dp.ts)
16
- tmp[dp.ts] = block.call(tmp[dp.ts], dp.value)
17
- else
18
- tmp[dp.ts] = dp.value
19
- end
20
- end
21
- tmp
22
- end
23
- end
24
-
25
- class Counter < Base
26
- def aggregate(dps, name = nil)
27
- tmp_hash = ts_hash_aggregated(dps) do |value1, value2|
28
- value1 + value2
29
- end
30
-
31
- result = []
32
- tmp_hash.each_pair do |key, value|
33
- result << SimpleMetrics::DataPoint::Counter.new(:name => name, :ts => key, :value => value)
34
- end
35
- result
36
- end
37
- end
38
-
39
- class Gauge < Base
40
- def aggregate(dps, name = nil)
41
- tmp_hash = ts_hash_aggregated(dps) do |value1, value2|
42
- (value1 + value2)/2
43
- end
44
-
45
- result = []
46
- tmp_hash.each_pair do |key, value|
47
- result << SimpleMetrics::DataPoint::Gauge.new(:name => name, :ts => key, :value => value)
48
- end
49
- result
50
- end
51
- end
52
-
53
- class Timing < Base
54
- end
55
-
56
- class Event < Base
57
- end
58
-
59
- def aggregate(dps, name = nil)
60
- raise SimpleMetrics::DataPoint::NonMatchingTypesError if has_non_matching_types?(dps)
61
-
62
- dp = dps.first
63
- strategy(dp).aggregate(dps, name)
64
- end
65
-
66
- private
67
-
68
- def strategy(dp)
69
- if dp.counter?
70
- Counter.new
71
- elsif dp.gauge?
72
- Gauge.new
73
- elsif dp.timing?
74
- Timing.new
75
- elsif dp.event?
76
- Event.new
77
- end
78
- end
79
-
80
- def has_non_matching_types?(dps)
81
- dps.group_by { |dp| dp.type }.size != 1
82
- end
83
- end
84
- end
@@ -1,51 +0,0 @@
1
- # encoding: utf-8
2
- require "spec_helper"
3
-
4
- module SimpleMetrics
5
-
6
- describe ArrayAggregation do
7
-
8
- before do
9
- DataPointRepository.truncate_collections
10
- DataPointRepository.ensure_collections_exist
11
- end
12
-
13
- let(:ts) do
14
- Time.now.utc.to_i
15
- end
16
-
17
- describe "#aggregate" do
18
- it "aggregates counter data points" do
19
- stats1 = DataPoint::Counter.new(:name => "com.test.key1", :value => 5, :ts => ts)
20
- stats2 = DataPoint::Counter.new(:name => "com.test.key1", :value => 7, :ts => ts)
21
- stats3 = DataPoint::Counter.new(:name => "com.test.key1", :value => 9, :ts => (ts + 60) )
22
-
23
- results = ArrayAggregation.aggregate([stats1, stats2, stats3], "com.test.*")
24
- results.should have(2).data_points
25
- results.first.name.should == "com.test.*"
26
- results.last.name.should == "com.test.*"
27
- results.first.value.should == 12
28
- results.last.value.should == 9
29
- end
30
-
31
- it "aggregates gauge data points" do
32
- stats1 = DataPoint::Gauge.new(:name => "com.test.key1", :value => 5, :ts => ts)
33
- stats2 = DataPoint::Gauge.new(:name => "com.test.key1", :value => 7, :ts => ts)
34
- stats3 = DataPoint::Gauge.new(:name => "com.test.key1", :value => 9, :ts => (ts + 60) )
35
-
36
- results = ArrayAggregation.aggregate([stats1, stats2, stats3], "com.test.*")
37
- results.should have(2).data_points
38
- results.first.name.should == "com.test.*"
39
- results.last.name.should == "com.test.*"
40
- results.first.value.should == 6
41
- results.last.value.should == 9
42
- end
43
-
44
- it "raises NonMatchingTypesError if types are different" do
45
- stats1 = DataPoint::Counter.new(:name => "com.test.key1", :value => 5, :ts => ts)
46
- stats2 = DataPoint::Gauge.new(:name => "com.test.key1", :value => 5, :ts => ts)
47
- expect { ArrayAggregation.aggregate([stats1, stats2], "com.test.*") }.to raise_error(SimpleMetrics::DataPoint::NonMatchingTypesError)
48
- end
49
- end
50
- end
51
- end