diff options
Diffstat (limited to 'server/controllers/api/requests.ts')
-rw-r--r-- | server/controllers/api/requests.ts | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/server/controllers/api/requests.ts b/server/controllers/api/requests.ts new file mode 100644 index 000000000..304499a4f --- /dev/null +++ b/server/controllers/api/requests.ts | |||
@@ -0,0 +1,57 @@ | |||
1 | import express = require('express') | ||
2 | import { parallel } from 'async' | ||
3 | |||
4 | import { | ||
5 | getRequestScheduler, | ||
6 | getRequestVideoQaduScheduler, | ||
7 | getRequestVideoEventScheduler | ||
8 | } from '../../lib' | ||
9 | import { authenticate, ensureIsAdmin } from '../../middlewares' | ||
10 | |||
11 | const requestsRouter = express.Router() | ||
12 | |||
13 | requestsRouter.get('/stats', | ||
14 | authenticate, | ||
15 | ensureIsAdmin, | ||
16 | getStatsRequests | ||
17 | ) | ||
18 | |||
19 | // --------------------------------------------------------------------------- | ||
20 | |||
21 | export { | ||
22 | requestsRouter | ||
23 | } | ||
24 | |||
25 | // --------------------------------------------------------------------------- | ||
26 | |||
27 | function getStatsRequests (req, res, next) { | ||
28 | parallel({ | ||
29 | requestScheduler: buildRequestSchedulerFunction(getRequestScheduler()), | ||
30 | requestVideoQaduScheduler: buildRequestSchedulerFunction(getRequestVideoQaduScheduler()), | ||
31 | requestVideoEventScheduler: buildRequestSchedulerFunction(getRequestVideoEventScheduler()) | ||
32 | }, function (err, result) { | ||
33 | if (err) return next(err) | ||
34 | |||
35 | return res.json(result) | ||
36 | }) | ||
37 | } | ||
38 | |||
39 | // --------------------------------------------------------------------------- | ||
40 | |||
41 | function buildRequestSchedulerFunction (requestScheduler) { | ||
42 | return function (callback) { | ||
43 | requestScheduler.remainingRequestsCount(function (err, count) { | ||
44 | if (err) return callback(err) | ||
45 | |||
46 | const result = { | ||
47 | totalRequests: count, | ||
48 | requestsLimitPods: requestScheduler.limitPods, | ||
49 | requestsLimitPerPod: requestScheduler.limitPerPod, | ||
50 | remainingMilliSeconds: requestScheduler.remainingMilliSeconds(), | ||
51 | milliSecondsInterval: requestScheduler.requestInterval | ||
52 | } | ||
53 | |||
54 | return callback(null, result) | ||
55 | }) | ||
56 | } | ||
57 | } | ||