Skip to content

Commit 76ed278

Browse files
committed
chore: use actor definition resources as defaults for connector resource ui (#15072)
1 parent 00e4f1f commit 76ed278

File tree

5 files changed

+68
-337
lines changed

5 files changed

+68
-337
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { mockDestinationDefinition } from "test-utils/mock-data/mockDestination";
2+
import { mockSourceDefinition } from "test-utils/mock-data/mockSource";
3+
4+
import { getResourceOptions } from "./ResourceAllocationMenu";
5+
6+
describe("#getOptions", () => {
7+
it("uses values from actor definition, if present, for source connector", () => {
8+
const sourceDefinition = {
9+
...mockSourceDefinition,
10+
resourceRequirements: { default: { memory_request: "9", cpu_request: "7" } },
11+
};
12+
const options = getResourceOptions(sourceDefinition);
13+
expect(options[0].value).toEqual({ memory: "9", cpu: "7" });
14+
});
15+
16+
it("uses values from hardcoded defaults, if no actor value present, for source connector", () => {
17+
const options = getResourceOptions(mockSourceDefinition);
18+
expect(options[0].value).toEqual({ memory: "2", cpu: "2" });
19+
});
20+
21+
it("uses values from actor definition, if present, for destination connector", () => {
22+
const destinationDefinition = {
23+
...mockDestinationDefinition,
24+
resourceRequirements: { default: { memory_request: "9", cpu_request: "7" } },
25+
};
26+
const options = getResourceOptions(destinationDefinition);
27+
expect(options[0].value).toEqual({ memory: "9", cpu: "7" });
28+
});
29+
it("uses values from hardcoded defaults, if no actor value present, for destination connector", () => {
30+
const options = getResourceOptions(mockDestinationDefinition);
31+
expect(options[0].value).toEqual({ memory: "2", cpu: "2" });
32+
});
33+
});

airbyte-webapp/src/area/connector/components/ResourceAllocationMenu.tsx

+33-27
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,59 @@ import { ConnectorFormValues } from "views/Connector/ConnectorForm";
1818
import { PropertyError } from "views/Connector/ConnectorForm/components/Property/PropertyError";
1919
import { useConnectorForm } from "views/Connector/ConnectorForm/connectorFormContext";
2020

21-
export const API_RESOURCES = {
21+
export const API_RESOURCE_DEFAULTS = {
2222
default: {
23-
memory: 2,
24-
cpu: 1,
23+
memory: "2",
24+
cpu: "1",
2525
},
2626
large: {
27-
memory: 3,
28-
cpu: 2,
27+
memory: "3",
28+
cpu: "2",
2929
},
3030
memoryIntensive: {
31-
memory: 6,
32-
cpu: 2,
31+
memory: "6",
32+
cpu: "2",
3333
},
3434
maximum: {
35-
memory: 8,
36-
cpu: 4,
35+
memory: "8",
36+
cpu: "4",
3737
},
3838
};
3939

40-
export const DB_RESOURCES = {
40+
export const DB_RESOURCE_DEFAULTS = {
4141
default: {
42-
memory: 2,
43-
cpu: 2,
42+
memory: "2",
43+
cpu: "2",
4444
},
4545
large: {
46-
memory: 3,
47-
cpu: 3,
46+
memory: "3",
47+
cpu: "3",
4848
},
4949
memoryIntensive: {
50-
memory: 6,
51-
cpu: 3,
50+
memory: "6",
51+
cpu: "3",
5252
},
5353
maximum: {
54-
memory: 8,
55-
cpu: 4,
54+
memory: "8",
55+
cpu: "4",
5656
},
5757
};
5858

59-
const getOptions = (connectorType: "api" | "database") => {
60-
const values = connectorType === "api" ? API_RESOURCES : DB_RESOURCES;
59+
export const getResourceOptions = (selectedConnectorDefinition?: ConnectorDefinition) => {
60+
const connectorType = getConnectorType(selectedConnectorDefinition);
61+
const hardcodedValues = connectorType === "api" ? API_RESOURCE_DEFAULTS : DB_RESOURCE_DEFAULTS;
62+
63+
const definitionResources = selectedConnectorDefinition?.resourceRequirements;
64+
65+
const valuesToUse = {
66+
...hardcodedValues,
67+
default: {
68+
memory: definitionResources?.default?.memory_request || hardcodedValues.default.memory,
69+
cpu: definitionResources?.default?.cpu_request ?? hardcodedValues.default.cpu,
70+
},
71+
};
6172

62-
return Object.entries(values).map(([size, value]) => {
73+
return Object.entries(valuesToUse).map(([size, value]) => {
6374
return {
6475
value,
6576
label: (
@@ -123,12 +134,7 @@ export const ResourceAllocationMenu: React.FC = () => {
123134
const { getValues, setValue, formState, getFieldState } = useFormContext<ConnectorFormValues>();
124135
const meta = getFieldState("resourceAllocation", formState);
125136

126-
const connectorType = getConnectorType(selectedConnectorDefinition);
127-
if (!connectorType) {
128-
return null;
129-
}
130-
131-
const options = getOptions(connectorType);
137+
const options = getResourceOptions(selectedConnectorDefinition);
132138

133139
const errorMessage = Array.isArray(meta.error) ? (
134140
<FlexContainer direction="column" gap="none">

0 commit comments

Comments
 (0)