Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e98f98

Browse files
committed
move schedule-related util functions to util/schedule
1 parent aa01e0f commit 6e98f98

File tree

5 files changed

+89
-70
lines changed

5 files changed

+89
-70
lines changed

site/src/pages/WorkspacePage/WorkspacePage.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { FullScreenLoader } from "../../components/Loader/FullScreenLoader"
1111
import { Workspace, WorkspaceErrors } from "../../components/Workspace/Workspace"
1212
import { firstOrItem } from "../../util/array"
1313
import { pageTitle } from "../../util/page"
14-
import { getFaviconByStatus, maxDeadline, minDeadline } from "../../util/workspace"
14+
import { maxDeadline, minDeadline } from "../../util/schedule"
15+
import { getFaviconByStatus } from "../../util/workspace"
1516
import { selectUser } from "../../xServices/auth/authSelectors"
1617
import { XServiceContext } from "../../xServices/StateContext"
1718
import { workspaceMachine } from "../../xServices/workspace/workspaceXService"

site/src/util/schedule.test.ts

+58-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
import { extractTimezone, stripTimezone } from "./schedule"
1+
import dayjs from "dayjs"
2+
import duration from "dayjs/plugin/duration"
3+
import { Template, Workspace } from "../api/typesGenerated"
4+
import * as Mocks from "../testHelpers/entities"
5+
import {
6+
deadlineExtensionMax,
7+
deadlineExtensionMin,
8+
extractTimezone,
9+
maxDeadline,
10+
minDeadline,
11+
stripTimezone,
12+
} from "./schedule"
13+
14+
dayjs.extend(duration)
15+
const now = dayjs()
216

317
describe("util/schedule", () => {
418
describe("stripTimezone", () => {
@@ -21,3 +35,46 @@ describe("util/schedule", () => {
2135
})
2236
})
2337
})
38+
39+
describe("maxDeadline", () => {
40+
// Given: a workspace built from a template with a max deadline equal to 25 hours which isn't really possible
41+
const workspace: Workspace = {
42+
...Mocks.MockWorkspace,
43+
latest_build: {
44+
...Mocks.MockWorkspaceBuild,
45+
deadline: now.add(8, "hours").utc().format(),
46+
},
47+
}
48+
describe("given a template with 25 hour max ttl", () => {
49+
it("should be never be greater than global max deadline", () => {
50+
const template: Template = {
51+
...Mocks.MockTemplate,
52+
max_ttl_ms: 25 * 60 * 60 * 1000,
53+
}
54+
55+
// Then: deadlineMinusDisabled should be falsy
56+
const delta = maxDeadline(workspace, template).diff(now)
57+
expect(delta).toBeLessThanOrEqual(deadlineExtensionMax.asMilliseconds())
58+
})
59+
})
60+
61+
describe("given a template with 4 hour max ttl", () => {
62+
it("should be never be greater than global max deadline", () => {
63+
const template: Template = {
64+
...Mocks.MockTemplate,
65+
max_ttl_ms: 4 * 60 * 60 * 1000,
66+
}
67+
68+
// Then: deadlineMinusDisabled should be falsy
69+
const delta = maxDeadline(workspace, template).diff(now)
70+
expect(delta).toBeLessThanOrEqual(deadlineExtensionMax.asMilliseconds())
71+
})
72+
})
73+
})
74+
75+
describe("minDeadline", () => {
76+
it("should never be less than 30 minutes", () => {
77+
const delta = minDeadline().diff(now)
78+
expect(delta).toBeGreaterThanOrEqual(deadlineExtensionMin.asMilliseconds())
79+
})
80+
})

site/src/util/schedule.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import duration from "dayjs/plugin/duration"
55
import relativeTime from "dayjs/plugin/relativeTime"
66
import timezone from "dayjs/plugin/timezone"
77
import utc from "dayjs/plugin/utc"
8-
import { Workspace } from "../api/typesGenerated"
8+
import { Template, Workspace } from "../api/typesGenerated"
99
import { isWorkspaceOn } from "./workspace"
1010

1111
// REMARK: some plugins depend on utc, so it's listed first. Otherwise they're
@@ -110,3 +110,31 @@ export const autoStopDisplay = (workspace: Workspace): string => {
110110
return `${duration.humanize()} ${Language.afterStart}`
111111
}
112112
}
113+
114+
export const deadlineExtensionMin = dayjs.duration(30, "minutes")
115+
export const deadlineExtensionMax = dayjs.duration(24, "hours")
116+
117+
export function maxDeadline(ws: Workspace, tpl: Template): dayjs.Dayjs {
118+
// note: we count runtime from updated_at as started_at counts from the start of
119+
// the workspace build process, which can take a while.
120+
const startedAt = dayjs(ws.latest_build.updated_at)
121+
const maxTemplateDeadline = startedAt.add(dayjs.duration(tpl.max_ttl_ms, "milliseconds"))
122+
const maxGlobalDeadline = startedAt.add(deadlineExtensionMax)
123+
return dayjs.min(maxTemplateDeadline, maxGlobalDeadline)
124+
}
125+
126+
export function minDeadline(): dayjs.Dayjs {
127+
return dayjs().add(deadlineExtensionMin)
128+
}
129+
130+
export function canExtendDeadline(
131+
deadline: dayjs.Dayjs,
132+
workspace: Workspace,
133+
template: Template,
134+
): boolean {
135+
return deadline < maxDeadline(workspace, template)
136+
}
137+
138+
export function canReduceDeadline(deadline: dayjs.Dayjs): boolean {
139+
return deadline > minDeadline()
140+
}

site/src/util/workspace.test.ts

-51
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import dayjs from "dayjs"
2-
import duration from "dayjs/plugin/duration"
32
import * as TypesGen from "../api/typesGenerated"
43
import * as Mocks from "../testHelpers/entities"
54
import {
6-
deadlineExtensionMax,
7-
deadlineExtensionMin,
85
defaultWorkspaceExtension,
96
getDisplayWorkspaceBuildInitiatedBy,
107
isWorkspaceDeleted,
118
isWorkspaceOn,
12-
maxDeadline,
13-
minDeadline,
149
} from "./workspace"
1510

16-
dayjs.extend(duration)
17-
const now = dayjs()
18-
1911
describe("util > workspace", () => {
2012
describe("isWorkspaceOn", () => {
2113
it.each<[TypesGen.WorkspaceTransition, TypesGen.ProvisionerJobStatus, boolean]>([
@@ -137,46 +129,3 @@ describe("util > workspace", () => {
137129
})
138130
})
139131
})
140-
141-
describe("maxDeadline", () => {
142-
// Given: a workspace built from a template with a max deadline equal to 25 hours which isn't really possible
143-
const workspace: TypesGen.Workspace = {
144-
...Mocks.MockWorkspace,
145-
latest_build: {
146-
...Mocks.MockWorkspaceBuild,
147-
deadline: now.add(8, "hours").utc().format(),
148-
},
149-
}
150-
describe("given a template with 25 hour max ttl", () => {
151-
it("should be never be greater than global max deadline", () => {
152-
const template: TypesGen.Template = {
153-
...Mocks.MockTemplate,
154-
max_ttl_ms: 25 * 60 * 60 * 1000,
155-
}
156-
157-
// Then: deadlineMinusDisabled should be falsy
158-
const delta = maxDeadline(workspace, template).diff(now)
159-
expect(delta).toBeLessThanOrEqual(deadlineExtensionMax.asMilliseconds())
160-
})
161-
})
162-
163-
describe("given a template with 4 hour max ttl", () => {
164-
it("should be never be greater than global max deadline", () => {
165-
const template: TypesGen.Template = {
166-
...Mocks.MockTemplate,
167-
max_ttl_ms: 4 * 60 * 60 * 1000,
168-
}
169-
170-
// Then: deadlineMinusDisabled should be falsy
171-
const delta = maxDeadline(workspace, template).diff(now)
172-
expect(delta).toBeLessThanOrEqual(deadlineExtensionMax.asMilliseconds())
173-
})
174-
})
175-
})
176-
177-
describe("minDeadline", () => {
178-
it("should never be less than 30 minutes", () => {
179-
const delta = minDeadline().diff(now)
180-
expect(delta).toBeGreaterThanOrEqual(deadlineExtensionMin.asMilliseconds())
181-
})
182-
})

site/src/util/workspace.ts

-16
Original file line numberDiff line numberDiff line change
@@ -254,19 +254,3 @@ export const getFaviconByStatus = (build: TypesGen.WorkspaceBuild): FaviconType
254254
}
255255
throw new Error("unknown status " + status)
256256
}
257-
258-
export const deadlineExtensionMin = dayjs.duration(30, "minutes")
259-
export const deadlineExtensionMax = dayjs.duration(24, "hours")
260-
261-
export function maxDeadline(ws: TypesGen.Workspace, tpl: TypesGen.Template): dayjs.Dayjs {
262-
// note: we count runtime from updated_at as started_at counts from the start of
263-
// the workspace build process, which can take a while.
264-
const startedAt = dayjs(ws.latest_build.updated_at)
265-
const maxTemplateDeadline = startedAt.add(dayjs.duration(tpl.max_ttl_ms, "milliseconds"))
266-
const maxGlobalDeadline = startedAt.add(deadlineExtensionMax)
267-
return dayjs.min(maxTemplateDeadline, maxGlobalDeadline)
268-
}
269-
270-
export function minDeadline(): dayjs.Dayjs {
271-
return dayjs().add(deadlineExtensionMin)
272-
}

0 commit comments

Comments
 (0)