-import { setInterval } from 'timers'
-import { Component, OnInit, OnDestroy } from '@angular/core';
+import { Component, OnInit, OnDestroy } from '@angular/core'
-import { RequestService, RequestStats } from '../shared';
+import { NotificationsService } from 'angular2-notifications'
+
+import { RequestService, RequestStats } from '../shared'
@Component({
- selector: 'my-request-stats',
- templateUrl: './request-stats.component.html',
+ selector: 'my-request-stats',
+ templateUrl: './request-stats.component.html',
styleUrls: [ './request-stats.component.scss' ]
})
export class RequestStatsComponent implements OnInit, OnDestroy {
- stats: RequestStats = null;
-
- private interval: NodeJS.Timer = null;
+ statsTitles = {
+ requestScheduler: 'Basic request scheduler',
+ requestVideoEventScheduler: 'Video events request scheduler',
+ requestVideoQaduScheduler: 'Quick and dirty video updates request scheduler'
+ }
- constructor(private requestService: RequestService) { }
+ stats: { [ id: string ]: RequestStats } = {
+ requestScheduler: null,
+ requestVideoEventScheduler: null,
+ requestVideoQaduScheduler: null
+ }
- ngOnInit() {
- this.getStats();
- this.runInterval();
+ private intervals: { [ id: string ]: number } = {
+ requestScheduler: null,
+ requestVideoEventScheduler: null,
+ requestVideoQaduScheduler: null
}
- ngOnDestroy() {
- if (this.stats !== null && this.stats.secondsInterval !== null) {
- clearInterval(this.interval);
- }
+ private timeouts: { [ id: string ]: number } = {
+ requestScheduler: null,
+ requestVideoEventScheduler: null,
+ requestVideoQaduScheduler: null
}
- getStats() {
- this.requestService.getStats().subscribe(
- stats => this.stats = stats,
+ constructor (
+ private notificationsService: NotificationsService,
+ private requestService: RequestService
+ ) { }
- err => alert(err.text)
- );
+ ngOnInit () {
+ this.getStats()
+ this.runIntervals()
}
- private runInterval() {
- this.interval = setInterval(() => {
- this.stats.remainingMilliSeconds -= 1000;
+ ngOnDestroy () {
+ Object.keys(this.stats).forEach(requestSchedulerName => {
+ if (this.intervals[requestSchedulerName] !== null) {
+ window.clearInterval(this.intervals[requestSchedulerName])
+ }
- if (this.stats.remainingMilliSeconds <= 0) {
- setTimeout(() => this.getStats(), this.stats.remainingMilliSeconds + 100);
+ if (this.timeouts[requestSchedulerName] !== null) {
+ window.clearTimeout(this.timeouts[requestSchedulerName])
}
- }, 1000);
+ })
}
+ getStats () {
+ this.requestService.getStats().subscribe(
+ stats => this.stats = stats,
+
+ err => this.notificationsService.error('Error', err.text)
+ )
+ }
+ private runIntervals () {
+ Object.keys(this.intervals).forEach(requestSchedulerName => {
+ this.intervals[requestSchedulerName] = window.setInterval(() => {
+ const stats = this.stats[requestSchedulerName]
+
+ stats.remainingMilliSeconds -= 1000
+
+ if (stats.remainingMilliSeconds <= 0) {
+ this.timeouts[requestSchedulerName] = window.setTimeout(() => this.getStats(), stats.remainingMilliSeconds + 100)
+ }
+ }, 1000)
+ })
+ }
}