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

View file

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