aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/metrics.ts
blob: ced9afc69180fdde62b8ee1a6866da6071633951 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import express from 'express'
import { body } from 'express-validator'
import { isValidPlayerMode } from '@server/helpers/custom-validators/metrics'
import { isIdOrUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc'
import { CONFIG } from '@server/initializers/config'
import { HttpStatusCode, PlaybackMetricCreate } from '@shared/models'
import { areValidationErrors, doesVideoExist } from './shared'

const addPlaybackMetricValidator = [
  body('resolution')
    .isInt({ min: 0 }),
  body('fps')
    .optional()
    .isInt({ min: 0 }),

  body('totalPeers')
    .optional()
    .isInt({ min: 0 }),

  body('p2pEnabled')
    .optional()
    .isBoolean(),

  body('playerMode')
    .custom(isValidPlayerMode),

  body('resolutionChanges')
    .isInt({ min: 0 }),

  body('errors')
    .isInt({ min: 0 }),

  body('downloadedBytesP2P')
    .isInt({ min: 0 }),
  body('downloadedBytesHTTP')
    .isInt({ min: 0 }),

  body('uploadedBytesP2P')
    .isInt({ min: 0 }),

  body('videoId')
    .customSanitizer(toCompleteUUID)
    .custom(isIdOrUUIDValid),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (!CONFIG.OPEN_TELEMETRY.METRICS.ENABLED) return res.sendStatus(HttpStatusCode.NO_CONTENT_204)

    const body: PlaybackMetricCreate = req.body

    if (areValidationErrors(req, res)) return
    if (!await doesVideoExist(body.videoId, res, 'only-immutable-attributes')) return

    return next()
  }
]

// ---------------------------------------------------------------------------

export {
  addPlaybackMetricValidator
}