From 46c8ae52426e54915bdd3480339671df3a5f3c2c Mon Sep 17 00:00:00 2001 From: Mark Wilson Date: Thu, 7 Jan 2016 11:11:38 -0800 Subject: [PATCH] Expose and test makeRecencyFilter so cgm-remote-monitor can use it --- filter.js | 24 ++++++++++++++++++++++++ index.js | 1 + run.js | 23 +++-------------------- test/filter.js | 30 ++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 filter.js create mode 100644 test/filter.js diff --git a/filter.js b/filter.js new file mode 100644 index 0000000..3142071 --- /dev/null +++ b/filter.js @@ -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; diff --git a/index.js b/index.js index 375ffdb..3aa5086 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ module.exports = { carelink: require('./carelink'), + filter: require('./filter'), logger: require('./logger'), nightscout: require('./nightscout'), transform: require('./transform') diff --git a/run.js b/run.js index 5decf51..d9576a2 100644 --- a/run.js +++ b/run.js @@ -2,6 +2,7 @@ "use strict"; var carelink = require('./carelink'), + filter = require('./filter'), logger = require('./logger'), nightscout = require('./nightscout'), transform = require('./transform'); @@ -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(); }); diff --git a/test/filter.js b/test/filter.js new file mode 100644 index 0000000..5c01573 --- /dev/null +++ b/test/filter.js @@ -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); + }); + }); +});