aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/controllers/api/requests.js
blob: 6fd5753ac5c69e1ff87952e62d9a6559f2c57d33 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                                  
                                          
 
                                            
                                                


                               














                                                                              




                                                                                                      

                             
                           

    



















                                                                              
'use strict'

const express = require('express')
const parallel = require('async/parallel')

const friends = require('../../lib/friends')
const middlewares = require('../../middlewares')
const admin = middlewares.admin
const oAuth = middlewares.oauth

const router = express.Router()

router.get('/stats',
  oAuth.authenticate,
  admin.ensureIsAdmin,
  getStatsRequests
)

// ---------------------------------------------------------------------------

module.exports = router

// ---------------------------------------------------------------------------

function getStatsRequests (req, res, next) {
  parallel({
    requestScheduler: buildRequestSchedulerFunction(friends.getRequestScheduler()),
    requestVideoQaduScheduler: buildRequestSchedulerFunction(friends.getRequestVideoQaduScheduler()),
    requestVideoEventScheduler: buildRequestSchedulerFunction(friends.getRequestVideoEventScheduler())
  }, function (err, result) {
    if (err) return next(err)

    return res.json(result)
  })
}

// ---------------------------------------------------------------------------

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)
    })
  }
}