aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/requests.js37
-rw-r--r--server/lib/base-request-scheduler.js7
-rw-r--r--server/lib/friends.js31
3 files changed, 57 insertions, 18 deletions
diff --git a/server/controllers/api/requests.js b/server/controllers/api/requests.js
index 3e0d246d1..be352113e 100644
--- a/server/controllers/api/requests.js
+++ b/server/controllers/api/requests.js
@@ -1,9 +1,10 @@
1'use strict' 1'use strict'
2 2
3const express = require('express') 3const express = require('express')
4const parallel = require('async/parallel')
4 5
5const constants = require('../../initializers/constants') 6const constants = require('../../initializers/constants')
6const db = require('../../initializers/database') 7const friends = require('../../lib/friends')
7const middlewares = require('../../middlewares') 8const middlewares = require('../../middlewares')
8const admin = middlewares.admin 9const admin = middlewares.admin
9const oAuth = middlewares.oauth 10const oAuth = middlewares.oauth
@@ -23,15 +24,33 @@ module.exports = router
23// --------------------------------------------------------------------------- 24// ---------------------------------------------------------------------------
24 25
25function getStatsRequests (req, res, next) { 26function getStatsRequests (req, res, next) {
26 db.Request.countTotalRequests(function (err, totalRequests) { 27 parallel({
28 requestScheduler: buildRequestSchedulerFunction(friends.getRequestScheduler()),
29 requestVideoQaduScheduler: buildRequestSchedulerFunction(friends.getRequestVideoQaduScheduler()),
30 requestVideoEventScheduler: buildRequestSchedulerFunction(friends.getRequestVideoEventScheduler())
31 }, function (err, result) {
27 if (err) return next(err) 32 if (err) return next(err)
28 33
29 return res.json({ 34 return res.json(result)
30 totalRequests: totalRequests,
31 requestsLimitPods: constants.REQUESTS_LIMIT_PODS,
32 requestsLimitPerPod: constants.REQUESTS_LIMIT_PER_POD,
33 remainingMilliSeconds: db.Request.remainingMilliSeconds(),
34 milliSecondsInterval: constants.REQUESTS_INTERVAL
35 })
36 }) 35 })
37} 36}
37
38// ---------------------------------------------------------------------------
39
40function buildRequestSchedulerFunction (requestScheduler) {
41 return function (callback) {
42 requestScheduler.remainingRequestsCount(function (err, count) {
43 if (err) return callback(err)
44
45 const result = {
46 totalRequests: count,
47 requestsLimitPods: requestScheduler.limitPods,
48 requestsLimitPerPod: requestScheduler.limitPerPod,
49 remainingMilliSeconds: requestScheduler.remainingMilliSeconds(),
50 milliSecondsInterval: requestScheduler.requestInterval
51 }
52
53 return callback(null, result)
54 })
55 }
56}
diff --git a/server/lib/base-request-scheduler.js b/server/lib/base-request-scheduler.js
index 309c1a261..1c6b78297 100644
--- a/server/lib/base-request-scheduler.js
+++ b/server/lib/base-request-scheduler.js
@@ -12,6 +12,7 @@ module.exports = class BaseRequestScheduler {
12 constructor (options) { 12 constructor (options) {
13 this.lastRequestTimestamp = 0 13 this.lastRequestTimestamp = 0
14 this.timer = null 14 this.timer = null
15 this.requestInterval = constants.REQUESTS_INTERVAL
15 } 16 }
16 17
17 activate () { 18 activate () {
@@ -21,7 +22,7 @@ module.exports = class BaseRequestScheduler {
21 this.timer = setInterval(() => { 22 this.timer = setInterval(() => {
22 this.lastRequestTimestamp = Date.now() 23 this.lastRequestTimestamp = Date.now()
23 this.makeRequests() 24 this.makeRequests()
24 }, constants.REQUESTS_INTERVAL) 25 }, this.requestInterval)
25 } 26 }
26 27
27 deactivate () { 28 deactivate () {
@@ -41,6 +42,10 @@ module.exports = class BaseRequestScheduler {
41 return constants.REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp) 42 return constants.REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp)
42 } 43 }
43 44
45 remainingRequestsCount (callback) {
46 return this.getRequestModel().countTotalRequests(callback)
47 }
48
44 // --------------------------------------------------------------------------- 49 // ---------------------------------------------------------------------------
45 50
46 // Make a requests to friends of a certain type 51 // Make a requests to friends of a certain type
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 203f0e52c..7bd087d8c 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -19,8 +19,8 @@ const RequestVideoEventScheduler = require('./request-video-event-scheduler')
19const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] 19const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
20 20
21const requestScheduler = new RequestScheduler() 21const requestScheduler = new RequestScheduler()
22const requestSchedulerVideoQadu = new RequestVideoQaduScheduler() 22const requestVideoQaduScheduler = new RequestVideoQaduScheduler()
23const requestSchedulerVideoEvent = new RequestVideoEventScheduler() 23const requestVideoEventScheduler = new RequestVideoEventScheduler()
24 24
25const friends = { 25const friends = {
26 activate, 26 activate,
@@ -33,13 +33,16 @@ const friends = {
33 makeFriends, 33 makeFriends,
34 quitFriends, 34 quitFriends,
35 removeVideoToFriends, 35 removeVideoToFriends,
36 sendOwnedVideosToPod 36 sendOwnedVideosToPod,
37 getRequestScheduler,
38 getRequestVideoQaduScheduler,
39 getRequestVideoEventScheduler
37} 40}
38 41
39function activate () { 42function activate () {
40 requestScheduler.activate() 43 requestScheduler.activate()
41 requestSchedulerVideoQadu.activate() 44 requestVideoQaduScheduler.activate()
42 requestSchedulerVideoEvent.activate() 45 requestVideoEventScheduler.activate()
43} 46}
44 47
45function addVideoToFriends (videoData, transaction, callback) { 48function addVideoToFriends (videoData, transaction, callback) {
@@ -142,7 +145,7 @@ function quitFriends (callback) {
142 }, 145 },
143 146
144 function flushVideoQaduRequests (callbackAsync) { 147 function flushVideoQaduRequests (callbackAsync) {
145 requestSchedulerVideoQadu.flush(err => callbackAsync(err)) 148 requestVideoQaduScheduler.flush(err => callbackAsync(err))
146 }, 149 },
147 150
148 function getPodsList (callbackAsync) { 151 function getPodsList (callbackAsync) {
@@ -215,6 +218,18 @@ function sendOwnedVideosToPod (podId) {
215 }) 218 })
216} 219}
217 220
221function getRequestScheduler () {
222 return requestScheduler
223}
224
225function getRequestVideoQaduScheduler () {
226 return requestVideoQaduScheduler
227}
228
229function getRequestVideoEventScheduler () {
230 return requestVideoEventScheduler
231}
232
218// --------------------------------------------------------------------------- 233// ---------------------------------------------------------------------------
219 234
220module.exports = friends 235module.exports = friends
@@ -345,13 +360,13 @@ function createRequest (options, callback) {
345function createVideoQaduRequest (options, callback) { 360function createVideoQaduRequest (options, callback) {
346 if (!callback) callback = utils.createEmptyCallback() 361 if (!callback) callback = utils.createEmptyCallback()
347 362
348 requestSchedulerVideoQadu.createRequest(options, callback) 363 requestVideoQaduScheduler.createRequest(options, callback)
349} 364}
350 365
351function createVideoEventRequest (options, callback) { 366function createVideoEventRequest (options, callback) {
352 if (!callback) callback = utils.createEmptyCallback() 367 if (!callback) callback = utils.createEmptyCallback()
353 368
354 requestSchedulerVideoEvent.createRequest(options, callback) 369 requestVideoEventScheduler.createRequest(options, callback)
355} 370}
356 371
357function isMe (host) { 372function isMe (host) {