1 import { Component, Input, NgZone, OnChanges, OnDestroy, OnInit } from '@angular/core';
3 import { Subscription, timer as observableTimer } from 'rxjs';
5 import { HostService } from '~/app/shared/api/host.service';
6 import { OrchestratorService } from '~/app/shared/api/orchestrator.service';
7 import { Icons } from '~/app/shared/enum/icons.enum';
8 import { OrchestratorStatus } from '~/app/shared/models/orchestrator.interface';
9 import { InventoryDevice } from './inventory-devices/inventory-device.model';
12 selector: 'cd-inventory',
13 templateUrl: './inventory.component.html',
14 styleUrls: ['./inventory.component.scss']
16 export class InventoryComponent implements OnChanges, OnInit, OnDestroy {
17 // Display inventory page only for this hostname, ignore to display all.
18 @Input() hostname?: string;
20 private reloadSubscriber: Subscription;
21 private reloadInterval = 5000;
22 private firstRefresh = true;
26 orchStatus: OrchestratorStatus;
29 devices: Array<InventoryDevice> = [];
32 private orchService: OrchestratorService,
33 private hostService: HostService,
34 private ngZone: NgZone
38 this.orchService.status().subscribe((status) => {
39 this.orchStatus = status;
40 this.showDocPanel = !status.available;
41 if (status.available) {
42 // Create a timer to get cached inventory from the orchestrator.
43 // Do not ask the orchestrator frequently to refresh its cache data because it's expensive.
44 this.ngZone.runOutsideAngular(() => {
45 // start after first pass because the embedded table calls refresh at init.
46 this.reloadSubscriber = observableTimer(
50 this.ngZone.run(() => {
51 this.getInventory(false);
60 this.reloadSubscriber?.unsubscribe();
64 if (this.orchStatus?.available) {
66 this.getInventory(false);
70 getInventory(refresh: boolean) {
71 if (this.hostname === '') {
74 this.hostService.inventoryDeviceList(this.hostname, refresh).subscribe(
75 (devices: InventoryDevice[]) => {
76 this.devices = devices;
85 // Make the first reload (triggered by table) use cached data, and
86 // the remaining reloads (triggered by users) ask orchestrator to refresh inventory.
87 this.getInventory(!this.firstRefresh);
88 this.firstRefresh = false;