Skip to content

feat: allow autostop to be specified in minutes and seconds #10707

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 2 commits into from
Nov 15, 2023
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 @@ -175,34 +175,63 @@ describe("validationSchema", () => {
const validate = () => validationSchema.validateSync(values);
expect(validate).toThrowError(Language.errorTtlMax);
});

it("allows a ttl of 1.2 hours", () => {
const values: WorkspaceScheduleFormValues = {
...valid,
ttl: 1.2,
};
const validate = () => validationSchema.validateSync(values);
expect(validate).not.toThrowError();
});
});

describe("ttlShutdownAt", () => {
it.each<[string, number, string]>([
[
"Manual shutdown --> manual helper text",
0,
Language.ttlCausesNoShutdownHelperText,
"Your workspace will not automatically shut down.",
],
[
"One hour --> helper text shows shutdown after an hour",
"One hour --> helper text shows shutdown after 1 hour",
1,
`${Language.ttlCausesShutdownHelperText} an hour ${Language.ttlCausesShutdownAfterStart}.`,
`Your workspace will shut down 1 hour after its next start. We delay shutdown by this time whenever we detect activity.`,
],
[
"Two hours --> helper text shows shutdown after 2 hours",
2,
`${Language.ttlCausesShutdownHelperText} 2 hours ${Language.ttlCausesShutdownAfterStart}.`,
`Your workspace will shut down 2 hours after its next start. We delay shutdown by this time whenever we detect activity.`,
],
[
"24 hours --> helper text shows shutdown after a day",
"24 hours --> helper text shows shutdown after 1 day",
24,
`${Language.ttlCausesShutdownHelperText} a day ${Language.ttlCausesShutdownAfterStart}.`,
`Your workspace will shut down 1 day after its next start. We delay shutdown by this time whenever we detect activity.`,
],
[
"48 hours --> helper text shows shutdown after 2 days",
48,
`${Language.ttlCausesShutdownHelperText} 2 days ${Language.ttlCausesShutdownAfterStart}.`,
`Your workspace will shut down 2 days after its next start. We delay shutdown by this time whenever we detect activity.`,
],
[
"1.2 hours --> helper text shows shutdown after 1 hour and 12 minutes",
1.2,
`Your workspace will shut down 1 hour and 12 minutes after its next start. We delay shutdown by this time whenever we detect activity.`,
],
[
"24.2 hours --> helper text shows shutdown after 1 day and 12 minutes",
24.2,
`Your workspace will shut down 1 day and 12 minutes after its next start. We delay shutdown by this time whenever we detect activity.`,
],
[
"0.2 hours --> helper text shows shutdown after 12 minutes",
0.2,
`Your workspace will shut down 12 minutes after its next start. We delay shutdown by this time whenever we detect activity.`,
],
[
"48.258 hours --> helper text shows shutdown after 2 days and 15 minutes and 28 seconds",
48.258,
`Your workspace will shut down 2 days and 15 minutes and 28 seconds after its next start. We delay shutdown by this time whenever we detect activity.`,
],
])("%p", (_, ttlHours, expected) => {
expect(ttlShutdownAt(ttlHours)).toEqual(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { getFormHelpers } from "utils/formUtils";
import { timeZones } from "utils/timeZones";
import { Pill } from "components/Pill/Pill";
import Tooltip from "@mui/material/Tooltip";
import { formatDuration, intervalToDuration } from "date-fns";

// REMARK: some plugins depend on utc, so it's listed first. Otherwise they're
// sorted alphabetically.
Expand Down Expand Up @@ -61,11 +62,6 @@ export const Language = {
startTimeLabel: "Start time",
timezoneLabel: "Timezone",
ttlLabel: "Time until shutdown (hours)",
ttlCausesShutdownHelperText: "Your workspace will shut down",
ttlCausesShutdownAfterStart:
"after its next start. We delay shutdown by this time whenever we detect activity",
ttlCausesNoShutdownHelperText:
"Your workspace will not automatically shut down.",
formTitle: "Workspace schedule",
startSection: "Start",
startSwitch: "Enable Autostart",
Expand Down Expand Up @@ -173,7 +169,6 @@ export const validationSchema = Yup.object({
}
}),
ttl: Yup.number()
.integer()
.min(0)
.max(24 * 30 /* 30 days */, Language.errorTtlMax)
.test("positive-if-autostop", Language.errorNoStop, function (value) {
Expand Down Expand Up @@ -404,7 +399,7 @@ export const WorkspaceScheduleForm: FC<
<TextField
{...formHelpers("ttl", ttlShutdownAt(form.values.ttl), "ttl_ms")}
disabled={isLoading || !form.values.autostopEnabled}
inputProps={{ min: 0, step: 1 }}
inputProps={{ min: 0, step: "any" }}
Copy link
Member

@Parkreiner Parkreiner Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the validator got updated to remove the integer constraint, I'm guessing the step was changed to allow floats. Just want to make sure, though: are negative and zero steps are expected here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Parkreiner Good eye! Don't worry - we still validate against negative and 0 inputs here.

I am using a step of 'any' because this defaults to a step of 1 while still allowing for floats.

label={Language.ttlLabel}
type="number"
fullWidth
Expand All @@ -417,12 +412,13 @@ export const WorkspaceScheduleForm: FC<
};

export const ttlShutdownAt = (formTTL: number): string => {
if (formTTL < 1) {
if (formTTL === 0) {
// Passing an empty value for TTL in the form results in a number that is not zero but less than 1.
return Language.ttlCausesNoShutdownHelperText;
return "Your workspace will not automatically shut down.";
} else {
return `${Language.ttlCausesShutdownHelperText} ${dayjs
.duration(formTTL, "hours")
.humanize()} ${Language.ttlCausesShutdownAfterStart}.`;
return `Your workspace will shut down ${formatDuration(
intervalToDuration({ start: 0, end: formTTL * 60 * 60 * 1000 }),
{ delimiter: " and " },
)} after its next start. We delay shutdown by this time whenever we detect activity.`;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ describe("WorkspaceSchedulePage", () => {
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",
"Your workspace will shut down 1 day after",
{ exact: false },
);
expect(autostopHelperText).toBeDefined();
Expand Down