diff --git a/examples/event-stream/aggregation-get.html b/examples/event-stream/aggregation-get.html index b13a5e9..0a59914 100644 --- a/examples/event-stream/aggregation-get.html +++ b/examples/event-stream/aggregation-get.html @@ -25,7 +25,7 @@

Streaming Aggregations

console.log("connected!"); socket.send(JSON.stringify({ name: aggregation, - start: new Date(new Date().getTime() - 15 * 60000) + start: new Date(new Date().getTime() - 30 * 60000) } )); }; diff --git a/lib/cube/aggregator.js b/lib/cube/aggregator.js index a4a0098..05153d7 100644 --- a/lib/cube/aggregator.js +++ b/lib/cube/aggregator.js @@ -2,8 +2,9 @@ /* * Aggregation have fixed times of execution: 1m and 5m */ -var util = require('util'); -var lock = false; +var util = require('util'), + mongoutils = require("./mongoutils.js"), + lock = false; //noinspection JSLint exports.register = function (dbs, endpoints, options) { if (dbs.length !== 2) { @@ -60,7 +61,7 @@ exports.register = function (dbs, endpoints, options) { } fiveMinAggregation = [ - {"$match": {t: {$in: [new Date(t0_5m), new Date(t0_5m + 60000), new Date(t0_5m + 120000), new Date(t0_5m + 180000), new Date(t0_5m + 240000)]}}}, + {"$match": {_id: {$gte: mongoutils.objectIdFromDate(t0_5m), $lt: mongoutils.objectIdFromDate(t0_5m + 300000)}}}, {"$group": aggregationGroup} ]; @@ -126,7 +127,7 @@ exports.register = function (dbs, endpoints, options) { t1_1m = new Date(t1_1m); if (t1_1m < new Date()) { //start aggregating - aggregation[0].$match.t = {"$gte": t0_1m, "$lt": t1_1m}; + aggregation[0].$match._id = {"$gte": mongoutils.objectIdFromDate(t0_1m), "$lt": mongoutils.objectIdFromDate(t1_1m)}; //console.log(util.inspect(aggregation, { depth: null, colors: true})); eventsDB.collection("events").aggregate(aggregation, {}, function (err, result) { if (err) { diff --git a/lib/cube/genericGetter.js b/lib/cube/genericGetter.js index 5fb3359..58e425f 100644 --- a/lib/cube/genericGetter.js +++ b/lib/cube/genericGetter.js @@ -1,5 +1,6 @@ "use strict"; var util = require("util"), + mongoutils = require("./mongoutils.js"), mongodb = require("mongodb"), type_re = /^[a-z][a-zA-Z0-9_]+$/, MAX_RETURNED_RECORDS = 10000; @@ -118,10 +119,9 @@ module.exports = function (db) { } // Copy any expression filters into the match object. - filter = {t: {$gte: start}}; - + filter = {_id: {$gte: mongoutils.objectIdFromDate(start)}}; if (stop !== undefined) { - filter.t.$lt = stop; + filter._id.$lt = mongoutils.objectIdFromDate(stop); } if (name === undefined) { diff --git a/lib/cube/mongoutils.js b/lib/cube/mongoutils.js new file mode 100644 index 0000000..4d66c12 --- /dev/null +++ b/lib/cube/mongoutils.js @@ -0,0 +1,15 @@ +"use strict"; + +var mongodb = require("mongodb"); +module.exports.objectIdFromDate = function (ts) { + return new mongodb.ObjectID(Math.floor(ts / 1000).toString(16) + "0000000000000000"); +}; +module.exports.dateFromObjectId = function (obj) { + return obj.getTimestamp(); +}; +module.exports.epochFromObjectId = function (obj) { + return parseInt(obj.valueOf().slice(0, 8), 16); +}; +module.exports.epochMSFromObjectId = function (obj) { + return parseInt(obj.valueOf().slice(0, 8), 16); +}; \ No newline at end of file