Class: Fluent::Plugin::ElasticLog::MetricAccumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/elastic_log/metric_accumulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value_key:) ⇒ MetricAccumulator

Returns a new instance of MetricAccumulator.



9
10
11
12
13
# File 'lib/fluent/plugin/elastic_log/metric_accumulator.rb', line 9

def initialize(value_key:)
  @value_key = value_key

  @data = []
end

Instance Attribute Details

#value_keyObject (readonly)

Returns the value of attribute value_key.



7
8
9
# File 'lib/fluent/plugin/elastic_log/metric_accumulator.rb', line 7

def value_key
  @value_key
end

Instance Method Details

#<<(metric) ⇒ Object



15
16
17
18
19
# File 'lib/fluent/plugin/elastic_log/metric_accumulator.rb', line 15

def <<(metric)
  @data << metric

  self
end

#groupObject



25
26
27
28
29
30
31
32
33
# File 'lib/fluent/plugin/elastic_log/metric_accumulator.rb', line 25

def group
  grouped_data = @data.group_by { |metric| metric.reject { |k, _| k == value_key } }
  grouped_data.map do |metric_base, metrics|
    accumulated_value = metrics.sum { |metric| metric.fetch(value_key, 0) }
    metric_base.update(
      value_key => accumulated_value
    )
  end
end

#to_aObject



21
22
23
# File 'lib/fluent/plugin/elastic_log/metric_accumulator.rb', line 21

def to_a
  group.to_a
end