]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | import * as _ from 'lodash'; |
2 | ||
3 | import { CdTableColumnFiltersChange } from '../../../../shared/models/cd-table-column-filters-change'; | |
4 | import { FormatterService } from '../../../../shared/services/formatter.service'; | |
5 | ||
6 | export class DriveGroup { | |
7 | spec: Object; | |
8 | ||
9 | // Map from filter column prop to device selection attribute name | |
10 | private deviceSelectionAttrs: { | |
11 | [key: string]: { | |
12 | name: string; | |
13 | formatter?: Function; | |
14 | }; | |
15 | }; | |
16 | ||
17 | private formatterService: FormatterService; | |
18 | ||
19 | constructor() { | |
20 | this.reset(); | |
21 | this.formatterService = new FormatterService(); | |
22 | this.deviceSelectionAttrs = { | |
23 | 'sys_api.vendor': { | |
24 | name: 'vendor' | |
25 | }, | |
26 | 'sys_api.model': { | |
27 | name: 'model' | |
28 | }, | |
29 | device_id: { | |
30 | name: 'device_id' | |
31 | }, | |
32 | human_readable_type: { | |
33 | name: 'rotational', | |
34 | formatter: (value: string) => { | |
35 | return value.toLowerCase() === 'hdd'; | |
36 | } | |
37 | }, | |
38 | 'sys_api.size': { | |
39 | name: 'size', | |
40 | formatter: (value: string) => { | |
41 | return this.formatterService | |
42 | .format_number(value, 1024, ['B', 'KB', 'MB', 'GB', 'TB', 'PB']) | |
43 | .replace(' ', ''); | |
44 | } | |
45 | } | |
46 | }; | |
47 | } | |
48 | ||
49 | reset() { | |
50 | this.spec = { | |
51 | service_type: 'osd', | |
52 | service_id: `dashboard-${_.now()}` | |
53 | }; | |
54 | } | |
55 | ||
56 | setName(name: string) { | |
57 | this.spec['service_id'] = name; | |
58 | } | |
59 | ||
60 | setHostPattern(pattern: string) { | |
61 | this.spec['host_pattern'] = pattern; | |
62 | } | |
63 | ||
64 | setDeviceSelection(type: string, appliedFilters: CdTableColumnFiltersChange['filters']) { | |
65 | const key = `${type}_devices`; | |
66 | this.spec[key] = {}; | |
67 | appliedFilters.forEach((filter) => { | |
68 | const attr = this.deviceSelectionAttrs[filter.prop]; | |
69 | if (attr) { | |
70 | const name = attr.name; | |
71 | this.spec[key][name] = attr.formatter ? attr.formatter(filter.value.raw) : filter.value.raw; | |
72 | } | |
73 | }); | |
74 | } | |
75 | ||
76 | clearDeviceSelection(type: string) { | |
77 | const key = `${type}_devices`; | |
78 | delete this.spec[key]; | |
79 | } | |
80 | ||
81 | setSlots(type: string, slots: number) { | |
82 | const key = `${type}_slots`; | |
83 | if (slots === 0) { | |
84 | delete this.spec[key]; | |
85 | } else { | |
86 | this.spec[key] = slots; | |
87 | } | |
88 | } | |
89 | ||
90 | setFeature(feature: string, enabled: boolean) { | |
91 | if (enabled) { | |
92 | this.spec[feature] = true; | |
93 | } else { | |
94 | delete this.spec[feature]; | |
95 | } | |
96 | } | |
97 | } |