-
Notifications
You must be signed in to change notification settings - Fork 3
/
cycle.js
374 lines (316 loc) · 9.24 KB
/
cycle.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
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2023 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| 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 General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
var timerID = null;
var image = ''
var html = ''
var nextid = -1
var previd = 1
var time = 5
var ltime = 0
var current = 0
var setfilter = 0
var clearfilter = 0
var timed
var newtime
var stime
var url
var graph_width = parseInt($(window).width() / $('#cols').val());
var graph_height = parseInt($('#height') * (graph_width / $('#width').val()));
$(window).resize(function() {
resizeGraphs();
});
function calcage(secs, num1, num2) {
return ((Math.floor(secs/num1))%num2).toString()
}
function formattime(secs) {
days = calcage(secs,86400,100000)
hours = calcage(secs,3600,24)
minutes = calcage(secs,60,60)
seconds = calcage(secs,1,60)
newtime = ''
if (days==1) { newtime=days+' Day ' }
if (days>1) { newtime=days+' Days ' }
if (hours==1) { newtime=newtime+hours+' Hour ' }
if (hours>1) { newtime=newtime+hours+' Hours ' }
if (minutes==1) { newtime=newtime+minutes+' Minute ' }
if (minutes>1) { newtime=newtime+minutes+' Minutes ' }
if (seconds==1) { newtime=newtime+seconds+' Second ' }
if (seconds>1) { newtime=newtime+seconds+' Seconds ' }
if (newtime=='') { return '0 Seconds' }
return newtime
}
function startTime() {
if (timerID == null)
timerID = setInterval(refreshTime, 1000)
$('#cstop').css('display', 'inline');
$('#cstart').css('display', 'none');
}
function stopTime() {
if (timerID != null)
{
clearInterval(timerID);
timerID = null;
}
$('#cstop').css('display', 'none');
$('#cstart').css('display', 'inline');
}
function resizeGraphs() {
graph_width = parseInt(($(window).width() - 60) / $('#cols').val());
if (graph_width > $('#width').val()) {
graph_width = $('#width').val();
}
graph_height = parseInt($('#height').val() * (graph_width / $('#width').val()));
$('.cycle_image').css('width', graph_width).css('height', graph_height).css('padding', '3px');
}
function loadGraphs(id) {
var hadTimer = (timerID != null);
if (hadTimer) {
stopTime();
}
if ($('#tree_id').length) {
tree=$('#tree_id').val();
}else{
tree='';
}
if ($('#leaf_id').length) {
leaf=$('#leaf_id').val();
}else{
leaf='';
}
strURL = 'cycle.php?action=graphs' +
'&id=' + id +
'&rfilter=' + $('#rfilter').val() +
'&cols=' + $('#cols').val() +
'×pan=' + $('#timespan').val() +
'&graphs=' + $('#graphs').val() +
'&tree_id=' + tree +
'&leaf_id=' + leaf +
'&legend=' + $('#legend').is(':checked') +
'&width=' + $('#width').val() +
'&height=' + $('#height').val() +
'&delay=' + $('#delay').val();
$.get(strURL, function(data) {
data = $.parseJSON(data);
if (data.image) {
image = base64_decode(data.image);
}
if (data.graphid) {
current = data.graphid;
}
if (data.nextgraphid) {
nextid = data.nextgraphid;
}
if (data.prevgraphid) {
previd = data.prevgraphid;
}
$('#image').html(image);
resizeGraphs();
if (hadTimer) {
startTime();
}
});
}
function saveFilter() {
if ($('#tree_id').length) {
tree=$('#tree_id').val();
}else{
tree='';
}
if ($('#leaf_id').length) {
leaf=$('#leaf_id').val();
}else{
leaf='';
}
var postData = {
rfilter: $('#rfilter').val(),
cols: $('#cols').val(),
timespan: $('#timespan').val(),
graphs: $('#graphs').val(),
tree_id: tree,
leaf_id: leaf,
legend: $('#legend').is(':checked'),
width: $('#width').val(),
height: $('#height').val(),
delay: $('#delay').val(),
__csrf_magic: csrfMagicToken
};
url = 'cycle.php?action=save';
$.post(url, postData).done(function(data) {
$('#text').show().text('Filter Settings Saved').fadeOut(2000);
});
}
function refreshTime() {
ltime++
$('#countdown').html(formattime(time));
if (time == 0)
{
stopTime();
getNext();
startTime();
}
time=time-1
}
function newRefresh() {
rtime=$('#delay').val() * 1000;
time=rtime/1000;
loadGraphs(current);
}
function newTimespan() {
time=rtime/1000;
loadGraphs(current);
}
function newGraph() {
rtime=$('#delay').val() * 1000;
time=rtime/1000;
loadGraphs(current);
}
function getNext() {
rtime=$('#delay').val() * 1000;
time=rtime/1000;
loadGraphs(nextid);
}
function getPrev() {
rtime=$('#delay').val() * 1000;
time=rtime/1000;
loadGraphs(previd);
}
function clearFilter() {
strURL = 'cycle.php?action=view&clear=true&header=false';
stopTime();
loadPageNoHeader(strURL, function() {
loadGraphs(current);
});
}
function applyFilter() {
if ($('#tree_id').length) {
tree=$('#tree_id').val();
}else{
tree='';
}
if ($('#leaf_id').length) {
leaf=$('#leaf_id').val();
}else{
leaf='';
}
strURL = 'cycle.php?action=view' +
'&header=false' +
'&id=' + nextid +
'&rfilter=' + $('#rfilter').val() +
'&cols=' + $('#cols').val() +
'×pan=' + $('#timespan').val() +
'&graphs=' + $('#graphs').val() +
'&tree_id=' + tree +
'&leaf_id=' + leaf +
'&legend=' + $('#legend').is(':checked') +
'&width=' + $('#width').val() +
'&height=' + $('#height').val() +
'&delay=' + $('#delay').val();
stopTime();
loadPageNoHeader(strURL);
}
function base64_decode(data) {
// http://kevin.vanzonneveld.net
// + original by: Tyler Akins (http://rumkin.com)
// + improved by: Thunder.m
// + input by: Aman Gupta
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + bugfixed by: Pellentesque Malesuada
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// - depends on: utf8_decode
// * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
// * returns 1: 'Kevin van Zonneveld'
// mozilla has this native
// - but breaks in 2.0.0.12!
//if (typeof this.window['btoa'] == 'function') {
// return btoa(data);
//}
var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
ac = 0,
dec = '',
tmp_arr = [];
if (!data) {
return data;
}
data += '';
do { // unpack four hexets into three octets using index points in b64
h1 = b64.indexOf(data.charAt(i++));
h2 = b64.indexOf(data.charAt(i++));
h3 = b64.indexOf(data.charAt(i++));
h4 = b64.indexOf(data.charAt(i++));
bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
o1 = bits >> 16 & 0xff;
o2 = bits >> 8 & 0xff;
o3 = bits & 0xff;
if (h3 == 64) {
tmp_arr[ac++] = String.fromCharCode(o1);
} else if (h4 == 64) {
tmp_arr[ac++] = String.fromCharCode(o1, o2);
} else {
tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
}
} while (i < data.length);
dec = tmp_arr.join('');
dec = this.utf8_decode(dec);
return dec;
}
function utf8_decode (str_data) {
// http://kevin.vanzonneveld.net
// + original by: Webtoolkit.info (http://www.webtoolkit.info/)
// + input by: Aman Gupta
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Norman 'zEh' Fuchs
// + bugfixed by: hitwork
// + bugfixed by: Onno Marsman
// + input by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: utf8_decode('Kevin van Zonneveld');
// * returns 1: 'Kevin van Zonneveld'
var tmp_arr = [],
i = 0,
ac = 0,
c1 = 0,
c2 = 0,
c3 = 0;
str_data += '';
while (i < str_data.length) {
c1 = str_data.charCodeAt(i);
if (c1 < 128) {
tmp_arr[ac++] = String.fromCharCode(c1);
i++;
} else if (c1 > 191 && c1 < 224) {
c2 = str_data.charCodeAt(i + 1);
tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = str_data.charCodeAt(i + 1);
c3 = str_data.charCodeAt(i + 2);
tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return tmp_arr.join('');
}