dataoperations-aggregate 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dataoperations-aggregate.rb +115 -11
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2cf76bae5e748e46672c8d59f8859358452f2fca813499b148e85dfa61f38c9
4
- data.tar.gz: 7a9fbbd0115edefa597b9d374407576f4dfedb133da781dffe066c3e4ba6f73a
3
+ metadata.gz: 9e69958cbf826f7524a31c8621afd7c554c61ceeead22b62460c464d0d584e79
4
+ data.tar.gz: c8451ad56f5cae4c5b4b4ff26d02bf9445f3fc925c02a2cf843a551f3006620a
5
5
  SHA512:
6
- metadata.gz: 403435362bd15744b154604e6322dcbba89ea9952e36d82ffe3ac3299f3ad28cd5c29187843e780cf149dabf2abdb4fbb6700f1e8ddce8e792ab033adf174255
7
- data.tar.gz: f883f091f074ebbe48e18c46790ceb37c5ba98d7a879ec8259f69c88a19225130864897ab8bb63e47c7ab31dbcc5a400ae6c7fe68f3b8a27abec476c6e34123d
6
+ metadata.gz: a85e773496690078a0ad56971ea0a17b15c0656abc2f9d28eefbfbeec86b867f378b4bc3175eb84462d75b4035a5249e356fc969f80302ed1b691f00be569e20
7
+ data.tar.gz: 0f1dd3bccb61205b3decbce1a2321ba5b2326978d9584e200cd3077b5f9eebe077015a8460f2261fc4583ec9850f3178f79171dc1f90a0ca622e47961dc2edb4
@@ -12,7 +12,10 @@ module DataOperations
12
12
  DEFAULT_TIME_STARTED_MODE = :first_message
13
13
  DEFAULT_FIELD_NO_DATA_VALUE = 'no_data'.freeze
14
14
  DEFAULT_AGGREGATIONS = %w[sum min max mean median variance standard_deviation].freeze
15
- VALID_AGGREGATIONS = %w[sum min max mean median variance standard_deviation].freeze
15
+ VALID_AGGREGATIONS = %w[sum min max mean median variance standard_deviation histogram].freeze
16
+ DEFAULT_HISTOGRAM_CUMULATIVE = false
17
+ DEFAULT_HISTOGRAM_BUCKET_INFINITE_ENABLED = true
18
+ DEFAULT_HISTOGRAM_BUCKET_COMPARATION = :less_or_equal
16
19
  DEFAULT_HASH_TIME_FORMAT = '%Y-%m-%dT%H'.freeze
17
20
  DEFAULT_INERVAL_SECONDS = 3600
18
21
 
@@ -30,7 +33,12 @@ module DataOperations
30
33
  log: Logger.new(STDOUT),
31
34
  aggregation_names:,
32
35
  group_field_names:,
33
- aggregate_field_names:
36
+ aggregate_field_names:,
37
+ histogram_cumulative: DEFAULT_HISTOGRAM_CUMULATIVE,
38
+ histogram_bucket_infinite_enabled: DEFAULT_HISTOGRAM_BUCKET_INFINITE_ENABLED,
39
+ histogram_bucket_comparation: DEFAULT_HISTOGRAM_BUCKET_COMPARATION,
40
+ histogram_buckets:[],
41
+ histogram_fields:[]
34
42
  )
35
43
  @aggregator = aggregator
36
44
  @time_format = time_format
@@ -43,6 +51,11 @@ module DataOperations
43
51
  @processing_mode = processing_mode
44
52
  @time_started_mode = time_started_mode
45
53
  @aggregator_name = aggregator_name
54
+ @histogram_cumulative = histogram_cumulative
55
+ @histogram_bucket_infinite_enabled = histogram_bucket_infinite_enabled
56
+ @histogram_bucket_comparation = histogram_bucket_comparation
57
+ @histogram_buckets = histogram_buckets
58
+ @histogram_fields = histogram_fields
46
59
 
47
60
 
48
61
  if aggregation_names.nil? || !aggregation_names.is_a?(Array)
@@ -219,16 +232,52 @@ module DataOperations
219
232
  # Aggregate data
220
233
  if aggregate_field_value.is_a?(Array)
221
234
  @aggregation_names.each do |operation|
222
- data = aggregate_field_value.method(operation).call
223
- aggregator_data["#{aggregate_field_key}_#{operation}"] = data
224
-
225
- # Add aggregated data to interval
226
- group_item_value['intervals'].keys[1..-1].each do |interval_secs|
227
- interval_aggregator_item_key = (aggregator_item_key / interval_secs.to_i) * interval_secs.to_i
228
- interval_aggregator_item_value = group_item_value['intervals'][interval_secs][interval_aggregator_item_key]
229
- interval_aggregator_item_value['aggregate_fields'][aggregate_field_key][operation] << data
235
+
236
+ #If histogram, calculate bucket for metric
237
+ if operation == 'histogram'
238
+ if @histogram_bucket_comparation == :less_or_equal
239
+ bucket_comparation = "le"
240
+ else
241
+ bucket_comparation = "ge"
242
+ end
243
+ #If set buckets and set metrics to calculate (bucket values depends of ranges based in metric activity)
244
+ if !@histogram_buckets.nil? && !@histogram_fields.nil? && @histogram_fields.include?(aggregate_field_key)
245
+ data_bucket = calculate_histogram_buckets(aggregate_field_value, @histogram_buckets)
246
+
247
+ data_bucket.each {|bucket,bucket_count|
248
+ #@log.info("#{aggregate_field_key}_#{bucket} = #{bucket_count}")
249
+ aggregator_data["#{aggregate_field_key}_bucket_#{bucket_comparation}_#{bucket}"] = bucket_count
250
+ }
251
+
252
+ # Add aggregated data to interval
253
+ group_item_value['intervals'].keys[1..-1].each do |interval_secs|
254
+
255
+ interval_aggregator_item_key = (aggregator_item_key / interval_secs.to_i) * interval_secs.to_i
256
+ interval_aggregator_item_value = group_item_value['intervals'][interval_secs][interval_aggregator_item_key]
257
+ data_bucket.each {|bucket,bucket_count|
258
+ #@log.info("#{aggregate_field_key}_#{bucket} = #{bucket_count}")
259
+ interval_aggregator_item_value['aggregate_fields'][aggregate_field_key]["bucket_#{bucket_comparation}_#{bucket}"] = [] if interval_aggregator_item_value['aggregate_fields'][aggregate_field_key]["bucket#{bucket}"].nil?
260
+ interval_aggregator_item_value['aggregate_fields'][aggregate_field_key]["bucket_#{bucket_comparation}_#{bucket}"] << bucket_count
261
+ }
262
+
263
+ end
264
+
265
+ end
266
+ else
267
+ data = aggregate_field_value.method(operation).call
268
+ aggregator_data["#{aggregate_field_key}_#{operation}"] = data
269
+
270
+ # Add aggregated data to interval
271
+ group_item_value['intervals'].keys[1..-1].each do |interval_secs|
272
+ interval_aggregator_item_key = (aggregator_item_key / interval_secs.to_i) * interval_secs.to_i
273
+ interval_aggregator_item_value = group_item_value['intervals'][interval_secs][interval_aggregator_item_key]
274
+ interval_aggregator_item_value['aggregate_fields'][aggregate_field_key][operation] << data
275
+ end
230
276
  end
277
+
278
+
231
279
  end
280
+
232
281
  end
233
282
  end
234
283
 
@@ -244,6 +293,7 @@ module DataOperations
244
293
  @aggregation_names.each do |operation|
245
294
  interval_aggregator_item_value['aggregate_fields'][aggregate_field_key][operation] = []
246
295
  end
296
+
247
297
  end
248
298
  end
249
299
 
@@ -297,10 +347,17 @@ module DataOperations
297
347
  case operation
298
348
  when 'max', 'min', 'mean', 'median'
299
349
  data = vector.method(operation).call
350
+ when 'histogram'
351
+ #Bucket operation generate bucket[\d]+
352
+ data = nil
353
+ when /^bucket.+/
354
+ #For buckets sum accumulations for internvals
355
+ data = vector.method('sum').call
300
356
  else
301
357
  data = vector.median
302
358
  end
303
- aggregator_data["#{field_name}_#{operation}"] = data
359
+ #Nil data is avoid (for example for 'bucket' name operation)
360
+ aggregator_data["#{field_name}_#{operation}"] = data unless data.nil?
304
361
  end
305
362
  end
306
363
  # @log.debug aggregator_item_value
@@ -310,5 +367,52 @@ module DataOperations
310
367
  aggregate_data[s_interval] << aggregator_data
311
368
  end
312
369
  end
370
+
371
+ #Return Array with count by each bucket
372
+ def calculate_histogram_buckets(data, buckets_config)
373
+
374
+ if @histogram_bucket_comparation == :less_or_equal
375
+ buckets_config.sort!.uniq!
376
+ else
377
+ buckets_config.reverse!.uniq!
378
+ end
379
+
380
+ buckets = {}
381
+
382
+ buckets_config.each {|bucket| buckets[bucket] = 0}
383
+
384
+ #By default add Inf bucket
385
+ buckets["Inf"] = 0 if @histogram_bucket_infinite_enabled
386
+
387
+ data.each do|item|
388
+ item_in_bucket = false
389
+ buckets_config.each do|bucket|
390
+ if @histogram_bucket_comparation == :less_or_equal
391
+ if item <= bucket
392
+ item_in_bucket = true
393
+ end
394
+ elsif item >= bucket
395
+ item_in_bucket = true
396
+ end
397
+
398
+ if item_in_bucket
399
+ buckets[bucket] += 1
400
+ #Not accumule if histogram cumulative is false
401
+ break if !@histogram_cumulative
402
+ end
403
+ end
404
+
405
+ #Only count Inf bucket if enabled
406
+ if @histogram_bucket_infinite_enabled
407
+ if !item_in_bucket
408
+ buckets["Inf"] += 1
409
+ elsif @histogram_cumulative
410
+ buckets["Inf"] += 1
411
+ end
412
+ end
413
+ end
414
+ return buckets
415
+ end
416
+
313
417
  end
314
418
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dataoperations-aggregate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Guillen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-19 00:00:00.000000000 Z
11
+ date: 2023-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: descriptive_statistics
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  - !ruby/object:Gem::Version
51
51
  version: '0'
52
52
  requirements: []
53
- rubygems_version: 3.0.3
53
+ rubygems_version: 3.0.3.1
54
54
  signing_key:
55
55
  specification_version: 4
56
56
  summary: Aggregate data