-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathgetdash.js
64 lines (50 loc) · 1.95 KB
/
getdash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* global _ */
/*
* Complex scripted dashboard
* This script generates a dashboard object that Grafana can load. It also takes a number of user
* supplied URL parameters (int ARGS variable)
*/
// Accessable variables in this scope
var window, document, ARGS, $, jQuery, moment, kbn;
// Helper Functions
// loadScripts :: [scriptSourceStr] -> Promise([jQuery.getScript Result])
var loadScripts = function loadScripts (scriptSrcs) {
var gettingScripts = _.map(scriptSrcs, function (src) {
return $.getScript(src);
});
return Promise.all(gettingScripts);
};
// Dashboard Functions
// scriptedDashboard :: callbackFunc -> dashboardJSON
return function scriptedDashboard (callback) {
'use strict';
loadScripts([
'public/app/getdash/getdash.app.js',
'public/app/getdash/getdash.conf.js'
]).then(function () {
// sanitize :: Str -> new Str
var sanitize = function sanitize (str) {
return str.replace(/[^\w\s-,.*/]/gi, '');
};
var displayHost = (_.isUndefined(ARGS.host)) ? '' : ARGS.host;
// Dashboard configuration
var dashConf = {
host: displayHost,
metric: (_.isUndefined(ARGS.metric)) ? '' : sanitize(ARGS.metric),
instance: (_.isUndefined(ARGS.instance)) ? undefined : ARGS.instance,
time: (_.isUndefined(ARGS.time)) ? undefined : sanitize(ARGS.time),
refresh: (_.isUndefined(ARGS.refresh)) ? undefined : sanitize(ARGS.refresh),
span: (_.isUndefined(ARGS.span)) ? 12 : sanitize(ARGS.span),
legend: ARGS.legend,
title: 'Dashboard for ' + displayHost,
// Series used to get the list of all hosts
// (Some metric that is common for all hosts).
// If there is none for all hpsts ypu can add a list
defaultQueries: [ 'load_midterm' ],
defaultHostTags: [ 'host' ]
};
var datasources = _.values(window.grafanaBootData.settings.datasources);
var dash = getDashApp(datasources, getDashConf());
dash.get(dashConf, callback);
});
};