diff options
Diffstat (limited to 'client/src/app/admin/requests/request-stats/request-stats.component.ts')
-rw-r--r-- | client/src/app/admin/requests/request-stats/request-stats.component.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/client/src/app/admin/requests/request-stats/request-stats.component.ts b/client/src/app/admin/requests/request-stats/request-stats.component.ts new file mode 100644 index 000000000..8ff4eb540 --- /dev/null +++ b/client/src/app/admin/requests/request-stats/request-stats.component.ts | |||
@@ -0,0 +1,59 @@ | |||
1 | import { Component, OnInit, OnDestroy } from '@angular/core'; | ||
2 | |||
3 | import { RequestService, RequestStats } from '../shared'; | ||
4 | |||
5 | @Component({ | ||
6 | selector: 'my-request-stats', | ||
7 | templateUrl: './request-stats.component.html', | ||
8 | styleUrls: [ './request-stats.component.scss' ] | ||
9 | }) | ||
10 | export class RequestStatsComponent implements OnInit, OnDestroy { | ||
11 | stats: RequestStats = null; | ||
12 | |||
13 | private interval: NodeJS.Timer = null; | ||
14 | |||
15 | constructor(private requestService: RequestService) { } | ||
16 | |||
17 | ngOnInit() { | ||
18 | this.getStats(); | ||
19 | } | ||
20 | |||
21 | ngOnDestroy() { | ||
22 | if (this.secondsInterval !== null) { | ||
23 | clearInterval(this.interval); | ||
24 | } | ||
25 | } | ||
26 | |||
27 | get remainingSeconds() { | ||
28 | return Math.floor(this.stats.remainingMilliSeconds / 1000); | ||
29 | } | ||
30 | |||
31 | get secondsInterval() { | ||
32 | return Math.floor(this.stats.milliSecondsInterval / 1000); | ||
33 | } | ||
34 | |||
35 | getStats() { | ||
36 | this.requestService.getStats().subscribe( | ||
37 | stats => { | ||
38 | console.log(stats); | ||
39 | this.stats = stats; | ||
40 | this.runInterval(); | ||
41 | }, | ||
42 | |||
43 | err => alert(err) | ||
44 | ); | ||
45 | } | ||
46 | |||
47 | private runInterval() { | ||
48 | this.interval = setInterval(() => { | ||
49 | this.stats.remainingMilliSeconds -= 1000; | ||
50 | |||
51 | if (this.stats.remainingMilliSeconds <= 0) { | ||
52 | setTimeout(() => this.getStats(), this.stats.remainingMilliSeconds + 100); | ||
53 | clearInterval(this.interval); | ||
54 | } | ||
55 | }, 1000); | ||
56 | } | ||
57 | |||
58 | |||
59 | } | ||