feat: Add codersdk.NullTime, change workspace build deadline (#3552)

Fixes #2015

Co-authored-by: Joe Previte <jjprevite@gmail.com>
This commit is contained in:
Mathias Fredriksson
2022-08-25 19:10:42 +03:00
committed by GitHub
co-authored by Joe Previte
parent a21a6d2f4a
commit 78a24941fe
18 changed files with 271 additions and 63 deletions
+1 -1
View File
@@ -527,7 +527,7 @@ export interface WorkspaceBuild {
readonly initiator_id: string
readonly initiator_name: string
readonly job: ProvisionerJob
readonly deadline: string
readonly deadline?: string
readonly reason: BuildReason
}
@@ -38,9 +38,7 @@ NoTTL.args = {
...Mocks.MockWorkspace,
latest_build: {
...Mocks.MockWorkspaceBuild,
// a manual shutdown has a deadline of '"0001-01-01T00:00:00Z"'
// SEE: #1834
deadline: "0001-01-01T00:00:00Z",
deadline: undefined,
},
ttl_ms: undefined,
},
@@ -10,12 +10,12 @@ describe("WorkspaceScheduleBanner", () => {
describe("shouldDisplay", () => {
// Manual TTL case
it("should not display if the build does not have a deadline", () => {
// Given: a workspace with deadline of '"0001-01-01T00:00:00Z"'
// Given: a workspace with deadline of undefined.
const workspace: TypesGen.Workspace = {
...Mocks.MockWorkspace,
latest_build: {
...Mocks.MockWorkspaceBuild,
deadline: "0001-01-01T00:00:00Z",
deadline: undefined,
transition: "start",
},
}
@@ -23,16 +23,12 @@ export interface WorkspaceScheduleBannerProps {
}
export const shouldDisplay = (workspace: TypesGen.Workspace): boolean => {
if (!isWorkspaceOn(workspace)) {
if (!isWorkspaceOn(workspace) || !workspace.latest_build.deadline) {
return false
} else {
// a manual shutdown has a deadline of '"0001-01-01T00:00:00Z"'
// SEE: #1834
const deadline = dayjs(workspace.latest_build.deadline).utc()
const hasDeadline = deadline.year() > 1
const thirtyMinutesFromNow = dayjs().add(30, "minutes").utc()
return hasDeadline && deadline.isSameOrBefore(thirtyMinutesFromNow)
}
const deadline = dayjs(workspace.latest_build.deadline).utc()
const thirtyMinutesFromNow = dayjs().add(30, "minutes").utc()
return deadline.isSameOrBefore(thirtyMinutesFromNow)
}
export const WorkspaceScheduleBanner: FC<React.PropsWithChildren<WorkspaceScheduleBannerProps>> = ({
@@ -40,9 +40,7 @@ NoTTL.args = {
...Mocks.MockWorkspace,
latest_build: {
...Mocks.MockWorkspaceBuild,
// a manual shutdown has a deadline of '"0001-01-01T00:00:00Z"'
// SEE: #1834
deadline: "0001-01-01T00:00:00Z",
deadline: undefined,
},
ttl_ms: undefined,
},
@@ -28,11 +28,7 @@ dayjs.extend(relativeTime)
dayjs.extend(timezone)
export const shouldDisplayPlusMinus = (workspace: Workspace): boolean => {
if (!isWorkspaceOn(workspace)) {
return false
}
const deadline = dayjs(workspace.latest_build.deadline).utc()
return deadline.year() > 1
return isWorkspaceOn(workspace) && Boolean(workspace.latest_build.deadline)
}
export interface WorkspaceScheduleButtonProps {
+6 -7
View File
@@ -73,27 +73,26 @@ export const autoStartDisplay = (schedule: string | undefined): string => {
export const isShuttingDown = (workspace: Workspace, deadline?: Dayjs): boolean => {
if (!deadline) {
if (!workspace.latest_build.deadline) {
return false
}
deadline = dayjs(workspace.latest_build.deadline).utc()
}
const hasDeadline = deadline.year() > 1
const now = dayjs().utc()
return isWorkspaceOn(workspace) && hasDeadline && now.isAfter(deadline)
return isWorkspaceOn(workspace) && now.isAfter(deadline)
}
export const autoStopDisplay = (workspace: Workspace): string => {
const deadline = dayjs(workspace.latest_build.deadline).utc()
// a manual shutdown has a deadline of '"0001-01-01T00:00:00Z"'
// SEE: #1834
const hasDeadline = deadline.year() > 1
const ttl = workspace.ttl_ms
if (isWorkspaceOn(workspace) && hasDeadline) {
if (isWorkspaceOn(workspace) && workspace.latest_build.deadline) {
// Workspace is on --> derive from latest_build.deadline. Note that the
// user may modify their workspace object (ttl) while the workspace is
// running and depending on system semantics, the deadline may still
// represent the previously defined ttl. Thus, we always derive from the
// deadline as the source of truth.
const deadline = dayjs(workspace.latest_build.deadline).utc()
if (isShuttingDown(workspace, deadline)) {
return Language.workspaceShuttingDownLabel
} else {