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'
import * as Promise 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'
import { authenticate, ensureIsAdmin } 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-06-25 10:19:46 -05:00
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-06-25 10:19:46 -05:00
function getRequestSchedulersStats (req: express.Request, res: express.Response, next: express.NextFunction) {
Promise.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
})
.then(result => res.json(result))
.catch(err => next(err))
2016-09-19 14:33:46 -05:00
}
2017-02-27 14:56:55 -06:00
// ---------------------------------------------------------------------------
function buildRequestSchedulerStats (requestScheduler: AbstractRequestScheduler<any>) {
return requestScheduler.remainingRequestsCount().then(count => {
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
}