diff --git a/lib/api/entries/index.js b/lib/api/entries/index.js index 21b59aa9b1a..0f1113ab2c6 100644 --- a/lib/api/entries/index.js +++ b/lib/api/entries/index.js @@ -472,11 +472,6 @@ function configure (app, wares, ctx, env) { }); } else { inMemoryCollection = ctx.cache.getData('entries'); - - inMemoryCollection = _.sortBy(inMemoryCollection, function(item) { - const age = item.mills | item.date; - return age; - }).reverse(); } if (inMemoryPossible && query.count <= inMemoryCollection.length) { diff --git a/lib/server/entries.js b/lib/server/entries.js index 50c8e0cc41a..7e6ac90d35f 100644 --- a/lib/server/entries.js +++ b/lib/server/entries.js @@ -99,8 +99,11 @@ function storage (env, ctx) { // Normalize dates to be in UTC, store offset in utcOffset - var _sysTime = moment(doc.dateString).isValid() ? moment.parseZone(doc.dateString) : moment(doc.date); - _sysTime = _sysTime.isValid() ? _sysTime : moment(); + var _sysTime; + + if (doc.dateString) { _sysTime = moment.parseZone(doc.dateString); } + if (!_sysTime && doc.date) { _sysTime = moment(doc.date); } + if (!_sysTime) _sysTime = moment(); doc.utcOffset = _sysTime.utcOffset(); doc.sysTime = _sysTime.toISOString(); @@ -163,17 +166,11 @@ function storage (env, ctx) { api.aggregate = require('./aggregate')({}, api); api.indexedFields = [ 'date' - , 'type' - , 'sgv' - , 'mbg' - , 'sysTime' - , 'dateString' - , { 'type': 1, 'date': -1, 'dateString': 1 } ]; return api; diff --git a/tests/api.entries.test.js b/tests/api.entries.test.js index cbb1c885631..07fe0cc4aeb 100644 --- a/tests/api.entries.test.js +++ b/tests/api.entries.test.js @@ -4,8 +4,12 @@ var request = require('supertest'); var load = require('./fixtures/load'); var bootevent = require('../lib/server/bootevent'); var language = require('../lib/language')(); +const _ = require('lodash'); + require('should'); +const FIVE_MINUTES=1000*60*5; + describe('Entries REST api', function ( ) { var entries = require('../lib/api/entries/'); var self = this; @@ -24,17 +28,38 @@ describe('Entries REST api', function ( ) { bootevent(self.env, language).boot(function booted (ctx) { self.app.use('/', entries(self.app, self.wares, ctx, self.env)); self.archive = require('../lib/server/entries')(self.env, ctx); - - var creating = load('json'); - creating.push({type: 'sgv', sgv: 100, date: Date.now()}); - self.archive.create(creating, done); + self.ctx = ctx; + done(); }); }); beforeEach(function (done) { var creating = load('json'); - creating.push({type: 'sgv', sgv: 100, date: Date.now()}); - self.archive.create(creating, done); + + for (let i = 0; i < 20; i++) { + const e = {type: 'sgv', sgv: 100, date: Date.now()}; + e.date = e.date - FIVE_MINUTES * i; + creating.push(e); + } + + creating = _.sortBy(creating, function(item) { + return item.date; + }); + + function setupDone() { + console.log('Setup complete'); + done(); + } + + function waitForASecond() { + // wait for event processing of cache entries to actually finish + setTimeout(function() { + setupDone(); + }, 100); + } + + self.archive.create(creating, waitForASecond); + }); afterEach(function (done) { @@ -89,6 +114,23 @@ describe('Entries REST api', function ( ) { }); }); + it('gets entries in right order without type specifier', function (done) { + var defaultCount = 10; + request(self.app) + .get('/entries.json') + .expect(200) + .end(function (err, res) { + res.body.should.be.instanceof(Array).and.have.lengthOf(defaultCount); + + var array = res.body; + var firstEntry = array[0]; + var secondEntry = array[1]; + + firstEntry.date.should.be.above(secondEntry.date); + + done( ); + }); + }); it('/echo/ api shows query', function (done) { request(self.app)