From 99fdec464802e5d720fe08ead06b63368b115baf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 27 Feb 2017 21:56:55 +0100 Subject: Fix request schedulers stats --- .../request-stats/request-stats.component.html | 50 +++++++++--------- .../request-stats/request-stats.component.scss | 4 ++ .../request-stats/request-stats.component.ts | 60 ++++++++++++++++------ .../app/+admin/requests/shared/request.service.ts | 14 ++++- client/src/app/shared/shared.module.ts | 3 ++ server/controllers/api/requests.js | 37 +++++++++---- server/lib/base-request-scheduler.js | 7 ++- server/lib/friends.js | 31 ++++++++--- 8 files changed, 146 insertions(+), 60 deletions(-) diff --git a/client/src/app/+admin/requests/request-stats/request-stats.component.html b/client/src/app/+admin/requests/request-stats/request-stats.component.html index 9dbed1739..ca531258c 100644 --- a/client/src/app/+admin/requests/request-stats/request-stats.component.html +++ b/client/src/app/+admin/requests/request-stats/request-stats.component.html @@ -1,33 +1,37 @@

Requests stats

-
-
-
- Remaining requests: - {{ stats.totalRequests }} -
+
+
+

{{ statsTitles[requestSchedulerName] }}

-
- Interval seconds between requests: - {{ stats.secondsInterval }} -
+
+
+ Remaining requests: + {{ stats[requestSchedulerName].totalRequests }} +
-
- Remaining time before the scheduled request: - {{ stats.remainingSeconds }} -
-
+
+ Interval seconds between requests: + {{ stats[requestSchedulerName].secondsInterval }} +
-
-
- Maximum number of different pods for a scheduled request: - {{ stats.requestsLimitPods }} +
+ Remaining time before the scheduled request: + {{ stats[requestSchedulerName].remainingSeconds }} +
-
- Maximum number of requests per pod for a scheduled request: - {{ stats.requestsLimitPerPod }} +
+
+ Maximum number of different pods for a scheduled request: + {{ stats[requestSchedulerName].requestsLimitPods }} +
+ +
+ Maximum number of requests per pod for a scheduled request: + {{ stats[requestSchedulerName].requestsLimitPerPod }} +
-
+
diff --git a/client/src/app/+admin/requests/request-stats/request-stats.component.scss b/client/src/app/+admin/requests/request-stats/request-stats.component.scss index 9c68fba99..c9f724604 100644 --- a/client/src/app/+admin/requests/request-stats/request-stats.component.scss +++ b/client/src/app/+admin/requests/request-stats/request-stats.component.scss @@ -1,3 +1,7 @@ +.block { + margin-bottom: 40px; +} + .label-description { display: inline-block; font-weight: bold; diff --git a/client/src/app/+admin/requests/request-stats/request-stats.component.ts b/client/src/app/+admin/requests/request-stats/request-stats.component.ts index 18855a5f8..85dd7e492 100644 --- a/client/src/app/+admin/requests/request-stats/request-stats.component.ts +++ b/client/src/app/+admin/requests/request-stats/request-stats.component.ts @@ -10,10 +10,30 @@ import { RequestService, RequestStats } from '../shared'; styleUrls: [ './request-stats.component.scss' ] }) export class RequestStatsComponent implements OnInit, OnDestroy { - stats: RequestStats = null; + statsTitles = { + requestScheduler: 'Basic request scheduler', + requestVideoEventScheduler: 'Video events request scheduler', + requestVideoQaduScheduler: 'Quick and dirty video updates request scheduler' + }; + + stats: { [ id: string ]: RequestStats } = { + requestScheduler: null, + requestVideoEventScheduler: null, + requestVideoQaduScheduler: null + }; + + private intervals: { [ id: string ]: number } = { + requestScheduler: null, + requestVideoEventScheduler: null, + requestVideoQaduScheduler: null + }; + + private timeouts: { [ id: string ]: number } = { + requestScheduler: null, + requestVideoEventScheduler: null, + requestVideoQaduScheduler: null + }; - private interval: number = null; - private timeout: number = null; constructor( private notificationsService: NotificationsService, @@ -22,17 +42,19 @@ export class RequestStatsComponent implements OnInit, OnDestroy { ngOnInit() { this.getStats(); - this.runInterval(); + this.runIntervals(); } ngOnDestroy() { - if (this.interval !== null) { - window.clearInterval(this.interval); - } + Object.keys(this.stats).forEach(requestSchedulerName => { + if (this.intervals[requestSchedulerName] !== null) { + window.clearInterval(this.intervals[requestSchedulerName]); + } - if (this.timeout !== null) { - window.clearTimeout(this.timeout); - } + if (this.timeouts[requestSchedulerName] !== null) { + window.clearTimeout(this.timeouts[requestSchedulerName]); + } + }); } getStats() { @@ -43,14 +65,18 @@ export class RequestStatsComponent implements OnInit, OnDestroy { ); } - private runInterval() { - this.interval = window.setInterval(() => { - this.stats.remainingMilliSeconds -= 1000; + private runIntervals() { + Object.keys(this.intervals).forEach(requestSchedulerName => { + this.intervals[requestSchedulerName] = window.setInterval(() => { + const stats = this.stats[requestSchedulerName]; - if (this.stats.remainingMilliSeconds <= 0) { - this.timeout = window.setTimeout(() => this.getStats(), this.stats.remainingMilliSeconds + 100); - } - }, 1000); + stats.remainingMilliSeconds -= 1000; + + if (stats.remainingMilliSeconds <= 0) { + this.timeouts[requestSchedulerName] = window.setTimeout(() => this.getStats(), stats.remainingMilliSeconds + 100); + } + }, 1000); + }); } diff --git a/client/src/app/+admin/requests/shared/request.service.ts b/client/src/app/+admin/requests/shared/request.service.ts index 55b28bcfc..915d192a6 100644 --- a/client/src/app/+admin/requests/shared/request.service.ts +++ b/client/src/app/+admin/requests/shared/request.service.ts @@ -15,10 +15,20 @@ export class RequestService { private restExtractor: RestExtractor ) {} - getStats(): Observable { + getStats(): Observable<{ [ id: string ]: RequestStats }> { return this.authHttp.get(RequestService.BASE_REQUEST_URL + 'stats') .map(this.restExtractor.extractDataGet) - .map((data) => new RequestStats(data)) + .map(this.buildRequestObjects) .catch((res) => this.restExtractor.handleError(res)); } + + private buildRequestObjects(data: any) { + const requestSchedulers = {}; + + Object.keys(data).forEach(requestSchedulerName => { + requestSchedulers[requestSchedulerName] = new RequestStats(data[requestSchedulerName]); + }); + + return requestSchedulers; + } } diff --git a/client/src/app/shared/shared.module.ts b/client/src/app/shared/shared.module.ts index 99893c8b1..0f57ef078 100644 --- a/client/src/app/shared/shared.module.ts +++ b/client/src/app/shared/shared.module.ts @@ -5,6 +5,7 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { RouterModule } from '@angular/router'; import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe'; +import { KeysPipe } from 'angular-pipes/src/object/keys.pipe'; import { DropdownModule } from 'ng2-bootstrap/dropdown'; import { ProgressbarModule } from 'ng2-bootstrap/progressbar'; import { PaginationModule } from 'ng2-bootstrap/pagination'; @@ -36,6 +37,7 @@ import { VideoAbuseService } from './video-abuse'; declarations: [ BytesPipe, + KeysPipe, SearchComponent ], @@ -53,6 +55,7 @@ import { VideoAbuseService } from './video-abuse'; ProgressbarModule, Ng2SmartTableModule, BytesPipe, + KeysPipe, SearchComponent ], 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 @@ 'use strict' const express = require('express') +const parallel = require('async/parallel') const constants = require('../../initializers/constants') -const db = require('../../initializers/database') +const friends = require('../../lib/friends') const middlewares = require('../../middlewares') const admin = middlewares.admin const oAuth = middlewares.oauth @@ -23,15 +24,33 @@ module.exports = router // --------------------------------------------------------------------------- function getStatsRequests (req, res, next) { - db.Request.countTotalRequests(function (err, totalRequests) { + parallel({ + requestScheduler: buildRequestSchedulerFunction(friends.getRequestScheduler()), + requestVideoQaduScheduler: buildRequestSchedulerFunction(friends.getRequestVideoQaduScheduler()), + requestVideoEventScheduler: buildRequestSchedulerFunction(friends.getRequestVideoEventScheduler()) + }, function (err, result) { if (err) return next(err) - return res.json({ - totalRequests: totalRequests, - requestsLimitPods: constants.REQUESTS_LIMIT_PODS, - requestsLimitPerPod: constants.REQUESTS_LIMIT_PER_POD, - remainingMilliSeconds: db.Request.remainingMilliSeconds(), - milliSecondsInterval: constants.REQUESTS_INTERVAL - }) + 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) + }) + } +} 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 { constructor (options) { this.lastRequestTimestamp = 0 this.timer = null + this.requestInterval = constants.REQUESTS_INTERVAL } activate () { @@ -21,7 +22,7 @@ module.exports = class BaseRequestScheduler { this.timer = setInterval(() => { this.lastRequestTimestamp = Date.now() this.makeRequests() - }, constants.REQUESTS_INTERVAL) + }, this.requestInterval) } deactivate () { @@ -41,6 +42,10 @@ module.exports = class BaseRequestScheduler { return constants.REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp) } + remainingRequestsCount (callback) { + return this.getRequestModel().countTotalRequests(callback) + } + // --------------------------------------------------------------------------- // 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') const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] const requestScheduler = new RequestScheduler() -const requestSchedulerVideoQadu = new RequestVideoQaduScheduler() -const requestSchedulerVideoEvent = new RequestVideoEventScheduler() +const requestVideoQaduScheduler = new RequestVideoQaduScheduler() +const requestVideoEventScheduler = new RequestVideoEventScheduler() const friends = { activate, @@ -33,13 +33,16 @@ const friends = { makeFriends, quitFriends, removeVideoToFriends, - sendOwnedVideosToPod + sendOwnedVideosToPod, + getRequestScheduler, + getRequestVideoQaduScheduler, + getRequestVideoEventScheduler } function activate () { requestScheduler.activate() - requestSchedulerVideoQadu.activate() - requestSchedulerVideoEvent.activate() + requestVideoQaduScheduler.activate() + requestVideoEventScheduler.activate() } function addVideoToFriends (videoData, transaction, callback) { @@ -142,7 +145,7 @@ function quitFriends (callback) { }, function flushVideoQaduRequests (callbackAsync) { - requestSchedulerVideoQadu.flush(err => callbackAsync(err)) + requestVideoQaduScheduler.flush(err => callbackAsync(err)) }, function getPodsList (callbackAsync) { @@ -215,6 +218,18 @@ function sendOwnedVideosToPod (podId) { }) } +function getRequestScheduler () { + return requestScheduler +} + +function getRequestVideoQaduScheduler () { + return requestVideoQaduScheduler +} + +function getRequestVideoEventScheduler () { + return requestVideoEventScheduler +} + // --------------------------------------------------------------------------- module.exports = friends @@ -345,13 +360,13 @@ function createRequest (options, callback) { function createVideoQaduRequest (options, callback) { if (!callback) callback = utils.createEmptyCallback() - requestSchedulerVideoQadu.createRequest(options, callback) + requestVideoQaduScheduler.createRequest(options, callback) } function createVideoEventRequest (options, callback) { if (!callback) callback = utils.createEmptyCallback() - requestSchedulerVideoEvent.createRequest(options, callback) + requestVideoEventScheduler.createRequest(options, callback) } function isMe (host) { -- cgit v1.2.3