]>
Commit | Line | Data |
---|---|---|
7f7e6c64 TL |
1 | import { Component, Input, NgZone, OnChanges, OnDestroy, OnInit } from '@angular/core'; |
2 | ||
3 | import { Subscription, timer as observableTimer } from 'rxjs'; | |
9f95a23c TL |
4 | |
5 | import { OrchestratorService } from '../../../shared/api/orchestrator.service'; | |
6 | import { Icons } from '../../../shared/enum/icons.enum'; | |
7 | import { InventoryDevice } from './inventory-devices/inventory-device.model'; | |
8 | ||
9 | @Component({ | |
10 | selector: 'cd-inventory', | |
11 | templateUrl: './inventory.component.html', | |
12 | styleUrls: ['./inventory.component.scss'] | |
13 | }) | |
7f7e6c64 | 14 | export class InventoryComponent implements OnChanges, OnInit, OnDestroy { |
9f95a23c TL |
15 | // Display inventory page only for this hostname, ignore to display all. |
16 | @Input() hostname?: string; | |
17 | ||
7f7e6c64 TL |
18 | private reloadSubscriber: Subscription; |
19 | private reloadInterval = 5000; | |
20 | private firstRefresh = true; | |
21 | ||
9f95a23c TL |
22 | icons = Icons; |
23 | ||
24 | hasOrchestrator = false; | |
adb31ebb | 25 | showDocPanel = false; |
9f95a23c TL |
26 | |
27 | devices: Array<InventoryDevice> = []; | |
28 | ||
7f7e6c64 | 29 | constructor(private orchService: OrchestratorService, private ngZone: NgZone) {} |
9f95a23c TL |
30 | |
31 | ngOnInit() { | |
32 | this.orchService.status().subscribe((status) => { | |
33 | this.hasOrchestrator = status.available; | |
adb31ebb | 34 | this.showDocPanel = !status.available; |
9f95a23c | 35 | if (status.available) { |
7f7e6c64 TL |
36 | // Create a timer to get cached inventory from the orchestrator. |
37 | // Do not ask the orchestrator frequently to refresh its cache data because it's expensive. | |
38 | this.ngZone.runOutsideAngular(() => { | |
39 | // start after first pass because the embedded table calls refresh at init. | |
40 | this.reloadSubscriber = observableTimer( | |
41 | this.reloadInterval, | |
42 | this.reloadInterval | |
43 | ).subscribe(() => { | |
44 | this.ngZone.run(() => { | |
45 | this.getInventory(false); | |
46 | }); | |
47 | }); | |
48 | }); | |
9f95a23c TL |
49 | } |
50 | }); | |
51 | } | |
52 | ||
7f7e6c64 TL |
53 | ngOnDestroy() { |
54 | if (this.reloadSubscriber) { | |
55 | this.reloadSubscriber.unsubscribe(); | |
56 | } | |
57 | } | |
58 | ||
9f95a23c TL |
59 | ngOnChanges() { |
60 | if (this.hasOrchestrator) { | |
61 | this.devices = []; | |
7f7e6c64 | 62 | this.getInventory(false); |
9f95a23c TL |
63 | } |
64 | } | |
65 | ||
7f7e6c64 | 66 | getInventory(refresh: boolean) { |
9f95a23c TL |
67 | if (this.hostname === '') { |
68 | return; | |
69 | } | |
7f7e6c64 | 70 | this.orchService.inventoryDeviceList(this.hostname, refresh).subscribe( |
9f95a23c TL |
71 | (devices: InventoryDevice[]) => { |
72 | this.devices = devices; | |
73 | }, | |
74 | () => { | |
75 | this.devices = []; | |
76 | } | |
77 | ); | |
78 | } | |
79 | ||
80 | refresh() { | |
7f7e6c64 TL |
81 | // Make the first reload (triggered by table) use cached data, and |
82 | // the remaining reloads (triggered by users) ask orchestrator to refresh inventory. | |
83 | this.getInventory(!this.firstRefresh); | |
84 | this.firstRefresh = false; | |
9f95a23c TL |
85 | } |
86 | } |