Skip to content

Commit

Permalink
Expose and test makeRecencyFilter so cgm-remote-monitor can use it
Browse files Browse the repository at this point in the history
  • Loading branch information
mddub committed Jan 7, 2016
1 parent 07018eb commit 46c8ae5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
24 changes: 24 additions & 0 deletions filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* jshint node: true */
"use strict";

// Returns a stateful filter which remembers the time of the last-seen entry to
// prevent uploading duplicates.
function makeRecencyFilter(timeFn) {
var lastTime = 0;

return function(items) {
var out = [];
items.forEach(function(item) {
if (timeFn(item) > lastTime) {
out.push(item);
}
});
out.forEach(function(item) {
lastTime = Math.max(lastTime, timeFn(item));
});

return out;
};
}

module.exports.makeRecencyFilter = makeRecencyFilter;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
carelink: require('./carelink'),
filter: require('./filter'),
logger: require('./logger'),
nightscout: require('./nightscout'),
transform: require('./transform')
Expand Down
23 changes: 3 additions & 20 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"use strict";

var carelink = require('./carelink'),
filter = require('./filter'),
logger = require('./logger'),
nightscout = require('./nightscout'),
transform = require('./transform');
Expand Down Expand Up @@ -43,29 +44,11 @@ var devicestatusUrl = (config.nsBaseUrl ? config.nsBaseUrl : 'https://' + config

logger.setVerbose(config.verbose);

function makeRecencyFilter(timeFn) {
var lastTime = 0;

return function(items) {
var out = [];
items.forEach(function(item) {
if (timeFn(item) > lastTime) {
out.push(item);
}
});
out.forEach(function(item) {
lastTime = Math.max(lastTime, timeFn(item));
});

return out;
};
}

var filterSgvs = makeRecencyFilter(function(item) {
var filterSgvs = filter.makeRecencyFilter(function(item) {
return item['date'];
});

var filterDeviceStatus = makeRecencyFilter(function(item) {
var filterDeviceStatus = filter.makeRecencyFilter(function(item) {
return new Date(item['created_at']).getTime();
});

Expand Down
30 changes: 30 additions & 0 deletions test/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* jshint node: true */
/* globals describe, it */
"use strict";

var _ = require('lodash'),
expect = require('expect.js');

var makeRecencyFilter = require('../filter.js').makeRecencyFilter;

describe('makeRecencyFilter()', function() {
it('should return a stateful filter which discards items older than the most recent one seen', function() {
function sgv(date) {
return {type: 'sgv', someDateKey: date};
}

var filter = makeRecencyFilter(function(item) {
return item['someDateKey'];
});

expect(filter([2, 3, 4].map(sgv))).to.have.length(3);

expect(filter([2, 3, 4].map(sgv))).to.have.length(0);

var filtered = filter([2, 3, 4, 8, 6, 7, 5].map(sgv));
expect(filtered).to.have.length(4);
[5, 6, 7, 8].forEach(function(val) {
expect(_.pluck(filtered, 'someDateKey')).to.contain(val);
});
});
});

0 comments on commit 46c8ae5

Please sign in to comment.