Skip to content

fix: use template default ttl when enabling auto-stop #5494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
defaultSchedule,
emptySchedule,
} from "pages/WorkspaceSchedulePage/schedule"
import { defaultTTL, emptyTTL } from "pages/WorkspaceSchedulePage/ttl"
import { emptyTTL } from "pages/WorkspaceSchedulePage/ttl"
import { makeMockApiError } from "testHelpers/entities"
import {
WorkspaceScheduleForm,
Expand Down Expand Up @@ -39,7 +39,7 @@ const defaultInitialValues = {
autoStartEnabled: true,
...defaultSchedule(),
autoStopEnabled: true,
ttl: defaultTTL,
ttl: 24,
}

export const AllDisabled = Template.bind({})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
defaultSchedule,
emptySchedule,
} from "pages/WorkspaceSchedulePage/schedule"
import { defaultTTL } from "pages/WorkspaceSchedulePage/ttl"
import { ChangeEvent, FC } from "react"
import * as Yup from "yup"
import { getFormHelpers } from "../../util/formUtils"
Expand Down Expand Up @@ -81,6 +80,7 @@ export interface WorkspaceScheduleFormProps {
onSubmit: (values: WorkspaceScheduleFormValues) => void
// for storybook
initialTouched?: FormikTouched<WorkspaceScheduleFormValues>
defaultTTL: number
}

export interface WorkspaceScheduleFormValues {
Expand Down Expand Up @@ -192,6 +192,7 @@ export const WorkspaceScheduleForm: FC<
onCancel,
onSubmit,
initialTouched,
defaultTTL,
}) => {
const styles = useStyles()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
} from "components/WorkspaceScheduleForm/WorkspaceScheduleForm"
import { WorkspaceSchedulePage } from "./WorkspaceSchedulePage"
import i18next from "i18next"
import { server } from "testHelpers/server"
import { rest } from "msw"

const { t } = i18next

Expand Down Expand Up @@ -295,4 +297,37 @@ describe("WorkspaceSchedulePage", () => {
expect(dialog).not.toBeInTheDocument()
})
})

describe("autostop", () => {
it("uses template default ttl when first enabled", async () => {
// have auto-stop disabled
server.use(
rest.get(
"/api/v2/users/:userId/workspace/:workspaceName",
(req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({ ...MockWorkspace, ttl_ms: 0 }),
)
},
),
)
renderWithAuth(<WorkspaceSchedulePage />, {
route: `/@${MockUser.username}/${MockWorkspace.name}/schedule`,
path: "/@:username/:workspace/schedule",
})
const user = userEvent.setup()
const autoStopToggle = await screen.findByLabelText(
FormLanguage.stopSwitch,
)
// enable auto-stop
await user.click(autoStopToggle)
// find helper text that describes the mock template's 24 hour default
const autoStopHelperText = await screen.findByText(
"Your workspace will shut down a day after",
{ exact: false },
)
expect(autoStopHelperText).toBeDefined()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMachine } from "@xstate/react"
import { AlertBanner } from "components/AlertBanner/AlertBanner"
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
import { Margins } from "components/Margins/Margins"
import dayjs from "dayjs"
import { scheduleToAutoStart } from "pages/WorkspaceSchedulePage/schedule"
import { ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl"
import React, { useEffect } from "react"
Expand Down Expand Up @@ -46,6 +47,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
getTemplateError,
permissions,
workspace,
template,
} = scheduleState.context

// Get workspace on mount and whenever the args for getting a workspace change.
Expand All @@ -60,7 +62,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
return <Navigate to="/workspaces" />
}

if (scheduleState.hasTag("loading")) {
if (scheduleState.hasTag("loading") || !template) {
return <FullScreenLoader />
}

Expand Down Expand Up @@ -104,6 +106,7 @@ export const WorkspaceSchedulePage: React.FC = () => {
...getAutoStop(workspace),
}}
isLoading={scheduleState.tags.has("loading")}
defaultTTL={dayjs.duration(template.default_ttl_ms, "ms").asHours()}
onCancel={() => {
navigate(`/@${username}/${workspaceName}`)
}}
Expand Down
2 changes: 0 additions & 2 deletions site/src/pages/WorkspaceSchedulePage/ttl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export interface AutoStop {

export const emptyTTL = 0

export const defaultTTL = 12

const msToHours = (ms: number) => Math.round(ms / (1000 * 60 * 60))

export const ttlMsToAutoStop = (ttl_ms?: number): AutoStop =>
Expand Down