From f40712abbbb74e51f06037ef02757c42736bccf8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 6 May 2022 14:23:02 +0200 Subject: Add ability to filter overall video stats by date --- server/controllers/api/videos/stats.ts | 11 +--- server/initializers/constants.ts | 2 +- server/lib/timeserie.ts | 14 +++- server/models/view/local-video-viewer.ts | 12 ++-- server/tests/api/check-params/views.ts | 2 +- .../tests/api/views/video-views-overall-stats.ts | 26 +++++++- .../tests/api/views/video-views-timeserie-stats.ts | 74 +++++++++++++++++++--- 7 files changed, 113 insertions(+), 28 deletions(-) (limited to 'server') diff --git a/server/controllers/api/videos/stats.ts b/server/controllers/api/videos/stats.ts index 30e2bb06c..e79f01888 100644 --- a/server/controllers/api/videos/stats.ts +++ b/server/controllers/api/videos/stats.ts @@ -67,18 +67,9 @@ async function getTimeserieStats (req: express.Request, res: express.Response) { const stats = await LocalVideoViewerModel.getTimeserieStats({ video, metric, - startDate: query.startDate ?? buildOneMonthAgo().toISOString(), + startDate: query.startDate ?? video.createdAt.toISOString(), endDate: query.endDate ?? new Date().toISOString() }) return res.json(stats) } - -function buildOneMonthAgo () { - const monthAgo = new Date() - monthAgo.setHours(0, 0, 0, 0) - - monthAgo.setDate(monthAgo.getDate() - 29) - - return monthAgo -} diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index fa0fbc19d..dca792b1b 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -813,7 +813,7 @@ const SEARCH_INDEX = { // --------------------------------------------------------------------------- const STATS_TIMESERIE = { - MAX_DAYS: 30 + MAX_DAYS: 365 * 10 // Around 10 years } // --------------------------------------------------------------------------- diff --git a/server/lib/timeserie.ts b/server/lib/timeserie.ts index bd3d1c1ca..08b12129a 100644 --- a/server/lib/timeserie.ts +++ b/server/lib/timeserie.ts @@ -9,7 +9,10 @@ function buildGroupByAndBoundaries (startDateString: string, endDateString: stri logger.debug('Found "%s" group interval.', groupInterval, { startDate, endDate }) // Remove parts of the date we don't need - if (groupInterval.endsWith(' day') || groupInterval.endsWith(' days')) { + if (groupInterval.endsWith(' month') || groupInterval.endsWith(' months')) { + startDate.setDate(1) + startDate.setHours(0, 0, 0, 0) + } else if (groupInterval.endsWith(' day') || groupInterval.endsWith(' days')) { startDate.setHours(0, 0, 0, 0) } else if (groupInterval.endsWith(' hour') || groupInterval.endsWith(' hours')) { startDate.setMinutes(0, 0, 0) @@ -33,16 +36,25 @@ export { // --------------------------------------------------------------------------- function buildGroupInterval (startDate: Date, endDate: Date): string { + const aYear = 31536000 + const aMonth = 2678400 const aDay = 86400 const anHour = 3600 const aMinute = 60 const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000 + if (diffSeconds >= 6 * aYear) return '6 months' + if (diffSeconds >= 2 * aYear) return '1 month' + if (diffSeconds >= 6 * aMonth) return '7 days' + if (diffSeconds >= 2 * aMonth) return '2 days' + if (diffSeconds >= 15 * aDay) return '1 day' if (diffSeconds >= 8 * aDay) return '12 hours' if (diffSeconds >= 4 * aDay) return '6 hours' + if (diffSeconds >= 15 * anHour) return '1 hour' + if (diffSeconds >= 180 * aMinute) return '10 minutes' return '1 minute' diff --git a/server/models/view/local-video-viewer.ts b/server/models/view/local-video-viewer.ts index 2862f8b96..2305c7262 100644 --- a/server/models/view/local-video-viewer.ts +++ b/server/models/view/local-video-viewer.ts @@ -136,7 +136,7 @@ export class LocalVideoViewerModel extends Model