PeerTube/server/controllers/api/request-schedulers.ts

54 lines
1.8 KiB
TypeScript
Raw Normal View History

2017-06-05 21:53:49 +02:00
import * as express from 'express'
2017-10-25 11:55:06 +02:00
import * as Bluebird from 'bluebird'
2016-09-19 21:33:46 +02:00
2017-05-15 22:22:03 +02:00
import {
AbstractRequestScheduler,
2017-05-15 22:22:03 +02:00
getRequestScheduler,
getRequestVideoQaduScheduler,
getRequestVideoEventScheduler
} from '../../lib'
import { authenticate, ensureUserHasRight, asyncMiddleware } from '../../middlewares'
import { RequestSchedulerStatsAttributes, UserRight } from '../../../shared'
2016-09-19 21:33:46 +02:00
2017-06-25 17:19:46 +02:00
const requestSchedulerRouter = express.Router()
2016-09-19 21:33:46 +02:00
2017-06-25 17:19:46 +02:00
requestSchedulerRouter.get('/stats',
2017-05-15 22:22:03 +02:00
authenticate,
ensureUserHasRight(UserRight.MANAGE_REQUEST_SCHEDULERS),
2017-10-25 11:55:06 +02:00
asyncMiddleware(getRequestSchedulersStats)
2016-09-19 21:33:46 +02:00
)
// ---------------------------------------------------------------------------
2017-05-15 22:22:03 +02:00
export {
2017-06-25 17:19:46 +02:00
requestSchedulerRouter
2017-05-15 22:22:03 +02:00
}
2016-09-19 21:33:46 +02:00
// ---------------------------------------------------------------------------
2017-10-25 11:55:06 +02:00
async function getRequestSchedulersStats (req: express.Request, res: express.Response, next: express.NextFunction) {
const result = await Bluebird.props({
2017-06-25 17:19:46 +02:00
requestScheduler: buildRequestSchedulerStats(getRequestScheduler()),
requestVideoQaduScheduler: buildRequestSchedulerStats(getRequestVideoQaduScheduler()),
requestVideoEventScheduler: buildRequestSchedulerStats(getRequestVideoEventScheduler())
2016-09-19 21:33:46 +02:00
})
2017-10-25 11:55:06 +02:00
return res.json(result)
2016-09-19 21:33:46 +02:00
}
2017-02-27 21:56:55 +01:00
// ---------------------------------------------------------------------------
2017-10-25 11:55:06 +02:00
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
2017-02-27 21:56:55 +01:00
}