From ed27a6f6a58062a601087d7bd2ed9424468641b0 Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Tue, 8 Jul 2025 18:01:24 +0200 Subject: [PATCH] -statistiche OK --- src/server/router/api_router.js | 48 +++++++++++++++++++++++++++++---- src/server/version.txt | 2 +- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/server/router/api_router.js b/src/server/router/api_router.js index b02ccd0..2f88dfe 100644 --- a/src/server/router/api_router.js +++ b/src/server/router/api_router.js @@ -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), }, }, ]; diff --git a/src/server/version.txt b/src/server/version.txt index 352aa14..6579f9d 100644 --- a/src/server/version.txt +++ b/src/server/version.txt @@ -1 +1 @@ -1.2.62 \ No newline at end of file +1.2.63 \ No newline at end of file