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:
Paul Belanger 2015-11-27 11:31:19 -05:00 committed by James E. Blair
parent 187caf92d7
commit f95956b3e2
20 changed files with 212 additions and 0 deletions

View File

@ -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

View 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

View 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)

View 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)

View File

@ -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"
} }

View File

@ -11,6 +11,10 @@
"title": "New row" "title": "New row"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View File

@ -23,6 +23,10 @@
"title": "New row" "title": "New row"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View File

@ -21,6 +21,10 @@
"title": "New row" "title": "New row"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View File

@ -31,6 +31,10 @@
"title": "New row" "title": "New row"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View File

@ -36,6 +36,10 @@
"title": "New row" "title": "New row"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View File

@ -41,6 +41,10 @@
"title": "bar" "title": "bar"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View File

@ -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"
} }

View File

@ -33,6 +33,10 @@
"title": "New row" "title": "New row"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View File

@ -37,6 +37,10 @@
"title": "New row" "title": "New row"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View File

@ -32,6 +32,10 @@
"title": "New row" "title": "New row"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View File

@ -36,6 +36,10 @@
"title": "New row" "title": "New row"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View File

@ -55,6 +55,10 @@
"title": "New row" "title": "New row"
} }
], ],
"templating": {
"enabled": false,
"list": []
},
"timezone": "utc", "timezone": "utc",
"title": "New dashboard" "title": "New dashboard"
} }

View 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"
}
}
}

View File

@ -0,0 +1,6 @@
dashboard:
templating:
- name: foobar
query: foobar.*
type: query
title: New dashboard

View File

@ -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',
}, },