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

Mccalluc/workspace list #2715

Merged
merged 5 commits into from
Jun 29, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG-more-workspace-api-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Demostrate the Workspaces API to start jobs and collect job information.
57 changes: 48 additions & 9 deletions context/app/static/js/components/workspaces/WorkspacesList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,53 @@ import { AppContext } from 'js/components/Providers';
import { DeleteIcon, AddIcon } from 'js/shared-styles/icons';
import { SpacedSectionButtonRow } from 'js/shared-styles/sections/SectionButtonRow';

import { createNotebookWorkspace } from './utils';
import { useWorkspacesList } from './hooks';
import { createNotebookWorkspace, startJob } from './utils';
import { useWorkspacesList, useJobsList } from './hooks';
import { StyledButton } from './style';

function WorkspacesList() {
const { workspacesEndpoint, workspacesToken } = useContext(AppContext);
const { workspacesList } = useWorkspacesList();
const { jobsList } = useJobsList();

async function handleDelete() {
// eslint-disable-next-line no-alert
alert('TODO: API does not yet support deletion.');
// TODO: Put up modal and get user input.
// TODO: Update workspacesList
// Waiting on delete to be implemented.
}

async function handleCreate() {
// TODO: Put up modal and get user input.
// TODO: Update workspacesList
// TODO: Put up a better modal and get user input.
// eslint-disable-next-line no-alert
const content = prompt('Intial content for notebook');

createNotebookWorkspace({
workspacesEndpoint,
workspacesToken,
workspaceName: 'Workspace Timestamp',
workspaceDescription: 'TODO: description',
notebookContent: 'TODO',
notebookContent: content,
});
// TODO: Update list on page
}

function createHandleStart(workspaceId) {
async function handleStart() {
startJob({ workspaceId, workspacesEndpoint, workspacesToken });
}
return handleStart;
}

return (
<>
<SpacedSectionButtonRow
leftText={<Typography variant="subtitle1">[TODO: Count] Workspaces</Typography>}
leftText={
<Typography variant="subtitle1">
{workspacesList.length} Workspace{workspacesList.length && 's'}
</Typography>
}
buttons={
<>
<StyledButton onClick={handleDelete}>
Expand All @@ -49,9 +65,32 @@ function WorkspacesList() {
}
/>
<Paper>
{`TODO: Use token "${workspacesToken}" with "${workspacesEndpoint}"`}
<hr />
{JSON.stringify(workspacesList)}
{workspacesList.map((workspace) => (
<div key={workspace.id}>
<details>
<summary>JSON</summary>
<pre>{JSON.stringify(workspace, null, 2)}</pre>
</details>
<div>
<b>{workspace.name}</b> | Created {workspace.datetime_created.slice(0, 10)}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure what the format is here, but we should move this towards being consistent with how other dates are formatted throughout the portal.

</div>
<button onClick={createHandleStart(workspace.id)} type="button">
Start Jupyter
</button>
</div>
))}
</Paper>
<SpacedSectionButtonRow leftText={<Typography variant="subtitle1">Jobs</Typography>} />
<Paper>
TODO: The current API responses give us no way to connect Workspaces to Jobs.
{jobsList.map((job) => (
<div key={job.id}>
<details>
<summary>JSON</summary>
<pre>{JSON.stringify(job, null, 2)}</pre>
</details>
</div>
))}
</Paper>
</>
);
Expand Down
36 changes: 34 additions & 2 deletions context/app/static/js/components/workspaces/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function useWorkspacesList() {
}
const results = await response.json();

setWorkspacesList(results);
setWorkspacesList(results.data.workspaces);
// TODO:
// setIsLoading(false);
}
Expand All @@ -35,4 +35,36 @@ function useWorkspacesList() {
return { workspacesList };
}

export { useWorkspacesList };
function useJobsList() {
// TODO: Right now the API does not support querying, so we just need to get the whole list.
const [jobsList, setJobsList] = useState([]);

const { workspacesEndpoint, workspacesToken } = useContext(AppContext);

useEffect(() => {
async function getAndSetJobsList() {
const response = await fetch(`${workspacesEndpoint}/jobs`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'UWS-Authorization': `Token ${workspacesToken}`,
},
});

if (!response.ok) {
console.error('Workspaces API failed', response);
return;
}
const results = await response.json();

setJobsList(results.data.jobs);
// TODO:
// setIsLoading(false);
}
getAndSetJobsList();
}, [workspacesEndpoint, workspacesToken]);

return { jobsList };
}

export { useWorkspacesList, useJobsList };
16 changes: 15 additions & 1 deletion context/app/static/js/components/workspaces/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,18 @@ async function createNotebookWorkspace({
});
}

export { createNotebookWorkspace };
async function startJob({ workspaceId, workspacesEndpoint, workspacesToken }) {
await fetch(`${workspacesEndpoint}/workspaces/${workspaceId}/start`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'UWS-Authorization': `Token ${workspacesToken}`,
},
body: JSON.stringify({
job_type: 'JupyterLabJob',
job_details: {},
}),
});
}
Comment on lines +30 to +42
Copy link
Collaborator

Choose a reason for hiding this comment

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

Based on our conversation in standup, we want to make another GET to jobs and update jobs state here, right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I see the changes in #2717. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep... but I hit some snags there, so filed #2717 and will come back to it.


export { createNotebookWorkspace, startJob };