From f1c684c1c297eb9f3c70a2029ba99ca22b9996bc Mon Sep 17 00:00:00 2001 From: Eldad Marciano Date: Tue, 6 Mar 2018 14:20:40 +0200 Subject: [PATCH] Support formatting. Adding format to schema base panel, in order to use graph unit e.g bytes, precent, deckbytes etc. Change-Id: I754596738970177bb70c5425d38e1291c56c84a9 --- grafana_dashboards/schema/panel/base.py | 18 ++++++ grafana_dashboards/schema/panel/graph.py | 78 +---------------------- tests/schema/fixtures/dashboard-0029.json | 49 ++++++++++++++ tests/schema/fixtures/dashboard-0029.yaml | 14 ++++ 4 files changed, 84 insertions(+), 75 deletions(-) create mode 100644 tests/schema/fixtures/dashboard-0029.json create mode 100644 tests/schema/fixtures/dashboard-0029.yaml diff --git a/grafana_dashboards/schema/panel/base.py b/grafana_dashboards/schema/panel/base.py index 25c72e3..4945579 100644 --- a/grafana_dashboards/schema/panel/base.py +++ b/grafana_dashboards/schema/panel/base.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + # Copyright 2015 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,6 +18,21 @@ import voluptuous as v class Base(object): + formats = v.Any(u'none', u'short', u'percent', u'percentunit', + u'humidity', u'ppm', u'dB', u'currencyUSD', + u'currencyGBP', u'currencyEUR', u'currencyJPY', u'hertz', + u'ns', u'µs', u'ms', u's', u'm', u'h', u'd', u'bits', + u'bytes', u'kbytes', u'mbytes', u'gbytes', u'decbits', + u'decbytes', u'deckbytes', u'decmbytes', u'decgbytes', + u'pps', u'bps', u'Bps', u'Kbits', u'KBs', u'Mbits', + u'MBs', u'GBs', u'Gbits', u'ops', u'rps', u'wps', u'iops', + u'opm', u'rpm', u'wpm', u'lengthmm', u'lengthm', + u'lengthkm', u'lengthmi', u'velocityms', u'velocitykmh', + u'velocitymph', u'velocityknot', u'mlitre', u'litre', + u'm3', u'watt', u'kwatt', u'watth', u'kwatth', u'joule', + u'ev', u'amp', u'volt', u'celsius', u'farenheit', + u'kelvin', u'pressurembar', u'pressurehpa', u'pressurehg', + u'pressurepsi') def __init__(self): self.base = { @@ -26,6 +43,7 @@ class Base(object): v.Required('type'): v.Any( 'dashlist', 'graph', 'singlestat', 'text'), v.Optional('id'): int, + v.Optional('format'): v.Any(self.formats, v.Length(min=1)), } def get_schema(self): diff --git a/grafana_dashboards/schema/panel/graph.py b/grafana_dashboards/schema/panel/graph.py index 50e6056..e33d773 100644 --- a/grafana_dashboards/schema/panel/graph.py +++ b/grafana_dashboards/schema/panel/graph.py @@ -22,83 +22,10 @@ from grafana_dashboards.schema.panel.base import Base class Graph(Base): def get_schema(self): - y_format = v.Any( - u'none', - u'short', - u'percent', - u'percentunit', - u'humidity', - u'ppm', - u'dB', - u'currencyUSD', - u'currencyGBP', - u'currencyEUR', - u'currencyJPY', - u'hertz', - u'ns', - u'µs', - u'ms', - u's', - u'm', - u'h', - u'd', - u'bits', - u'bytes', - u'kbytes', - u'mbytes', - u'gbytes', - u'decbits', - u'decbytes', - u'deckbytes', - u'decmbytes', - u'decgbytes', - u'pps', - u'bps', - u'Bps', - u'Kbits', - u'KBs', - u'Mbits', - u'MBs', - u'GBs', - u'Gbits', - u'ops', - u'rps', - u'wps', - u'iops', - u'opm', - u'rpm', - u'wpm', - u'lengthmm', - u'lengthm', - u'lengthkm', - u'lengthmi', - u'velocityms', - u'velocitykmh', - u'velocitymph', - u'velocityknot', - u'mlitre', - u'litre', - u'm3', - u'watt', - u'kwatt', - u'watth', - u'kwatth', - u'joule', - u'ev', - u'amp', - u'volt', - u'celsius', - u'farenheit', - u'kelvin', - u'pressurembar', - u'pressurehpa', - u'pressurehg', - u'pressurepsi', - ) yaxes_format = [ { v.Optional('decimals'): v.All(int), - v.Optional('format', default='short'): y_format, + v.Optional('format', default='short'): Base.formats, v.Optional('label', default=''): v.All(str), v.Optional('logBase', default=1): v.All(int, v.Range(min=1)), v.Optional('max'): v.All(int), @@ -175,7 +102,8 @@ class Graph(Base): v.Optional('tooltip'): v.All(tooltip), v.Required('x-axis', default=True): v.All(bool), v.Required('y-axis', default=True): v.All(bool), - v.Optional('y_formats'): v.All([y_format], v.Length(min=2, max=2)), + v.Optional('y_formats'): v.All([Base.formats], + v.Length(min=2, max=2)), v.Optional('yaxes'): v.All(yaxes_format, v.Length(min=2, max=2)), } graph.update(self.base) diff --git a/tests/schema/fixtures/dashboard-0029.json b/tests/schema/fixtures/dashboard-0029.json new file mode 100644 index 0000000..19f2bd4 --- /dev/null +++ b/tests/schema/fixtures/dashboard-0029.json @@ -0,0 +1,49 @@ +{ + "dashboard": { + "new-dashboard": { + "rows": [ + { + "collapse": false, + "editable": true, + "height": "250px", + "panels": [ + { + "bars": false, + "editable": true, + "error": false, + "fill": 1, + "format": "bytes", + "leftYAxisLabel": "left label", + "lines": true, + "linewidth": 2, + "percentage": false, + "pointradius": 5, + "points": false, + "rightYAxisLabel": "right label", + "span": 12, + "stack": false, + "steppedLine": false, + "targets": [], + "title": "no title (click here)", + "type": "graph", + "x-axis": true, + "y-axis": true, + "y_formats": [ + "hertz", + "\u00b5s" + ] + } + ], + "showTitle": false, + "title": "New row" + } + ], + "templating": { + "enabled": false, + "list": [] + }, + "timezone": "utc", + "title": "New dashboard" + } + } +} diff --git a/tests/schema/fixtures/dashboard-0029.yaml b/tests/schema/fixtures/dashboard-0029.yaml new file mode 100644 index 0000000..5cdb306 --- /dev/null +++ b/tests/schema/fixtures/dashboard-0029.yaml @@ -0,0 +1,14 @@ +dashboard: + title: New dashboard + rows: + - title: New row + height: 250px + panels: + - title: no title (click here) + type: graph + leftYAxisLabel: left label + rightYAxisLabel: right label + y_formats: + - hertz + - µs + format: bytes