Skip to content

Commit

Permalink
Added indexes to 'entries' and 'treatments' along with other updates (#…
Browse files Browse the repository at this point in the history
…5463)

* Added compound indexes for treatments and entries collections. Updated ensureIndex to createIndex in mongo-strage.js as ensureIndex has been deprecated. Finally, updated testing/populate.js to be compatible with more recent versions of the node driver, as well as fixing a path issue.

* Fixed missing end quote in lib/server/treatments.js. Changed all newly added double quotes to single quote to match style guide.

* Removed indexes that referenced key600.
  • Loading branch information
a-harrison authored Feb 5, 2020
1 parent 82f0076 commit b1ec21c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
11 changes: 9 additions & 2 deletions lib/server/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,15 @@ function storage(env, ctx) {
api.query_for = query_for;
api.getEntry = getEntry;
api.aggregate = require('./aggregate')({ }, api);
api.indexedFields = [ 'date', 'type', 'sgv', 'mbg', 'sysTime', 'dateString' ];
api.indexedFields = [
'date'
, 'type'
, 'sgv'
, 'mbg'
, 'sysTime'
, 'dateString'
, { 'type' : 1, 'date' : -1, 'dateString' : 1 }
];
return api;
}

Expand All @@ -160,4 +168,3 @@ storage.queryOpts = {
// expose module
storage.storage = storage;
module.exports = storage;

1 change: 1 addition & 0 deletions lib/server/treatments.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ function storage (env, ctx) {
, 'percent'
, 'absolute'
, 'duration'
, { 'eventType' : 1, 'duration' : 1, 'created_at' : 1 }
];

api.remove = remove;
Expand Down
6 changes: 3 additions & 3 deletions lib/storage/mongo-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ function init (env, cb, forceNewConnection) {
console.log('Error connecting to MongoDB: %j - retrying in ' + timeout/1000 + ' sec', err);
setTimeout(connect_with_retry, timeout, i+1);
} else if (err.message) {
throw new Error('MongoDB connection string '+env.storageURI+' seems invalid: '+err.message) ;
throw new Error('MongoDB connection string '+env.storageURI+' seems invalid: '+err.message) ;
}
} else {
console.log('Successfully established a connected to MongoDB');

var dbName = env.storageURI.split('/').pop().split('?');
dbName=dbName[0]; // drop Connection Options
mongo.db = client.db(dbName);
Expand All @@ -66,7 +66,7 @@ function init (env, cb, forceNewConnection) {
mongo.ensureIndexes = function ensureIndexes (collection, fields) {
fields.forEach(function (field) {
console.info('ensuring index for: ' + field);
collection.ensureIndex(field, function (err) {
collection.createIndex(field, { 'background': true }, function (err) {
if (err) {
console.error('unable to ensureIndex for: ' + field + ' - ' + err);
}
Expand Down
7 changes: 5 additions & 2 deletions testing/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
var mongodb = require('mongodb');
var env = require('./../env')();

var util = require('./helpers/util');
var util = require('./util');

main();

function main() {
var MongoClient = mongodb.MongoClient;
MongoClient.connect(env.storageURI, function connected(err, db) {
MongoClient.connect(env.storageURI, { "useUnifiedTopology" : true, "useNewUrlParser" : true }, function connected(err, client) {

console.log('Connecting to mongo...');
if (err) {
console.log('Error occurred: ', err);
throw err;
}

var db = client.db();

populate_collection(db);
});
}
Expand Down

0 comments on commit b1ec21c

Please sign in to comment.