Skip to content

Commit

Permalink
fix: some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
astandrik committed Dec 27, 2024
1 parent 78cf654 commit e8f359b
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import type {TNodeInfo} from '../../../../types/api/nodes';
import {TPDiskState} from '../../../../types/api/pdisk';
import {calculateMaximumDisksPerNode} from '../utils';

describe('calculateMaximumDisksPerNode', () => {
it('should return providedMaximumDisksPerNode when it is provided', () => {
const nodes: TNodeInfo[] = [];
const providedMaximumDisksPerNode = '5';

expect(calculateMaximumDisksPerNode(nodes, providedMaximumDisksPerNode)).toBe('5');
});

it('should return "1" for empty nodes array', () => {
const nodes: TNodeInfo[] = [];

expect(calculateMaximumDisksPerNode(nodes)).toBe('1');
});

it('should return "1" for undefined nodes', () => {
expect(calculateMaximumDisksPerNode(undefined)).toBe('1');
});

it('should return "1" for nodes without PDisks', () => {
const nodes: TNodeInfo[] = [
{
NodeId: 1,
SystemState: {},
},
];

expect(calculateMaximumDisksPerNode(nodes)).toBe('1');
});

it('should calculate maximum disks correctly for single node with multiple PDisks', () => {
const nodes: TNodeInfo[] = [
{
NodeId: 1,
SystemState: {},
PDisks: [
{
PDiskId: 1,
State: TPDiskState.Normal,
},
{
PDiskId: 2,
State: TPDiskState.Normal,
},
{
PDiskId: 3,
State: TPDiskState.Normal,
},
],
},
];

expect(calculateMaximumDisksPerNode(nodes)).toBe('3');
});

it('should calculate maximum disks across multiple nodes', () => {
const nodes: TNodeInfo[] = [
{
NodeId: 1,
SystemState: {},
PDisks: [
{
PDiskId: 1,
State: TPDiskState.Normal,
},
],
},
{
NodeId: 2,
SystemState: {},
PDisks: [
{
PDiskId: 2,
State: TPDiskState.Normal,
},
{
PDiskId: 3,
State: TPDiskState.Normal,
},
],
},
{
NodeId: 3,
SystemState: {},
PDisks: [
{
PDiskId: 4,
State: TPDiskState.Normal,
},
{
PDiskId: 5,
State: TPDiskState.Normal,
},
{
PDiskId: 6,
State: TPDiskState.Normal,
},
{
PDiskId: 7,
State: TPDiskState.Normal,
},
],
},
];

expect(calculateMaximumDisksPerNode(nodes)).toBe('4');
});

it('should handle nodes with empty PDisks array', () => {
const nodes: TNodeInfo[] = [
{
NodeId: 1,
SystemState: {},
PDisks: [],
},
{
NodeId: 2,
SystemState: {},
PDisks: [
{
PDiskId: 1,
State: TPDiskState.Normal,
},
{
PDiskId: 2,
State: TPDiskState.Normal,
},
],
},
];

expect(calculateMaximumDisksPerNode(nodes)).toBe('2');
});
});
41 changes: 26 additions & 15 deletions src/store/reducers/storage/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,37 +223,48 @@ const prepareStorageNodeData = (
};
};

/**
* Calculates the maximum number of VDisk slots per PDisk across all nodes
* A slot represents a VDisk that can be allocated to a PDisk
*/
export const calculateMaximumSlotsPerDisk = (
nodes: TNodeInfo[] | undefined,
providedMaximumSlotsPerDisk?: string,
) => {
): string => {
if (providedMaximumSlotsPerDisk) {
return providedMaximumSlotsPerDisk;
}

return String(
Math.max(
1,
...(nodes || []).flatMap((node) =>
(node.PDisks || []).map(
(pDisk) =>
(node.VDisks || []).filter((vDisk) => vDisk.PDiskId === pDisk.PDiskId)
.length || 0,
),
),
),
);
const safeNodes = nodes || [];
const slotsPerDiskCounts = safeNodes.flatMap((node) => {
const safePDisks = node.PDisks || [];
const safeVDisks = node.VDisks || [];

return safePDisks.map((pDisk) => {
const vDisksOnPDisk = safeVDisks.filter((vDisk) => vDisk.PDiskId === pDisk.PDiskId);
return vDisksOnPDisk.length || 0;
});
});

const maxSlots = Math.max(1, ...slotsPerDiskCounts);
return String(maxSlots);
};

/**
* Calculates the maximum number of PDisks per node across all nodes
*/
export const calculateMaximumDisksPerNode = (
nodes: TNodeInfo[] | undefined,
providedMaximumDisksPerNode?: string,
) => {
): string => {
if (providedMaximumDisksPerNode) {
return providedMaximumDisksPerNode;
}

return String(Math.max(1, ...(nodes || []).map((node) => node.PDisks?.length || 0)));
const safeNodes = nodes || [];
const disksPerNodeCounts = safeNodes.map((node) => node.PDisks?.length || 0);
const maxDisks = Math.max(1, ...disksPerNodeCounts);
return String(maxDisks);
};

// ==== Prepare responses ====
Expand Down

0 comments on commit e8f359b

Please sign in to comment.