diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2da345a6..7c480404 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,12 +8,14 @@ Changelog * Added ... * Added Minimum option for Timeseries * Added Maximum option for Timeseries -* Added Number of decimals displays option for Timeseries* Added Bar_Chart_ panel support +* Added Number of decimals displays option for Timeseries +* Added Bar_Chart_ panel support * Extended SqlTarget to support parsing queries from files * Fix AlertCondition backwards compatibility (``useNewAlerts`` default to ``False``) +* Added RateMetricAgg_ for ElasticSearch -.. _Bar_Chart: basehttps://grafana.com/docs/grafana/latest/panels-visualizations/visualizations/bar-chart/ - +.. _`Bar_Chart`: https://grafana.com/docs/grafana/latest/panels-visualizations/visualizations/bar-chart/ +.. _`RateMetricAgg`: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-rate-aggregation.html 0.7.0 (2022-10-02) ================== @@ -28,7 +30,7 @@ Changelog * Fix typo in unit constant ``GIGA_WATT`` (was ``GAGA_WATT``) * Fix typo in unit constant ``NORMAL_CUBIC_METER`` (was ``NORMAIL_CUBIC_METER``) -.. _ePict: basehttps://grafana.com/grafana/plugins/larona-epict-panel/ +.. _`ePict`: https://grafana.com/grafana/plugins/larona-epict-panel/ 0.6.3 (2022-03-30) ================== diff --git a/grafanalib/elasticsearch.py b/grafanalib/elasticsearch.py index 97269836..67158c1d 100644 --- a/grafanalib/elasticsearch.py +++ b/grafanalib/elasticsearch.py @@ -531,6 +531,9 @@ class RateMetricAgg(object): def to_json_data(self): self.settings = {} + if self.unit: + self.settings["unit"] = self.unit + if self.mode: self.settings["mode"] = self.mode diff --git a/grafanalib/tests/test_elasticsearch.py b/grafanalib/tests/test_elasticsearch.py new file mode 100644 index 00000000..61361c5f --- /dev/null +++ b/grafanalib/tests/test_elasticsearch.py @@ -0,0 +1,41 @@ +"""Tests for elasticsearch.""" + +import grafanalib.elasticsearch as E +import pytest + + +def test_rate_metric_agg(): + t = E.RateMetricAgg() + json_data = t.to_json_data() + + assert json_data["id"] == "0" + assert json_data["hide"] is False + assert json_data["field"] == "" + assert len(json_data["settings"]) == 0 + assert json_data["type"] == "rate" + assert len(json_data) == 5 + + t = E.RateMetricAgg( + field="some-field", + hide=True, + id=2, + unit="minute", + mode="sum", + script="some script" + ) + json_data = t.to_json_data() + + assert json_data["id"] == "2" + assert json_data["hide"] is True + assert json_data["field"] == "some-field" + assert len(json_data["settings"]) == 3 + assert json_data["settings"]["unit"] == "minute" + assert json_data["settings"]["mode"] == "sum" + assert json_data["settings"]["script"] == "some script" + assert json_data["type"] == "rate" + assert len(json_data) == 5 + + with pytest.raises(ValueError): + t = E.RateMetricAgg( + mode="invalid mode" + )