Skip to content

Commit

Permalink
refactory in-memory history into History object
Browse files Browse the repository at this point in the history
  • Loading branch information
cromwellryan committed Apr 3, 2015
1 parent 6f86dac commit a247d6f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/dashing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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('');
}

Expand Down Expand Up @@ -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();
Expand All @@ -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;
};
24 changes: 24 additions & 0 deletions lib/inMemoryHistory.js
Original file line number Diff line number Diff line change
@@ -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;
};
38 changes: 38 additions & 0 deletions test/history_test.js
Original file line number Diff line number Diff line change
@@ -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 }
});
});
});

0 comments on commit a247d6f

Please sign in to comment.