Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FE: Broker not shown in a list if disk usage is unknown for it #3981

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import { ColumnDef } from '@tanstack/react-table';
import { clusterBrokerPath } from 'lib/paths';
import Tooltip from 'components/common/Tooltip/Tooltip';
import ColoredCell from 'components/common/NewTable/ColoredCell';
import { BrokerDiskUsage } from 'generated-sources';

import SkewHeader from './SkewHeader/SkewHeader';
import * as S from './BrokersList.styled';

const NA = 'N/A';
const NA = 'N/A' as unknown as number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that we cast string to number

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because in this component we use N/A as a failsafe for a number types. The other way would be to change autogenerated types to also accept string for size and count props

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can actually write sanitizer function and link it to useBrokers that way the FE will have control over the type, do it once after we fetch inside the hook useBrokers instead of doing it with useMemo on every render.


const BrokersList: React.FC = () => {
const navigate = useNavigate();
const { clusterName } = useAppParams<{ clusterName: ClusterName }>();
const { data: clusterStats = {} } = useClusterStats(clusterName);
const { data: brokers } = useBrokers(clusterName);
const { data: brokers = [] } = useBrokers(clusterName);

const {
brokerCount,
Expand All @@ -37,7 +38,7 @@ const BrokersList: React.FC = () => {
} = clusterStats;

const rows = React.useMemo(() => {
let brokersResource;
let brokersResource: BrokerDiskUsage[];
if (!diskUsage || !diskUsage?.length) {
brokersResource =
brokers?.map((broker) => {
Expand All @@ -51,20 +52,32 @@ const BrokersList: React.FC = () => {
brokersResource = diskUsage;
}

return brokersResource.map(({ brokerId, segmentSize, segmentCount }) => {
const broker = brokers?.find(({ id }) => id === brokerId);
return {
brokerId,
size: segmentSize || NA,
count: segmentCount || NA,
port: broker?.port,
host: broker?.host,
partitionsLeader: broker?.partitionsLeader,
partitionsSkew: broker?.partitionsSkew,
leadersSkew: broker?.leadersSkew,
inSyncPartitions: broker?.inSyncPartitions,
};
});
return brokers?.map(
({
id,
port,
host,
partitionsLeader,
partitionsSkew,
leadersSkew,
inSyncPartitions,
}) => {
const brokerResource = brokersResource.find(
({ brokerId }) => id === brokerId
);
return {
brokerId: id,
size: brokerResource?.segmentSize || NA,
count: brokerResource?.segmentCount || NA,
port,
host,
partitionsLeader,
partitionsSkew,
leadersSkew,
inSyncPartitions,
};
}
);
}, [diskUsage, brokers]);

const columns = React.useMemo<ColumnDef<(typeof rows)[number]>[]>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ describe('BrokersList Component', () => {
it('renders', async () => {
renderComponent();
expect(screen.getByRole('table')).toBeInTheDocument();
expect(screen.getAllByRole('row').length).toEqual(3);
expect(screen.getAllByRole('row').length).toEqual(
brokersPayload.length + 1
);
});
it('opens broker when row clicked', async () => {
renderComponent();
await userEvent.click(screen.getByRole('cell', { name: '0' }));
await userEvent.click(screen.getByRole('cell', { name: '1' }));

await waitFor(() =>
expect(mockedUsedNavigate).toBeCalledWith(
clusterBrokerPath(clusterName, '0')
clusterBrokerPath(clusterName, '1')
)
);
});
Expand Down Expand Up @@ -153,7 +155,9 @@ describe('BrokersList Component', () => {
it('renders list of all brokers', async () => {
renderComponent();
expect(screen.getByRole('table')).toBeInTheDocument();
expect(screen.getAllByRole('row').length).toEqual(3);
expect(screen.getAllByRole('row').length).toEqual(
brokersPayload.length + 1
);
});
it('opens broker when row clicked', async () => {
renderComponent();
Expand All @@ -166,5 +170,26 @@ describe('BrokersList Component', () => {
);
});
});

describe('when some brokers have no diskUsage', () => {
beforeEach(() => {
(useBrokers as jest.Mock).mockImplementation(() => ({
data: brokersPayload,
}));
(useClusterStats as jest.Mock).mockImplementation(() => ({
data: {
...clusterStatsPayload,
diskUsage: [clusterStatsPayload.diskUsage[0]],
},
}));
});
it('renders list of all brokers', async () => {
renderComponent();
expect(screen.getByRole('table')).toBeInTheDocument();
expect(screen.getAllByRole('row').length).toEqual(
brokersPayload.length + 1
);
});
});
});
});