PeerTube/server/controllers/api/requests.ts

58 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-06-05 21:53:49 +02:00
import * as express from 'express'
2017-05-15 22:22:03 +02:00
import { parallel } from 'async'
2016-09-19 21:33:46 +02:00
2017-05-15 22:22:03 +02:00
import {
getRequestScheduler,
getRequestVideoQaduScheduler,
getRequestVideoEventScheduler
} from '../../lib'
import { authenticate, ensureIsAdmin } from '../../middlewares'
2016-09-19 21:33:46 +02:00
2017-05-15 22:22:03 +02:00
const requestsRouter = express.Router()
2016-09-19 21:33:46 +02:00
2017-05-15 22:22:03 +02:00
requestsRouter.get('/stats',
authenticate,
ensureIsAdmin,
2016-09-19 21:33:46 +02:00
getStatsRequests
)
// ---------------------------------------------------------------------------
2017-05-15 22:22:03 +02:00
export {
requestsRouter
}
2016-09-19 21:33:46 +02:00
// ---------------------------------------------------------------------------
function getStatsRequests (req, res, next) {
2017-02-27 21:56:55 +01:00
parallel({
2017-05-15 22:22:03 +02:00
requestScheduler: buildRequestSchedulerFunction(getRequestScheduler()),
requestVideoQaduScheduler: buildRequestSchedulerFunction(getRequestVideoQaduScheduler()),
requestVideoEventScheduler: buildRequestSchedulerFunction(getRequestVideoEventScheduler())
2017-02-27 21:56:55 +01:00
}, function (err, result) {
2016-09-19 21:33:46 +02:00
if (err) return next(err)
2017-02-27 21:56:55 +01:00
return res.json(result)
2016-09-19 21:33:46 +02:00
})
}
2017-02-27 21:56:55 +01:00
// ---------------------------------------------------------------------------
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)
})
}
}