aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/videos/video-stats.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-04-07 10:53:35 +0200
committerChocobozzz <chocobozzz@cpy.re>2022-04-15 09:49:35 +0200
commit901bcf5c188ea79350fecd499ad76460b866617b (patch)
tree1e79f26cc3f2b952371d31bfa9b94a2b150be38a /server/middlewares/validators/videos/video-stats.ts
parentac907dc7c158056e9b6a5cb58acd27df5c7c2670 (diff)
downloadPeerTube-901bcf5c188ea79350fecd499ad76460b866617b.tar.gz
PeerTube-901bcf5c188ea79350fecd499ad76460b866617b.tar.zst
PeerTube-901bcf5c188ea79350fecd499ad76460b866617b.zip
Add ability to set start/end date to timeserie
Diffstat (limited to 'server/middlewares/validators/videos/video-stats.ts')
-rw-r--r--server/middlewares/validators/videos/video-stats.ts41
1 files changed, 39 insertions, 2 deletions
diff --git a/server/middlewares/validators/videos/video-stats.ts b/server/middlewares/validators/videos/video-stats.ts
index 358b6b473..12509abde 100644
--- a/server/middlewares/validators/videos/video-stats.ts
+++ b/server/middlewares/validators/videos/video-stats.ts
@@ -1,7 +1,9 @@
1import express from 'express' 1import express from 'express'
2import { param } from 'express-validator' 2import { param, query } from 'express-validator'
3import { isDateValid } from '@server/helpers/custom-validators/misc'
3import { isValidStatTimeserieMetric } from '@server/helpers/custom-validators/video-stats' 4import { isValidStatTimeserieMetric } from '@server/helpers/custom-validators/video-stats'
4import { HttpStatusCode, UserRight } from '@shared/models' 5import { STATS_TIMESERIE } from '@server/initializers/constants'
6import { HttpStatusCode, UserRight, VideoStatsTimeserieQuery } from '@shared/models'
5import { logger } from '../../../helpers/logger' 7import { logger } from '../../../helpers/logger'
6import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared' 8import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
7 9
@@ -45,12 +47,40 @@ const videoTimeserieStatsValidator = [
45 .custom(isValidStatTimeserieMetric) 47 .custom(isValidStatTimeserieMetric)
46 .withMessage('Should have a valid timeserie metric'), 48 .withMessage('Should have a valid timeserie metric'),
47 49
50 query('startDate')
51 .optional()
52 .custom(isDateValid)
53 .withMessage('Should have a valid start date'),
54
55 query('endDate')
56 .optional()
57 .custom(isDateValid)
58 .withMessage('Should have a valid end date'),
59
48 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 60 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
49 logger.debug('Checking videoTimeserieStatsValidator parameters', { parameters: req.body }) 61 logger.debug('Checking videoTimeserieStatsValidator parameters', { parameters: req.body })
50 62
51 if (areValidationErrors(req, res)) return 63 if (areValidationErrors(req, res)) return
52 if (!await commonStatsCheck(req, res)) return 64 if (!await commonStatsCheck(req, res)) return
53 65
66 const query: VideoStatsTimeserieQuery = req.query
67 if (
68 (query.startDate && !query.endDate) ||
69 (!query.startDate && query.endDate)
70 ) {
71 return res.fail({
72 status: HttpStatusCode.BAD_REQUEST_400,
73 message: 'Both start date and end date should be defined if one of them is specified'
74 })
75 }
76
77 if (query.startDate && getIntervalByDays(query.startDate, query.endDate) > STATS_TIMESERIE.MAX_DAYS) {
78 return res.fail({
79 status: HttpStatusCode.BAD_REQUEST_400,
80 message: 'Star date and end date interval is too big'
81 })
82 }
83
54 return next() 84 return next()
55 } 85 }
56] 86]
@@ -71,3 +101,10 @@ async function commonStatsCheck (req: express.Request, res: express.Response) {
71 101
72 return true 102 return true
73} 103}
104
105function getIntervalByDays (startDateString: string, endDateString: string) {
106 const startDate = new Date(startDateString)
107 const endDate = new Date(endDateString)
108
109 return (endDate.getTime() - startDate.getTime()) / 1000 / 86400
110}