Skip to content

Commit fcbe2d3

Browse files
presleyppull[bot]
authored andcommitted
fix: send auto start/stop api calls only when changed (#5184)
* Send auto start/stop api calls only when changed * Format * Extract and test util function
1 parent 7a0a683 commit fcbe2d3

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { scheduleToAutoStart } from "pages/WorkspaceSchedulePage/schedule"
44
import { ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl"
55
import React, { useEffect, useState } from "react"
66
import { Navigate, useNavigate, useParams } from "react-router-dom"
7+
import { scheduleChanged } from "util/schedule"
78
import * as TypesGen from "../../api/typesGenerated"
89
import { FullScreenLoader } from "../../components/Loader/FullScreenLoader"
910
import { WorkspaceScheduleForm } from "../../components/WorkspaceScheduleForm/WorkspaceScheduleForm"
@@ -108,10 +109,10 @@ export const WorkspaceSchedulePage: React.FC = () => {
108109
onSubmit={(values) => {
109110
scheduleSend({
110111
type: "SUBMIT_SCHEDULE",
111-
autoStart: values.autoStartEnabled
112-
? formValuesToAutoStartRequest(values)
113-
: undefined,
112+
autoStart: formValuesToAutoStartRequest(values),
114113
ttl: formValuesToTTLRequest(values),
114+
autoStartChanged: scheduleChanged(autoStart, values),
115+
autoStopChanged: scheduleChanged(autoStop, values),
115116
})
116117
}}
117118
/>

site/src/util/schedule.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import dayjs from "dayjs"
22
import duration from "dayjs/plugin/duration"
3+
import { emptySchedule } from "pages/WorkspaceSchedulePage/schedule"
4+
import { emptyTTL } from "pages/WorkspaceSchedulePage/ttl"
35
import { Template, Workspace } from "../api/typesGenerated"
46
import * as Mocks from "../testHelpers/entities"
57
import {
@@ -12,6 +14,7 @@ import {
1214
getMaxDeadlineChange,
1315
getMinDeadline,
1416
stripTimezone,
17+
scheduleChanged,
1518
} from "./schedule"
1619

1720
dayjs.extend(duration)
@@ -141,3 +144,71 @@ describe("getMaxDeadlineChange", () => {
141144
expect(getMaxDeadlineChange(deadline, minDeadline)).toEqual(2)
142145
})
143146
})
147+
148+
describe("scheduleChanged", () => {
149+
describe("autoStart", () => {
150+
it("should be true if toggle values are different", () => {
151+
const autoStart = { autoStartEnabled: true, ...emptySchedule }
152+
const formValues = {
153+
autoStartEnabled: false,
154+
...emptySchedule,
155+
autoStopEnabled: false,
156+
ttl: emptyTTL,
157+
}
158+
expect(scheduleChanged(autoStart, formValues)).toBe(true)
159+
})
160+
it("should be true if schedule values are different", () => {
161+
const autoStart = { autoStartEnabled: true, ...emptySchedule }
162+
const formValues = {
163+
autoStartEnabled: true,
164+
...{ ...emptySchedule, monday: true, startTime: "09:00" },
165+
autoStopEnabled: false,
166+
ttl: emptyTTL,
167+
}
168+
expect(scheduleChanged(autoStart, formValues)).toBe(true)
169+
})
170+
it("should be false if all autostart values are the same", () => {
171+
const autoStart = { autoStartEnabled: true, ...emptySchedule }
172+
const formValues = {
173+
autoStartEnabled: true,
174+
...emptySchedule,
175+
autoStopEnabled: false,
176+
ttl: emptyTTL,
177+
}
178+
expect(scheduleChanged(autoStart, formValues)).toBe(false)
179+
})
180+
})
181+
182+
describe("autoStop", () => {
183+
it("should be true if toggle values are different", () => {
184+
const autoStop = { autoStopEnabled: true, ttl: 1000 }
185+
const formValues = {
186+
autoStartEnabled: false,
187+
...emptySchedule,
188+
autoStopEnabled: false,
189+
ttl: 1000,
190+
}
191+
expect(scheduleChanged(autoStop, formValues)).toBe(true)
192+
})
193+
it("should be true if ttl values are different", () => {
194+
const autoStop = { autoStopEnabled: true, ttl: 1000 }
195+
const formValues = {
196+
autoStartEnabled: false,
197+
...emptySchedule,
198+
autoStopEnabled: true,
199+
ttl: 2000,
200+
}
201+
expect(scheduleChanged(autoStop, formValues)).toBe(true)
202+
})
203+
it("should be false if all autostop values are the same", () => {
204+
const autoStop = { autoStopEnabled: true, ttl: 1000 }
205+
const formValues = {
206+
autoStartEnabled: false,
207+
...emptySchedule,
208+
autoStopEnabled: true,
209+
ttl: 1000,
210+
}
211+
expect(scheduleChanged(autoStop, formValues)).toBe(false)
212+
})
213+
})
214+
})

site/src/util/schedule.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import map from "lodash/map"
2+
import some from "lodash/some"
13
import cronstrue from "cronstrue"
24
import dayjs, { Dayjs } from "dayjs"
35
import advancedFormat from "dayjs/plugin/advancedFormat"
@@ -7,6 +9,9 @@ import timezone from "dayjs/plugin/timezone"
79
import utc from "dayjs/plugin/utc"
810
import { Template, Workspace } from "../api/typesGenerated"
911
import { isWorkspaceOn } from "./workspace"
12+
import { WorkspaceScheduleFormValues } from "components/WorkspaceScheduleForm/WorkspaceScheduleForm"
13+
import { AutoStop } from "pages/WorkspaceSchedulePage/ttl"
14+
import { AutoStart } from "pages/WorkspaceSchedulePage/schedule"
1015

1116
// REMARK: some plugins depend on utc, so it's listed first. Otherwise they're
1217
// sorted alphabetically.
@@ -179,3 +184,15 @@ export const getMaxDeadlineChange = (
179184
deadline: dayjs.Dayjs,
180185
extremeDeadline: dayjs.Dayjs,
181186
): number => Math.abs(deadline.diff(extremeDeadline, "hours"))
187+
188+
export const scheduleChanged = (
189+
initialValues: AutoStart | AutoStop,
190+
formValues: WorkspaceScheduleFormValues,
191+
): boolean =>
192+
some(
193+
map(
194+
{ ...initialValues },
195+
(v: boolean | string, k: keyof typeof initialValues) =>
196+
formValues[k] !== v,
197+
),
198+
)

site/src/xServices/workspaceSchedule/workspaceScheduleXService.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ export type WorkspaceScheduleEvent =
4545
| { type: "GET_WORKSPACE"; username: string; workspaceName: string }
4646
| {
4747
type: "SUBMIT_SCHEDULE"
48-
autoStart: TypesGen.UpdateWorkspaceAutostartRequest | undefined
48+
autoStart: TypesGen.UpdateWorkspaceAutostartRequest
49+
autoStartChanged: boolean
4950
ttl: TypesGen.UpdateWorkspaceTTLRequest
51+
autoStopChanged: boolean
5052
}
5153

5254
export const workspaceSchedule = createMachine(
@@ -195,10 +197,12 @@ export const workspaceSchedule = createMachine(
195197
throw new Error("Failed to load workspace.")
196198
}
197199

198-
if (event.autoStart?.schedule !== undefined) {
200+
if (event.autoStartChanged) {
199201
await API.putWorkspaceAutostart(context.workspace.id, event.autoStart)
200202
}
201-
await API.putWorkspaceAutostop(context.workspace.id, event.ttl)
203+
if (event.autoStopChanged) {
204+
await API.putWorkspaceAutostop(context.workspace.id, event.ttl)
205+
}
202206
},
203207
},
204208
},

0 commit comments

Comments
 (0)