aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/request-schedulers.ts
blob: 28f46f3ee6a007a9981de9e63d7b769e25577d10 (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
import * as express from 'express'
import * as Bluebird from 'bluebird'

import {
  AbstractRequestScheduler,
  getRequestScheduler,
  getRequestVideoQaduScheduler,
  getRequestVideoEventScheduler
} from '../../lib'
import { authenticate, ensureIsAdmin, asyncMiddleware } from '../../middlewares'
import { RequestSchedulerStatsAttributes } from '../../../shared'

const requestSchedulerRouter = express.Router()

requestSchedulerRouter.get('/stats',
  authenticate,
  ensureIsAdmin,
  asyncMiddleware(getRequestSchedulersStats)
)

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

export {
  requestSchedulerRouter
}

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

async function getRequestSchedulersStats (req: express.Request, res: express.Response, next: express.NextFunction) {
  const result = await Bluebird.props({
    requestScheduler: buildRequestSchedulerStats(getRequestScheduler()),
    requestVideoQaduScheduler: buildRequestSchedulerStats(getRequestVideoQaduScheduler()),
    requestVideoEventScheduler: buildRequestSchedulerStats(getRequestVideoEventScheduler())
  })

  return res.json(result)
}

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

async function buildRequestSchedulerStats (requestScheduler: AbstractRequestScheduler<any>) {
  const count = await requestScheduler.remainingRequestsCount()

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

  return result
}