Class: Fluent::Plugin::ElasticLog::MetricAccumulator
- Inherits:
-
Object
- Object
- Fluent::Plugin::ElasticLog::MetricAccumulator
- Defined in:
- lib/fluent/plugin/elastic_log/metric_accumulator.rb
Instance Attribute Summary collapse
-
#value_key ⇒ Object
readonly
Returns the value of attribute value_key.
Instance Method Summary collapse
- #<<(metric) ⇒ Object
- #group ⇒ Object
-
#initialize(value_key:) ⇒ MetricAccumulator
constructor
A new instance of MetricAccumulator.
- #to_a ⇒ Object
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_key ⇒ Object (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 |
#group ⇒ Object
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_a ⇒ Object
21 22 23 |
# File 'lib/fluent/plugin/elastic_log/metric_accumulator.rb', line 21 def to_a group.to_a end |