-statistiche OK
This commit is contained in:
@@ -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),
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
@@ -1 +1 @@
|
||||
1.2.62
|
||||
1.2.63
|
||||
Reference in New Issue
Block a user