Skip to content

fix: send auto start/stop api calls only when changed #5184

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 3 commits into from
Nov 29, 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 @@ -4,6 +4,7 @@ import { scheduleToAutoStart } from "pages/WorkspaceSchedulePage/schedule"
import { ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl"
import React, { useEffect, useState } from "react"
import { Navigate, useNavigate, useParams } from "react-router-dom"
import { scheduleChanged } from "util/schedule"
import * as TypesGen from "../../api/typesGenerated"
import { FullScreenLoader } from "../../components/Loader/FullScreenLoader"
import { WorkspaceScheduleForm } from "../../components/WorkspaceScheduleForm/WorkspaceScheduleForm"
Expand Down Expand Up @@ -108,10 +109,10 @@ export const WorkspaceSchedulePage: React.FC = () => {
onSubmit={(values) => {
scheduleSend({
type: "SUBMIT_SCHEDULE",
autoStart: values.autoStartEnabled
? formValuesToAutoStartRequest(values)
: undefined,
autoStart: formValuesToAutoStartRequest(values),
ttl: formValuesToTTLRequest(values),
autoStartChanged: scheduleChanged(autoStart, values),
autoStopChanged: scheduleChanged(autoStop, values),
})
}}
/>
Expand Down
71 changes: 71 additions & 0 deletions site/src/util/schedule.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import dayjs from "dayjs"
import duration from "dayjs/plugin/duration"
import { emptySchedule } from "pages/WorkspaceSchedulePage/schedule"
import { emptyTTL } from "pages/WorkspaceSchedulePage/ttl"
import { Template, Workspace } from "../api/typesGenerated"
import * as Mocks from "../testHelpers/entities"
import {
Expand All @@ -12,6 +14,7 @@ import {
getMaxDeadlineChange,
getMinDeadline,
stripTimezone,
scheduleChanged,
} from "./schedule"

dayjs.extend(duration)
Expand Down Expand Up @@ -141,3 +144,71 @@ describe("getMaxDeadlineChange", () => {
expect(getMaxDeadlineChange(deadline, minDeadline)).toEqual(2)
})
})

describe("scheduleChanged", () => {
describe("autoStart", () => {
it("should be true if toggle values are different", () => {
const autoStart = { autoStartEnabled: true, ...emptySchedule }
const formValues = {
autoStartEnabled: false,
...emptySchedule,
autoStopEnabled: false,
ttl: emptyTTL,
}
expect(scheduleChanged(autoStart, formValues)).toBe(true)
})
it("should be true if schedule values are different", () => {
const autoStart = { autoStartEnabled: true, ...emptySchedule }
const formValues = {
autoStartEnabled: true,
...{ ...emptySchedule, monday: true, startTime: "09:00" },
autoStopEnabled: false,
ttl: emptyTTL,
}
expect(scheduleChanged(autoStart, formValues)).toBe(true)
})
it("should be false if all autostart values are the same", () => {
const autoStart = { autoStartEnabled: true, ...emptySchedule }
const formValues = {
autoStartEnabled: true,
...emptySchedule,
autoStopEnabled: false,
ttl: emptyTTL,
}
expect(scheduleChanged(autoStart, formValues)).toBe(false)
})
})

describe("autoStop", () => {
it("should be true if toggle values are different", () => {
const autoStop = { autoStopEnabled: true, ttl: 1000 }
const formValues = {
autoStartEnabled: false,
...emptySchedule,
autoStopEnabled: false,
ttl: 1000,
}
expect(scheduleChanged(autoStop, formValues)).toBe(true)
})
it("should be true if ttl values are different", () => {
const autoStop = { autoStopEnabled: true, ttl: 1000 }
const formValues = {
autoStartEnabled: false,
...emptySchedule,
autoStopEnabled: true,
ttl: 2000,
}
expect(scheduleChanged(autoStop, formValues)).toBe(true)
})
it("should be false if all autostop values are the same", () => {
const autoStop = { autoStopEnabled: true, ttl: 1000 }
const formValues = {
autoStartEnabled: false,
...emptySchedule,
autoStopEnabled: true,
ttl: 1000,
}
expect(scheduleChanged(autoStop, formValues)).toBe(false)
})
})
})
17 changes: 17 additions & 0 deletions site/src/util/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import map from "lodash/map"
import some from "lodash/some"
import cronstrue from "cronstrue"
import dayjs, { Dayjs } from "dayjs"
import advancedFormat from "dayjs/plugin/advancedFormat"
Expand All @@ -7,6 +9,9 @@ import timezone from "dayjs/plugin/timezone"
import utc from "dayjs/plugin/utc"
import { Template, Workspace } from "../api/typesGenerated"
import { isWorkspaceOn } from "./workspace"
import { WorkspaceScheduleFormValues } from "components/WorkspaceScheduleForm/WorkspaceScheduleForm"
import { AutoStop } from "pages/WorkspaceSchedulePage/ttl"
import { AutoStart } from "pages/WorkspaceSchedulePage/schedule"

// REMARK: some plugins depend on utc, so it's listed first. Otherwise they're
// sorted alphabetically.
Expand Down Expand Up @@ -179,3 +184,15 @@ export const getMaxDeadlineChange = (
deadline: dayjs.Dayjs,
extremeDeadline: dayjs.Dayjs,
): number => Math.abs(deadline.diff(extremeDeadline, "hours"))

export const scheduleChanged = (
initialValues: AutoStart | AutoStop,
formValues: WorkspaceScheduleFormValues,
): boolean =>
some(
map(
{ ...initialValues },
(v: boolean | string, k: keyof typeof initialValues) =>
formValues[k] !== v,
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ export type WorkspaceScheduleEvent =
| { type: "GET_WORKSPACE"; username: string; workspaceName: string }
| {
type: "SUBMIT_SCHEDULE"
autoStart: TypesGen.UpdateWorkspaceAutostartRequest | undefined
autoStart: TypesGen.UpdateWorkspaceAutostartRequest
autoStartChanged: boolean
ttl: TypesGen.UpdateWorkspaceTTLRequest
autoStopChanged: boolean
}

export const workspaceSchedule = createMachine(
Expand Down Expand Up @@ -195,10 +197,12 @@ export const workspaceSchedule = createMachine(
throw new Error("Failed to load workspace.")
}

if (event.autoStart?.schedule !== undefined) {
if (event.autoStartChanged) {
await API.putWorkspaceAutostart(context.workspace.id, event.autoStart)
}
await API.putWorkspaceAutostop(context.workspace.id, event.ttl)
if (event.autoStopChanged) {
Copy link
Member

Choose a reason for hiding this comment

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

Nice! Should we write a test for this?

await API.putWorkspaceAutostop(context.workspace.id, event.ttl)
}
},
},
},
Expand Down