9 } from '@angular/core';
10 import { I18n } from '@ngx-translate/i18n-polyfill';
12 import * as _ from 'lodash';
13 import { BsModalService } from 'ngx-bootstrap/modal';
14 import { Subscription } from 'rxjs';
16 import { OrchestratorService } from '../../../../shared/api/orchestrator.service';
17 import { FormModalComponent } from '../../../../shared/components/form-modal/form-modal.component';
18 import { TableComponent } from '../../../../shared/datatable/table/table.component';
19 import { CellTemplate } from '../../../../shared/enum/cell-template.enum';
20 import { Icons } from '../../../../shared/enum/icons.enum';
21 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
22 import { CdTableAction } from '../../../../shared/models/cd-table-action';
23 import { CdTableColumn } from '../../../../shared/models/cd-table-column';
24 import { CdTableColumnFiltersChange } from '../../../../shared/models/cd-table-column-filters-change';
25 import { CdTableSelection } from '../../../../shared/models/cd-table-selection';
26 import { Permission } from '../../../../shared/models/permissions';
27 import { DimlessBinaryPipe } from '../../../../shared/pipes/dimless-binary.pipe';
28 import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
29 import { NotificationService } from '../../../../shared/services/notification.service';
30 import { InventoryDevice } from './inventory-device.model';
33 selector: 'cd-inventory-devices',
34 templateUrl: './inventory-devices.component.html',
35 styleUrls: ['./inventory-devices.component.scss']
37 export class InventoryDevicesComponent implements OnInit, OnDestroy {
38 @ViewChild(TableComponent, { static: true })
39 table: TableComponent;
42 @Input() devices: InventoryDevice[] = [];
44 // Do not display these columns
45 @Input() hiddenColumns: string[] = [];
47 // Show filters for these columns, specify empty array to disable
48 @Input() filterColumns = [
50 'human_readable_type',
57 // Device table row selection type
58 @Input() selectionType: string = undefined;
60 @Output() filterChange = new EventEmitter<CdTableColumnFiltersChange>();
62 @Output() fetchInventory = new EventEmitter();
65 columns: Array<CdTableColumn> = [];
66 selection: CdTableSelection = new CdTableSelection();
67 permission: Permission;
68 tableActions: CdTableAction[];
69 fetchInventorySub: Subscription;
72 private authStorageService: AuthStorageService,
73 private dimlessBinary: DimlessBinaryPipe,
75 private modalService: BsModalService,
76 private notificationService: NotificationService,
77 private orchService: OrchestratorService
81 this.permission = this.authStorageService.getPermissions().osd;
86 click: () => this.identifyDevice(),
87 name: this.i18n('Identify'),
88 disable: () => !this.selection.hasSingleSelection,
89 canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection,
90 visible: () => _.isString(this.selectionType)
95 name: this.i18n('Hostname'),
100 name: this.i18n('Device path'),
105 name: this.i18n('Type'),
106 prop: 'human_readable_type',
108 cellTransformation: CellTemplate.badge,
109 customTemplateConfig: {
111 hdd: { value: 'HDD', class: 'badge-hdd' },
112 ssd: { value: 'SSD', class: 'badge-ssd' }
117 name: this.i18n('Available'),
120 cellClass: 'text-center',
121 cellTransformation: CellTemplate.checkIcon
124 name: this.i18n('Vendor'),
125 prop: 'sys_api.vendor',
129 name: this.i18n('Model'),
130 prop: 'sys_api.model',
134 name: this.i18n('Size'),
135 prop: 'sys_api.size',
137 pipe: this.dimlessBinary
140 name: this.i18n('OSDs'),
143 cellTransformation: CellTemplate.badge,
144 customTemplateConfig: {
151 this.columns = columns.filter((col: any) => {
152 return !this.hiddenColumns.includes(col.prop);
155 // init column filters
156 _.forEach(this.filterColumns, (prop) => {
157 const col = _.find(this.columns, { prop: prop });
159 col.filterable = true;
163 if (this.fetchInventory.observers.length > 0) {
164 this.fetchInventorySub = this.table.fetchData.subscribe(() => {
165 this.fetchInventory.emit();
171 if (this.fetchInventorySub) {
172 this.fetchInventorySub.unsubscribe();
176 onColumnFiltersChanged(event: CdTableColumnFiltersChange) {
177 this.filterChange.emit(event);
180 updateSelection(selection: CdTableSelection) {
181 this.selection = selection;
185 const selected = this.selection.first();
186 const hostname = selected.hostname;
187 const device = selected.path || selected.device_id;
188 this.modalService.show(FormModalComponent, {
190 titleText: this.i18n(`Identify device {{device}}`, { device }),
191 message: this.i18n('Please enter the duration how long to blink the LED.'),
200 { text: this.i18n('1 minute'), value: 60 },
201 { text: this.i18n('2 minutes'), value: 120 },
202 { text: this.i18n('5 minutes'), value: 300 },
203 { text: this.i18n('10 minutes'), value: 600 },
204 { text: this.i18n('15 minutes'), value: 900 }
209 submitButtonText: this.i18n('Execute'),
210 onSubmit: (values: any) => {
211 this.orchService.identifyDevice(hostname, device, values.duration).subscribe(() => {
212 this.notificationService.show(
213 NotificationType.success,
214 this.i18n(`Identifying '{{device}}' started on host '{{hostname}}'`, {