Skip to content

Commit

Permalink
Merge 510c71c into 32cf585
Browse files Browse the repository at this point in the history
  • Loading branch information
dawnerd committed Oct 29, 2018
2 parents 32cf585 + 510c71c commit 29ac55e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"joi": "^13.4.0",
"json2csv": "^4.1.3",
"lodash": "^4.17.10",
"moment": "^2.22.2",
"rapptor": "^6.1.0"
},
"devDependencies": {
Expand Down
18 changes: 15 additions & 3 deletions routes/api/report/aggregate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Joi = require('joi');
const moment = require('moment');

exports.aggregate = {
path: 'aggregate',
Expand Down Expand Up @@ -41,8 +42,14 @@ exports.aggregate = {
let current = start;
const dataset = {};
while (current < end) {
const date = moment(current);

if (request.query.startDate && request.query.endDate) {
date.utcOffset(moment(request.query.startDate).format());
}

dataset[current] = {
dateString: new Date(current).toISOString(),
dateString: date.toISOString(),
sum: 0,
avg: 0,
max: 0,
Expand Down Expand Up @@ -74,8 +81,13 @@ exports.aggregate = {
{ $group }
], { explain: false, cursor: {} }).toArray();
aggregate.forEach((item) => {
const date = new Date(item._id.year, item._id.month - 1, item._id.day, item._id.hour || 0, item._id.minute || 0); // eslint-disable-line no-underscore-dangle
const timestamp = date.getTime();
const date = moment(new Date(item._id.year, item._id.month - 1, item._id.day, item._id.hour || 0, item._id.minute || 0)); // eslint-disable-line no-underscore-dangle

if (request.query.startDate && request.query.endDate) {
date.utcOffset(moment(request.query.startDate).format());
}

const timestamp = date.valueOf();
item.dateString = date.toISOString();
delete item._id; //eslint-disable-line no-underscore-dangle
dataset[timestamp] = item;
Expand Down
75 changes: 75 additions & 0 deletions test/test.report.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const tap = require('tap');
const setup = require('./setup.test.js');
const os = require('os');
const async = require('async');
const moment = require('moment');

const twentyMinutes = 1000 * 60 * 20;
const twoDays = 1000 * 60 * 60 * 24 * 2;
Expand Down Expand Up @@ -542,6 +543,80 @@ tap.test('can use the report method to get an aggregate list of metrics from the
t.end();
});

tap.test('can use the report method to get an aggregate list of metrics from the db using dates', async(t) => {
await setup.server.db.tracks.insertMany([{
type: 'BankAccount',
tags: { currency: 'yen' },
tagKeys: { currency: 'yen' },
fields: ['wc', 'strawberry'],
value: 142000000,
data: 'liquid Assets only',
userId: '2d',
createdOn: new Date(current - twoDays)
},
{
type: 'StockAccount',
tags: { currency: 'dollars' },
value: 142000000,
data: 'purchasing account',
userId: 'Montgomery Burns',
},
{
type: 'StockAccount',
tags: { transactionNumber: '1234' },
value: 142000000,
data: 'purchasing account',
userId: 'Montgomery Burns',
},
{
type: 'BankAccount',
tags: { currency: 'dollars', units: 'cents' },
value: 0.15,
data: 'liquid assets only',
userId: '20m',
createdOn: new Date(current - twentyMinutes)
},
{
type: 'BankAccount',
tags: { currency: 'dollars', units: 'cents' },
value: 0.15,
data: 'liquid assets only',
userId: '3h',
createdOn: new Date(current - threeHours)
},
{
type: 'BankAccount',
tags: { currency: 'dollars', units: 'cents' },
value: 0.15,
data: 'liquid assets only',
userId: 'current',
createdOn: new Date(current)
},
{
type: 'Radish',
tags: { animalVegetableMineral: 'vegetable' },
value: 1,
data: 'radishes are a good source of electrolytes and minerals ',
userId: 'user1234',
},
{
type: 'BankAccount',
tags: { currency: 'dollars', units: 'cents' },
value: 0.15,
data: 'liquid assets only',
userId: '2d3h',
createdOn: new Date(current - twoDays - threeHours)
}]);
const response = await setup.server.inject({
method: 'GET',
url: `/api/report/aggregate?startDate=${moment(current).subtract(1, 'days')}&endDate=${moment(current).endOf('days')}&last=0`
});
t.equal(response.statusCode, 200, 'returns HTTP 200');
t.equal(typeof response.result, 'object');
t.equal(response.headers['content-type'], 'application/json; charset=utf-8');
t.end();
});

tap.test('can use the report method to get an aggregate list of metrics grouped by values for a given tag', async(t) => {
await setup.server.db.tracks.insertMany([{
type: 'BankAccount',
Expand Down

0 comments on commit 29ac55e

Please sign in to comment.