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

54 lines
1.7 KiB
TypeScript
Raw Normal View History

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