Skip to content

chore: refactor time.Duration -> int64 milliseconds for FE consumption #1944

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 8 commits into from
Jun 2, 2022
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
refactor: typegen
  • Loading branch information
johnstcn committed Jun 2, 2022
commit 1af3630e84cb0781fea223293df01d67cc2c8edc
8 changes: 4 additions & 4 deletions site/src/components/WorkspaceSchedule/WorkspaceSchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ dayjs.extend(duration)
dayjs.extend(relativeTime)

export const Language = {
autoStartDisplay: (schedule: string): string => {
autoStartDisplay: (schedule: string | undefined): string => {
if (schedule) {
return cronstrue.toString(stripTimezone(schedule), { throwExceptionOnParseError: false })
}
return "Manual"
},
autoStartLabel: (schedule: string): string => {
autoStartLabel: (schedule: string | undefined): string => {
const prefix = "Start"

if (schedule) {
Expand All @@ -40,7 +40,7 @@ export const Language = {
// a mannual shutdown has a deadline of '"0001-01-01T00:00:00Z"'
// SEE: #1834
const hasDeadline = deadline.year() > 1
const ttl = workspace.ttl
const ttl = workspace.ttl_ms

if (isWorkspaceOn(workspace) && hasDeadline) {
// Workspace is on --> derive from latest_build.deadline. Note that the
Expand All @@ -61,7 +61,7 @@ export const Language = {
} else {
// The workspace has a ttl set, but is either in an unknown state or is
// not running. Therefore, we derive from workspace.ttl.
const duration = dayjs.duration(ttl / 1_000_000, "milliseconds")
const duration = dayjs.duration(ttl, "milliseconds")
return `${duration.humanize()} after start`
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe("WorkspaceSchedulePage", () => {
ttl: 0,
},
{
ttl: undefined,
ttl_ms: undefined,
},
],
[
Expand All @@ -133,7 +133,7 @@ describe("WorkspaceSchedulePage", () => {
ttl: 2,
},
{
ttl: 7_200_000_000_000,
ttl_ms: 7_200_000,
},
],
[
Expand All @@ -143,7 +143,7 @@ describe("WorkspaceSchedulePage", () => {
ttl: 8,
},
{
ttl: 28_800_000_000_000,
ttl_ms: 28_800_000,
},
],
])(`formValuesToTTLRequest(%p) returns %p`, (values, request) => {
Expand All @@ -157,8 +157,8 @@ describe("WorkspaceSchedulePage", () => {
[
{
...Mocks.MockWorkspace,
autostart_schedule: "",
ttl: undefined,
autostart_schedule: undefined,
ttl_ms: undefined,
},
{
sunday: false,
Expand All @@ -179,7 +179,7 @@ describe("WorkspaceSchedulePage", () => {
{
...Mocks.MockWorkspace,
autostart_schedule: "",
ttl: 7_200_000_000_000,
ttl_ms: 7_200_000,
},
{
sunday: false,
Expand All @@ -203,7 +203,7 @@ describe("WorkspaceSchedulePage", () => {
{
...Mocks.MockWorkspace,
autostart_schedule: "CRON_TZ=UTC 30 9 * * 1-5",
ttl: 7_200_000_000_000,
ttl_ms: 7_200_000,
},
{
sunday: false,
Expand All @@ -224,7 +224,7 @@ describe("WorkspaceSchedulePage", () => {
{
...Mocks.MockWorkspace,
autostart_schedule: "CRON_TZ=Canada/Eastern 20 16 * * 1,3-4,6",
ttl: 28_800_000_000_000,
ttl_ms: 28_800_000,
},
{
sunday: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ export const formValuesToAutoStartRequest = (
export const formValuesToTTLRequest = (values: WorkspaceScheduleFormValues): TypesGen.UpdateWorkspaceTTLRequest => {
return {
// minutes to nanoseconds
ttl: values.ttl ? values.ttl * 60 * 60 * 1000 * 1_000_000 : undefined,
ttl_ms: values.ttl ? values.ttl * 60 * 60 * 1000 : undefined,
}
}

export const workspaceToInitialValues = (workspace: TypesGen.Workspace): WorkspaceScheduleFormValues => {
const schedule = workspace.autostart_schedule
const ttl = workspace.ttl ? workspace.ttl / (1_000_000 * 1000 * 60 * 60) : 0
const ttl = workspace.ttl_ms ? workspace.ttl_ms / (1000 * 60 * 60) : 0

if (!schedule) {
return {
Expand All @@ -106,7 +106,7 @@ export const workspaceToInitialValues = (workspace: TypesGen.Workspace): Workspa
saturday: false,
startTime: "",
timezone: "",
ttl,
ttl: ttl,
}
}

Expand All @@ -133,7 +133,7 @@ export const workspaceToInitialValues = (workspace: TypesGen.Workspace): Workspa
saturday: weeklyFlags[6],
startTime: `${HH.padStart(2, "0")}:${mm.padStart(2, "0")}`,
timezone,
ttl,
ttl: ttl,
}
}

Expand Down
2 changes: 1 addition & 1 deletion site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const MockWorkspace: TypesGen.Workspace = {
owner_id: MockUser.id,
owner_name: MockUser.username,
autostart_schedule: MockWorkspaceAutostartEnabled.schedule,
ttl: 2 * 60 * 60 * 1000 * 1_000_000, // 2 hours as nanoseconds
ttl_ms: 2 * 60 * 60 * 1000 * 1_000_000, // 2 hours as nanoseconds
latest_build: MockWorkspaceBuild,
}

Expand Down