aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/requests.ts
blob: 304499a4f67832a4c0b05099e8b22b251a5c066a (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
import express = require('express')
import { parallel } from 'async'

import {
  getRequestScheduler,
  getRequestVideoQaduScheduler,
  getRequestVideoEventScheduler
} from '../../lib'
import { authenticate, ensureIsAdmin } from '../../middlewares'

const requestsRouter = express.Router()

requestsRouter.get('/stats',
  authenticate,
  ensureIsAdmin,
  getStatsRequests
)

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

export {
  requestsRouter
}

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

function getStatsRequests (req, res, next) {
  parallel({
    requestScheduler: buildRequestSchedulerFunction(getRequestScheduler()),
    requestVideoQaduScheduler: buildRequestSchedulerFunction(getRequestVideoQaduScheduler()),
    requestVideoEventScheduler: buildRequestSchedulerFunction(getRequestVideoEventScheduler())
  }, function (err, result) {
    if (err) return next(err)

    return res.json(result)
  })
}

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

function buildRequestSchedulerFunction (requestScheduler) {
  return function (callback) {
    requestScheduler.remainingRequestsCount(function (err, count) {
      if (err) return callback(err)

      const result = {
        totalRequests: count,
        requestsLimitPods: requestScheduler.limitPods,
        requestsLimitPerPod: requestScheduler.limitPerPod,
        remainingMilliSeconds: requestScheduler.remainingMilliSeconds(),
        milliSecondsInterval: requestScheduler.requestInterval
      }

      return callback(null, result)
    })
  }
}