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
@ -15,6 +15,7 @@
|
|||||||
import voluptuous as v
|
import voluptuous as v
|
||||||
|
|
||||||
from grafana_dashboards.schema.row import Row
|
from grafana_dashboards.schema.row import Row
|
||||||
|
from grafana_dashboards.schema.template import Template
|
||||||
|
|
||||||
|
|
||||||
class Dashboard(object):
|
class Dashboard(object):
|
||||||
@ -27,4 +28,7 @@ class Dashboard(object):
|
|||||||
}
|
}
|
||||||
rows = Row().get_schema()
|
rows = Row().get_schema()
|
||||||
dashboard.update(rows.schema)
|
dashboard.update(rows.schema)
|
||||||
|
templating = Template().get_schema()
|
||||||
|
dashboard.update(templating.schema)
|
||||||
|
|
||||||
return dashboard
|
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": {
|
"dashboard": {
|
||||||
"new-dashboard": {
|
"new-dashboard": {
|
||||||
"rows": [],
|
"rows": [],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,10 @@
|
|||||||
"title": "New row"
|
"title": "New row"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,10 @@
|
|||||||
"title": "New row"
|
"title": "New row"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,10 @@
|
|||||||
"title": "New row"
|
"title": "New row"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,10 @@
|
|||||||
"title": "New row"
|
"title": "New row"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,10 @@
|
|||||||
"title": "New row"
|
"title": "New row"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,10 @@
|
|||||||
"title": "bar"
|
"title": "bar"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,10 @@
|
|||||||
"dashboard": {
|
"dashboard": {
|
||||||
"new-dashboard": {
|
"new-dashboard": {
|
||||||
"rows": [],
|
"rows": [],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "browser",
|
"timezone": "browser",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,10 @@
|
|||||||
"title": "New row"
|
"title": "New row"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,10 @@
|
|||||||
"title": "New row"
|
"title": "New row"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,10 @@
|
|||||||
"title": "New row"
|
"title": "New row"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,10 @@
|
|||||||
"title": "New row"
|
"title": "New row"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"title": "New dashboard"
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,10 @@
|
|||||||
"title": "New row"
|
"title": "New row"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"templating": {
|
||||||
|
"enabled": false,
|
||||||
|
"list": []
|
||||||
|
},
|
||||||
"timezone": "utc",
|
"timezone": "utc",
|
||||||
"title": "New dashboard"
|
"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 = {
|
dashboard = {
|
||||||
'foobar': {
|
'foobar': {
|
||||||
'rows': [],
|
'rows': [],
|
||||||
|
'templating': {
|
||||||
|
'enabled': False,
|
||||||
|
'list': [],
|
||||||
|
},
|
||||||
'timezone': 'utc',
|
'timezone': 'utc',
|
||||||
'title': 'foobar',
|
'title': 'foobar',
|
||||||
},
|
},
|
||||||
'new-dashboard': {
|
'new-dashboard': {
|
||||||
'rows': [],
|
'rows': [],
|
||||||
|
'templating': {
|
||||||
|
'enabled': False,
|
||||||
|
'list': [],
|
||||||
|
},
|
||||||
'timezone': 'utc',
|
'timezone': 'utc',
|
||||||
'title': 'New dashboard',
|
'title': 'New dashboard',
|
||||||
},
|
},
|
||||||
@ -71,6 +79,10 @@ class TestCaseParser(TestCase):
|
|||||||
dashboard = {
|
dashboard = {
|
||||||
'new-dashboard': {
|
'new-dashboard': {
|
||||||
'rows': [],
|
'rows': [],
|
||||||
|
'templating': {
|
||||||
|
'enabled': False,
|
||||||
|
'list': [],
|
||||||
|
},
|
||||||
'timezone': 'utc',
|
'timezone': 'utc',
|
||||||
'title': 'New dashboard',
|
'title': 'New dashboard',
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user