Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for RateMetricAgg in elasticsearch #594

Merged
merged 5 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
==================
Expand All @@ -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)
==================
Expand Down
3 changes: 3 additions & 0 deletions grafanalib/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 41 additions & 0 deletions grafanalib/tests/test_elasticsearch.py
Original file line number Diff line number Diff line change
@@ -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"
)