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

feat: Use filtered resource pools when creating notebook #9045

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions webui/react/src/components/JupyterLabModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const SIMPLE_CONFIG_TEMPLATE_TEXT = 'Template';
const SHOW_SIMPLE_CONFIG_TEXT = 'Show Simple Config';

vi.mock('services/api', () => ({
getAvailableResourcePools: () => Promise.resolve([]),
getCurrentUser: () => Promise.resolve({ id: 1 }),
getResourcePools: () => Promise.resolve([]),
getTaskTemplates: () => Promise.resolve([]),
Expand Down
35 changes: 25 additions & 10 deletions webui/react/src/components/JupyterLabModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { paths } from 'routes/utils';
import { getTaskTemplates } from 'services/api';
import clusterStore from 'stores/cluster';
import workspaceStore from 'stores/workspaces';
import { RawJson, Template, Workspace } from 'types';
import { RawJson, ResourcePool, Template, Workspace } from 'types';
import handleError from 'utils/error';
import { JupyterLabOptions, launchJupyterLab, previewJupyterLab } from 'utils/jupyter';
import { useObservable } from 'utils/observable';
Expand Down Expand Up @@ -349,12 +349,23 @@ const JupyterLabForm: React.FC<{
}> = ({ form, formId, currentWorkspace, defaults, lockedWorkspace, setWorkspace, workspaces }) => {
const [templates, setTemplates] = useState<Template[]>([]);

const resourcePools = Loadable.getOrElse([], useObservable(clusterStore.resourcePools));
const selectedWorkspace = Form.useWatch('workspaceId', form);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
const selectedWorkspace = Form.useWatch('workspaceId', form);
const selectedWorkspaceId = Form.useWatch('workspaceId', form);


const resourcePools = useObservable(clusterStore.resourcePools);
const boundResourcePoolsMap = useObservable(workspaceStore.boundResourcePoolsMap());

const boundResourcePools: ResourcePool[] = useMemo(() => {
if (!Loadable.isLoaded(resourcePools) || !boundResourcePoolsMap || !selectedWorkspace)
Copy link
Contributor

Choose a reason for hiding this comment

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

what does !boundResourcePoolsMap mean here? i dont think boundResourcePoolsMap can be undefined. and empty map still return true.

nit: selectedWorkspace === undefined

return [];
return resourcePools.data.filter(
(rp) => boundResourcePoolsMap.get(selectedWorkspace)?.includes(rp.name),
);
}, [resourcePools, boundResourcePoolsMap, selectedWorkspace]);

const selectedPoolName = Form.useWatch('pool', form);

const resourceInfo = useMemo(() => {
const selectedPool = resourcePools.find((pool) => pool.name === selectedPoolName);
const selectedPool = boundResourcePools.find((pool) => pool.name === selectedPoolName);
if (!selectedPool) return { hasAux: false, hasCompute: false, maxSlots: 0 };

/**
Expand All @@ -372,7 +383,7 @@ const JupyterLabForm: React.FC<{
hasCompute: hasComputeCapacity,
maxSlots: maxSlots,
};
}, [selectedPoolName, resourcePools]);
}, [selectedPoolName, boundResourcePools]);

useEffect(() => {
if (!resourceInfo.hasCompute && resourceInfo.hasAux) form.setFieldValue('slots', 0);
Expand All @@ -382,6 +393,10 @@ const JupyterLabForm: React.FC<{
}
}, [resourceInfo, form]);

useEffect(() => {
selectedWorkspace && workspaceStore.fetchAvailableResourcePools(selectedWorkspace);
}, [selectedWorkspace]);

const fetchTemplates = useCallback(async () => {
try {
setTemplates(await getTaskTemplates({}));
Expand All @@ -396,11 +411,11 @@ const JupyterLabForm: React.FC<{

useEffect(() => {
const fields = form.getFieldsValue(true);
if (!fields?.pool && resourcePools[0]?.name) {
const firstPoolInList = resourcePools[0]?.name;
if (!fields?.pool && boundResourcePools[0]?.name) {
const firstPoolInList = boundResourcePools[0]?.name;
form.setFieldValue('pool', firstPoolInList);
}
}, [resourcePools, form]);
}, [boundResourcePools, form]);

useEffect(() => {
form.setFieldValue('workspaceId', currentWorkspace?.id);
Expand Down Expand Up @@ -442,9 +457,9 @@ const JupyterLabForm: React.FC<{
<Form.Item initialValue={defaults?.name} label="Name" name="name">
<Input placeholder="Name (optional)" />
</Form.Item>
<Form.Item initialValue={defaults?.pool} label="Resource Pool" name="pool">
<Select allowClear placeholder="Pick the best option">
{resourcePools.map((pool) => (
<Form.Item label="Resource Pool" name="pool">
<Select allowClear disabled={!selectedWorkspace} placeholder="Pick the best option">
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: i dont think workspace id would take 0 in the future, but as a convention, selectedWorkspaceId === undefined can avoid a potential bug.

{boundResourcePools.map((pool) => (
<Option key={pool.name} value={pool.name}>
{pool.name}
</Option>
Expand Down
2 changes: 2 additions & 0 deletions webui/react/src/stores/workspaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class WorkspaceStore extends PollingStore {
public readonly boundResourcePools = (workspaceId: number) =>
this.#boundResourcePools.select((map) => map.get(workspaceId)?.toJS());

public readonly boundResourcePoolsMap = () => this.#boundResourcePools.readOnly();
Copy link
Contributor

Choose a reason for hiding this comment

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

Does that need to be a function?
why not like this?

Suggested change
public readonly boundResourcePoolsMap = () => this.#boundResourcePools.readOnly();
public readonly boundResourcePoolsMap = this.#boundResourcePools.readOnly();


public async fetchAvailableResourcePools(workspaceId: number) {
const response = await getAvailableResourcePools({ workspaceId });
this.#boundResourcePools.update((map) => map.set(workspaceId, List(response)));
Expand Down
Loading