-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluster-ui: create node regions selector
This commit adds a select component which allows users to select nodes by region. Epic: CRDB-37558 Fixes: #131032 Release note: None
- Loading branch information
Showing
5 changed files
with
240 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
pkg/ui/workspaces/cluster-ui/src/components/nodeRegionsSelector/nodeRegionsSelector.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
import React from "react"; | ||
|
||
import { useNodeStatuses } from "src/api"; | ||
import { getRegionFromLocality } from "src/store/nodes"; | ||
import { StoreID } from "src/types/clusterTypes"; | ||
|
||
import { NodeRegionsSelector } from "./nodeRegionsSelector"; | ||
|
||
// Mock the useNodeStatuses hook | ||
jest.mock("src/api", () => ({ | ||
useNodeStatuses: jest.fn(), | ||
})); | ||
|
||
// Mock the getRegionFromLocality function | ||
jest.mock("src/store/nodes", () => ({ | ||
getRegionFromLocality: jest.fn(), | ||
})); | ||
|
||
const mockNodeData = { | ||
nodes: [ | ||
{ | ||
desc: { node_id: 1, locality: { region: "us-east" } }, | ||
store_statuses: [ | ||
{ desc: { store_id: 101 } }, | ||
{ desc: { store_id: 102 } }, | ||
], | ||
}, | ||
{ | ||
desc: { node_id: 2, locality: { region: "us-west" } }, | ||
store_statuses: [{ desc: { store_id: 201 } }], | ||
}, | ||
{ | ||
desc: { node_id: 3, locality: { region: "us-east" } }, | ||
store_statuses: [{ desc: { store_id: 301 } }], | ||
}, | ||
], | ||
}; | ||
|
||
describe("NodeRegionsSelector", () => { | ||
beforeEach(() => { | ||
(useNodeStatuses as jest.Mock).mockReturnValue({ | ||
isLoading: false, | ||
data: mockNodeData, | ||
}); | ||
(getRegionFromLocality as jest.Mock).mockImplementation( | ||
locality => locality.region, | ||
); | ||
}); | ||
|
||
it("renders without crashing", () => { | ||
render(<NodeRegionsSelector value={[]} onChange={() => {}} />); | ||
expect(screen.getByText("Nodes")).toBeTruthy(); | ||
}); | ||
|
||
it("displays correct options based on node data", async () => { | ||
render(<NodeRegionsSelector value={[]} onChange={() => {}} />); | ||
|
||
const select = screen.getByText("Nodes"); | ||
fireEvent.keyDown(select, { key: "ArrowDown" }); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("us-east")).toBeTruthy(); | ||
expect(screen.getByText("us-west")).toBeTruthy(); | ||
expect(screen.getByText("n1")).toBeTruthy(); | ||
expect(screen.getByText("n2")).toBeTruthy(); | ||
expect(screen.getByText("n3")).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
it("calls onChange with correct values when selecting options", async () => { | ||
const value: StoreID[] = []; | ||
const mockOnChange = jest.fn((selected: StoreID[]) => { | ||
value.push(...selected); | ||
}); | ||
render(<NodeRegionsSelector value={value} onChange={mockOnChange} />); | ||
|
||
const select = screen.getByText("Nodes"); | ||
fireEvent.keyDown(select, { key: "ArrowDown" }); | ||
|
||
await waitFor(() => { | ||
fireEvent.click(screen.getByText("n1")); | ||
}); | ||
|
||
expect(mockOnChange).toHaveBeenCalledWith([101, 102]); | ||
}); | ||
|
||
it("displays selected values correctly", () => { | ||
render( | ||
<NodeRegionsSelector | ||
value={[101 as StoreID, 201 as StoreID]} | ||
onChange={() => {}} | ||
/>, | ||
); | ||
|
||
expect(screen.getByText("n1")).toBeTruthy(); | ||
expect(screen.getByText("n2")).toBeTruthy(); | ||
}); | ||
|
||
it("handles loading state", () => { | ||
(useNodeStatuses as jest.Mock).mockReturnValue({ | ||
isLoading: true, | ||
data: mockNodeData, | ||
}); | ||
|
||
render(<NodeRegionsSelector value={[]} onChange={() => {}} />); | ||
|
||
const select = screen.getByText("Nodes"); | ||
fireEvent.keyDown(select, { key: "ArrowDown" }); | ||
|
||
// In the loading state, the component should still render options | ||
// based on the existing data | ||
expect(screen.getByText("us-east")).toBeTruthy(); | ||
expect(screen.getByText("us-west")).toBeTruthy(); | ||
expect(screen.getByText("n1")).toBeTruthy(); | ||
expect(screen.getByText("n2")).toBeTruthy(); | ||
expect(screen.getByText("n3")).toBeTruthy(); | ||
}); | ||
}); |
86 changes: 86 additions & 0 deletions
86
pkg/ui/workspaces/cluster-ui/src/components/nodeRegionsSelector/nodeRegionsSelector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
import React, { useMemo } from "react"; | ||
import Select, { OptionsType } from "react-select"; | ||
|
||
import { useNodeStatuses } from "src/api"; | ||
import { getRegionFromLocality } from "src/store/nodes"; | ||
import { NodeID, StoreID } from "src/types/clusterTypes"; | ||
import { | ||
GroupedReactSelectOption, | ||
ReactSelectOption, | ||
} from "src/types/selectTypes"; | ||
|
||
type NodeRegionsSelectorProps = { | ||
value: StoreID[]; | ||
onChange: (selected: StoreID[]) => void; | ||
}; | ||
|
||
export const NodeRegionsSelector: React.FC<NodeRegionsSelectorProps> = ({ | ||
value, | ||
onChange, | ||
}) => { | ||
const nodesResp = useNodeStatuses(); | ||
|
||
const nodeOptions: GroupedReactSelectOption<StoreID[]>[] = useMemo(() => { | ||
const optionsMap: Record<string, { nid: NodeID; sids: StoreID[] }[]> = {}; | ||
if (nodesResp.isLoading && !nodesResp.data?.nodes) { | ||
return []; | ||
} | ||
|
||
nodesResp.data.nodes.forEach(node => { | ||
const region = getRegionFromLocality(node.desc.locality); | ||
if (optionsMap[region] == null) { | ||
optionsMap[region] = []; | ||
} | ||
optionsMap[region].push({ | ||
nid: node.desc.node_id as NodeID, | ||
sids: node.store_statuses.map(s => s.desc.store_id as StoreID), | ||
}); | ||
}); | ||
|
||
return Object.entries(optionsMap).map(([region, nodes]) => { | ||
return { | ||
label: region, | ||
options: nodes.map(n => ({ | ||
label: `n${n.nid}`, | ||
value: n.sids, | ||
})), | ||
}; | ||
}); | ||
}, [nodesResp]); | ||
|
||
const onSelectChange = ( | ||
selected: OptionsType<ReactSelectOption<StoreID[]>>, | ||
) => { | ||
onChange(selected.map(s => s.value).reduce((acc, v) => acc.concat(v), [])); | ||
}; | ||
|
||
const selectValue: OptionsType<ReactSelectOption<StoreID[]>> = | ||
nodeOptions.reduce((acc, region) => { | ||
const nodes = region.options.filter(n => | ||
value.some(v => n.value.includes(v)), | ||
); | ||
return [...acc, ...nodes]; | ||
}, []); | ||
|
||
return ( | ||
<Select | ||
placeholder={"Nodes"} | ||
name="nodeRegions" | ||
options={nodeOptions} | ||
clearable={true} | ||
isMulti | ||
value={selectValue} | ||
onChange={onSelectChange} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters