-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
326 lines (309 loc) · 8.44 KB
/
functions.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
function scrollBack(timechangeratio) {
var from = datestr2unix($("from").value);
var until = datestr2unix($("until").value);
var timespan = until - from;
var timechange = timespan * timechangeratio;
if (timechange < 60) {
if (parseInt(new Date().getTime().toString().substr(-3)) % 75 < 17) {
timechange = 60;
}
else {
timechange = 0;
}
}
var newfrom = from - timechange;
var newuntil = until - timechange;
if (newfrom < mindatetime) {
newfrom = mindatetime;
newuntil = newfrom + timespan;
}
if (newuntil > maxdatetime) {
newuntil = maxdatetime;
newfrom = newuntil - timespan;
}
$("from").value = unix2datestr(newfrom);
$("until").value = unix2datestr(newuntil);
updateEvents();
}
function scrollForward(timechangeratio) {
var from = datestr2unix($("from").value);
var until = datestr2unix($("until").value);
var timespan = (until - from);
var timechange = Math.round(timespan * timechangeratio);
if (timechange < 60) {
if (parseInt(new Date().getTime().toString().substr(-3)) % 75 < 17) {
timechange = 60;
}
else {
timechange = 0;
}
}
var newfrom = from + timechange;
var newuntil = until + timechange;
if (newfrom < mindatetime) {
newfrom = mindatetime;
newuntil = newfrom + timespan;
}
if (newuntil > maxdatetime) {
newuntil = maxdatetime;
newfrom = newuntil - timespan;
}
$("from").value = unix2datestr(newfrom);
$("until").value = unix2datestr(newuntil);
updateEvents();
}
function updateViewDropdown() {
views = JSON.parse(GET('api.php?getViews'));
var sv = $("selectView");
sv.options.length = 0;
sv.options.add(new Option("Select a view", -1));
for (var viewi in views) {
var view = views[viewi];
sv.options.add(new Option(view.name + " (" + view.filter.substr(2) + ")", view.rowid));
}
}
function filterEvents() {
var starttime = new Date().getTime();
filteredEvents = [];
var filter = $("filter").value;
if (filter.length == 0) {
filteredEvents = events;
return;
}
for (var eventi in events) {
var event = events[eventi];
if ($("advfilter").checked) {
var filters = filter.split("|");
for (var filters2 in filters) {
filters3 = filters[filters2];
var ok = true;
var filters3 = filters3.split("&");
for (var filters4 in filters3) {
filters4 = filters3[filters4].split(":");
var field = filters4[0];
var filter5 = filters4[1];
switch (field) {
case 'dtls':
field = event.details;
break;
case 'sum':
field = event.summary;
break;
case 'dev':
field = event.device;
break;
case 'src':
field = event.source;
break;
default:
return [];
}
if (!new RegExp(filter5).test(field)) {
ok = false;
break;
}
}
if (!ok) {
continue;
}
else {
filteredEvents.push(event);
break;
}
}
}
else {
// No advanced filters
if ($("regexfilter").checked) {
re = new RegExp(filter);
if (re.test(event.summary)
|| re.test(event.details)
|| re.test(event.source)
|| re.test(event.device)) {
filteredEvents.push(event);
}
}
else {
if (event.summary.indexOf(filter) != -1
|| event.details.indexOf(filter) != -1
|| event.source.indexOf(filter) != -1
|| event.device.indexOf(filter) != -1) {
filteredEvents.push(event);
}
}
}
if (starttime + maxtime < new Date().getTime()) {
console.log("Error 1895: I won't be working overtime.");
return;
}
}
if (displayRenderingTime) {
console.log("Filtering time: " + (new Date().getTime() - starttime) + "ms");
}
}
function updateEvents() {
var starttime = new Date().getTime();
var from = datestr2unix($("from").value);
var until = datestr2unix($("until").value);
if (from == false || until == false) {
console.log("Invalid date.");
return;
}
var timespan = until - from;
$("timeline").innerHTML = '';
var previousEventPosPerc = 0;
var previousEventTop = 0;
var eventcount = 0;
$("scrollBack").style = "font-weight: normal; color: black;";
$("scrollForward").style = "font-weight: normal; color: black;";
for (var eventi in filteredEvents) {
var event = filteredEvents[eventi];
if (event.datetime < from) {
$("scrollBack").style = "font-weight: bold; color: red;";
continue;
}
if (event.datetime > until) {
$("scrollForward").style = "font-weight: bold; color: red;";
continue;
}
var posPerc = (event.datetime - from) / timespan * 100;
var left = Math.round(posPerc) + "%";
var top = 0;
if (previousEventPosPerc / posPerc > 0.9) {
if (previousEventTop > 600) {
previousEventTop = 0;
}
top = previousEventTop + 95;
}
var col = (eventi % 2) * 8 + 247;
var evtDiv = newDiv($("timeline"));
var cur = (event.details.length > 0 && event.details != "\n" ? "pointer" : "auto");
evtDiv.style = obj2style(
{ width: "150px"
, position: "absolute"
, left: left
, "background-color": "rgb(" + col + "," + col + "," + col + ")"
, border: "1px solid grey"
, "border-left": "2px solid black"
, "border-bottom": "2px solid black"
, "word-wrap": "break-word"
, top: top + "px"
, cursor: cur
});
var summary = (event.summary.length > 0 ? event.summary + "<br>" : "");
evtDiv.innerHTML = "<i>" + event.printableDatetime + "</i><br>"
+ "@" + event.device + ":<br>"
+ summary
+ "<i>Source: " + event.source + "</i>";
evtDiv.pleasedontkillme = {details: event.details, rowid: event.rowid};
evtDiv.onmouseover = function(ev) {
ev.target.style.zIndex = 9999;
};
evtDiv.onmouseout = function(ev) {
ev.target.style.zIndex = 0;
};
evtDiv.onclick = function(ev) {
if (ev.ctrlKey) {
if (confirm("Are you sure you want to delete this event?")) {
var result = GET("api.php?deleteEvent=" + ev.target.pleasedontkillme.rowid);
if (result != 'true') {
alert('Error deleting this event');
}
else {
events = JSON.parse(GET('api.php?getEvents'));
filterEvents();
updateEvents();
}
}
}
else {
if (ev.target.pleasedontkillme && ev.target.pleasedontkillme.details && ev.target.pleasedontkillme.details.length > 1) {
alert(ev.target.pleasedontkillme.details);
}
}
};
eventcount += 1;
if (top == 0) {
previousEventPosPerc = posPerc;
}
previousEventTop = top;
if (starttime + maxtime < new Date().getTime()) {
console.log("Error 6571: I won't be working overtime.");
return;
}
}
$("eventcount").innerHTML = eventcount.toString() + "/" + filteredEvents.length.toString() + "/" + events.length.toString();
if (displayRenderingTime) {
console.log("Event render time: " + (new Date().getTime() - starttime) + "ms");
}
starttime = new Date().getTime();
if (window.lastTimespan != timespan) {
// Render timeline block background
var altcol = "235, 250, 235";
col = altcol;
var html = '';
var daywidth = (3600 * 24) / (until - from) * 100;
var hourwidth = 3600 / (until - from) * 100;
for (var i = from; i < until - 3600 * 24; i += 3600 * 24) {
html += "<div style='display:inline-block; width:" + daywidth + "%; "
+ "height:20px; background-color: rgb(" + col + ");'></div>";
if (col == altcol) {
col = "255, 255, 255";
}
else {
col = altcol;
}
}
html += "<br>";
if (hourwidth > 0.5) {
for (var i = from; i < until - 3600; i += 3600) {
html += "<div style='display:inline-block; width:" + hourwidth + "%; "
+ " height:20px; background-color: rgb(" + col + ");'></div>";
if (col == altcol) {
col = "255, 255, 255";
}
else {
col = altcol;
}
}
}
$("timelinebg").innerHTML = html;
}
window.lastTimespan = timespan;
if (displayRenderingTime) {
console.log("Block bg rendering: " + (new Date().getTime() - starttime) + "ms");
}
}
function zoom(direction, ratio) {
var from = datestr2unix($("from").value);
var until = datestr2unix($("until").value);
var timespan = until - from;
var fromdiff = timespan * 0.3 * ratio;
var untildiff = timespan * 0.3 * (1 - ratio);
if (direction) {
if (timespan > 3600 * 24 * 200) {
timespan = 3600 * 24 * 200;
return;
}
fromdiff = -fromdiff;
untildiff = -untildiff;
}
else {
if (timespan < 90) {
return;
}
}
var newfrom = from + fromdiff;
var newuntil = until - untildiff;
if (newfrom < mindatetime) {
newfrom = mindatetime;
newuntil = newfrom + timespan;
}
if (newuntil > maxdatetime) {
newuntil = maxdatetime;
newfrom = newuntil - timespan;
}
$("from").value = unix2datestr(newfrom);
$("until").value = unix2datestr(newuntil);
updateEvents();
}