]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/requests/request-stats/request-stats.component.ts
Use typescript standard and lint all files
[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 constructor (
38 private notificationsService: NotificationsService,
39 private requestService: RequestService
40 ) { }
41
42 ngOnInit () {
43 this.getStats()
44 this.runIntervals()
45 }
46
47 ngOnDestroy () {
48 Object.keys(this.stats).forEach(requestSchedulerName => {
49 if (this.intervals[requestSchedulerName] !== null) {
50 window.clearInterval(this.intervals[requestSchedulerName])
51 }
52
53 if (this.timeouts[requestSchedulerName] !== null) {
54 window.clearTimeout(this.timeouts[requestSchedulerName])
55 }
56 })
57 }
58
59 getStats () {
60 this.requestService.getStats().subscribe(
61 stats => this.stats = stats,
62
63 err => this.notificationsService.error('Error', err.text)
64 )
65 }
66
67 private runIntervals () {
68 Object.keys(this.intervals).forEach(requestSchedulerName => {
69 this.intervals[requestSchedulerName] = window.setInterval(() => {
70 const stats = this.stats[requestSchedulerName]
71
72 stats.remainingMilliSeconds -= 1000
73
74 if (stats.remainingMilliSeconds <= 0) {
75 this.timeouts[requestSchedulerName] = window.setTimeout(() => this.getStats(), stats.remainingMilliSeconds + 100)
76 }
77 }, 1000)
78 })
79 }
80 }