From 137b47cf0bfe82004dc79b1fb0148b44ab95e261 Mon Sep 17 00:00:00 2001 From: Jack Foltz Date: Wed, 19 Sep 2018 16:12:43 -0400 Subject: [PATCH] Finish stats tests and fix all time calculation --- app/routes/api/stats.js | 20 ++++++++++++++++---- test/api.js | 9 +++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/routes/api/stats.js b/app/routes/api/stats.js index b11d5d4..b96a51e 100644 --- a/app/routes/api/stats.js +++ b/app/routes/api/stats.js @@ -72,7 +72,6 @@ router.get('/week', requireAuth('stats.get'), wrap(async (req, res) => { } } ]).toArray()); - const viewStats = await (View.collection.aggregate([ { $match: { @@ -105,7 +104,7 @@ router.get('/week', requireAuth('stats.get'), wrap(async (req, res) => { })); router.get('/all', requireAuth('stats.get'), wrap(async (req, res) => { - const stats = await (Upload.collection.aggregate([ + const uploadStats = await (Upload.collection.aggregate([ { $match: { 'uploader': req.username @@ -113,7 +112,6 @@ router.get('/all', requireAuth('stats.get'), wrap(async (req, res) => { }, { $project: { - 'views': '$views', 'size': '$file.size' } }, @@ -121,11 +119,25 @@ router.get('/all', requireAuth('stats.get'), wrap(async (req, res) => { $group: { '_id': 'total', 'count': {$sum: 1}, - 'views': {$sum: '$views'}, 'size': {$sum: '$size'} } } ]).toArray()); + const viewStats = await (View.collection.aggregate([ + { + $match: { + 'uploader': req.username + } + }, + { + $group: { + '_id': 'total', + 'views': {$sum: 1}, + } + } + ]).toArray()); + + const stats = mergeAggregations(uploadStats, viewStats); res.status(200).json(stats); })); diff --git a/test/api.js b/test/api.js index a6e9544..07a6d5d 100644 --- a/test/api.js +++ b/test/api.js @@ -1021,8 +1021,13 @@ describe('Stats', () => { await setupUploadsAndViews(); await util.createSession(agent, ['stats.get'], 'user'); - const stats = await util.getStatsAll(agent); - console.log(stats.body); + const stats = (await util.getStatsAll(agent)).body; + + stats.should.have.property('total'); + stats.total.should.have.property('count').equal(8); + stats.total.should.have.property('size').equal(8); + stats.total.should.have.property('views').equal(8); + await util.logout(agent); }); });