Skip to content

Commit

Permalink
Merge pull request #210 from nightscout/wip/indexes
Browse files Browse the repository at this point in the history
ensure indexes on the date, type, and sgv fields at startup
  • Loading branch information
jasoncalabrese committed Oct 31, 2014
2 parents 30644ea + f37942a commit ad5261f
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 22 deletions.
18 changes: 16 additions & 2 deletions lib/devicestatus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function configure (collection, storage) {
function storage (collection, storage) {

function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
Expand Down Expand Up @@ -31,4 +31,18 @@ function configure (collection, storage) {
api.last = last;
return api;
}
module.exports = configure;

function ensureIndexes(name, storage) {
storage.with_collection(name)(function (err, collection) {
if (err) {
console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
} else {
storage.ensureIndexes(collection, ['created_at']);
}
});
}

module.exports = {
storage: storage,
ensureIndexes: ensureIndexes
};
18 changes: 15 additions & 3 deletions lib/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var TEN_MINS = 10 * 60 * 1000;
* Encapsulate persistent storage of sgv entries.
\**********/

function entries (name, storage, pushover) {
function storage(name, storage, pushover) {

// TODO: Code is a little redundant.

Expand Down Expand Up @@ -81,7 +81,6 @@ function entries (name, storage, pushover) {
if (err) return fn(err, result);
// batch insert a list of records
create(result, fn);
return;
}
// lint and store the entire list
return es.pipeline(map( ), es.writeArray(done));
Expand Down Expand Up @@ -188,6 +187,19 @@ function entries (name, storage, pushover) {
return api;
}

function ensureIndexes(name, storage) {
storage.with_collection(name)(function(err, collection) {
if (err) {
console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
} else {
storage.ensureIndexes(collection, ['date', 'type', 'sgv']);
}
});
}

// expose module
module.exports = entries;
module.exports = {
storage: storage,
ensureIndexes: ensureIndexes
};

25 changes: 20 additions & 5 deletions lib/storage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
var mongodb = require('mongodb');

function init (env) {
function init (env, cb) {
var MongoClient = mongodb.MongoClient;

var my = { };
Expand All @@ -13,8 +13,12 @@ function init (env) {
}
console.log("Connecting to mongo");
MongoClient.connect(env.mongo, function connected (err, db) {
console.log("Connected to mongo, ERROR: %j", err);
if (err) { throw err; }
if (err) {
console.log("Error connecting to mongo, ERROR: %j", err);
throw err;
} else {
console.log("Connected to mongo");
}
my.db = db;
mongo.pool.db = my.db = db;

Expand All @@ -30,7 +34,7 @@ function init (env) {

mongo.pool = function ( ) {
return my;
}
};

mongo.collection = function get_collection (name) {
return mongo.pool( ).db.collection(name);
Expand All @@ -42,7 +46,18 @@ function init (env) {
};
};

return mongo( );
mongo.ensureIndexes = function(collection, fields) {
fields.forEach(function (field) {
console.info("ensuring index for: " + field);
collection.ensureIndex(field, function (err) {
if (err) {
console.error("unable to ensureIndex for: " + field + " - " + err);
}
});
});
};

return mongo(cb);
}

module.exports = init;
18 changes: 16 additions & 2 deletions lib/treatments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function configure (collection, storage, pushover) {
function storage (collection, storage, pushover) {

function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
Expand Down Expand Up @@ -45,4 +45,18 @@ function configure (collection, storage, pushover) {
api.create = create;
return api;
}
module.exports = configure;

function ensureIndexes(name, storage) {
storage.with_collection(name)(function (err, collection) {
if (err) {
console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
} else {
storage.ensureIndexes(collection, ['created_at', 'eventType']);
}
});
}

module.exports = {
storage: storage,
ensureIndexes: ensureIndexes
};
26 changes: 18 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,30 @@

var software = require('./package.json');
var env = require('./env')( );
var store = require('./lib/storage')(env);
var pushover = require('./lib/pushover')(env);

var entries = require('./lib/entries');
var treatments = require('./lib/treatments');
var devicestatus = require('./lib/devicestatus');

var store = require('./lib/storage')(env, function() {
console.info("Mongo ready");
entries.ensureIndexes(env.mongo_collection, store);
treatments.ensureIndexes(env.treatments_collection, store);
devicestatus.ensureIndexes(env.devicestatus_collection, store);
});


var express = require('express');

var pushover = require('./lib/pushover')(env);
///////////////////////////////////////////////////
// api and json object variables
///////////////////////////////////////////////////
var entries = require('./lib/entries')(env.mongo_collection, store, pushover);
var entriesStorage = entries.storage(env.mongo_collection, store, pushover);
var settings = require('./lib/settings')(env.settings_collection, store);
var treatments = require('./lib/treatments')(env.treatments_collection, store, pushover);
var devicestatus = require('./lib/devicestatus')(env.devicestatus_collection, store);
var api = require('./lib/api/')(env, entries, settings, treatments, devicestatus);
var treatmentsStorage = treatments.storage(env.treatments_collection, store, pushover);
var devicestatusStorage = devicestatus.storage(env.devicestatus_collection, store);
var api = require('./lib/api/')(env, entriesStorage, settings, treatmentsStorage, devicestatusStorage);
var pebble = require('./lib/pebble');
///////////////////////////////////////////////////

Expand All @@ -61,7 +71,7 @@ app.enable('trust proxy'); // Allows req.secure test on heroku https connections
app.use('/api/v1', api);

// pebble data
app.get('/pebble', pebble(entries, devicestatus));
app.get('/pebble', pebble(entriesStorage, treatmentsStorage));

//app.get('/package.json', software);

Expand All @@ -86,7 +96,7 @@ store(function ready ( ) {
// setup socket io for data and message transmission
///////////////////////////////////////////////////
var websocket = require('./lib/websocket');
var io = websocket(env, server, entries, treatments);
var io = websocket(env, server, entriesStorage, treatmentsStorage);
});

///////////////////////////////////////////////////
2 changes: 1 addition & 1 deletion tests/api.entries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Entries REST api', function ( ) {
var env = require('../env')( );
this.wares = require('../lib/middleware/')(env);
var store = require('../lib/storage')(env);
this.archive = require('../lib/entries')(env.mongo_collection, store);
this.archive = require('../lib/entries').storage(env.mongo_collection, store);
this.app = require('express')( );
this.app.enable('api');
var self = this;
Expand Down
2 changes: 1 addition & 1 deletion tests/security.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('API_SECRET', function ( ) {
var ctx = { };
ctx.wares = require('../lib/middleware/')(env);
ctx.store = require('../lib/storage')(env);
ctx.archive = require('../lib/entries')(env.mongo_collection, ctx.store);
ctx.archive = require('../lib/entries').storage(env.mongo_collection, ctx.store);
ctx.settings = require('../lib/settings')(env.settings_collection, ctx.store);

ctx.store(function ( ) {
Expand Down

0 comments on commit ad5261f

Please sign in to comment.