This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monit.js
413 lines (381 loc) · 13.6 KB
/
monit.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* monit.js :+: :+: */
/* +:+ */
/* By: fbes <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/11/11 19:23:05 by fbes #+# #+# */
/* Updated: 2021/11/25 16:48:17 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
function sum(prevVal, curVal) {
return (prevVal + curVal);
}
var monit = {
httpReq: null,
requirements: {
today: 205,
min: 1440,
achievement1: 3000,
achievement2: 4800
},
dayOfWeek: -1,
bhContainer: null,
logTimes: [],
logTimesTotal: 0,
username: "me",
/**
* Get the color of the user's coalition
*/
getCoalitionColor: function() {
try {
return (document.getElementsByClassName("coalition-span")[0].style.color);
}
catch (err) {
return ("#FF0000");
}
},
/**
* Get username of profile
*/
getUserName: function() {
try {
return (document.querySelector(".login[data-login]").getAttribute("data-login"));
}
catch (err) {
return (null);
}
},
/**
* Get the dates of this week's days
*/
getWeekDates: function() {
var thisWeek = [];
var timestamp = new Date().getTime();
for (var i = 0; i <= monit.dayOfWeek; i++) {
thisWeek.push(new Date(timestamp - 86400000 * i).toISOString().split("T")[0]);
}
console.log("This week's dates: ", thisWeek);
return (thisWeek);
},
/**
* Get the expectations for this week, based on the minutes the user has currently
* and how many days are left. The required minutes left are expected to be spread
* out, equally divided over all remaining days.
*/
setExpected: function() {
var logTimesNoToday = this.logTimes.slice(1);
var logTimesTotalNoToday;
if (logTimesNoToday && logTimesNoToday.length > 0) {
logTimesTotalNoToday = logTimesNoToday.reduce(sum);
}
else {
logTimesTotalNoToday = 0;
}
if (this.dayOfWeek == 7 || this.logTimesTotal > this.requirements.min) {
this.requirements.today = this.requirements.min;
}
else {
this.requirements.today = logTimesTotalNoToday + Math.round((this.requirements.min - logTimesTotalNoToday) / (7 - this.dayOfWeek));
}
console.log("Logtime up until today", logTimesTotalNoToday);
console.log("Expected minutes today", this.requirements.today - logTimesTotalNoToday);
console.log("Expected minutes after today", this.requirements.today);
},
/**
* Parse a piece of logtime text: in format HHhMM or HH:MM(:SS)
*/
parseLogTime: function(logTimeText) {
var logTime = 0;
var logTimeSplit;
if (logTimeText.indexOf("h") > -1) {
logTimeSplit = logTimeText.split("h");
}
else {
logTimeSplit = logTimeText.split(":");
}
if (logTimeSplit.length < 2) {
return (0);
}
logTime += parseInt(logTimeSplit[0]) * 60;
logTime += parseInt(logTimeSplit[1]);
return (logTime);
},
/**
* Convert a logTime into a string with format HHhMM
*/
logTimeToString: function(logTime) {
return (Math.floor(logTime / 60) + "h" + (logTime % 60).toLocaleString(undefined, {minimumIntegerDigits: 2}));
},
/**
* Get a user's logtime from the web and parse it into the logtime array
*/
getLogTimesWeb: function(username) {
return (new Promise(function(resolve, reject) {
if (monit.httpReq != null) {
monit.httpReq.abort();
}
monit.httpReq = new XMLHttpRequest();
monit.httpReq.addEventListener("load", function() {
try {
var stats = JSON.parse(this.responseText);
var weekDates = monit.getWeekDates();
monit.logTimes = [];
for (var i = 0; i < weekDates.length; i++) {
if (weekDates[i] in stats) {
monit.logTimes.push(monit.parseLogTime(stats[weekDates[i]]));
}
else {
monit.logTimes.push(0);
}
}
if (monit.logTimes && monit.logTimes.length > 0) {
monit.logTimesTotal = monit.logTimes.reduce(sum);
}
else {
monit.logTimesTotal = 0;
}
resolve();
}
catch (err) {
reject(err);
}
});
monit.httpReq.addEventListener("error", function(err) {
reject(err);
});
monit.httpReq.open("GET", window.location.origin + "/users/" + username + "/locations_stats.json");
monit.httpReq.send();
}));
},
/**
* Get the logtime from the logtime chart, modify the chart to include
* progress for previous weeks, and parse this week's logtime into the logtime array
*/
getLogTimes: function() {
return (new Promise(function (resolve, reject) {
var ltSvg = document.getElementById("user-locations");
if (!ltSvg) {
reject("Element #user-locations not found");
}
var ltDays = ltSvg.getElementsByTagName("g");
var ltDay = ltDays[ltDays.length - 1];
var i, j;
monit.logTimes = [];
for (i = 0; i <= monit.dayOfWeek; i++) {
ltDay = ltDays[ltDays.length - i - 1];
if (!ltDay) {
reject("Not enough days in logtimes overview SVG");
}
monit.logTimes.push(monit.parseLogTime(ltDay.getAttribute("data-original-title")));
}
if (monit.logTimes && monit.logTimes.length > 0) {
monit.logTimesTotal = monit.logTimes.reduce(sum);
}
else {
monit.logTimesTotal = 0;
}
var daysInWeek = monit.dayOfWeek + 1;
var remainingWeeks = Math.floor(ltDays.length / 7) + (monit.dayOfWeek != 6 ? 1 : 0);
var r = 0;
var tempLogTimes;
for (i = 0; i < remainingWeeks; i++) {
if (i == 1) {
daysInWeek = 7;
}
tempLogTimes = [];
// parse individual logtimes
for (j = 0; j < daysInWeek; j++) {
ltDay = ltDays[ltDays.length - r - 1];
if (!ltDay) {
resolve();
return;
}
tempLogTimes.push(monit.parseLogTime(ltDay.getAttribute("data-original-title")));
if (tempLogTimes[j] == 0) {
ltDay.setAttribute("data-nolog", "");
}
r++;
}
// calculate cumulative logtime
for (j = daysInWeek - 2; j > -1; j--) {
tempLogTimes[j] = tempLogTimes[j] + tempLogTimes[j + 1];
}
// add cumulative logtime and percentage to tooltips
for (j = daysInWeek - 1; j > -1; j--) {
ltDay = ltDays[ltDays.length - r + j];
if (!ltDay) {
resolve();
return;
}
ltDay.setAttribute("data-original-title", ltDay.getAttribute("data-original-title") + " (" + monit.logTimeToString(tempLogTimes[daysInWeek - 1 - j]) + " / " + Math.floor(tempLogTimes[daysInWeek - 1 - j] / monit.requirements.min * 100) + "%)");
}
}
resolve();
}));
},
/**
* Get the progress towards the Monitoring System's goals from the current webpage.
* The logtime data is read from the SVG logtime chart, but in case that fails there's
* a fallback available to read from the web instead.
*/
getProgress: function() {
if (window.location.pathname.indexOf("/users/") == 0) {
var iconLocation = document.getElementsByClassName("icon-location");
if (iconLocation.length == 0) {
return;
}
if (iconLocation[0].nextSibling.nextSibling.textContent != "Amsterdam") {
return;
}
}
this.bhContainer = document.getElementById("goals_container");
if (!this.bhContainer) {
return;
}
this.username = this.getUserName();
for (var i = 0; i < this.bhContainer.children.length; i++) {
this.bhContainer.children[i].style.display = "none";
}
this.getLogTimes()
.then(this.writeProgress)
.catch(function(err) {
console.warn("Could not read logtimes chart:", err);
monit.getLogTimesWeb(monit.username).then(monit.writeProgress)
.catch(function(err) {
console.error("Could not retrieve logtimes from the web", err);
});
});
},
addTooltip: function() {
// add bootstrap tooltip to holder
var actualCode = '$("#lt-holder").tooltip();';
var script = document.createElement('script');
script.appendChild(document.createTextNode(actualCode));
(document.head || document.documentElement).appendChild(script);
script.parentNode.removeChild(script);
},
/**
* Write the progress data to the Black Hole box
*/
writeProgress: function() {
monit.setExpected();
console.log("Logtimes", monit.logTimes);
console.log("Total minutes", monit.logTimesTotal);
var progressNode = document.createElement("div");
progressNode.setAttribute("id", "monit-progress-old");
var progressTitle = document.createElement("div");
progressTitle.setAttribute("class", "mb-1");
progressTitle.innerHTML = '<span class="coalition-span" style="color: '+monit.getCoalitionColor()+';">Monitoring System progress</span>';
progressNode.appendChild(progressTitle);
var progressText = document.createElement("div");
progressText.setAttribute("id", "monit-progress-old-text");
var emoteHolder = document.createElement("div");
emoteHolder.setAttribute("id", "lt-holder");
emoteHolder.setAttribute("class", "emote-lt");
emoteHolder.setAttribute("data-toggle", "tooltip");
emoteHolder.setAttribute("data-original-title", "Logtime this week: " + monit.logTimeToString(monit.logTimesTotal));
emoteHolder.setAttribute("title", "");
var smiley = document.createElement("span");
smiley.setAttribute("id", "lt-emote-old");
var progressPerc = document.createElement("span");
progressPerc.innerHTML = Math.floor(monit.logTimesTotal / 1440 * 100) + "% complete";
if (monit.logTimesTotal < monit.requirements.today) {
smiley.setAttribute("class", "icon-smiley-sad-1");
smiley.setAttribute("style", "color: rgb(238, 97, 115);");
progressPerc.setAttribute("style", "color: rgb(238, 97, 115);");
}
else if (monit.logTimesTotal < monit.requirements.min) {
smiley.setAttribute("class", "icon-smiley-relax");
smiley.setAttribute("style", "color: rgb(223, 149, 57);");
progressPerc.setAttribute("style", "color: rgb(223, 149, 57);");
}
else if (monit.logTimesTotal < monit.requirements.achievement1) {
smiley.setAttribute("class", "icon-smiley-happy-3");
smiley.setAttribute("style", "color: rgb(83, 210, 122);");
progressPerc.setAttribute("style", "color: rgb(83, 210, 122);");
}
else if (monit.logTimesTotal < monit.requirements.achievement2) {
smiley.setAttribute("class", "icon-smiley-happy-5");
smiley.setAttribute("style", "color: rgb(83, 210, 122);");
progressPerc.setAttribute("style", "color: rgb(83, 210, 122);");
}
else {
smiley.setAttribute("class", "icon-smiley-surprise");
smiley.setAttribute("style", "color: rgb(83, 210, 122);");
progressPerc.setAttribute("style", "color: rgb(83, 210, 122);");
}
// profile easter egg: use a certain emote on certain user pages
switch (monit.username) {
case "fbes":
smiley.setAttribute("data-oclass", smiley.getAttribute("class"));
smiley.setAttribute("class", "iconf-canon");
break;
case "lde-la-h":
smiley.setAttribute("data-oclass", smiley.getAttribute("class"));
smiley.setAttribute("class", "iconf-cactus");
break;
case "jgalloni":
smiley.setAttribute("data-oclass", smiley.getAttribute("class"));
smiley.setAttribute("class", "iconf-bug-1");
break;
case "ieilat":
smiley.setAttribute("data-oclass", smiley.getAttribute("class"));
smiley.setAttribute("class", "iconf-pacman-ghost");
break;
case "pde-bakk":
smiley.setAttribute("data-oclass", smiley.getAttribute("class"));
smiley.setAttribute("class", "iconf-crown-1");
break;
}
smiley.addEventListener("click", function() {
if (!smiley.getAttribute("data-oclass")) {
return;
}
var tempClass = smiley.getAttribute("class");
smiley.setAttribute("class", smiley.getAttribute("data-oclass"));
smiley.setAttribute("data-oclass", tempClass);
});
emoteHolder.appendChild(smiley);
emoteHolder.appendChild(progressPerc);
progressText.appendChild(emoteHolder);
progressNode.appendChild(progressText);
var deprecatedMessage = document.createElement("div");
deprecatedMessage.setAttribute("id", "codam_intra_monit_system_display_deprecation_notice");
deprecatedMessage.setAttribute("class", "upgraded");
if (navigator.userAgent.indexOf("Chrome") > -1) {
deprecatedMessage.innerHTML = '<span><b>This extension is deprecated!</b><br>Please install <a href="https://chrome.google.com/webstore/detail/hmflgigeigiejaogcgamkecmlibcpdgo">Improved Intra 42</a> instead.</span>';
}
else {
deprecatedMessage.innerHTML = '<span><b>This extension is deprecated!</b><br>Please install <a href="https://github.com/FreekBes/improved_intra/">Improved Intra 42</a> instead.</span>';
}
progressNode.appendChild(deprecatedMessage);
monit.bhContainer.appendChild(progressNode);
monit.bhContainer.className = monit.bhContainer.className.replace("hidden", "");
monit.addTooltip();
},
init: function() {
this.dayOfWeek = new Date().getDay() - 1;
if (this.dayOfWeek < 0) {
this.dayOfWeek = 6;
}
}
};
// check if already exists due to Improved Intra extension
// this extension is now deprecated, please install Improved Intra instead, it has the same functionality and more
// https://github.com/FreekBes/improved_intra/
setTimeout(function() {
if (!document.getElementById("monit-progress")) {
monit.init();
setTimeout(function() {
monit.getProgress();
}, 500);
}
else {
var deprecatedMessage = document.createElement("div");
deprecatedMessage.setAttribute("id", "codam_intra_monit_system_display_deprecation_notice");
deprecatedMessage.setAttribute("class", "upgraded");
document.getElementById("monit-progress").appendChild(deprecatedMessage);
}
}, 1000);