-
Notifications
You must be signed in to change notification settings - Fork 974
Closed
Labels
apiArea: HTTP APIArea: HTTP APIsiteArea: frontend dashboardArea: frontend dashboardstaleThis issue is like stale bread.This issue is like stale bread.
Description
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
coder/site/src/util/workspace.ts
Lines 22 to 54 in 482feef
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
coder/codersdk/workspacedisplaystatus.go
Lines 1 to 48 in 28d0f4c
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.
johnstcn
Metadata
Metadata
Assignees
Labels
apiArea: HTTP APIArea: HTTP APIsiteArea: frontend dashboardArea: frontend dashboardstaleThis issue is like stale bread.This issue is like stale bread.