Make stats calculation more robust, ignore obvious errors

This commit is contained in:
Paul Bienkowski 2021-05-14 19:25:27 +02:00
parent 95a28ec457
commit 5c28f1d344
2 changed files with 17 additions and 8 deletions

View file

@ -7,6 +7,9 @@ const wrapRoute = require('../../_helpers/wrapRoute');
// round to this number of meters for privacy reasons // round to this number of meters for privacy reasons
const TRACK_LENGTH_ROUNDING = 1000; const TRACK_LENGTH_ROUNDING = 1000;
// round to this number of seconds for privacy reasons
const TRACK_DURATION_ROUNDING = 120;
router.get( router.get(
'/', '/',
wrapRoute(async (req, res) => { wrapRoute(async (req, res) => {
@ -17,11 +20,17 @@ router.get(
const trackStats = await Track.aggregate([ const trackStats = await Track.aggregate([
{ {
$addFields: { $addFields: {
trackLength: '$statistics.length', trackLength: {
$cond: [
{$lt: ['$statistics.length', 500000]},
'$statistics.length',
0,
],
},
numEvents: '$statistics.numEvents', numEvents: '$statistics.numEvents',
trackDuration: { trackDuration: {
$cond: [ $cond: [
{ $and: ['$statistics.recordedUntil', '$statistics.recordedAt'] }, { $and: ['$statistics.recordedUntil', '$statistics.recordedAt', {$gt: ['$statistics.recordedAt', new Date('2010-01-01')]}] },
{ $subtract: ['$statistics.recordedUntil', '$statistics.recordedAt'] }, { $subtract: ['$statistics.recordedUntil', '$statistics.recordedAt'] },
0, 0,
], ],
@ -44,14 +53,14 @@ router.get(
: [0,0,0]; : [0,0,0];
const trackLengthPrivatized = Math.floor(trackLength / TRACK_LENGTH_ROUNDING) * TRACK_LENGTH_ROUNDING; const trackLengthPrivatized = Math.floor(trackLength / TRACK_LENGTH_ROUNDING) * TRACK_LENGTH_ROUNDING;
const trackDurationPrivatized = Math.round(trackDuration / 1000 / TRACK_DURATION_ROUNDING) * TRACK_DURATION_ROUNDING;
return res.json({ return res.json({
publicTrackCount, publicTrackCount,
publicTrackLength: trackLengthPrivatized,
trackLength: trackLengthPrivatized, trackLength: trackLengthPrivatized,
trackDuration: trackDurationPrivatized,
numEvents, numEvents,
trackCount, trackCount,
trackDuration: Math.round(trackDuration / 1000),
userCount, userCount,
}); });
}), }),

View file

@ -47,12 +47,12 @@ function Stats() {
<Statistic.Group widths={2} size="mini"> <Statistic.Group widths={2} size="mini">
<Statistic> <Statistic>
<Statistic.Value>{Number(stats?.publicTrackLength / 1000).toFixed(1)}</Statistic.Value> <Statistic.Value>{Number(stats?.trackLength / 1000).toFixed(1)} km</Statistic.Value>
<Statistic.Label>km track length</Statistic.Label> <Statistic.Label>track length</Statistic.Label>
</Statistic> </Statistic>
<Statistic> <Statistic>
<Statistic.Value>{formatDuration(stats?.trackDuration)}</Statistic.Value> <Statistic.Value>{formatDuration(stats?.trackDuration)} h</Statistic.Value>
<Statistic.Label>hours recorded</Statistic.Label> <Statistic.Label>recorded</Statistic.Label>
</Statistic> </Statistic>
<Statistic> <Statistic>
<Statistic.Value>{stats?.numEvents}</Statistic.Value> <Statistic.Value>{stats?.numEvents}</Statistic.Value>