-
Notifications
You must be signed in to change notification settings - Fork 2
/
tsdb.js
executable file
·641 lines (598 loc) · 21.7 KB
/
tsdb.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// This file is part of OpenTSDB Dashboard.
// // Copyright (C) 2013 Turn Inc.
// //
// // This program is free software: you can redistribute it and/or modify it
// // under the terms of the GNU Lesser General Public License as published by
// // the Free Software Foundation, either version 2.1 of the License, or (at your
// // option) any later version. This program is distributed in the hope that it
// // will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// // General Public License for more details. You should have received a copy
// // of the GNU Lesser General Public License along with this program. If not,
// // see <http://www.gnu.org/licenses/>.
/*
* Declaring and Initializing globals
*/
var OPENTSDB_URL = '<OpenTSDB URL>:<PORT>/q?start='; //eg: 'http://tsdb.com:4242/q?start='
var DASHBOARDS_DIR = '<JSON_FILE_PATH>'; // path of json files
var DEFAULT_FILTER1 = '';
var DEFAULT_FILTER2 = '';
var DEFAULT_FROM = '24h-ago';
var DEFAULT_GRAPH_HEIGHT = 300;
var DEFAULT_GRAPH_WIDTH = 600;
var DEFAULT_TOOLTIP = 'View full screen';
var DEFAULT_AGGREGATION = 'sum';
var DEFAULT_NODISPLAY = 'false';
var DEFAULT_KEY = 'top right box';
var DEFAULT_BOX = 'box';
var DEFAULT_Y_FORMAT = "%1.1s %c";
var DEFAULT_COL=2;
var DEFAULT_HEIGHT=200;
var DEFAULT_WIDTH=750;
var DS_FACTOR = 6;
var UNIT = 'avg';
var axes_params='';
var graphWidth=DEFAULT_WIDTH;
var graphHeight=DEFAULT_HEIGHT;
var key_params='';
var style_params='';
var o='';
var duration = 0;
var start_ds = 0;
var end_ds = 0;
var ds_duration = 0;
var url = '';
var k = 0;
var tsdb_filter = {
FILTER1: "filter1",
FILTER2: "filter2",
};
String.prototype.startsWith = function(s) {
return (this.indexOf(s) === 0); };
String.prototype.endsWith = function(s) {
return (this.indexOf(s) === (this.length-s.length) && s.length <= this.length); };
jQuery.support.cors = true;
var dashboard = (function($) {
function buildGraphs(fval1, from, data,fval2,smooth,flip) {
if (!data) {
return;
}
var now = new Date();
var dashboard_properties = data['dashboard_properties'];
var panel_objects;
var graph_objects;
var datastream_objects;
var tag_objects ;
var start = '';
var end = '';
var columns = 0;
var html = '';
var extras = '';
var sum =0;
var graph_object_count = 0;
var i=0;
var counter=0;
var col_separator=0;
if (dashboard_properties['title']) {
document.title = dashboard_properties['title'] + ' ' + document.title;
}
if (dashboard_properties['dc']) {
document.title = '[' + dashboard_properties['dc'] + '] ' + document.title;
}
if (data['panel_objects']) {
/*
* Calculating the total graph objects.
*/
for (i=0; i < data['panel_objects'].length; i++) {
panel_objects = data['panel_objects'][i];
graph_object_count += panel_objects['graph_objects'].length;
}
col_separator = parseInt((graph_object_count)/(dashboard_properties['column']|| DEFAULT_COL)); //Used to split the dashboard into columns
for (i=0; i < data['panel_objects'].length; i++) {
panel_objects = data['panel_objects'][i];
for (var j=0; j < panel_objects['graph_objects'].length; j++) {
graph_objects = panel_objects['graph_objects'][j];
if ( (counter==0 && i==0) || (counter%col_separator==0) &&
((dashboard_properties['column']&&(columns < dashboard_properties['column'])) ||
(!dashboard_properties['column'] && columns < DEFAULT_COL))) {
columns++;
html += '<td width="' + Math.round(100/data['panel_objects'].length) + '%">\n';
//If there is column change then print the panel object title.
if (panel_objects['title']) {
html += '<h2>' + panel_objects['title'] + '</h2>\n\n';
}
}
else if (j==0) {
if (panel_objects['title']) {
html += '<h2>' + panel_objects['title'] + '</h2>\n\n';
}
}
if (graph_objects['title']) {
html += '<h3>' + graph_objects['title'] + '</h2>\n\n';
}
k=0;
url=''
/*
Setting start and end time. If it is passed from dashboard then the value will override all other value.
Time specified in the dashboard overrides the one specified in graph_objects.
Precedence Order:
1) Passed from Url
2) Value Present in Dashboard Object
3) Value Present in Graph Properties
*/
if (from.indexOf("-")!=-1) {
if (from.substring(from.indexOf("-")+1)=="ago" || from.substring(from.indexOf("-")+1)=="win") {
start = from;
start_ds = getFormattedDate(from);
}
} else {
start = dashboard_properties['start_time'] || graph_objects['start_time'] || from ;
start_ds = getFormattedDate(dashboard_properties['start_time'] || graph_objects['start_time'] || from) ;
if ((dashboard_properties['end_time']!='') || (graph_objects['end_time'])) {
end = dashboard_properties['end_time'] || graph_objects['end_time'] ;
end_ds = getFormattedDate(dashboard_properties['end_time'] || graph_objects['end_time']) ;
}
}
/*
Calculating the difference between start and endtime. Downsmapling will be decided on the
length of this interval. We divide the interval with the graphWidth and then multiply the
ratio with a constant factor. Format is yyyy/mm/dd-HH:MM:SS
*/
if(start_ds.indexOf("/")!=-1) {
start_ds = start_ds.replace(/:|-/g,'/');
start_ds = new Date(start_ds.split("/")[0],start_ds.split("/")[1]-1,start_ds.split("/")[2],start_ds.split("/")[3],start_ds.split("/")[4],start_ds.split("/")[5]).getTime();
}
if(end_ds && end_ds.indexOf("/")!=-1) {
end_ds = end_ds.replace(/:|-/g,'/');
end_ds = (new Date(end_ds.split("/")[0],end_ds.split("/")[1]-1,end_ds.split("/")[2],end_ds.split("/")[3],end_ds.split("/")[4],end_ds.split("/")[5])).getTime();
}
duration = (end_ds > start_ds)?(end_ds-start_ds):((new Date()).getTime()-start_ds);
/*
Looping over datastream objects and building the m-parameter of url
*/
for (k=0; k < graph_objects['datastream_objects'].length; k++) {
datastream_objects = graph_objects['datastream_objects'][k];
if (!datastream_objects['metric_name']) {
continue;
}
/*
Building the Axes, Key and Style params of url
*/
if (graph_objects['axes']) {
if (datastream_objects['right_axes']) {
axes_params = getAxesParams(graph_objects['axes'],true);
} else {
axes_params = getAxesParams(graph_objects['axes'],false);
}
}
if (graph_objects['key']) {
key_params = getKeyParams(graph_objects['key']);
}
if (graph_objects['style']) {
style_params = getStyleParams(graph_objects['style'],smooth);
}
/*
* Setting up the optional parameter per datastream object
*/
o = axes_params + key_params + style_params;
/*
Setting graph width in the following precedence
1) Value set in Graph Object width
2) Value set in dashboard properties OR panel_objects OR 95% of windowWidth divided by min(2, panel_objects count)
3) Default value
*/
graphWidth = graph_objects['width'] || ( (dashboard_properties['width']|| panel_objects['width'] ||
0.95 * window.innerWidth) / (Math.min(2,data['panel_objects'].length) ) ) || DEFAULT_GRAPH_WIDTH;
graphWidth = parseInt(graphWidth);
/*
Setting graph height in the following precedence
1) Value set in Graph Object height
2) Value set in 2*(dashboard properties) divided by total graph count OR
panel_object height divided by count_of_graph_objects in this panel_object OR
95% of windowHeight divided by count_of_datastream_objects length
3) Default value
*/
graphHeight = graph_objects['height'] || (2*dashboard_properties['height']/graph_object_count) ||
(panel_objects['height']/panel_objects['graph_objects'].length) ||
(0.95 * window.innerHeight) / graph_objects['datastream_objects'].length || DEFAULT_GRAPH_HEIGHT;
graphHeight = parseInt(graphHeight);
var graphTooltip = graph_objects['tooltip'] || DEFAULT_TOOLTIP;
var aggregation = datastream_objects['aggregation'] || DEFAULT_AGGREGATION;
url = url + '&m='+ aggregation ;
ds_duration = ((DS_FACTOR*duration)/(60*graphWidth*1000));
/*
Setting up scale
*/
if (datastream_objects['scale']) {
url = url + ':' + escape(encodeURIComponent(datastream_objects['scale']));
} else {
url = url + ':';
}
/*
Setting up gnubox
*/
if (datastream_objects['gnubox']) {
url = url + ':' + escape(encodeURIComponent(datastream_objects['gnubox']));
} else {
url = url + ':';
}
/*
Setting up alias
*/
if (datastream_objects['alias']) {
var alias = escape(encodeURIComponent(datastream_objects['alias'])) ;
url = url + ':' + alias;
} else {
url = url + ':';
}
/*
Setting up downsampling
*/
if (datastream_objects['downsampling']) {
url = url + ':' + datastream_objects['downsampling'];
} else {
var ds = getDownsampleDuration(ds_duration);
if (ds > 1) {
url = url + ':' + getDownsampleDuration(ds_duration);
} else {
url = url + ':' + '';
}
}
/*
Setting up rate
*/
if (datastream_objects['rate']) {
url = url + ':' + datastream_objects['rate'];
} else {
url = url + ':';
}
/*
Setting up metric name
*/
url = url + ':' + datastream_objects['metric_name'];
/*
Setting up tags
*/
if (datastream_objects['tags']) {
if (datastream_objects['tags'].length > 0) { /// appending tagvalues in url if present
url = url + '{' +getTagValue(datastream_objects,fval1,fval2)+ '}';
}
} else if(fval1 || fval2) { /// To handle case where tags is missing but we have the default fval1 or fval2
url = url + '{';
if (fval1) {
url = url + tsdb_filter['FILTER1']+"="+fval1;
}
if (fval2) {
if (fval1) {
url = url +",";
}
url = url + tsdb_filter['FILTER2']+"="+fval2;
}
url = url + '}';
}
if (datastream_objects['nodisplay']) {
url = url + '&nodisplay='+datastream_objects['nodisplay'];
} else {
url = url + '&nodisplay=false';
}
if (datastream_objects['filter']) {
url = url + '&filter='+datastream_objects['filter']+o;
} else {
url = url + '&filter=none:1'+o;
}
/*
Setting up right axes
*/
if (datastream_objects['right_axes']) {
url = url +'&o=axis x1y2'+o;
} else {
url = url +'&o='+o;
}
} // datastream loop ends here
/*
Setting up end time if exists
*/
if (end!='') {
url = OPENTSDB_URL + start + '&end=' +end + url;
} else {
url = OPENTSDB_URL + start + url;
}
/*
Setting up flip to change the foreground and background color
*/
if (flip!=null) {
url = url +'&flip=true';
}
url = url + '&wxh=' + graphWidth +'x' + graphHeight ;
html += '<a href="' + url.replace('q?','#') + '" target="_blank"><img title="' +
graphTooltip + '" class="loading" src="' + url + '&png'+ '" width="' + graphWidth +
'" height="' + graphHeight + '" /></a>\n\n';
counter++;
}
}
html += '</td>\n\n';
}
$('#contents tr').html(html);
$('#contents img[title]').qtip({
position: {
my: 'top left',
target: 'mouse',
viewport: $(window),
adjust: { x: 10, y: 10 },
},
hide: { fixed: true },
});
$('#contents img').load(function() {
$(this).removeClass('loading'); });
$('h1').html(data['dashboard_properties']['title'] + ' ' +
$('h1').html());
}
function getURLParam(name) {
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (!results) { return null; }
return results[1] || null;
}
function setClock(e) {
var now = new Date();
e.text(now.getHours() + ':' +
(now.getMinutes() < 10 ? '0' : '') + now.getMinutes() + ':' +
(now.getSeconds() < 10 ? '0' : '') + now.getSeconds());
}
Number.prototype.pad = function (len) {
return (new Array(len+1).join("0")+this).slice(-len);
}
/*
Takes a date and convert it into yyyy/mm/dd-HH:MM:SS format.
*/
function getFormattedDate(time) {
var input_time = time;
var interval ;
var metric;
var date;
var formatted_date;
/*
input_time is already in yyyy/mm/dd-HH:MM:SS format. Return it.
*/
if (input_time.indexOf("/") != -1) {
return input_time;
}
/*
input_time is like 2h-ago. Convert it in yyyy/mm/dd-HH:MM:SS format
*/
if (input_time.indexOf("-") != -1) {
interval = input_time.substring(0,input_time.indexOf("-")-1);
metric = input_time.substring(input_time.indexOf("-")-1,input_time.indexOf("-"));
} else {
match = input_time.match(/^(\d+)([a-zA-Z])/);
interval = match[1];
metric = match[2];
}
switch(metric.toLowerCase()) {
case 's': break; // seconds
case 'm': interval *= 60; break; // minutes
case 'h': interval *= 3600; break; // hours
case 'd': interval *= 3600 * 24; break; // days
case 'w': interval *= 3600 * 24 * 7; break; // weeks
case 'M': interval *= 3600 * 24 * 30; break; // month
case 'y': interval *= 3600 * 24 * 365; break; // year
}
date = (new Date).getTime();
date = date -interval*1000;
var d = new Date(date);
formatted_date = d.getFullYear()+"/"+(d.getMonth()+1).pad(2)+"/"+d.getDate().pad(2)+"-"+d.getHours().pad(2)+":"+d.getMinutes().pad(2)+":"+d.getSeconds().pad(2);
return formatted_date;
}
/*
Returns the axes params.
*/
function getAxesParams(axes, y2axis_flag) {
var axes_param='';
if (axes['ylabel']) {
axes_param = axes_param + '&ylabel=' + escape(encodeURI(axes['ylabel']));
}
if (axes['yformat']) {
axes_param = axes_param + '&yformat=' + escape(encodeURI(axes['yformat']));
} else {
axes_param = axes_param + '&yformat=' + escape(encodeURI(DEFAULT_Y_FORMAT));
}
if (axes['yrange']) {
axes_param = axes_param + '&yrange=' + axes['yrange'];
}
if (axes['ylog']) {
axes_param = axes_param + '&ylog=' + axes['ylog'];
}
if (y2axis_flag) {
if (axes['y2label']) {
axes_param = axes_param + '&y2label=' + escape(encodeURI(axes['y2label']));
}
if (axes['y2format']) {
axes_param = axes_param + '&y2format=' + escape(encodeURI(axes['y2format']));
} else {
axes_param = axes_param + '&y2format=' + escape(encodeURI(DEFAULT_Y_FORMAT));
}
if (axes['y2range']) {
axes_param = axes_param + '&y2range=' + axes['y2range'];
}
if (axes['y2log']) {
axes_param = axes_param + '&y2log=' + axes['y2log'];
}
}
return axes_param;
}
/*
Returns the key params.
*/
function getKeyParams(key) {
var key_param='';
key_param = key_param + '&key=' + ( key['key'] || DEFAULT_KEY+' '+DEFAULT_BOX );
if (key['nokey']) {
key_param = key_param + '&nokey=' + key['nokey'];
}
return key_param;
}
/*
Returns the style params.
*/
function getStyleParams(style,smooth) {
var style_param='';
if (style['smooth'] || (smooth!=null)) {
style_param = style_param + '&smooth=' + (smooth || style['smooth']);
}
if (style['grid']) {
style_param = style_param + '&grid=' + style['grid'];
}
if (style['flip']) {
style_param = style_param + '&flip=' + style['flip'];
}
if (style['outstyle']) {
style_param = style_param + '&outstyle=' + style['outstyle'];
}
return style_param;
}
/*
Returns the downsampling interval
*/
function getDownsampleDuration(ds) {
var ds_duration = ds;
if (ds_duration < 60 ) {
ds_duration = parseInt(ds_duration) +'m-'+UNIT;
} else if (ds_duration >=60 && ds_duration <1440) {
ds_duration = parseInt(Math.ceil(ds_duration/60)) +'h-'+UNIT;
} else if (ds_duration >= 1440 && ds_duration < 10080) {
ds_duration = parseInt(Math.ceil(ds_duration/1440)) +'d-'+UNIT;
} else if (ds_duration >= 10080 && ds_duration < 43200) {
ds_duration = parseInt(Math.ceil(ds_duration/10080)) +'w-'+UNIT;
} else if (ds_duration >= 43200 && ds_duration < 525600) {
ds_duration = parseInt(Math.ceil(ds_duration/43200)) +'M-'+UNIT;
} else {
ds_duration = parseInt(Math.ceil(ds_duration/43200)) +'y-'+UNIT;
}
return ds_duration;
}
/*
Returns tag,value pair for a given metric.
*/
function getTagValue(datastream_objects,fval1,fval2) {
var tag_objects;
var tagval = '';
var tagkey ='';
var tagValueStrg = '';
var fval1_flag = fval1 ? 1:0;
var fval2_flag = fval2 ? 1:0;
for (var l=0; l < datastream_objects['tags'].length; l++) {
tag_objects = datastream_objects['tags'][l];
tagkey = tag_objects['tagkey'];
tagval = tag_objects['tagvalue'];
/*
For Sclass and DC, if the value is specified in the tagvalue, then the value will get higher precendence.
*/
if (tagkey == tsdb_filter['FILTER1']) {
fval1_flag = 0;
tagval = tag_objects['tagvalue'] || fval1 ;
}
if (tagkey == tsdb_filter['FILTER2']) {
fval2_flag = 0;
tagval = tag_objects['tagvalue'] || fval2 ;
}
if (tagval != '') { // not writing if tagval is empty
if (tagValueStrg == '') {
tagValueStrg = tagkey+'='+tagval;
} else {
tagValueStrg = tagValueStrg + ',' + tagkey+'='+tagval;
}
}
}
if (fval1_flag==1) {
if (tagValueStrg == '') {
tagValueStrg = tsdb_filter['FILTER1']+'='+fval1;
} else {
tagValueStrg = tagValueStrg + ','+tsdb_filter['FILTER1']+'='+fval1;
}
}
if (fval2_flag==1) {
if (tagValueStrg == '') {
tagValueStrg = tsdb_filter['FILTER2']+'='+fval2;
} else {
tagValueStrg = tagValueStrg + ','+tsdb_filter['FILTER2']+'='+fval2;
}
}
return tagValueStrg;
}
var _public = {
create: function() {
setClock($('#time'));
var url = decodeURIComponent(getURLParam('url'));
var options = $('#url').prop('options');
$.get(DASHBOARDS_DIR + '/', function(data) {
$('a', $(data)).each(function(i, link) {
if (link.href.endsWith('.json')) {
var parts = link.href.split('/');
var name = parts[parts.length-1];
options[options.length] = new Option(name, name);
}
});
$('#url').val(url);
if (!url || url == 'null') {
return;
}
var target = DASHBOARDS_DIR + '/' + url + '?_salt=' + new Date().getTime();
var t = setTimeout('location.reload(true);', 15000);
$('#footer').html('Refreshes every 15s.');
$.getJSON(target, function(data) {
var fval1 = getURLParam('fval1') || data['dashboard_properties'][tsdb_filter['FILTER1']] || DEFAULT_FILTER1;
var from = data['dashboard_properties']['start_time'] || DEFAULT_FROM;
var fval2 = getURLParam('fval2') || data['dashboard_properties'][tsdb_filter['FILTER2']] || DEFAULT_FILTER2;
var smooth = getURLParam('smooth');
var flip = getURLParam('flip');
buildGraphs(fval1, from, data,fval2,smooth,flip);
$('#url').val(url);
$('#from').val(from);
$('#fval1').val(fval1).focus().select();
$('#fval2').val(fval2).focus().select();
if (smooth!=null) {
$('#smooth').attr('checked',true);
}
if (flip!=null) {
$('#flip').attr('checked',true);
}
// Reset the timeout with the configured timeout (if present)
if (data['dashboard_properties']['refresh']) {
clearTimeout(t);
setTimeout('location.reload(true);', data['dashboard_properties']['refresh']);
}
// Build footer text
var footer = 'Refreshes every ' + Math.round(data['dashboard_properties']['refresh']/1000) + 's.';
if (data['dashboard_properties']['info']) {
if (data['dashboard_properties']['info']['author']) {
footer += ' Dashboard by ' + data['dashboard_properties']['info']['author'];
}
if (data['dashboard_properties']['info']['team']) {
footer += ' for ' + data['dashboard_properties']['info']['team'];
}
}
$('#footer').text(footer);
}).error(function() {
$('#footer').addClass('error').text('Error loading dashboard from ' + target + ' !');
$('#url').val(url).focus();
$('#from').val(getURLParam('from') || DEFAULT_FROM);
$('#fval1').val(getURLParam('fval1') || DEFAULT_FILTER1);
});
});
},
/**
* Change to another dashboard from the dropdown.
*
* Resets the datacenter and from time to their defaults (empty, so the
* default in the dashboard description is used) and submits the form.
*/
change: function(picker) {
$('#fval1').val('');
$('#fval2').val('');
$('#from').val('');
$('#smooth').val('');
$('#flip').val('');
picker.form.submit();
},
};
return _public;
})(jQuery);