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'
10 const videoOverallStatsValidator = [
11 isValidVideoIdParam('videoId'),
16 .withMessage('Should have a valid start date'),
21 .withMessage('Should have a valid end date'),
23 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
24 logger.debug('Checking videoOverallStatsValidator parameters', { parameters: req.body })
26 if (areValidationErrors(req, res)) return
27 if (!await commonStatsCheck(req, res)) return
33 const videoRetentionStatsValidator = [
34 isValidVideoIdParam('videoId'),
36 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
37 logger.debug('Checking videoRetentionStatsValidator parameters', { parameters: req.body })
39 if (areValidationErrors(req, res)) return
40 if (!await commonStatsCheck(req, res)) return
42 if (res.locals.videoAll.isLive) {
44 status: HttpStatusCode.BAD_REQUEST_400,
45 message: 'Cannot get retention stats of live video'
53 const videoTimeserieStatsValidator = [
54 isValidVideoIdParam('videoId'),
57 .custom(isValidStatTimeserieMetric)
58 .withMessage('Should have a valid timeserie metric'),
63 .withMessage('Should have a valid start date'),
68 .withMessage('Should have a valid end date'),
70 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
71 logger.debug('Checking videoTimeserieStatsValidator parameters', { parameters: req.body })
73 if (areValidationErrors(req, res)) return
74 if (!await commonStatsCheck(req, res)) return
76 const query: VideoStatsTimeserieQuery = req.query
78 (query.startDate && !query.endDate) ||
79 (!query.startDate && query.endDate)
82 status: HttpStatusCode.BAD_REQUEST_400,
83 message: 'Both start date and end date should be defined if one of them is specified'
87 if (query.startDate && getIntervalByDays(query.startDate, query.endDate) > STATS_TIMESERIE.MAX_DAYS) {
89 status: HttpStatusCode.BAD_REQUEST_400,
90 message: 'Star date and end date interval is too big'
98 // ---------------------------------------------------------------------------
101 videoOverallStatsValidator,
102 videoTimeserieStatsValidator,
103 videoRetentionStatsValidator
106 // ---------------------------------------------------------------------------
108 async function commonStatsCheck (req: express.Request, res: express.Response) {
109 if (!await doesVideoExist(req.params.videoId, res, 'all')) return false
110 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.SEE_ALL_VIDEOS, res)) return false
115 function getIntervalByDays (startDateString: string, endDateString: string) {
116 const startDate = new Date(startDateString)
117 const endDate = new Date(endDateString)
119 return (endDate.getTime() - startDate.getTime()) / 1000 / 86400