From 55054c51de2bb67a6f859ee8ab8a5e8a11c09c1a Mon Sep 17 00:00:00 2001 From: Jim Beatty Date: Thu, 18 May 2023 11:08:59 -0400 Subject: [PATCH 1/4] Add unit test for RateMetricAgg in elasticsearch --- grafanalib/tests/test_elasticsearch.py | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 grafanalib/tests/test_elasticsearch.py 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" + ) From 65375ecedff80aad1fb0ed8454e480b353e44296 Mon Sep 17 00:00:00 2001 From: Jim Beatty Date: Thu, 18 May 2023 11:10:02 -0400 Subject: [PATCH 2/4] Add missing code for `unit` param --- grafanalib/elasticsearch.py | 3 +++ 1 file changed, 3 insertions(+) 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 From e9c836428902a36af93452ff179bea6ee5376844 Mon Sep 17 00:00:00 2001 From: Jim Beatty Date: Thu, 18 May 2023 11:13:20 -0400 Subject: [PATCH 3/4] Fix formatting in Changelog --- CHANGELOG.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2da345a6..fa75dcf4 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,11 +8,12 @@ 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``) -.. _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/ 0.7.0 (2022-10-02) @@ -28,7 +29,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) ================== From 3ee5b960a2e62390b561e2932562c420864af061 Mon Sep 17 00:00:00 2001 From: Jim Beatty Date: Thu, 18 May 2023 11:15:12 -0400 Subject: [PATCH 4/4] Add missing changelog for Rate Aggregation --- CHANGELOG.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fa75dcf4..7c480404 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,9 +12,10 @@ Changelog * 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`: 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) ==================