From 8df6ed4b6a38e048b291ec9378fdb6dc597a12c3 Mon Sep 17 00:00:00 2001 From: Vasiliy Ostanin Date: Tue, 9 Apr 2019 10:06:42 +0500 Subject: [PATCH] Support panel repeat, transparent, span and description by default --- grafana_dashboards/components/panels.py | 30 ++++++++++++++----------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/grafana_dashboards/components/panels.py b/grafana_dashboards/components/panels.py index c6f7769..a69ddcc 100644 --- a/grafana_dashboards/components/panels.py +++ b/grafana_dashboards/components/panels.py @@ -29,21 +29,28 @@ def __init__(self, data, registry): class PanelsItemBase(JsonGenerator): - pass + _default_span = None + def copy_basic_fields(self, source, dest): + for field in {'description', 'transparent', 'repeat'}: + if field in source: + dest[field] = source[field] + + dest['span'] = source.get('span', self._default_span) + dest['title'] = source.get('title', None) -class Graph(PanelsItemBase): +class Graph(PanelsItemBase): + _default_span = 12 _copy_fields = {'stack', 'fill', 'aliasColors', 'leftYAxisLabel', 'bars', 'lines', 'linewidth', 'y_formats', 'x-axis', 'y-axis', 'points', 'pointradius', 'percentage', 'steppedLine', 'repeat', 'minSpan', 'datasource'} def gen_json_from_data(self, data, context): panel_json = super(Graph, self).gen_json_from_data(data, context) + self.copy_basic_fields(data, panel_json) panel_json.update({ 'type': 'graph', - 'title': self.data.get('title', None), - 'span': self.data.get('span', 12), }) targets = self.data.get('targets', []) if 'target' in self.data: @@ -106,14 +113,13 @@ class SingleStat(PanelsItemBase): # noinspection PySetFunctionToLiteral _copy_fields = set(['prefix', 'postfix', 'nullText', 'format', 'thresholds', 'colorValue', 'colorBackground', 'colors', 'prefixFontSize', 'valueFontSize', 'postfixFontSize', 'maxDataPoints', 'datasource', - 'repeat', 'decimals', 'minSpan']) + 'decimals', 'minSpan']) def gen_json_from_data(self, data, context): panel_json = super(SingleStat, self).gen_json_from_data(data, context) + self.copy_basic_fields(data, panel_json) panel_json.update({ 'type': 'singlestat', - 'title': data.get('title', None), - 'span': data.get('span', None), 'nullPointMode': data.get('nullPointMode', 'null'), 'valueName': data.get('valueName', 'current') }) @@ -153,10 +159,9 @@ class Table(PanelsItemBase): def gen_json_from_data(self, data, context): panel_json = super(Table, self).gen_json_from_data(data, context) + self.copy_basic_fields(data, panel_json) panel_json.update({ 'type': 'table', - 'title': data.get('title', None), - 'span': data.get('span', None), 'targets': [{'target': v} for v in data.get('targets', [])], 'transform': data.get('transform', None), 'columns': [{'text': v, 'value': str(v).lower()} for v in data.get('columns', [])] @@ -178,10 +183,9 @@ def gen_json_from_data(self, data, context): class Text(PanelsItemBase): def gen_json_from_data(self, data, context): panel_json = super(Text, self).gen_json_from_data(data, context) + self.copy_basic_fields(data, panel_json) panel_json.update({ 'type': 'text', - 'title': data.get('title', None), - 'span': data.get('span', None), 'mode': data.get('mode', 'text'), 'content': data.get('content', '') }) @@ -190,13 +194,13 @@ def gen_json_from_data(self, data, context): class Dashlist(PanelsItemBase): _copy_fields = {'headings', 'limit', 'recent', 'tags', 'query'} + _default_span = 12 def gen_json_from_data(self, data, context): panel_json = super(Dashlist, self).gen_json_from_data(data, context) + self.copy_basic_fields(data, panel_json) panel_json.update({ 'type': 'dashlist', - 'title': data.get('title', None), - 'span': data.get('span', 12), 'search': 'query' in data or 'tags' in data, 'starred': data.get('starred') or ('query' not in data and 'tags' not in data) })