-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathinspect_graph.php
433 lines (375 loc) · 12.2 KB
/
inspect_graph.php
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
<?php
include_once "./eval_conf.php";
?>
<div id="inspect_graph_container"></div>
<?php
include_once "./eval_conf.php";
$dataurl = "graph.php?" . $_SERVER['QUERY_STRING'];
$refresh_interval = $conf['default_refresh'];
?>
<script>
$(function () {
var refresh_timer = null;
var first_time = true;
var VALUE_SEPARATOR = " :: ";
var plot = null;
var zooming = false;
var graphContainer = $("#inspect_graph_container");
var placeHolder = null;
var graphControls = null;
var spacer = null;
var selectSeries = null;
var selectLine = null;
var selectStack = null;
var tooltip = null;
var lastUpdate = 0;
var tz = getTimezone();
function zeroOutMissing(dataset, index) {
if (dataset.data == null || index < 0)
return;
for (var i = 0; i <= index; i++) {
if (dataset.data[i][1] == "NaN")
dataset.data[i][1] = 0;
}
}
function lastUpdateIndex(datasets) {
var index = 0;
$.each(datasets, function(key, dataset) {
if (dataset.data == null)
return;
for (var i = dataset.data.length - 1;
i >= 0 && i > index;
i--) {
if (dataset.data[i][1] != "NaN") {
index = i;
break;
}
}
});
return index;
}
function getTimezone() {
var tzValue = "browser";
var tz = $("#tz");
if (tz[0] && tz.val() === "")
tzValue = server_timezone;
return tzValue;
}
function resize() {
var popupDialog = $("#popup-dialog");
placeHolder.height(popupDialog.height() -
graphControls.height() -
spacer.height());
graphContainer.width(popupDialog.width() - 20);
}
$("#popup-dialog").bind("dialogresizestop.inspect",
function() {
resize();
plot.resize();
plotAccordingToChoices();
});
$("#popup-dialog").bind("dialogclose.inspect",
function(event) {
$(this).unbind(".inspect");
if (refresh_timer) {
clearTimeout(refresh_timer);
refresh_timer = null;
}
});
graphContainer.append('<div id="placeholder" style="overflow:hidden"></div><div id="spacer" style="height:5px;"></div><div id="graphcontrols"></div>');
spacer = graphContainer.find("#spacer");
var datasets = []; // global array of dataset objects {label, data, color}
var graph_title = null;
placeHolder = graphContainer.find("#placeholder");
placeHolder.bind("plothover", hoverHandler);
placeHolder.bind("plotselected", selectRangeHandler);
var dataurl = '<?php print $dataurl; ?>';
var refresh_interval = '<?php print $refresh_interval; ?>';
graphControls = graphContainer.find("#graphcontrols");
var series_select = '<select id="select_series" name="select_series" multiple="multiple"></select>';
// Add multi-select menu to controls
graphControls.append(series_select);
selectSeries = graphControls.find("#select_series");
selectSeries.multiselect({
height: "auto",
position : {
my: "right bottom",
at: "left top"
},
checkAll: function(event, ui) {
plotAccordingToChoices();
},
uncheckAll: function(event, ui) {
plotAccordingToChoices();
},
click: function(event, ui) {
plotAccordingToChoices();
}
}).multiselectfilter();
$(".ui-multiselect-menu").draggable();
var html = '<span id="gopt" style="margin-left:10px;"><input type="radio" id="line" name="gopt"/><label style="font-size:0.825em;" for="line">Line</label><input type="radio" id="stack" name="gopt"/><label style="font-size:0.825em" for="stack">Stack</label></span>';
html += '<input id="resetzoom" type="button" style="font-size:0.825em;" value="Reset zoom"/>';
// Add option buttons to controls
graphControls.append(html);
graphControls.find("#gopt").controlgroup();
selectLine = graphControls.find("#line");
selectLine.checkboxradio({icon: false}).click(function() {
plotAccordingToChoices();
});
selectStack = graphControls.find("#stack");
selectStack.checkboxradio({icon: false}).click(function() {
plotAccordingToChoices();
});
var resetZoomElem = graphControls.find("#resetzoom");
resetZoomElem.button();
resetZoomElem.click(function () {
delete plotOpt.xaxis.min;
delete plotOpt.xaxis.max;
delete plotOpt.yaxis.min;
delete plotOpt.yaxis.max;
zooming = false;
plotAccordingToChoices();
});
var plotOpt = {
lines: { show: true, fill: false },
points: { show: false },
crosshair: { mode: "x" },
xaxis: { mode: "time", timezone: tz },
yaxis: {tickFormatter: suffixFormatter},
selection: { mode: "xy" },
legend: {show : false},
grid: { hoverable: true, autoHighlight: true },
series: {stack: null}
};
// then fetch the data with jQuery
function onDataReceived(series) {
datasets = series;
var stacked = false;
var series_labels =
selectSeries.children("option").map(function(){
var text = $(this).text();
var label = text;
if (text.indexOf(VALUE_SEPARATOR) != -1)
label = text.split(VALUE_SEPARATOR)[0];
return label;
});
var series_options = selectSeries.children("option").map(function(){
return $(this);
});
var start_time = Number.MAX_VALUE;
var end_time = 0;
// Determine point index corresponding to last update
// Typically the dataset will have trailing NaNs that
// should be ignored
lastUpdate = lastUpdateIndex(datasets);
var i = 0;
$.each(datasets, function(key, dataset) {
if (dataset.data == null)
return;
start_time = Math.min(dataset.data[0][0], start_time);
end_time = Math.max(dataset.data[dataset.data.length - 1][0],
end_time);
// Explicity delete the stack attribute if it exists because stacking
// is controlled locally. The incoming datasets will contain a
// stack attribute if they were generated from a stacked graph.
if ("stack" in dataset) {
delete dataset.stack;
stacked = true;
}
if ((graph_title == null) && ("graph_title" in dataset))
$("#popup-dialog").dialog('option', 'title', dataset.graph_title);
if (typeof dataset.color == 'undefined')
dataset.color = i;
i++;
var current_value = dataset.data[lastUpdate][1];
if (current_value != "NaN")
current_value = formattedSiVal(current_value, 2);
else
current_value = "";
var seriesIndex = $.inArray(dataset.label, series_labels);
if (seriesIndex == -1) {
var option = $('<option/>',
{value: key,
text: dataset.label +
VALUE_SEPARATOR +
current_value});
option.prop('selected', true);
option.appendTo(selectSeries);
var colorBox = '<div style="border:1px solid #ccc;padding:1px;display:inline-block;"><div style="width:4px;height:0;border:5px solid ' + dataset.color + ';overflow:hidden"></div></div>';
option.data("pre_checkbox_html", colorBox);
} else {
var option = series_options[seriesIndex];
var label = series_labels[seriesIndex] +
VALUE_SEPARATOR +
current_value;
option.text(label);
}
});
selectSeries.multiselect('refresh');
if (first_time) {
$("#popup-dialog-navigation").html("");
var gopt = stacked ? selectStack : selectLine;
gopt.prop("checked", true).checkboxradio("refresh");
if (($("#popup-dialog").dialog("option", "height") == "auto") ||
($("#popup-dialog").dialog("option", "width") == "auto")) {
$("#popup-dialog").dialog("option", "height", 500);
$("#popup-dialog").dialog("option", "width", 800);
}
resize();
first_time = false;
}
$.ajax({
url: "get_overlay_events.php?start=" + (start_time/1000) + "&end=" + (end_time/1000),
method: 'GET',
dataType: 'json',
success: onEventsReceived
});
}
function onEventsReceived(overlay_events) {
var events = [];
$.each(overlay_events, function(key, val) {
var event = {};
event['min'] = val['start_time'] * 1000;
event['max'] = val['end_time'] * 1000;
event['eventType'] = 'info';
event['title'] = val['summary'];
event['description'] = val['description'];
events[events.length] = event;
});
var event_types = {};
event_types["info"] = {eventType: "info",
level: 1,
icon: {image: "img/red-pointer.png",
width: 10,
height: 10}};
plotOpt.events = {
levels: 1,
data: events,
types: event_types,
xaxis: 1
};
plotAccordingToChoices();
if ((!zooming) && (dataurl.indexOf("&r=custom") == -1))
refresh_timer = setTimeout(refresh, refresh_interval * 1000);
}
$.ajax({
url: dataurl + '&maxrows=' + placeHolder.width(),
method: 'GET',
dataType: 'json',
success: onDataReceived
});
function showTooltip(x, y, contents) {
tooltip = $('<div id="tooltip">' + contents + '</div>').css( {
position: 'absolute',
display: 'none',
'z-index': 2000,
top: y - 20,
left: x + 10,
border: '1px solid #fdd',
padding: '2px',
'background-color': '#fee',
opacity: 1.0
}).appendTo("body").fadeIn(200);
}
function formattedSiVal(val, places) {
if (val >= 1000000000) {
return (val / 1000000000).toFixed(places) + " G";
}
if (val >= 1000000) {
return (val / 1000000).toFixed(places) + " M";
}
if (val >= 1000) {
return (val / 1000).toFixed(places) + " k";
}
return (val/1).toFixed(places);
}
function suffixFormatter(val, axis) {
var tickd = axis.tickDecimals;
if (tickd <= 0) {
tickd = 1;
}
return formattedSiVal(val, tickd);
}
function selectRangeHandler(event, ranges) {
if ($("#event_tooltip")[0])
return;
var plotRanges = ranges;
if (plotRanges.xaxis.to - plotRanges.xaxis.from < 0.00001)
plotRanges.xaxis.to = plotRanges.xaxis.from + 0.00001;
if (plotRanges.yaxis.to - plotRanges.yaxis.from < 0.00001)
plotRanges.yaxis.to = plotRanges.yaxis.from + 0.00001;
plotOpt.xaxis.min = plotRanges.xaxis.from;
plotOpt.xaxis.max = plotRanges.xaxis.to;
plotOpt.yaxis.min = plotRanges.yaxis.from;
plotOpt.yaxis.max = plotRanges.yaxis.to;
zooming = true;
plotAccordingToChoices();
}
function hoverHandler(event, pos, item) {
if (item) {
if (tooltip != null)
tooltip.remove();
var y = formattedSiVal(item.datapoint[1], 2);
var time = (tz === "browser") ?
moment(item.datapoint[0]) : moment(item.datapoint[0]).tz(tz);
showTooltip(item.pageX,
item.pageY,
item.series.label + " at " +
time.format("MM/DD/YYYY HH:mm:ss") +
" = " + y);
} else {
if (tooltip != null)
tooltip.remove();
}
}
function refresh() {
$.ajax({
url: dataurl + '&maxrows=' + placeHolder.width(),
method: 'GET',
dataType: 'json',
success: onDataReceived});
}
function updatePlotOptions() {
plot.getOptions().events.data = plotOpt.events.data;
if (zooming) {
plot.getOptions().xaxes[0].min = plotOpt.xaxis.min;
plot.getOptions().xaxes[0].max = plotOpt.xaxis.max;
plot.getOptions().yaxes[0].min = plotOpt.yaxis.min;
plot.getOptions().yaxes[0].max = plotOpt.yaxis.max;
} else {
delete plot.getOptions().xaxes[0].min;
delete plot.getOptions().xaxes[0].max;
delete plot.getOptions().yaxes[0].min;
delete plot.getOptions().yaxes[0].max;
}
plot.getOptions().series.stack = plotOpt.series.stack;
plot.getOptions().series.lines.fill = plotOpt.lines.fill;
tz = getTimezone();
plot.getOptions().xaxis.timezone = tz;
}
function plotAccordingToChoices() {
var stack = selectStack.prop('checked');
var selected_series = selectSeries.multiselect("getChecked").map(function(){return this.value}).get();
var data = [];
for (var i = 0; i < selected_series.length; i++) {
var dataset = datasets[selected_series[i]];
data.push(dataset);
// The Flot stack plugin does not handle missing data correctly
if (stack)
zeroOutMissing(dataset, lastUpdate);
}
plotOpt.lines.fill = stack;
plotOpt.series.stack = stack ? 1 : null;
if (plot == null) {
plot = $.plot(placeHolder, data, plotOpt);
} else {
plot.clearEvents();
plot.clearSelection();
updatePlotOptions(); // must precede call to setData()
plot.setData(data);
plot.setupGrid();
plot.draw();
}
}
});
</script>