Commit 27492cf0 authored by Alan Szlosek's avatar Alan Szlosek

prelim digest module

parent 5aa08a8d
......@@ -3,6 +3,7 @@ module.exports = {
IfNotSeen: require('./lib/after'),
Since: require('./lib/since'),
Digest: require('./lib/digest'),
Timeseries: require('./lib/timeseries'),
Input: {
......
/*
Purpose: To help you control the number of messages/alerts that are delivered
So you can roll up messages and deliver at 0:00, 0:05, 0:10 (every 5 minutes). Not sure the correct name for this sort of scheduling.
Each Digest instance is a delivery scheduler
You can roll up multiple message ids into this same schedule
Digest.toSeconds(seconds modulo)
*/
// Prelim digest module. Replaces throttling in the short term
/*
Valid intervals, for now:
1 second
5 second
10 second
30 second
1 minute
5 minute
10 minute
30 minute
1 hour
// Think I want to let devs specify their own method for rolling up messages
// Maybe our default will be a line with the message id, followed by a count of those rolled up
*/
var Digest = function(interval, cacheSize, deliveryCallback) {
var self = this;
// Maybe cacheSize can let us specify how many messages to keep a copy of
// We don't want to cache all messages added to the digest, as we'd eath through memory
this.cacheSize = cacheSize || 10;
this.counts = {};
this.cache = {};
this.timeoutHandle = null;
this.deliveryCallback = deliveryCallback;
// Schedule delivery
this.schedule(interval);
};
Digest.prototype.schedule = function(interval) {
var self = this;
var callback = function() {
// Schedule next timeout ASAP
// Also give deliveryCallback chance to cancel the timeout
clearTimeout(self.timeoutHandle);
reschedule();
self.deliveryCallback(self.counts, self.cache);
self.counts = {};
self.cache = {};
};
var reschedule = function() {
var seconds = 2 * 1000;
self.timeoutHandle = setTimeout(
callback,
seconds
);
};
reschedule();
};
Digest.prototype.add = function(id, message) {
if (!(id in this.cache)) {
this.counts[id] = 0;
this.cache[id] = [];
}
this.counts[id]++;
// Only push up to our recentCacheSize
this.cache[id].push(message);
};
Digest.prototype.stop = function() {
console.log('digest.stop');
if (this.timeoutHandle) {
clearTimeout(this.timeoutHandle);
}
};
/*
Digest.prototype.deliver = function() {
var out = '';
for (var id in this.cache) {
var messages = this.cache[id];
// Roll up
}
this.deliveryCallback(this.cache);
this.cache = {};
};
*/
module.exports = Digest;
......@@ -6,10 +6,8 @@
"statistics": "^3.3.0"
},
"devDependencies": {
"async": "^2.2.0",
"nodeunit": "^0.10.2"
"tap": "^10.7.2"
},
"scripts": {
"test": "node_modules/nodeunit/bin/nodeunit"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment