1
0
Fork 0
mirror of synced 2024-11-21 08:25:34 -05:00

Add script to format dashboards

Change-Id: I1c34dad29ad5807603b547c504fcf81185ca2e55
This commit is contained in:
Simon Pasquier 2017-06-26 15:18:19 +02:00
parent f6e5544264
commit ab3d122fd0

88
contrib/format.py Executable file
View file

@ -0,0 +1,88 @@
#!/usr/bin/python3
# Copyright 2016 Mirantis, 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 sys
import glob
import os
import json
def usage():
h = """
Format and sanitize Grafana dashboards
Usage: {} <DIRECTORY|FILE>
The program will:
* Sort keys alphabetically.
* Remove sections:
templating.list[].current
templating.list[].options
* Override the time entry to {{"from": "now-1h","to": "now"}}
* Enable the sharedCrosshair tooltip
* Increment the version number
WARNING: this script modifies directly the given file(s).
If a DIRECTORY is provided, all files with suffix '.json' are modified.
"""
print(h.format(sys.argv[0]))
if len(sys.argv) != 2:
usage()
sys.exit(1)
arg = sys.argv[1]
if os.path.isdir(arg):
path = "{}/*.json".format(arg)
elif os.path.isfile(arg):
path = arg
else:
print("'{}' no such file or directory".format(arg))
usage()
sys.exit(1)
for f in glob.glob(path):
data = None
absf = os.path.abspath(f)
with open(absf) as out:
data = json.load(out)
for k, v in data.items():
if k == 'annotations':
for anno in v.get('list', []):
anno['datasource'] = 'lma'
if k == 'templating':
variables = v.get('list', [])
for o in variables:
if o['type'] == 'query':
o['options'] = []
o['current'] = {}
o['refresh_on_load'] = True
data['time'] = {'from': 'now-1h', 'to': 'now'}
data['sharedCrosshair'] = True
data['refresh'] = '1m'
data['id'] = None
if data.get('version', None):
data['version'] = data['version'] + 1
else:
data['version'] = 1
with open(absf, 'w') as out:
out.write(json.dumps(data, indent=2, sort_keys=True))