Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify, wip
  • Loading branch information
presleyp committed Dec 20, 2022
commit 56c2a793340a0c769539499b5178f2343dc9d45c
11 changes: 0 additions & 11 deletions site/src/components/Workspace/Workspace.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { action } from "@storybook/addon-actions"
import { Story } from "@storybook/react"
import dayjs from "dayjs"
import { canExtendDeadline, canReduceDeadline } from "util/schedule"
import * as Mocks from "../../testHelpers/entities"
import { Workspace, WorkspaceErrors, WorkspaceProps } from "./Workspace"

Expand All @@ -22,15 +20,6 @@ Running.args = {
onDeadlinePlus: () => {
// do nothing, this is just for storybook
},
deadlineMinusEnabled: () => {
return canReduceDeadline(dayjs(Mocks.MockWorkspace.latest_build.deadline))
},
deadlinePlusEnabled: () => {
return canExtendDeadline(
dayjs(Mocks.MockWorkspace.latest_build.deadline),
Mocks.MockWorkspace,
)
},
maxDeadlineDecrease: 1000,
maxDeadlineIncrease: 1000,
},
Expand Down
46 changes: 0 additions & 46 deletions site/src/util/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { emptyTTL } from "pages/WorkspaceSchedulePage/ttl"
import { Workspace } from "../api/typesGenerated"
import * as Mocks from "../testHelpers/entities"
import {
canExtendDeadline,
canReduceDeadline,
deadlineExtensionMax,
deadlineExtensionMin,
extractTimezone,
Expand Down Expand Up @@ -64,50 +62,6 @@ describe("minDeadline", () => {
})
})

describe("canExtendDeadline", () => {
it("should be falsy if the deadline is more than 24 hours from the start time", () => {
expect(
canExtendDeadline(
startTime.add(25, "hours"),
Mocks.MockWorkspace,
),
).toBeFalsy()
})
it("should be falsy if the deadline is less than an hour below the max deadline", () => {
expect(
canExtendDeadline(
startTime.add(23.5, "hours"),
Mocks.MockWorkspace
)
).toBeFalsy()
})
it("should be true if the deadline is one hour below the max deadline", () => {
expect(
canExtendDeadline(
startTime.add(23, "hours"),
Mocks.MockWorkspace
)
).toBeTruthy()
})
})

describe("canReduceDeadline", () => {
// the minimum ttl is 30 minutes from the current time
// ttl can be reduced by one hour at a time
// so current deadline must be >=90 minutes from current time to be reducible
it("should be falsy if the deadline is 90 minutes or less in the future", () => {
expect(canReduceDeadline(dayjs())).toBeFalsy()
expect(canReduceDeadline(dayjs().add(1, "minutes"))).toBeFalsy()
expect(canReduceDeadline(dayjs().add(89, "minutes"))).toBeFalsy()
expect(canReduceDeadline(dayjs().add(90, "minutes"))).toBeTruthy()
})

it("should be truthy if the deadline is 90 minutes or more in the future", () => {
expect(canReduceDeadline(dayjs().add(91, "minutes"))).toBeTruthy()
expect(canReduceDeadline(dayjs().add(100, "years"))).toBeTruthy()
})
})

describe("getMaxDeadlineChange", () => {
it("should return the number of hours you can add before hitting the max deadline", () => {
const deadline = dayjs()
Expand Down
25 changes: 0 additions & 25 deletions site/src/util/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,6 @@ export function getMinDeadline(): dayjs.Dayjs {
return dayjs().add(deadlineExtensionMin)
}

/**
* Determines if ScheduleBanner can increase ttl by one or more hours
* without hitting the global max deadline.
* @param deadline
* @param workspace
*/
export function canExtendDeadline(
deadline: dayjs.Dayjs,
workspace: Workspace,
): boolean {
const diff = (getMaxDeadline(workspace)).diff(deadline, 'hours')
return diff >= 1
}

/**
* Determines if ScheduleBanner can reduce ttl by one or more hours
* without hitting the global min remaining time to live.
* Depends on workspace deadline, current time, and a global constant.
* @param deadline current workspace deadline
*/
export function canReduceDeadline(deadline: dayjs.Dayjs): boolean {
const diff = deadline.diff(getMinDeadline(), 'hours')
return diff >= 1
}

export const getDeadline = (workspace: Workspace): dayjs.Dayjs =>
dayjs(workspace.latest_build.deadline).utc()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Workspace } from "api/typesGenerated"
import dayjs from "dayjs"
import minMax from "dayjs/plugin/minMax"
import {
canExtendDeadline,
canReduceDeadline,
getDeadline,
getMaxDeadline,
getMinDeadline,
Expand Down Expand Up @@ -58,51 +56,27 @@ export const workspaceScheduleBannerMachine = createMachine(
events: {} as WorkspaceScheduleBannerEvent,
context: {} as WorkspaceScheduleBannerContext,
},
initial: "initialize",
initial: "idle",
on: {
REFRESH_WORKSPACE: { actions: "assignWorkspace" },
},
states: {
initialize: {
always: [
{ cond: "isAtMaxDeadline", target: "atMaxDeadline" },
{ cond: "isAtMinDeadline", target: "atMinDeadline" },
{ target: "midRange" },
],
},
midRange: {
idle: {
on: {
INCREASE_DEADLINE: "increasingDeadline",
DECREASE_DEADLINE: "decreasingDeadline",
},
},
atMaxDeadline: {
on: {
DECREASE_DEADLINE: "decreasingDeadline",
},
},
atMinDeadline: {
on: {
INCREASE_DEADLINE: "increasingDeadline",
},
},
increasingDeadline: {
invoke: {
src: "increaseDeadline",
id: "increaseDeadline",
onDone: [
{
cond: "isAtMaxDeadline",
target: "atMaxDeadline",
actions: "displaySuccessMessage",
},
{
target: "midRange",
actions: "displaySuccessMessage",
},
],
onDone: {
target: "idle",
actions: "displaySuccessMessage",
},
onError: {
target: "midRange",
target: "idle",
actions: "displayFailureMessage",
},
},
Expand All @@ -112,19 +86,12 @@ export const workspaceScheduleBannerMachine = createMachine(
invoke: {
src: "decreaseDeadline",
id: "decreaseDeadline",
onDone: [
{
cond: "isAtMinDeadline",
target: "atMinDeadline",
actions: "displaySuccessMessage",
},
{
target: "midRange",
actions: "displaySuccessMessage",
},
],
onDone: {
target: "idle",
actions: "displaySuccessMessage",
},
onError: {
target: "midRange",
target: "idle",
actions: "displayFailureMessage",
},
},
Expand All @@ -133,19 +100,6 @@ export const workspaceScheduleBannerMachine = createMachine(
},
},
{
guards: {
isAtMaxDeadline: (context) => {
return context.workspace.latest_build.deadline
? !canExtendDeadline(
getDeadline(context.workspace),
context.workspace,
)
: false
},
isAtMinDeadline: (context) => {
return context.workspace.latest_build.deadline ? !canReduceDeadline(getDeadline(context.workspace)) : false
}
},
actions: {
// This error does not have a detail, so using the snackbar is okay
displayFailureMessage: (_, event) => {
Expand Down