fix: check for trackCount before the aggregation
This commit is contained in:
parent
fc99d8a03b
commit
eb7acf7fc6
1131
api/package-lock.json
generated
1131
api/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -31,7 +31,6 @@
|
|||
"express": "4.17.1",
|
||||
"express-jwt": "^6.0.0",
|
||||
"express-session": "1.17.1",
|
||||
"jest": "^26.6.3",
|
||||
"joi": "^17.4.0",
|
||||
"jsonwebtoken": "8.5.1",
|
||||
"method-override": "3.0.0",
|
||||
|
@ -46,6 +45,7 @@
|
|||
"request": "2.88.2",
|
||||
"sanitize-filename": "^1.6.3",
|
||||
"slug": "^3.5.2",
|
||||
"supertest": "^6.1.3",
|
||||
"turf": "^3.0.14",
|
||||
"underscore": "^1.12.0"
|
||||
},
|
||||
|
@ -58,13 +58,16 @@
|
|||
"eslint-plugin-node": "^11.1.0",
|
||||
"eslint-plugin-prettier": "^3.3.1",
|
||||
"eslint-plugin-promise": "^4.3.1",
|
||||
"jest": "^26.6.3",
|
||||
"mockingoose": "^2.15.2",
|
||||
"nodemon": "^2.0.7",
|
||||
"prettier": "^2.2.1"
|
||||
},
|
||||
"jest": {
|
||||
"modulePathIgnorePatterns": [
|
||||
"local"
|
||||
]
|
||||
],
|
||||
"testEnvironment": "node"
|
||||
},
|
||||
"prettier": {
|
||||
"useTabs": false,
|
||||
|
|
|
@ -13,41 +13,47 @@ router.get(
|
|||
const trackCount = await Track.find().count();
|
||||
const publicTrackCount = await Track.find({ visible: true }).count();
|
||||
const userCount = await User.find().count();
|
||||
let trackLength = 0;
|
||||
let publicTrackLength = 0;
|
||||
let numEvents = 0;
|
||||
let trackDuration = 0;
|
||||
|
||||
const [{ trackLength, publicTrackLength, numEvents, trackDuration }] = await Track.aggregate([
|
||||
{ $lookup: { from: 'trackdatas', localField: 'publicTrackData', foreignField: '_id', as: 'publicTrackDatas' } },
|
||||
{ $lookup: { from: 'trackdatas', localField: 'trackData', foreignField: '_id', as: 'trackDatas' } },
|
||||
{
|
||||
$addFields: {
|
||||
publicTrackData: { $arrayElemAt: ['$publicTrackDatas', 0] },
|
||||
trackData: { $arrayElemAt: ['$trackDatas', 0] },
|
||||
},
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
publicTrackLength: '$publicTrackData.trackLength',
|
||||
trackLength: '$trackData.trackLength',
|
||||
numEvents: '$publicTrackData.numEvents',
|
||||
trackDuration: {
|
||||
$cond: [
|
||||
{ $and: ['$publicTrackData.recordedUntil', '$publicTrackData.recordedAt'] },
|
||||
{ $subtract: ['$publicTrackData.recordedUntil', '$publicTrackData.recordedAt'] },
|
||||
0,
|
||||
],
|
||||
if (trackCount) {
|
||||
[{ trackLength, publicTrackLength, numEvents, trackDuration }] = await Track.aggregate([
|
||||
{ $lookup: { from: 'trackdatas', localField: 'publicTrackData', foreignField: '_id', as: 'publicTrackDatas' } },
|
||||
{ $lookup: { from: 'trackdatas', localField: 'trackData', foreignField: '_id', as: 'trackDatas' } },
|
||||
{
|
||||
$addFields: {
|
||||
publicTrackData: { $arrayElemAt: ['$publicTrackDatas', 0] },
|
||||
trackData: { $arrayElemAt: ['$trackDatas', 0] },
|
||||
},
|
||||
},
|
||||
},
|
||||
{ $project: { publicTrackLength: true, trackLength: true, numEvents: true, trackDuration: true } },
|
||||
{
|
||||
$group: {
|
||||
_id: 'sum',
|
||||
trackLength: { $sum: '$trackLength' },
|
||||
publicTrackLength: { $sum: '$publicTrackLength' },
|
||||
numEvents: { $sum: '$numEvents' },
|
||||
trackDuration: { $sum: '$trackDuration' },
|
||||
{
|
||||
$addFields: {
|
||||
publicTrackLength: '$publicTrackData.trackLength',
|
||||
trackLength: '$trackData.trackLength',
|
||||
numEvents: '$publicTrackData.numEvents',
|
||||
trackDuration: {
|
||||
$cond: [
|
||||
{ $and: ['$publicTrackData.recordedUntil', '$publicTrackData.recordedAt'] },
|
||||
{ $subtract: ['$publicTrackData.recordedUntil', '$publicTrackData.recordedAt'] },
|
||||
0,
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
{ $project: { publicTrackLength: true, trackLength: true, numEvents: true, trackDuration: true } },
|
||||
{
|
||||
$group: {
|
||||
_id: 'sum',
|
||||
trackLength: { $sum: '$trackLength' },
|
||||
publicTrackLength: { $sum: '$publicTrackLength' },
|
||||
numEvents: { $sum: '$numEvents' },
|
||||
trackDuration: { $sum: '$trackDuration' },
|
||||
},
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
const trackLengthPrivatized = Math.floor(trackLength / TRACK_LENGTH_ROUNDING) * TRACK_LENGTH_ROUNDING;
|
||||
|
||||
|
|
56
api/src/routes/api/stats.test.js
Normal file
56
api/src/routes/api/stats.test.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
const request = require('supertest');
|
||||
const mockingoose = require('mockingoose');
|
||||
const express = require('express');
|
||||
|
||||
const Track = require('../../models/Track');
|
||||
const User = require('../../models/User');
|
||||
|
||||
const stats = require('./stats');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use('/stats', stats);
|
||||
|
||||
describe('stats', () => {
|
||||
it('checks for available trackCount', async () => {
|
||||
mockingoose(Track).toReturn(undefined, 'find').toReturn(0, 'count');
|
||||
mockingoose(User).toReturn({}, 'find');
|
||||
|
||||
await request(app).get('/stats').expect(200).expect({
|
||||
publicTrackCount: 0,
|
||||
publicTrackLength: 0,
|
||||
trackLength: 0,
|
||||
numEvents: 0,
|
||||
trackCount: 0,
|
||||
trackDuration: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns with json', async () => {
|
||||
mockingoose(Track)
|
||||
.toReturn([{}], 'find')
|
||||
.toReturn(1, 'count')
|
||||
.toReturn(
|
||||
[
|
||||
{
|
||||
trackLength: 1900,
|
||||
publicTrackLength: 500,
|
||||
numEvents: 1,
|
||||
trackDuration: 90,
|
||||
},
|
||||
],
|
||||
'aggregate',
|
||||
);
|
||||
|
||||
mockingoose(User).toReturn({}, 'find');
|
||||
|
||||
await request(app).get('/stats').expect(200).expect({
|
||||
publicTrackCount: 1,
|
||||
publicTrackLength: 500,
|
||||
trackLength: 1000,
|
||||
numEvents: 1,
|
||||
trackCount: 1,
|
||||
trackDuration: 0,
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue