Skip to content

Commit a72b187

Browse files
committed
refactor: isWorkspaceOn utility
Summary: A utility is function is added that answers the question if a workspace is on. Impact: This is a shared piece of logic in workspace scheduling presentations. In particular it unblocks work in 1779, or at least allows an implementation that shares details with the WorkspaceScheduleBanner. Notes: We could possibly instead return whether the workspace is "ON", "UNKNOWN", or "OFF". Maybe a future improvement for that could be made as the neds arrises.
1 parent 6f7b7f0 commit a72b187

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

site/src/components/WorkspaceScheduleBanner/WorkspaceScheduleBanner.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ describe("WorkspaceScheduleBanner", () => {
9191
latest_build: {
9292
...Mocks.MockWorkspaceBuild,
9393
deadline: dayjs().add(27, "minutes").utc().format(),
94-
job: Mocks.MockRunningProvisionerJob,
9594
transition: "start",
9695
},
9796
}

site/src/components/WorkspaceScheduleBanner/WorkspaceScheduleBanner.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import isSameOrBefore from "dayjs/plugin/isSameOrBefore"
55
import utc from "dayjs/plugin/utc"
66
import { FC } from "react"
77
import * as TypesGen from "../../api/typesGenerated"
8+
import { isWorkspaceOn } from "../../util/workspace"
89

910
dayjs.extend(utc)
1011
dayjs.extend(isSameOrBefore)
@@ -18,12 +19,7 @@ export interface WorkspaceScheduleBannerProps {
1819
}
1920

2021
export const shouldDisplay = (workspace: TypesGen.Workspace): boolean => {
21-
const transition = workspace.latest_build.transition
22-
const status = workspace.latest_build.job.status
23-
24-
if (transition !== "start") {
25-
return false
26-
} else if (status === "canceled" || status === "canceling" || status === "failed") {
22+
if (!isWorkspaceOn(workspace)) {
2723
return false
2824
} else {
2925
// a mannual shutdown has a deadline of '"0001-01-01T00:00:00Z"'

site/src/util/workspace.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as TypesGen from "../api/typesGenerated"
2+
import * as Mocks from "../testHelpers/entities"
3+
import { isWorkspaceOn } from "./workspace"
4+
5+
describe("util > workspace", () => {
6+
describe("isWorkspaceOn", () => {
7+
it.each<[TypesGen.WorkspaceTransition, TypesGen.ProvisionerJobStatus, boolean]>([
8+
["delete", "canceled", false],
9+
["delete", "canceling", false],
10+
["delete", "failed", false],
11+
["delete", "pending", false],
12+
["delete", "running", false],
13+
["delete", "succeeded", false],
14+
15+
["stop", "canceled", false],
16+
["stop", "canceling", false],
17+
["stop", "failed", false],
18+
["stop", "pending", false],
19+
["stop", "running", false],
20+
["stop", "succeeded", false],
21+
22+
["start", "canceled", false],
23+
["start", "canceling", false],
24+
["start", "failed", false],
25+
["start", "pending", false],
26+
["start", "running", false],
27+
["start", "succeeded", true],
28+
])(`transition=%p, status=%p, isWorkspaceOn=%p`, (transition, status, isOn) => {
29+
const workspace: TypesGen.Workspace = {
30+
...Mocks.MockWorkspace,
31+
latest_build: {
32+
...Mocks.MockWorkspaceBuild,
33+
job: {
34+
...Mocks.MockProvisionerJob,
35+
status,
36+
},
37+
transition,
38+
},
39+
}
40+
expect(isWorkspaceOn(workspace)).toBe(isOn)
41+
})
42+
})
43+
})

site/src/util/workspace.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Theme } from "@material-ui/core/styles"
22
import dayjs from "dayjs"
33
import { WorkspaceBuildTransition } from "../api/types"
4-
import { WorkspaceAgent, WorkspaceBuild } from "../api/typesGenerated"
4+
import { Workspace, WorkspaceAgent, WorkspaceBuild } from "../api/typesGenerated"
55

66
export type WorkspaceStatus =
77
| "queued"
@@ -185,3 +185,9 @@ export const getDisplayAgentStatus = (
185185
}
186186
}
187187
}
188+
189+
export const isWorkspaceOn = (workspace: Workspace): boolean => {
190+
const transition = workspace.latest_build.transition
191+
const status = workspace.latest_build.job.status
192+
return transition === "start" && status === "succeeded"
193+
}

0 commit comments

Comments
 (0)