From 2994fb28f57f6580f53053db2898a16a4b36c3ea Mon Sep 17 00:00:00 2001 From: Thomas Nyambati <12892110+nyambati@users.noreply.github.com> Date: Mon, 2 Sep 2024 13:49:09 +0200 Subject: [PATCH] add gauge --- grafana_dashboards/schema/panel/gauge.py | 71 ++++++++++++++++++++++++ tests/schema/panels/gauge.py | 46 +++++++++++++++ tests/schema/panels/test_table.py | 23 ++++++++ 3 files changed, 140 insertions(+) create mode 100644 grafana_dashboards/schema/panel/gauge.py create mode 100644 tests/schema/panels/gauge.py create mode 100644 tests/schema/panels/test_table.py diff --git a/grafana_dashboards/schema/panel/gauge.py b/grafana_dashboards/schema/panel/gauge.py new file mode 100644 index 0000000..84e028b --- /dev/null +++ b/grafana_dashboards/schema/panel/gauge.py @@ -0,0 +1,71 @@ +# Copyright 2015 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import voluptuous as v + +from grafana_dashboards.schema.panel.base import Base + + +class Gauge(Base): + def get_schema(self): + reduceOptions = { + v.Required("values", default=False): v.All(bool), + v.Required("calcs", default=["last"]): v.All(list), + v.Required("fields", default=""): v.Any(str), + } + + fieldConfigDefaults = { + v.Optional("thresholds"): v.All( + {v.Required("mode"): v.Any(str), v.Required("steps"): v.Any(list)} + ), + v.Optional("color"): v.All( + { + v.Required("mode", default="palette-classic"): v.Any(str), + v.Required("palette", default="cool"): v.Any(str), + } + ), + v.Optional("min", default="0"): v.Any(int), + v.Optional("max", default="0"): v.Any(int), + v.Optional("unit"): v.Any(str), + v.Optional("links"): v.Any(list), + v.Optional("decimal", default=2): v.Any(int), + } + + fieldConfig = { + v.Required("overrides", default=[]): v.All(list), + v.Optional("defaults"): v.All(fieldConfigDefaults), + } + + options = { + v.Optional("reduceOptions"): v.All(reduceOptions), + v.Optional("showThresholdLabels", default=True): v.Any(str), + v.Optional("showThresholdMarkers", default=True): v.Any(str), + } + + gridPos = { + v.Required("h"): v.Any(int), + v.Required("w"): v.Any(int), + v.Required("x"): v.Any(int), + v.Required("y"): v.Any(int), + } + + bargauge = { + v.Required("targets", default=[]): v.All(list), + v.Optional("fieldConfig"): v.All(fieldConfig), + v.Optional("options"): v.All(options), + v.Optional("datasource"): v.All(str), + v.Optional("gridPos"): v.All(gridPos), + } + bargauge.update(self.base) + return v.Schema(bargauge, extra=True) diff --git a/tests/schema/panels/gauge.py b/tests/schema/panels/gauge.py new file mode 100644 index 0000000..7052255 --- /dev/null +++ b/tests/schema/panels/gauge.py @@ -0,0 +1,46 @@ +import testtools.matchers +from testtools import TestCase + +from grafana_dashboards.schema.panel.timeseries import Gauge + + +class TestCaseGauge(TestCase): + def setUp(self): + super(TestCaseGauge, self).setUp() + self.schema = Gauge().get_schema() + + def test_defaults(self): + defaults = { + "type": "gauge", + "title": "Example Gauge", + "gridPos": {"h": 9, "w": 12, "x": 0, "y": 0}, + "id": 1, + "options": { + "reduceOptions": { + "calcs": ["mean"], + "fields": "", + "values": False, + "limit": 100, + }, + "showThresholdLabels": True, + "showThresholdMarkers": True, + }, + "fieldConfig": { + "defaults": { + "min": 0, + "max": 100, + "unit": "percent", + "decimals": 2, + "color": {"mode": "palette-classic", "palette": "cool"}, + "thresholds": {}, + }, + "overrides": [], + }, + "targets": [], + "datasource": {"uid": "cgJzmlq7z", "type": "prometheus"}, + } + + self.assertThat( + self.schema(defaults), + testtools.matchers.Equals(defaults), + ) diff --git a/tests/schema/panels/test_table.py b/tests/schema/panels/test_table.py new file mode 100644 index 0000000..fa5aad8 --- /dev/null +++ b/tests/schema/panels/test_table.py @@ -0,0 +1,23 @@ +from testtools import TestCase + +from grafana_dashboards.schema.panel.table import Table + + +class TestCaseTable(TestCase): + def setUp(self): + super(TestCaseTable, self).setUp() + self.schema = Table().get_schema() + + def test_defaults(self): + defaults = { + "columns": [], + "editable": True, + "error": False, + "scroll": False, + "showHeader": False, + "span": 12, + "targets": [], + "title": "foobar", + } + + self.assertEqual(self.schema(defaults), defaults)