added callbacks to allow custom dashboard filter alarms
Costa Tsaousis (ktsaou) committed
Jan 21, 2018 at 21:29 UTC
58ef767e621aa54e2c1030ba5d115b5e933f1191
2 files changed
+85
-38
web/dashboard.js
+84
-37
@@ -51,6 +51,12 @@
51
* (default: null) */
52
/*global netdataAlarmsRecipients *//* array, an array of alarm recipients to show notifications for
53
* (default: null) */
54
+/*global netdataAlarmsRemember *//* boolen, keep our position in the alarm log at browser local storage
55
+ * (default: true) */
56
+/*global netdataAlarmsActiveCallback *//* function, a hook for the alarm logs
57
+ * (default: undefined) */
58
+/*global netdataAlarmsNotifCallback *//* function, a hook for alarm notifications
59
+ * (default: undefined) */
60
/*global netdataIntersectionObserver *//* boolean, enable or disable the use of intersection observer
61
* (default: true) */
62
@@ -8243,6 +8249,8 @@ var NETDATA = window.NETDATA || {};
8249
ms_between_notifications: 500, // firefox moves the alarms off-screen (above, outside the top of the screen)
8250
// if alarms are shown faster than: one per 500ms
8251
8252
+ update_every: 10000, // the time in ms between alarm checks
8253
+
8254
notifications: false, // when true, the browser supports notifications (may not be granted though)
8255
last_notification_id: 0, // the id of the last alarm_log we have raised an alarm for
8256
first_notification_id: 0, // the id of the first alarm_log entry for this session
@@ -8251,9 +8259,48 @@ var NETDATA = window.NETDATA || {};
8259
8260
server: null, // the server to connect to for fetching alarms
8261
current: null, // the list of raised alarms - updated in the background
8254
- callback: null, // a callback function to call every time the list of raised alarms is refreshed
8262
+
8263
+ // a callback function to call every time the list of raised alarms is refreshed
8264
+ callback: (typeof netdataAlarmsActiveCallback === 'function')?netdataAlarmsActiveCallback:null,
8265
+
8266
+ // a callback function to call every time a notification is shown
8267
+ // the return value is used to decide if the notification will be shown
8268
+ notificationCallback: (typeof netdataAlarmsNotifCallback === 'function')?netdataAlarmsNotifCallback:null,
8269
+
8270
recipients: null, // the list (array) of recipients to show alarms for, or null
8271
8272
+ recipientMatches: function(to_string, wanted_array) {
8273
+ if(typeof wanted_array === 'undefined' || wanted_array === null || Array.isArray(wanted_array) === false)
8274
+ return true;
8275
+
8276
+ var r = ' ' + to_string.toString() + ' ';
8277
+ var len = wanted_array.length;
8278
+ while(len--) {
8279
+ if(r.indexOf(' ' + wanted_array[len] + ' ') >= 0)
8280
+ return true;
8281
+ }
8282
+
8283
+ return false;
8284
+ },
8285
+
8286
+ activeForRecipients: function() {
8287
+ var active = {};
8288
+ var data = NETDATA.alarms.current;
8289
+
8290
+ if(typeof data === 'undefined' || data === null)
8291
+ return active;
8292
+
8293
+ for(var x in data.alarms) {
8294
+ if(!data.alarms.hasOwnProperty(x)) continue;
8295
+
8296
+ var alarm = data.alarms[x];
8297
+ if((alarm.status === 'WARNING' || alarm.status === 'CRITICAL') && NETDATA.alarms.recipientMatches(alarm.recipient, NETDATA.alarms.recipients))
8298
+ active[x] = alarm;
8299
+ }
8300
+
8301
+ return active;
8302
+ },
8303
+
8304
notify: function(entry) {
8305
// console.log('alarm ' + entry.unique_id);
8306
@@ -8333,17 +8380,8 @@ var NETDATA = window.NETDATA || {};
8380
}
8381
8382
// filter recipients
8336
- if(show === true && NETDATA.alarms.recipients !== null) {
8337
- show = false;
8338
- var r = ' ' + entry.recipient + ' ';
8339
- var len = NETDATA.alarms.recipients.length;
8340
- while(len--) {
8341
- if(r.indexOf(' ' + NETDATA.alarms.recipients[len] + ' ') >= 0) {
8342
- show = true;
8343
- break;
8344
- }
8345
- }
8346
- }
8383
+ if(show === true)
8384
+ show = NETDATA.alarms.recipientMatches(entry.recipient, NETDATA.alarms.recipients);
8385
8386
/*
8387
// cleanup old notifications with the same alarm_id as this one
@@ -8365,28 +8403,33 @@ var NETDATA = window.NETDATA || {};
8403
*/
8404
8405
if(show === true) {
8368
- setTimeout(function() {
8369
- // show this notification
8370
- // console.log('new notification: ' + title);
8371
- var n = new Notification(title, {
8372
- body: entry.hostname + ' - ' + entry.chart + ' (' + entry.family + ') - ' + status + ': ' + entry.info,
8373
- tag: tag,
8374
- requireInteraction: interaction,
8375
- icon: NETDATA.serverStatic + icon,
8376
- data: data
8377
- });
8406
+ if(typeof NETDATA.alarms.notificationCallback === 'function')
8407
+ show = NETDATA.alarms.notificationCallback(entry);
8408
+
8409
+ if(show === true) {
8410
+ setTimeout(function() {
8411
+ // show this notification
8412
+ // console.log('new notification: ' + title);
8413
+ var n = new Notification(title, {
8414
+ body: entry.hostname + ' - ' + entry.chart + ' (' + entry.family + ') - ' + status + ': ' + entry.info,
8415
+ tag: tag,
8416
+ requireInteraction: interaction,
8417
+ icon: NETDATA.serverStatic + icon,
8418
+ data: data
8419
+ });
8420
8379
- n.onclick = function(event) {
8380
- event.preventDefault();
8381
- NETDATA.alarms.onclick(event.target.data);
8382
- };
8421
+ n.onclick = function(event) {
8422
+ event.preventDefault();
8423
+ NETDATA.alarms.onclick(event.target.data);
8424
+ };
8425
8384
- // console.log(n);
8385
- // NETDATA.alarms.notifications_shown.push(n);
8386
- // console.log(entry);
8387
- }, NETDATA.alarms.ms_penalty);
8426
+ // console.log(n);
8427
+ // NETDATA.alarms.notifications_shown.push(n);
8428
+ // console.log(entry);
8429
+ }, NETDATA.alarms.ms_penalty);
8430
8389
- NETDATA.alarms.ms_penalty += NETDATA.alarms.ms_between_notifications;
8431
+ NETDATA.alarms.ms_penalty += NETDATA.alarms.ms_between_notifications;
8432
+ }
8433
}
8434
},
8435
@@ -8447,7 +8490,9 @@ var NETDATA = window.NETDATA || {};
8490
}
8491
8492
NETDATA.alarms.last_notification_id = data[0].unique_id;
8450
- NETDATA.localStorageSet('last_notification_id', NETDATA.alarms.last_notification_id, null);
8493
+
8494
+ if(typeof netdataAlarmsRemember === 'undefined' || netdataAlarmsRemember === true)
8495
+ NETDATA.localStorageSet('last_notification_id', NETDATA.alarms.last_notification_id, null);
8496
// console.log('last notification id = ' + NETDATA.alarms.last_notification_id);
8497
})
8498
},
@@ -8456,12 +8501,12 @@ var NETDATA = window.NETDATA || {};
8501
// returns true if we should fire 1+ notifications
8502
8503
if(NETDATA.alarms.notifications !== true) {
8459
- // console.log('notifications not available');
8504
+ // console.log('web notifications are not available');
8505
return false;
8506
}
8507
8508
if(Notification.permission !== 'granted') {
8464
- // console.log('notifications not granted');
8509
+ // console.log('web notifications are not granted');
8510
return false;
8511
}
8512
@@ -8525,7 +8570,7 @@ var NETDATA = window.NETDATA || {};
8570
if(data.status === false) return;
8571
}
8572
8528
- setTimeout(NETDATA.alarms.update_forever, 10000);
8573
+ setTimeout(NETDATA.alarms.update_forever, NETDATA.alarms.update_every);
8574
});
8575
},
8576
@@ -8556,8 +8601,10 @@ var NETDATA = window.NETDATA || {};
8601
init: function() {
8602
NETDATA.alarms.server = NETDATA.fixHost(NETDATA.serverDefault);
8603
8559
- NETDATA.alarms.last_notification_id =
8560
- NETDATA.localStorageGet('last_notification_id', NETDATA.alarms.last_notification_id, null);
8604
+ if(typeof netdataAlarmsRemember === 'undefined' || netdataAlarmsRemember === true) {
8605
+ NETDATA.alarms.last_notification_id =
8606
+ NETDATA.localStorageGet('last_notification_id', NETDATA.alarms.last_notification_id, null);
8607
+ }
8608
8609
if(NETDATA.alarms.onclick === null)
8610
NETDATA.alarms.onclick = NETDATA.alarms.scrollToAlarm;
web/index.html
+1
-1
@@ -5622,6 +5622,6 @@
5622
</div>
5623
</div>
5624
<div id="hiddenDownloadLinks" style="display: none;" hidden></div>
5625
- <script type="text/javascript" src="dashboard.js?v20180115-2"></script>
5625
+ <script type="text/javascript" src="dashboard.js?v20180121-2"></script>
5626
</body>
5627
</html>