Skip to content

Commit

Permalink
Add support for Elasticsearch Rate Metric Aggregator (#583)
Browse files Browse the repository at this point in the history
* Add support for Elasticsearch Rate Metric Aggregator

* fix linting error

Co-authored-by: JamesGibo <[email protected]>

* Add import of attr.validators.in_

---------

Co-authored-by: JamesGibo <[email protected]>
  • Loading branch information
jimbolimbo13 and JamesGibo authored May 18, 2023
1 parent bfdae85 commit 2ca57fc
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion grafanalib/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import attr
import itertools
from attr.validators import instance_of
from attr.validators import in_, instance_of
from grafanalib.core import AlertCondition

DATE_HISTOGRAM_DEFAULT_FIELD = 'time_iso8601'
Expand Down Expand Up @@ -498,3 +498,49 @@ def to_json_data(self):
'inlineScript': self.inline,
'settings': self.settings,
}


@attr.s
class RateMetricAgg(object):
"""An aggregator that provides the rate of the values.
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-rate-aggregation.html
:param field: name of elasticsearch field to provide the sum over
:param hide: show/hide the metric in the final panel display
:param id: id of the metric
:param unit: calendar interval to group by
supported calendar intervals
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html#calendar_intervals
"minute"
"hour"
"day"
"week"
"month"
"quarter"
"year"
:param mode: sum or count the values
:param script: script to apply to the data, using '_value'
"""

field = attr.ib(default="", validator=instance_of(str))
id = attr.ib(default=0, validator=instance_of(int))
hide = attr.ib(default=False, validator=instance_of(bool))
unit = attr.ib(default="", validator=instance_of(str))
mode = attr.ib(default="", validator=in_(["", "value_count", "sum"]))
script = attr.ib(default="", validator=instance_of(str))

def to_json_data(self):
self.settings = {}

if self.mode:
self.settings["mode"] = self.mode

if self.script:
self.settings["script"] = self.script

return {
"id": str(self.id),
"hide": self.hide,
"field": self.field,
"settings": self.settings,
"type": "rate",
}

0 comments on commit 2ca57fc

Please sign in to comment.