diff --git a/lib/dashing.js b/lib/dashing.js index 68ac790..e41cd44 100644 --- a/lib/dashing.js +++ b/lib/dashing.js @@ -2,7 +2,8 @@ var fs = require('fs') , path = require('path') , express = require('express') , Mincer = require('mincer') - , coffee = require('coffee-script'); + , coffee = require('coffee-script') + , InMemoryHistory = require('./inMemoryHistory'); global.SCHEDULER = require('node-schedule'); @@ -79,7 +80,6 @@ module.exports.Dashing = function Dashing() { app.set('production', dashing.NODE_ENV === 'production'); var connections = {}; - var history = {}; app.get('/events', dashing._protected, function(req, res) { // let request last as long as possible @@ -178,7 +178,7 @@ module.exports.Dashing = function Dashing() { body.id = id; body.updatedAt = Date.now(); var event = format_event(body); - history[id] = event; + dashing.history.record(id, event); for (var k in connections) { connections[k].send(event); } @@ -191,9 +191,9 @@ module.exports.Dashing = function Dashing() { function latest_events() { var str = []; - for (var id in history) { - str.push(history[id]); - } + dashing.history.forEach( function(_id, data) { + str.push(data); + }); return str.join(''); } @@ -241,6 +241,10 @@ module.exports.Dashing = function Dashing() { logger.info('Listening on http://0.0.0.0:' + dashing.port + (process.env.__daemon === 'false' ? ', CTRL+C to stop' : '')); } + dashing.useHistoryStore = function(store) { + dashing.history = store; + } + dashing.start = function() { try { start_jobs(); @@ -250,6 +254,7 @@ module.exports.Dashing = function Dashing() { logger.error(e.toString()); // winston is not catching these? } } + dashing.useHistoryStore(new InMemoryHistory()); dashing.app = app; return dashing; }; diff --git a/lib/inMemoryHistory.js b/lib/inMemoryHistory.js new file mode 100644 index 0000000..fd0ecb4 --- /dev/null +++ b/lib/inMemoryHistory.js @@ -0,0 +1,24 @@ +var logger = require('./logger'); + +var InMemoryHistory = module.exports = function() { + var self = this; + + self._event_data = {}; + + self.record = function(id, body) { + self._event_data[id] = body; + }; + + self.last_by_id = function(id) { + return self._event_data[id]; + }; + + self.forEach = function(func) { + var history = self._event_data; + for (var id in history) { + func(id, history[id]); + } + }; + + return self; +}; diff --git a/test/history_test.js b/test/history_test.js new file mode 100644 index 0000000..2722b94 --- /dev/null +++ b/test/history_test.js @@ -0,0 +1,38 @@ +var should = require('should'), + History = require('../lib/history'); + +describe('History', function() { + it('is a thing', function() { + should.exist(History); + }); + + it('provides access to last record by id', function() { + var history = History.empty(); + + history.record('buzzwords', { + all_the_things: [ 1,2,3 ] + }); + + history.last_by_id('buzzwords') + .should.eql( { + all_the_things: [ 1,2,3] + }); + }); + + it('can iterate latest event data',function() { + var history = History.empty(); + + history.record('buzzwords', { a: 1 }); + history.record('builds', { time: 1.2 }); + + var found = { }; + history.forEach( function(id, event) { + found[id] = event; + }); + + found.should.eql( { + 'buzzwords': { a: 1}, + 'builds': { time: 1.2 } + }); + }); +});