]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/requests/request-stats/request-stats.component.ts
Client: better notifications for a beautiful world
[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 stats: RequestStats = null;
14
15 private interval: number = null;
16 private timeout: number = null;
17
18 constructor(
19 private notificationsService: NotificationsService,
20 private requestService: RequestService
21 ) { }
22
23 ngOnInit() {
24 this.getStats();
25 this.runInterval();
26 }
27
28 ngOnDestroy() {
29 if (this.interval !== null) {
30 window.clearInterval(this.interval);
31 }
32
33 if (this.timeout !== null) {
34 window.clearTimeout(this.timeout);
35 }
36 }
37
38 getStats() {
39 this.requestService.getStats().subscribe(
40 stats => this.stats = stats,
41
42 err => this.notificationsService.error('Error', err.text)
43 );
44 }
45
46 private runInterval() {
47 this.interval = window.setInterval(() => {
48 this.stats.remainingMilliSeconds -= 1000;
49
50 if (this.stats.remainingMilliSeconds <= 0) {
51 this.timeout = window.setTimeout(() => this.getStats(), this.stats.remainingMilliSeconds + 100);
52 }
53 }, 1000);
54 }
55
56
57 }