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

Support panel's repeat, transparent, span and description by default #145

Closed
Closed
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
30 changes: 17 additions & 13 deletions grafana_dashboards/components/panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}:
Copy link
Owner

@jakubplichta jakubplichta Apr 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already support for smth similar in JsonGenerator through _copy_fields attribute - it's already defined in e.g. Graph class. I think it should be pretty simple to define commons fields in PanelsItemBase and just extend it in all subclasses.

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please instead of defining new method I think it would be better to rely on inheritance and just define method gen_json_from_data in PanelsItemBase and handle common properties there.

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:
Expand Down Expand Up @@ -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')
})
Expand Down Expand Up @@ -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', [])]
Expand All @@ -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', '')
})
Expand All @@ -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)
})
Expand Down