aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/admin/requests/request-stats/request-stats.component.ts
blob: 8ff4eb540d5a4a9110a7bb64dbfdb8218b29f7fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Component, OnInit, OnDestroy } from '@angular/core';

import { RequestService, RequestStats } from '../shared';

@Component({
	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;

  constructor(private requestService: RequestService) {  }

  ngOnInit() {
    this.getStats();
  }

  ngOnDestroy() {
    if (this.secondsInterval !== null) {
      clearInterval(this.interval);
    }
  }

  get remainingSeconds() {
    return Math.floor(this.stats.remainingMilliSeconds / 1000);
  }

  get secondsInterval() {
    return Math.floor(this.stats.milliSecondsInterval / 1000);
  }

  getStats() {
    this.requestService.getStats().subscribe(
      stats => {
        console.log(stats);
        this.stats = stats;
        this.runInterval();
      },

      err => alert(err)
    );
  }

  private runInterval() {
    this.interval = setInterval(() => {
      this.stats.remainingMilliSeconds -= 1000;

      if (this.stats.remainingMilliSeconds <= 0) {
        setTimeout(() => this.getStats(), this.stats.remainingMilliSeconds + 100);
        clearInterval(this.interval);
      }
    }, 1000);
  }


}