Skip to content

Commit

Permalink
Calculate a has for treatments for deduplication (type + timestamp) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sulkaharo authored Jul 14, 2019
1 parent a6787b5 commit 8959287
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
27 changes: 17 additions & 10 deletions lib/plugins/treatmentnotify.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

var _ = require('lodash');
var times = require('../times');
var simplealarms = require('./simplealarms')();
const _ = require('lodash');
const times = require('../times');
const simplealarms = require('./simplealarms')();
const crypto = require('crypto');

var MANUAL_TREATMENTS = ['BG Check', 'Meal Bolus', 'Carb Correction', 'Correction Bolus'];
const MANUAL_TREATMENTS = ['BG Check', 'Meal Bolus', 'Carb Correction', 'Correction Bolus'];

function init() {

var treatmentnotify = {
const treatmentnotify = {
name: 'treatmentnotify'
, label: 'Treatment Notifications'
, pluginType: 'notification'
Expand Down Expand Up @@ -102,26 +103,32 @@ function init() {
if (lastTreatment.isAnnouncement) {
requestAnnouncementNotify(lastTreatment, sbx);
} else {
var message = buildTreatmentMessage(lastTreatment, sbx);
let message = buildTreatmentMessage(lastTreatment, sbx);

var eventType = lastTreatment.eventType;
let eventType = lastTreatment.eventType;
if (lastTreatment.duration === 0 && eventType === 'Temporary Target') {
eventType += ' Cancel';
message = translate('Canceled');
}

var timestamp = lastTreatment.timestamp;
const timestamp = lastTreatment.timestamp;

if (!message) {
message = '...';
}

const hash = crypto.createHash('sha1');
const info = JSON.stringify({ eventType, timestamp});
hash.update(info);
const notifyhash = hash.digest('hex');

sbx.notifications.requestNotify({
level: sbx.levels.INFO
, title: translate(eventType)
, message: message
, timestamp: timestamp
, message
, timestamp
, plugin: treatmentnotify
, notifyhash
});
}
}
Expand Down
52 changes: 27 additions & 25 deletions lib/server/pushnotify.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

var _ = require('lodash');
var crypto = require('crypto');
var NodeCache = require('node-cache');
const _ = require('lodash');
const crypto = require('crypto');
const NodeCache = require('node-cache');

var levels = require('../levels');
var times = require('../times');
const levels = require('../levels');
const times = require('../times');

function init(env, ctx) {
function init (env, ctx) {

function pushnotify() {
function pushnotify () {
return pushnotify;
}

Expand All @@ -23,19 +23,22 @@ function init(env, ctx) {
return;
}

var key = null;
if (notify.isAnnouncement) {
//Announcement notifications are sent if they are different from whats been recently sent
key = notifyToHash(notify);
} else if (levels.isAlarm(notify.level)) {
//Alarms can be snoozed
//for WARN and higher use the plugin name and notification level so that louder alarms aren't triggered too often
key = notify.plugin.name + '_' + notify.level;
} else {
//INFO and lower notifications should be sent as long as they are different from whats been recently sent
key = notifyToHash(notify);
}
var key = notify.notifyhash || false;

if (!key) {
if (notify.isAnnouncement) {
//Announcement notifications are sent if they are different from whats been recently sent
key = notifyToHash(notify);
} else if (levels.isAlarm(notify.level)) {
//Alarms can be snoozed
//for WARN and higher use the plugin name and notification level so that louder alarms aren't triggered too often
key = notify.plugin.name + '_' + notify.level;
} else {
//INFO and lower notifications should be sent as long as they are different from whats been recently sent
key = notifyToHash(notify);
}
}

notify.key = key;

if (recentlySent.get(key)) {
Expand Down Expand Up @@ -69,12 +72,12 @@ function init(env, ctx) {
return !!notify;
};

function cancelPushoverNotifications ( ) {
function cancelPushoverNotifications () {
if (ctx.pushover) {
var receiptKeys = receipts.keys();

_.each(receiptKeys, function eachKey(receipt) {
ctx.pushover.cancelWithReceipt(receipt, function cancelCallback(err) {
_.each(receiptKeys, function eachKey (receipt) {
ctx.pushover.cancelWithReceipt(receipt, function cancelCallback (err) {
if (err) {
console.error('error canceling receipt:' + receipt + ', err: ', err);
} else {
Expand All @@ -89,7 +92,7 @@ function init(env, ctx) {
function sendPushoverNotifications (notify) {
if (ctx.pushover) {
//add the key to the cache before sending, but with a short TTL
ctx.pushover.send(notify, function pushoverCallback(err, result) {
ctx.pushover.send(notify, function pushoverCallback (err, result) {
if (err) {
console.warn('Unable to send pushover', notify, err);
} else {
Expand Down Expand Up @@ -152,5 +155,4 @@ function init(env, ctx) {
return pushnotify();
}


module.exports = init;
module.exports = init;

0 comments on commit 8959287

Please sign in to comment.