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 gauge schema to grafyaml #44

Merged
merged 1 commit into from
Sep 2, 2024
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
71 changes: 71 additions & 0 deletions grafana_dashboards/schema/panel/gauge.py
Original file line number Diff line number Diff line change
@@ -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)
46 changes: 46 additions & 0 deletions tests/schema/panels/gauge.py
Original file line number Diff line number Diff line change
@@ -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),
)
23 changes: 23 additions & 0 deletions tests/schema/panels/test_table.py
Original file line number Diff line number Diff line change
@@ -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)
Loading