Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2020-01-25 17:02:18 +0100
committerArthur de Jong <arthur@arthurdejong.org>2020-01-27 23:51:50 +0100
commite11103026650a35990a8deb5a5399abd50da5704 (patch)
tree74fd060bfe56b17de5bdf23c6488766aab42edd8
parent2c2e0d4f9f94f53f20d6a136dc0afff33331ddb8 (diff)
Add a dashboard chooser
-rw-r--r--muninplot/wsgi.py26
-rw-r--r--src/index.html7
-rw-r--r--src/munin-plot.js24
3 files changed, 55 insertions, 2 deletions
diff --git a/muninplot/wsgi.py b/muninplot/wsgi.py
index 10f2500..b24bdec 100644
--- a/muninplot/wsgi.py
+++ b/muninplot/wsgi.py
@@ -30,6 +30,10 @@ import pkg_resources
from muninplot.data import get_info, get_resolutions, get_values
+# The directory that contains the JSON files that describe the dashboards
+DASHBOARDS_DIR = os.environ.get('DASHBOARDS_DIR', None)
+
+
def static_serve(environ, start_response):
"""Server static files that are shipped with the package."""
path = environ.get('PATH_INFO', '').lstrip('/') or 'index.html'
@@ -67,6 +71,24 @@ def list_graphs(environ, start_response):
return [json.dumps(get_info(), indent=2, sort_keys=True).encode('utf-8')]
+def list_dashboards(environ, start_response):
+ """Return the configured dashboards as JSON."""
+ dashboards = {}
+ # Go over DASHBOARDS_DIR and load JSON files from that
+ dashboards_dir = environ.get('DASHBOARDS_DIR', DASHBOARDS_DIR)
+ if dashboards_dir and os.path.isdir(dashboards_dir):
+ for filename in sorted(os.listdir(dashboards_dir)):
+ filename = os.path.join(dashboards_dir, filename)
+ if filename.endswith('.json') and os.path.isfile(filename):
+ with open(filename, 'rt') as f:
+ dashboard = json.load(f)
+ dashboard.setdefault('name', os.path.basename(filename)[:-5])
+ dashboards[dashboard['name']] = dashboard
+ start_response('200 OK', [
+ ('Content-Type', 'application/json')])
+ return [json.dumps(dashboards, indent=2, sort_keys=True).encode('utf-8')]
+
+
def _field_key(x):
"""Order field.min, field, field.max."""
if x.endswith('.min'):
@@ -116,7 +138,7 @@ def get_data(environ, start_response):
end = s
# return the values as CSV
start_response('200 OK', [
- ('Content-Type', 'text/plain')])
+ ('Content-Type', 'text/csv')])
if values:
keys = (x for x in values[0].keys() if x != 'remove')
keys = ['time'] + sorted((k for k in keys if k != 'time'), key=_field_key)
@@ -132,6 +154,8 @@ def application(environ, start_response):
path = environ.get('PATH_INFO', '').lstrip('/')
if path.startswith('graphs'):
return list_graphs(environ, start_response)
+ if path.startswith('dashboards'):
+ return list_dashboards(environ, start_response)
elif path.startswith('data/'):
return get_data(environ, start_response)
else:
diff --git a/src/index.html b/src/index.html
index 8e39b0b..eab26ac 100644
--- a/src/index.html
+++ b/src/index.html
@@ -43,6 +43,13 @@
<button class="btn btn-outline-secondary" type="button" id="clearGraphs" title="Close all graphs">
<i class="fa fa-eraser"></i>
</button>
+ <div class="btn-group" id="dashboards">
+ <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown">
+ <i class="fa fa-tachometer-alt"></i>
+ <span>Dashboards</span>
+ </button>
+ <div class="dropdown-menu"></div>
+ </div>
</div>
<div id="reportrange" class="dropdown-toggle btn btn-outline-secondary ml-auto d-none">
<span></span>
diff --git a/src/munin-plot.js b/src/munin-plot.js
index 291c6dc..c9c1261 100644
--- a/src/munin-plot.js
+++ b/src/munin-plot.js
@@ -672,7 +672,29 @@ $(document).ready(function () {
}
// configure the clearGraphs button
- $('#clearGraphs').click(clearGraphs)
+ $('#clearGraphs').click(function () {
+ clearGraphs()
+ $('#dashboards button.dropdown-toggle span').text('Dashboards')
+ })
+
+ // configure the dashboards button
+ $.getJSON('dashboards', function (dashboards) {
+ if (Object.keys(dashboards).length === 0) {
+ $('#dashboards').remove()
+ } else {
+ Object.keys(dashboards).sort().forEach(function (name) {
+ var option = $('<button class="dropdown-item" type="button">').text(name)
+ $('#dashboards .dropdown-menu').append(option)
+ option.click(function () {
+ var dashboard = dashboards[name]
+ setGraphs(dashboard.graphs)
+ setDateRange(dashboard.dateRange.start, dashboard.dateRange.end)
+ $('#dashboards button.dropdown-toggle span').text(name)
+ })
+ })
+ }
+ })
+
// load information on available graphs
$.getJSON('graphs', function (data) {
document.graph_data = data