ezmetrics 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +64 -23
  3. data/lib/ezmetrics.rb +28 -5
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61d26fa5d7a779917ac2ba154e82f29745a22b64261442117d15b286d0e06276
4
- data.tar.gz: 44ff087c54cf0e5fdf8763b88deaa59f3ccdfa1e11c12ab5c1da2eebdeea7764
3
+ metadata.gz: 408332cfc86d05b75d8b6004098d736ad60a624189f46022af822fdf6c24960d
4
+ data.tar.gz: 6d4ef89fa6a80e28cb443e16f1528b351a93bf7bf50e2a8fc756dc943a25f11f
5
5
  SHA512:
6
- metadata.gz: 6bf28edd6c644648198905bcdf50a77a0083c53b6eba312b709e9c1cb951adba1c407cbc28ac3ae0588b15185b5aa0132ef1f568ff30af0158b770d388460885
7
- data.tar.gz: a08685fea3025b23e5dca7439e9b7f4f2313933363b01a716e68eba800e4b96fce0d976cf25111c0e7a36f655abb6497e3481d39caba881b2ee706e49958b365
6
+ metadata.gz: 99a4ac48560305d2967cdcc8b248978ee3f0748ab1fb7da2830a3804d732d29664f25bfdc4bd2bfa50c449994913ecd455362f57af1f39f0f45e904e36876c12
7
+ data.tar.gz: c53617fbc324765b9b388b1280b6d363a3707b002a2718cd0467a16ff24708a8adbf81a812aca111a643924de4834a155efdef252911195570837fdb8f6a9dce
data/README.md CHANGED
@@ -27,25 +27,25 @@ and stores them for the timeframe you specified, 60 seconds by default.
27
27
  You can change the timeframe according to your needs and save the metrics by calling `log` method:
28
28
 
29
29
  ```ruby
30
- # Store the metrics for 60 seconds (default behaviour)
31
- EZmetrics.new.log(
32
- duration: 100.5,
33
- views: 40.7,
34
- db: 59.8,
35
- queries: 4,
36
- status: 200
37
- )
30
+ # Store the metrics for 60 seconds (default behaviour)
31
+ EZmetrics.new.log(
32
+ duration: 100.5,
33
+ views: 40.7,
34
+ db: 59.8,
35
+ queries: 4,
36
+ status: 200
37
+ )
38
38
  ```
39
39
 
40
40
  ```ruby
41
- # Store the metrics for 10 minutes
42
- EZmetrics.new(10.minutes).log(
43
- duration: 100.5,
44
- views: 40.7,
45
- db: 59.8,
46
- queries: 4,
47
- status: 200
48
- )
41
+ # Store the metrics for 10 minutes
42
+ EZmetrics.new(10.minutes).log(
43
+ duration: 100.5,
44
+ views: 40.7,
45
+ db: 59.8,
46
+ queries: 4,
47
+ status: 200
48
+ )
49
49
  ```
50
50
 
51
51
  ---
@@ -53,13 +53,13 @@ You can change the timeframe according to your needs and save the metrics by cal
53
53
  For displaying metrics you need to call `show` method:
54
54
 
55
55
  ```ruby
56
- # Aggregate and show metrics for last 60 seconds (default behaviour)
57
- EZmetrics.new.show
56
+ # Aggregate and show metrics for last 60 seconds (default behaviour)
57
+ EZmetrics.new.show
58
58
  ```
59
59
 
60
60
  ```ruby
61
- # Aggregate and show metrics for last 10 minutes
62
- EZmetrics.new(10.minutes).show
61
+ # Aggregate and show metrics for last 10 minutes
62
+ EZmetrics.new(10.minutes).show
63
63
  ```
64
64
 
65
65
  You can combine these timeframes, for example - store for 10 minutes, display for 5 minutes.
@@ -131,6 +131,47 @@ This will return a hash with the following structure:
131
131
  }
132
132
  ```
133
133
 
134
+ ---
135
+
136
+ If you prefer a single level object - you can change the default output structure by calling `.flatten` before `.show`
137
+
138
+ ```ruby
139
+ EZmetrics.new(1.hour).flatten.show(db: :avg, duration: [:avg, :max])
140
+ ```
141
+
142
+ ```ruby
143
+ {
144
+ db_avg: 182,
145
+ duration_avg: 205,
146
+ duration_max: 5171
147
+ }
148
+ ```
149
+
150
+ ---
151
+
152
+ Same for [partitioned aggregation](#partitioning)
153
+
154
+ ```ruby
155
+ EZmetrics.new(1.hour).partition_by(:minute).flatten.show(db: :avg, duration: [:avg, :max])
156
+ ```
157
+
158
+ ```ruby
159
+ [
160
+ {
161
+ timestamp: 1575242880,
162
+ db_avg: 387,
163
+ duration_avg: 477,
164
+ duration_max: 8566
165
+ },
166
+ {
167
+ timestamp: 1575242940,
168
+ db_avg: 123,
169
+ duration_avg: 234,
170
+ duration_max: 3675
171
+ }
172
+ ]
173
+ ```
174
+
134
175
  ### Aggregation
135
176
 
136
177
  The aggregation can be easily configured by specifying aggregation options as in the following examples:
@@ -221,11 +262,11 @@ EZmetrics.new.show(views: :avg, :db: [:avg, :max], requests: true)
221
262
 
222
263
  If you want to visualize your metrics by using a **line chart**, you will need to use partitioning.
223
264
 
224
- To aggregate metrics, partitioned by a unit of time you need to call `partition_by({time_unit})` before calling `show`
265
+ To aggregate metrics, partitioned by a unit of time you need to call `.partition_by({time_unit})` before calling `.show`
225
266
 
226
267
  ```ruby
227
- # Aggregate metrics for last hour, partition by minute
228
- EZmetrics.new(1.hour).partition_by(:minute).show(duration: [:avg, :max], db: :avg)
268
+ # Aggregate metrics for last hour, partition by minute
269
+ EZmetrics.new(1.hour).partition_by(:minute).show(duration: [:avg, :max], db: :avg)
229
270
  ```
230
271
 
231
272
  This will return an array of objects with the following structure:
@@ -63,6 +63,11 @@ class EZmetrics
63
63
  partitioned_metrics ? aggregate_partitioned_data : aggregate_data
64
64
  end
65
65
 
66
+ def flatten
67
+ @flat = true
68
+ self
69
+ end
70
+
66
71
  def partition_by(time_unit=:minute)
67
72
  time_unit = PARTITION_UNITS.include?(time_unit) ? time_unit : :minute
68
73
  @partitioned_metrics = interval_metrics.group_by { |h| second_to_partition_unit(time_unit, h["second"]) }
@@ -71,7 +76,7 @@ class EZmetrics
71
76
 
72
77
  private
73
78
 
74
- attr_reader :redis, :interval_seconds, :interval_metrics, :requests,
79
+ attr_reader :redis, :interval_seconds, :interval_metrics, :requests, :flat,
75
80
  :storage_key, :safe_payload, :this_second_metrics, :partitioned_metrics, :options
76
81
 
77
82
  def aggregate_data
@@ -86,7 +91,7 @@ class EZmetrics
86
91
  partitioned_metrics.map do |partition, metrics|
87
92
  @interval_metrics = metrics
88
93
  @requests = interval_metrics.sum { |hash| hash["statuses"]["all"] }
89
- { timestamp: partition, data: build_result }
94
+ flat ? { timestamp: partition, **build_result } : { timestamp: partition, data: build_result }
90
95
  end
91
96
  rescue
92
97
  new(options)
@@ -95,7 +100,9 @@ class EZmetrics
95
100
  def build_result
96
101
  result = {}
97
102
 
98
- result[:requests] = { all: requests, grouped: count_all_status_groups } if options[:requests]
103
+ if options[:requests]
104
+ append_requests_to_result(result, { all: requests, grouped: count_all_status_groups })
105
+ end
99
106
 
100
107
  options.each do |metrics, aggregation_functions|
101
108
  next unless METRICS.include?(metrics)
@@ -103,8 +110,8 @@ class EZmetrics
103
110
  next unless aggregation_functions.any?
104
111
 
105
112
  aggregation_functions.each do |aggregation_function|
106
- result[metrics] ||= {}
107
- result[metrics][aggregation_function] = aggregate(metrics, aggregation_function)
113
+ aggregated_metrics = aggregate(metrics, aggregation_function)
114
+ append_metrics_to_result(result, metrics, aggregation_function, aggregated_metrics)
108
115
  end
109
116
  end
110
117
  result
@@ -112,6 +119,22 @@ class EZmetrics
112
119
  result
113
120
  end
114
121
 
122
+ def append_requests_to_result(result, aggregated_requests)
123
+ return result[:requests] = aggregated_requests unless flat
124
+
125
+ result[:requests_all] = aggregated_requests[:all]
126
+ aggregated_requests[:grouped].each do |group, counter|
127
+ result[:"requests_#{group}"] = counter
128
+ end
129
+ end
130
+
131
+ def append_metrics_to_result(result, metrics, aggregation_function, aggregated_metrics)
132
+ return result[:"#{metrics}_#{aggregation_function}"] = aggregated_metrics if flat
133
+
134
+ result[metrics] ||= {}
135
+ result[metrics][aggregation_function] = aggregated_metrics
136
+ end
137
+
115
138
  def second_to_partition_unit(time_unit, second)
116
139
  return second if time_unit == :second
117
140
  time = Time.at(second)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ezmetrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolae Rotaru