]>
Commit | Line | Data |
---|---|---|
9f95a23c | 1 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'; |
a4b75251 | 2 | import { Router } from '@angular/router'; |
9f95a23c | 3 | |
f67539c2 | 4 | import _ from 'lodash'; |
9f95a23c | 5 | |
f67539c2 | 6 | import { InventoryDevice } from '~/app/ceph/cluster/inventory/inventory-devices/inventory-device.model'; |
a4b75251 | 7 | import { OsdService } from '~/app/shared/api/osd.service'; |
f67539c2 TL |
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'; | |
9f95a23c TL |
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'; | |
14 | ||
15 | @Component({ | |
16 | selector: 'cd-osd-devices-selection-groups', | |
17 | templateUrl: './osd-devices-selection-groups.component.html', | |
18 | styleUrls: ['./osd-devices-selection-groups.component.scss'] | |
19 | }) | |
20 | export class OsdDevicesSelectionGroupsComponent implements OnInit, OnChanges { | |
21 | // data, wal, db | |
22 | @Input() type: string; | |
23 | ||
24 | // Data, WAL, DB | |
25 | @Input() name: string; | |
26 | ||
27 | @Input() hostname: string; | |
28 | ||
29 | @Input() availDevices: InventoryDevice[]; | |
30 | ||
31 | @Input() canSelect: boolean; | |
32 | ||
33 | @Output() | |
34 | selected = new EventEmitter<DevicesSelectionChangeEvent>(); | |
35 | ||
36 | @Output() | |
37 | cleared = new EventEmitter<DevicesSelectionClearEvent>(); | |
38 | ||
39 | icons = Icons; | |
40 | devices: InventoryDevice[] = []; | |
41 | capacity = 0; | |
a4b75251 TL |
42 | appliedFilters = new Array(); |
43 | expansionCanSelect = false; | |
44 | isOsdPage: boolean; | |
9f95a23c TL |
45 | |
46 | addButtonTooltip: String; | |
47 | tooltips = { | |
f67539c2 TL |
48 | noAvailDevices: $localize`No available devices`, |
49 | addPrimaryFirst: $localize`Please add primary devices first`, | |
50 | addByFilters: $localize`Add devices by using filters` | |
9f95a23c TL |
51 | }; |
52 | ||
a4b75251 TL |
53 | constructor( |
54 | private modalService: ModalService, | |
55 | public osdService: OsdService, | |
56 | private router: Router | |
57 | ) { | |
58 | this.isOsdPage = this.router.url.includes('/osd'); | |
59 | } | |
9f95a23c TL |
60 | |
61 | ngOnInit() { | |
a4b75251 TL |
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); | |
70 | } | |
9f95a23c TL |
71 | this.updateAddButtonTooltip(); |
72 | } | |
73 | ||
74 | ngOnChanges() { | |
75 | this.updateAddButtonTooltip(); | |
76 | } | |
77 | ||
78 | showSelectionModal() { | |
39ae355f TL |
79 | const filterColumns = [ |
80 | 'hostname', | |
81 | 'human_readable_type', | |
82 | 'sys_api.vendor', | |
83 | 'sys_api.model', | |
84 | 'sys_api.size' | |
85 | ]; | |
86 | const diskType = this.name === 'Primary' ? 'hdd' : 'ssd'; | |
f67539c2 TL |
87 | const initialState = { |
88 | hostname: this.hostname, | |
89 | deviceType: this.name, | |
39ae355f | 90 | diskType: diskType, |
f67539c2 TL |
91 | devices: this.availDevices, |
92 | filterColumns: filterColumns | |
9f95a23c | 93 | }; |
f67539c2 TL |
94 | const modalRef = this.modalService.show(OsdDevicesSelectionModalComponent, initialState, { |
95 | size: 'xl' | |
96 | }); | |
97 | modalRef.componentInstance.submitAction.subscribe((result: CdTableColumnFiltersChange) => { | |
9f95a23c TL |
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); | |
a4b75251 TL |
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; | |
107 | } | |
9f95a23c TL |
108 | this.selected.emit(event); |
109 | }); | |
110 | } | |
111 | ||
112 | private updateAddButtonTooltip() { | |
113 | if (this.type === 'data' && this.availDevices.length === 0) { | |
114 | this.addButtonTooltip = this.tooltips.noAvailDevices; | |
115 | } else { | |
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; | |
121 | } else { | |
122 | this.addButtonTooltip = this.tooltips.addByFilters; | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
127 | clearDevices() { | |
a4b75251 TL |
128 | if (!this.isOsdPage) { |
129 | this.expansionCanSelect = false; | |
130 | this.osdService.osdDevices['disableSelect'] = false; | |
131 | this.osdService.osdDevices = []; | |
132 | } | |
9f95a23c TL |
133 | const event = { |
134 | type: this.type, | |
135 | clearedDevices: [...this.devices] | |
136 | }; | |
137 | this.devices = []; | |
138 | this.cleared.emit(event); | |
139 | } | |
140 | } |