metrics.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import express from 'express'
  2. import { body } from 'express-validator'
  3. import { isValidPlayerMode } from '@server/helpers/custom-validators/metrics'
  4. import { isIdOrUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc'
  5. import { CONFIG } from '@server/initializers/config'
  6. import { HttpStatusCode, PlaybackMetricCreate } from '@shared/models'
  7. import { areValidationErrors, doesVideoExist } from './shared'
  8. const addPlaybackMetricValidator = [
  9. body('resolution')
  10. .isInt({ min: 0 }),
  11. body('fps')
  12. .optional()
  13. .isInt({ min: 0 }),
  14. body('playerMode')
  15. .custom(isValidPlayerMode),
  16. body('resolutionChanges')
  17. .isInt({ min: 0 }),
  18. body('errors')
  19. .isInt({ min: 0 }),
  20. body('downloadedBytesP2P')
  21. .isInt({ min: 0 }),
  22. body('downloadedBytesHTTP')
  23. .isInt({ min: 0 }),
  24. body('uploadedBytesP2P')
  25. .isInt({ min: 0 }),
  26. body('videoId')
  27. .customSanitizer(toCompleteUUID)
  28. .custom(isIdOrUUIDValid),
  29. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  30. if (!CONFIG.OPEN_TELEMETRY.METRICS.ENABLED) return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
  31. const body: PlaybackMetricCreate = req.body
  32. if (areValidationErrors(req, res)) return
  33. if (!await doesVideoExist(body.videoId, res, 'only-immutable-attributes')) return
  34. return next()
  35. }
  36. ]
  37. // ---------------------------------------------------------------------------
  38. export {
  39. addPlaybackMetricValidator
  40. }