]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/requests/request-stats/request-stats.component.ts
85dd7e492918d733d8cc4d18af75dc8cf4e33485
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / requests / request-stats / request-stats.component.ts
1 import { Component, OnInit, OnDestroy } from '@angular/core';
2
3 import { NotificationsService } from 'angular2-notifications';
4
5 import { RequestService, RequestStats } from '../shared';
6
7 @Component({
8 selector: 'my-request-stats',
9 templateUrl: './request-stats.component.html',
10 styleUrls: [ './request-stats.component.scss' ]
11 })
12 export class RequestStatsComponent implements OnInit, OnDestroy {
13 statsTitles = {
14 requestScheduler: 'Basic request scheduler',
15 requestVideoEventScheduler: 'Video events request scheduler',
16 requestVideoQaduScheduler: 'Quick and dirty video updates request scheduler'
17 };
18
19 stats: { [ id: string ]: RequestStats } = {
20 requestScheduler: null,
21 requestVideoEventScheduler: null,
22 requestVideoQaduScheduler: null
23 };
24
25 private intervals: { [ id: string ]: number } = {
26 requestScheduler: null,
27 requestVideoEventScheduler: null,
28 requestVideoQaduScheduler: null
29 };
30
31 private timeouts: { [ id: string ]: number } = {
32 requestScheduler: null,
33 requestVideoEventScheduler: null,
34 requestVideoQaduScheduler: null
35 };
36
37
38 constructor(
39 private notificationsService: NotificationsService,
40 private requestService: RequestService
41 ) { }
42
43 ngOnInit() {
44 this.getStats();
45 this.runIntervals();
46 }
47
48 ngOnDestroy() {
49 Object.keys(this.stats).forEach(requestSchedulerName => {
50 if (this.intervals[requestSchedulerName] !== null) {
51 window.clearInterval(this.intervals[requestSchedulerName]);
52 }
53
54 if (this.timeouts[requestSchedulerName] !== null) {
55 window.clearTimeout(this.timeouts[requestSchedulerName]);
56 }
57 });
58 }
59
60 getStats() {
61 this.requestService.getStats().subscribe(
62 stats => this.stats = stats,
63
64 err => this.notificationsService.error('Error', err.text)
65 );
66 }
67
68 private runIntervals() {
69 Object.keys(this.intervals).forEach(requestSchedulerName => {
70 this.intervals[requestSchedulerName] = window.setInterval(() => {
71 const stats = this.stats[requestSchedulerName];
72
73 stats.remainingMilliSeconds -= 1000;
74
75 if (stats.remainingMilliSeconds <= 0) {
76 this.timeouts[requestSchedulerName] = window.setTimeout(() => this.getStats(), stats.remainingMilliSeconds + 100);
77 }
78 }, 1000);
79 });
80 }
81
82
83 }