Skip to content

Commit e8f359b

Browse files
committed
fix: some refactoring
1 parent 78cf654 commit e8f359b

File tree

2 files changed

+163
-15
lines changed

2 files changed

+163
-15
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import type {TNodeInfo} from '../../../../types/api/nodes';
2+
import {TPDiskState} from '../../../../types/api/pdisk';
3+
import {calculateMaximumDisksPerNode} from '../utils';
4+
5+
describe('calculateMaximumDisksPerNode', () => {
6+
it('should return providedMaximumDisksPerNode when it is provided', () => {
7+
const nodes: TNodeInfo[] = [];
8+
const providedMaximumDisksPerNode = '5';
9+
10+
expect(calculateMaximumDisksPerNode(nodes, providedMaximumDisksPerNode)).toBe('5');
11+
});
12+
13+
it('should return "1" for empty nodes array', () => {
14+
const nodes: TNodeInfo[] = [];
15+
16+
expect(calculateMaximumDisksPerNode(nodes)).toBe('1');
17+
});
18+
19+
it('should return "1" for undefined nodes', () => {
20+
expect(calculateMaximumDisksPerNode(undefined)).toBe('1');
21+
});
22+
23+
it('should return "1" for nodes without PDisks', () => {
24+
const nodes: TNodeInfo[] = [
25+
{
26+
NodeId: 1,
27+
SystemState: {},
28+
},
29+
];
30+
31+
expect(calculateMaximumDisksPerNode(nodes)).toBe('1');
32+
});
33+
34+
it('should calculate maximum disks correctly for single node with multiple PDisks', () => {
35+
const nodes: TNodeInfo[] = [
36+
{
37+
NodeId: 1,
38+
SystemState: {},
39+
PDisks: [
40+
{
41+
PDiskId: 1,
42+
State: TPDiskState.Normal,
43+
},
44+
{
45+
PDiskId: 2,
46+
State: TPDiskState.Normal,
47+
},
48+
{
49+
PDiskId: 3,
50+
State: TPDiskState.Normal,
51+
},
52+
],
53+
},
54+
];
55+
56+
expect(calculateMaximumDisksPerNode(nodes)).toBe('3');
57+
});
58+
59+
it('should calculate maximum disks across multiple nodes', () => {
60+
const nodes: TNodeInfo[] = [
61+
{
62+
NodeId: 1,
63+
SystemState: {},
64+
PDisks: [
65+
{
66+
PDiskId: 1,
67+
State: TPDiskState.Normal,
68+
},
69+
],
70+
},
71+
{
72+
NodeId: 2,
73+
SystemState: {},
74+
PDisks: [
75+
{
76+
PDiskId: 2,
77+
State: TPDiskState.Normal,
78+
},
79+
{
80+
PDiskId: 3,
81+
State: TPDiskState.Normal,
82+
},
83+
],
84+
},
85+
{
86+
NodeId: 3,
87+
SystemState: {},
88+
PDisks: [
89+
{
90+
PDiskId: 4,
91+
State: TPDiskState.Normal,
92+
},
93+
{
94+
PDiskId: 5,
95+
State: TPDiskState.Normal,
96+
},
97+
{
98+
PDiskId: 6,
99+
State: TPDiskState.Normal,
100+
},
101+
{
102+
PDiskId: 7,
103+
State: TPDiskState.Normal,
104+
},
105+
],
106+
},
107+
];
108+
109+
expect(calculateMaximumDisksPerNode(nodes)).toBe('4');
110+
});
111+
112+
it('should handle nodes with empty PDisks array', () => {
113+
const nodes: TNodeInfo[] = [
114+
{
115+
NodeId: 1,
116+
SystemState: {},
117+
PDisks: [],
118+
},
119+
{
120+
NodeId: 2,
121+
SystemState: {},
122+
PDisks: [
123+
{
124+
PDiskId: 1,
125+
State: TPDiskState.Normal,
126+
},
127+
{
128+
PDiskId: 2,
129+
State: TPDiskState.Normal,
130+
},
131+
],
132+
},
133+
];
134+
135+
expect(calculateMaximumDisksPerNode(nodes)).toBe('2');
136+
});
137+
});

src/store/reducers/storage/utils.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -223,37 +223,48 @@ const prepareStorageNodeData = (
223223
};
224224
};
225225

226+
/**
227+
* Calculates the maximum number of VDisk slots per PDisk across all nodes
228+
* A slot represents a VDisk that can be allocated to a PDisk
229+
*/
226230
export const calculateMaximumSlotsPerDisk = (
227231
nodes: TNodeInfo[] | undefined,
228232
providedMaximumSlotsPerDisk?: string,
229-
) => {
233+
): string => {
230234
if (providedMaximumSlotsPerDisk) {
231235
return providedMaximumSlotsPerDisk;
232236
}
233237

234-
return String(
235-
Math.max(
236-
1,
237-
...(nodes || []).flatMap((node) =>
238-
(node.PDisks || []).map(
239-
(pDisk) =>
240-
(node.VDisks || []).filter((vDisk) => vDisk.PDiskId === pDisk.PDiskId)
241-
.length || 0,
242-
),
243-
),
244-
),
245-
);
238+
const safeNodes = nodes || [];
239+
const slotsPerDiskCounts = safeNodes.flatMap((node) => {
240+
const safePDisks = node.PDisks || [];
241+
const safeVDisks = node.VDisks || [];
242+
243+
return safePDisks.map((pDisk) => {
244+
const vDisksOnPDisk = safeVDisks.filter((vDisk) => vDisk.PDiskId === pDisk.PDiskId);
245+
return vDisksOnPDisk.length || 0;
246+
});
247+
});
248+
249+
const maxSlots = Math.max(1, ...slotsPerDiskCounts);
250+
return String(maxSlots);
246251
};
247252

253+
/**
254+
* Calculates the maximum number of PDisks per node across all nodes
255+
*/
248256
export const calculateMaximumDisksPerNode = (
249257
nodes: TNodeInfo[] | undefined,
250258
providedMaximumDisksPerNode?: string,
251-
) => {
259+
): string => {
252260
if (providedMaximumDisksPerNode) {
253261
return providedMaximumDisksPerNode;
254262
}
255263

256-
return String(Math.max(1, ...(nodes || []).map((node) => node.PDisks?.length || 0)));
264+
const safeNodes = nodes || [];
265+
const disksPerNodeCounts = safeNodes.map((node) => node.PDisks?.length || 0);
266+
const maxDisks = Math.max(1, ...disksPerNodeCounts);
267+
return String(maxDisks);
257268
};
258269

259270
// ==== Prepare responses ====

0 commit comments

Comments
 (0)