]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-stats.ts
Add ability to set start/end date to timeserie
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-stats.ts
1 import express from 'express'
2 import { param, query } from 'express-validator'
3 import { isDateValid } from '@server/helpers/custom-validators/misc'
4 import { isValidStatTimeserieMetric } from '@server/helpers/custom-validators/video-stats'
5 import { STATS_TIMESERIE } from '@server/initializers/constants'
6 import { HttpStatusCode, UserRight, VideoStatsTimeserieQuery } from '@shared/models'
7 import { logger } from '../../../helpers/logger'
8 import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
9
10 const videoOverallStatsValidator = [
11 isValidVideoIdParam('videoId'),
12
13 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14 logger.debug('Checking videoOverallStatsValidator parameters', { parameters: req.body })
15
16 if (areValidationErrors(req, res)) return
17 if (!await commonStatsCheck(req, res)) return
18
19 return next()
20 }
21 ]
22
23 const videoRetentionStatsValidator = [
24 isValidVideoIdParam('videoId'),
25
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking videoRetentionStatsValidator parameters', { parameters: req.body })
28
29 if (areValidationErrors(req, res)) return
30 if (!await commonStatsCheck(req, res)) return
31
32 if (res.locals.videoAll.isLive) {
33 return res.fail({
34 status: HttpStatusCode.BAD_REQUEST_400,
35 message: 'Cannot get retention stats of live video'
36 })
37 }
38
39 return next()
40 }
41 ]
42
43 const videoTimeserieStatsValidator = [
44 isValidVideoIdParam('videoId'),
45
46 param('metric')
47 .custom(isValidStatTimeserieMetric)
48 .withMessage('Should have a valid timeserie metric'),
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
60 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
61 logger.debug('Checking videoTimeserieStatsValidator parameters', { parameters: req.body })
62
63 if (areValidationErrors(req, res)) return
64 if (!await commonStatsCheck(req, res)) return
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
84 return next()
85 }
86 ]
87
88 // ---------------------------------------------------------------------------
89
90 export {
91 videoOverallStatsValidator,
92 videoTimeserieStatsValidator,
93 videoRetentionStatsValidator
94 }
95
96 // ---------------------------------------------------------------------------
97
98 async function commonStatsCheck (req: express.Request, res: express.Response) {
99 if (!await doesVideoExist(req.params.videoId, res, 'all')) return false
100 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.SEE_ALL_VIDEOS, res)) return false
101
102 return true
103 }
104
105 function 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 }