Skip to content

Send workspace display status in API response #2772

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

Closed
AbhineetJain opened this issue Jul 1, 2022 · 1 comment
Closed

Send workspace display status in API response #2772

AbhineetJain opened this issue Jul 1, 2022 · 1 comment
Labels
api Area: HTTP API site Area: frontend dashboard stale This issue is like stale bread.

Comments

@AbhineetJain
Copy link
Contributor

As per the discussion on #2743, we want to use consolidated logic from backend to compute workspace status to be displayed in UI as well as CLI.

Currently, frontend computes the display status itself, and backend also has duplicated logic after #2743.

Frontend code

const inProgressToStatus: Record<WorkspaceBuildTransition, WorkspaceStatus> = {
start: "starting",
stop: "stopping",
delete: "deleting",
}
const succeededToStatus: Record<WorkspaceBuildTransition, WorkspaceStatus> = {
start: "started",
stop: "stopped",
delete: "deleted",
}
// Converts a workspaces status to a human-readable form.
export const getWorkspaceStatus = (workspaceBuild?: TypesGen.WorkspaceBuild): WorkspaceStatus => {
const transition = workspaceBuild?.transition as WorkspaceBuildTransition
const jobStatus = workspaceBuild?.job.status
switch (jobStatus) {
case undefined:
return "loading"
case "succeeded":
return succeededToStatus[transition]
case "pending":
return "queued"
case "running":
return inProgressToStatus[transition]
case "canceling":
return "canceling"
case "canceled":
return "canceled"
case "failed":
return "error"
}
}

Backend code

package codersdk
// Maps workspace transition to display status for Running job status
var runningStatusFromTransition = map[WorkspaceTransition]string{
WorkspaceTransitionStart: "Starting",
WorkspaceTransitionStop: "Stopping",
WorkspaceTransitionDelete: "Deleting",
}
// Maps workspace transition to display status for Succeeded job status
var succeededStatusFromTransition = map[WorkspaceTransition]string{
WorkspaceTransitionStart: "Started",
WorkspaceTransitionStop: "Stopped",
WorkspaceTransitionDelete: "Deleted",
}
const unknownStatus = "Unknown"
// WorkspaceDisplayStatus computes a status to display on CLI/UI based on
// the workspace transition and the status of the provisioner job.
// This code is in sync with how we compute the status on frontend.
// Ref: site/src/util/workspace.ts (getWorkspaceStatus)
func WorkspaceDisplayStatus(jobStatus ProvisionerJobStatus, transition WorkspaceTransition) string {
switch jobStatus {
case ProvisionerJobSucceeded:
status, ok := succeededStatusFromTransition[transition]
if !ok {
return unknownStatus
}
return status
case ProvisionerJobRunning:
status, ok := runningStatusFromTransition[transition]
if !ok {
return unknownStatus
}
return status
case ProvisionerJobPending:
return "Queued"
case ProvisionerJobCanceling:
return "Canceling action"
case ProvisionerJobCanceled:
return "Canceled action"
case ProvisionerJobFailed:
return "Failed"
default:
return unknownStatus
}
}

We plan to send the display status as part of the API response along with other workspace details, and use it in the frontend directly. This should get rid of the duplicated logic.

@github-actions
Copy link

This issue is becoming stale. In order to keep the tracker readable and actionable, I'm going close to this issue in 7 days if there isn't more activity.

@github-actions github-actions bot added the stale This issue is like stale bread. label Sep 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Area: HTTP API site Area: frontend dashboard stale This issue is like stale bread.
Projects
None yet
Development

No branches or pull requests

1 participant