1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core';
2 import { Router } from '@angular/router';
4 import _ from 'lodash';
6 import { InventoryDevice } from '~/app/ceph/cluster/inventory/inventory-devices/inventory-device.model';
7 import { OsdService } from '~/app/shared/api/osd.service';
8 import { Icons } from '~/app/shared/enum/icons.enum';
9 import { CdTableColumnFiltersChange } from '~/app/shared/models/cd-table-column-filters-change';
10 import { ModalService } from '~/app/shared/services/modal.service';
11 import { OsdDevicesSelectionModalComponent } from '../osd-devices-selection-modal/osd-devices-selection-modal.component';
12 import { DevicesSelectionChangeEvent } from './devices-selection-change-event.interface';
13 import { DevicesSelectionClearEvent } from './devices-selection-clear-event.interface';
16 selector: 'cd-osd-devices-selection-groups',
17 templateUrl: './osd-devices-selection-groups.component.html',
18 styleUrls: ['./osd-devices-selection-groups.component.scss']
20 export class OsdDevicesSelectionGroupsComponent implements OnInit, OnChanges {
22 @Input() type: string;
25 @Input() name: string;
27 @Input() hostname: string;
29 @Input() availDevices: InventoryDevice[];
31 @Input() canSelect: boolean;
34 selected = new EventEmitter<DevicesSelectionChangeEvent>();
37 cleared = new EventEmitter<DevicesSelectionClearEvent>();
40 devices: InventoryDevice[] = [];
42 appliedFilters = new Array();
43 expansionCanSelect = false;
46 addButtonTooltip: String;
48 noAvailDevices: $localize`No available devices`,
49 addPrimaryFirst: $localize`Please add primary devices first`,
50 addByFilters: $localize`Add devices by using filters`
54 private modalService: ModalService,
55 public osdService: OsdService,
56 private router: Router
58 this.isOsdPage = this.router.url.includes('/osd');
62 if (!this.isOsdPage) {
63 this.osdService?.osdDevices[this.type]
64 ? (this.devices = this.osdService.osdDevices[this.type])
65 : (this.devices = []);
66 this.capacity = _.sumBy(this.devices, 'sys_api.size');
67 this.osdService?.osdDevices
68 ? (this.expansionCanSelect = this.osdService?.osdDevices['disableSelect'])
69 : (this.expansionCanSelect = false);
71 this.updateAddButtonTooltip();
75 this.updateAddButtonTooltip();
78 showSelectionModal() {
79 const filterColumns = [
81 'human_readable_type',
86 const diskType = this.name === 'Primary' ? 'hdd' : 'ssd';
87 const initialState = {
88 hostname: this.hostname,
89 deviceType: this.name,
91 devices: this.availDevices,
92 filterColumns: filterColumns
94 const modalRef = this.modalService.show(OsdDevicesSelectionModalComponent, initialState, {
97 modalRef.componentInstance.submitAction.subscribe((result: CdTableColumnFiltersChange) => {
98 this.devices = result.data;
99 this.capacity = _.sumBy(this.devices, 'sys_api.size');
100 this.appliedFilters = result.filters;
101 const event = _.assign({ type: this.type }, result);
102 if (!this.isOsdPage) {
103 this.osdService.osdDevices[this.type] = this.devices;
104 this.osdService.osdDevices['disableSelect'] =
105 this.canSelect || this.devices.length === this.availDevices.length;
106 this.osdService.osdDevices[this.type]['capacity'] = this.capacity;
108 this.selected.emit(event);
112 private updateAddButtonTooltip() {
113 if (this.type === 'data' && this.availDevices.length === 0) {
114 this.addButtonTooltip = this.tooltips.noAvailDevices;
116 if (!this.canSelect) {
117 // No primary devices added yet.
118 this.addButtonTooltip = this.tooltips.addPrimaryFirst;
119 } else if (this.availDevices.length === 0) {
120 this.addButtonTooltip = this.tooltips.noAvailDevices;
122 this.addButtonTooltip = this.tooltips.addByFilters;
128 if (!this.isOsdPage) {
129 this.expansionCanSelect = false;
130 this.osdService.osdDevices['disableSelect'] = false;
131 this.osdService.osdDevices = [];
135 clearedDevices: [...this.devices]
138 this.cleared.emit(event);