Add support for template query
A cool feature grafana has is the ability to template dashboards with variables. This code now provides that ability to the user. Change-Id: Ib2f565e3d39523105b2c07d29d5257494a8bae67 Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
parent
187caf92d7
commit
f95956b3e2
grafana_dashboards/schema
tests
schema/fixtures
dashboard-0001.jsondashboard-0002.jsondashboard-0003.jsondashboard-0004.jsondashboard-0005.jsondashboard-0006.jsondashboard-0007.jsondashboard-0008.jsondashboard-0009.jsondashboard-0010.jsondashboard-0011.jsondashboard-0012.jsondashboard-0013.jsondashboard-0014.jsondashboard-0014.yaml
test_parser.py@ -15,6 +15,7 @@
|
||||
import voluptuous as v
|
||||
|
||||
from grafana_dashboards.schema.row import Row
|
||||
from grafana_dashboards.schema.template import Template
|
||||
|
||||
|
||||
class Dashboard(object):
|
||||
@ -27,4 +28,7 @@ class Dashboard(object):
|
||||
}
|
||||
rows = Row().get_schema()
|
||||
dashboard.update(rows.schema)
|
||||
templating = Template().get_schema()
|
||||
dashboard.update(templating.schema)
|
||||
|
||||
return dashboard
|
||||
|
59
grafana_dashboards/schema/template/__init__.py
Normal file
59
grafana_dashboards/schema/template/__init__.py
Normal file
@ -0,0 +1,59 @@
|
||||
# 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.template.base import Base
|
||||
from grafana_dashboards.schema.template.query import Query
|
||||
|
||||
|
||||
class Template(object):
|
||||
|
||||
def __init__(self):
|
||||
# TODO(pabelanger): This is pretty ugly, there much be a better way to
|
||||
# set default values.
|
||||
self.defaults = {
|
||||
'enabled': False,
|
||||
'list': [],
|
||||
}
|
||||
|
||||
def _validate(self):
|
||||
|
||||
def f(data):
|
||||
res = self.defaults
|
||||
if not isinstance(data, list):
|
||||
raise v.Invalid('Should be a list')
|
||||
|
||||
for template in data:
|
||||
res['enabled'] = True
|
||||
validate = Base().get_schema()
|
||||
validate(template)
|
||||
|
||||
if template['type'] == 'query':
|
||||
schema = Query().get_schema()
|
||||
|
||||
res['list'].append(schema(template))
|
||||
|
||||
return res
|
||||
|
||||
return f
|
||||
|
||||
def get_schema(self):
|
||||
schema = v.Schema({
|
||||
v.Required(
|
||||
'templating', default=self.defaults): v.All(
|
||||
self._validate()),
|
||||
})
|
||||
|
||||
return schema
|
27
grafana_dashboards/schema/template/base.py
Normal file
27
grafana_dashboards/schema/template/base.py
Normal file
@ -0,0 +1,27 @@
|
||||
# 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
|
||||
|
||||
|
||||
class Base(object):
|
||||
|
||||
def __init__(self):
|
||||
self.base = {
|
||||
v.Required('name'): v.All(str, v.Length(min=1)),
|
||||
v.Required('type'): v.Any('query'),
|
||||
}
|
||||
|
||||
def get_schema(self):
|
||||
return v.Schema(self.base, extra=True)
|
30
grafana_dashboards/schema/template/query.py
Normal file
30
grafana_dashboards/schema/template/query.py
Normal file
@ -0,0 +1,30 @@
|
||||
# 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.template.base import Base
|
||||
|
||||
|
||||
class Query(Base):
|
||||
|
||||
def get_schema(self):
|
||||
query = {
|
||||
v.Required('includeAll', default=False): v.All(bool),
|
||||
v.Required('multi', default=False): v.All(bool),
|
||||
v.Required('query', default=''): v.All(str),
|
||||
v.Required('refresh', default=False): v.All(bool),
|
||||
}
|
||||
query.update(self.base)
|
||||
return v.Schema(query)
|
@ -2,6 +2,10 @@
|
||||
"dashboard": {
|
||||
"new-dashboard": {
|
||||
"rows": [],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -11,6 +11,10 @@
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -23,6 +23,10 @@
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -21,6 +21,10 @@
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -31,6 +31,10 @@
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -36,6 +36,10 @@
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -41,6 +41,10 @@
|
||||
"title": "bar"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -2,6 +2,10 @@
|
||||
"dashboard": {
|
||||
"new-dashboard": {
|
||||
"rows": [],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "browser",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -33,6 +33,10 @@
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -37,6 +37,10 @@
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -32,6 +32,10 @@
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -36,6 +36,10 @@
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
@ -55,6 +55,10 @@
|
||||
"title": "New row"
|
||||
}
|
||||
],
|
||||
"templating": {
|
||||
"enabled": false,
|
||||
"list": []
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
|
22
tests/schema/fixtures/dashboard-0014.json
Normal file
22
tests/schema/fixtures/dashboard-0014.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"dashboard": {
|
||||
"new-dashboard": {
|
||||
"rows": [],
|
||||
"templating": {
|
||||
"enabled": true,
|
||||
"list": [
|
||||
{
|
||||
"includeAll": false,
|
||||
"multi": false,
|
||||
"name": "foobar",
|
||||
"query": "foobar.*",
|
||||
"refresh": false,
|
||||
"type": "query"
|
||||
}
|
||||
]
|
||||
},
|
||||
"timezone": "utc",
|
||||
"title": "New dashboard"
|
||||
}
|
||||
}
|
||||
}
|
6
tests/schema/fixtures/dashboard-0014.yaml
Normal file
6
tests/schema/fixtures/dashboard-0014.yaml
Normal file
@ -0,0 +1,6 @@
|
||||
dashboard:
|
||||
templating:
|
||||
- name: foobar
|
||||
query: foobar.*
|
||||
type: query
|
||||
title: New dashboard
|
@ -35,11 +35,19 @@ class TestCaseParser(TestCase):
|
||||
dashboard = {
|
||||
'foobar': {
|
||||
'rows': [],
|
||||
'templating': {
|
||||
'enabled': False,
|
||||
'list': [],
|
||||
},
|
||||
'timezone': 'utc',
|
||||
'title': 'foobar',
|
||||
},
|
||||
'new-dashboard': {
|
||||
'rows': [],
|
||||
'templating': {
|
||||
'enabled': False,
|
||||
'list': [],
|
||||
},
|
||||
'timezone': 'utc',
|
||||
'title': 'New dashboard',
|
||||
},
|
||||
@ -71,6 +79,10 @@ class TestCaseParser(TestCase):
|
||||
dashboard = {
|
||||
'new-dashboard': {
|
||||
'rows': [],
|
||||
'templating': {
|
||||
'enabled': False,
|
||||
'list': [],
|
||||
},
|
||||
'timezone': 'utc',
|
||||
'title': 'New dashboard',
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user