-statistiche OK

This commit is contained in:
Surya Paolo
2025-07-08 18:01:24 +02:00
parent d6579763f4
commit ed27a6f6a5
2 changed files with 44 additions and 6 deletions

View File

@@ -59,15 +59,53 @@ router.get('/pageviews/stats', authenticate_noerror, async (req, res) => {
// Definiamo la base della pipeline
const basePipeline = [{ $match: matchStage }];
// Pipeline per statistiche temporali (oggi, settimana, mese)
const today = startOfDay(new Date());
const weekAgo = startOfDay(subDays(new Date(), 7));
const monthAgo = startOfDay(subDays(new Date(), 30));
// Funzione dinamica per creare pipeline di conteggio
const countStage = (date) => {
const match = { timestamp: { $gte: date } };
if (!unique) {
return [{ $match: match }, { $count: 'count' }];
}
return [
{ $match: match },
{
$group: {
_id: {
url: '$url',
user: { $ifNull: ['$userId', '$ip'] },
date: { $dateToString: { format: '%Y-%m-%d', date: '$timestamp' } },
},
},
},
{ $count: 'count' },
];
};
// Pipeline completa
const timeStatsPipeline = [
...basePipeline,
{
$facet: {
total: [{ $count: 'total' }],
today: [{ $match: { timestamp: { $gte: startOfDay(new Date()) } } }, { $count: 'count' }],
week: [{ $match: { timestamp: { $gte: startOfDay(subDays(new Date(), 7)) } } }, { $count: 'count' }],
month: [{ $match: { timestamp: { $gte: startOfDay(subDays(new Date(), 30)) } } }, { $count: 'count' }],
total: !unique
? [{ $count: 'count' }]
: [
{
$group: {
_id: {
url: '$url',
user: { $ifNull: ['$userId', '$ip'] },
date: { $dateToString: { format: '%Y-%m-%d', date: '$timestamp' } },
},
},
},
{ $count: 'count' },
],
today: countStage(today),
week: countStage(weekAgo),
month: countStage(monthAgo),
},
},
];

View File

@@ -1 +1 @@
1.2.62
1.2.63