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

* Changes all public-facing codersdk types to use a plain int64 (milliseconds) instead of time.Duration.
* Makes autostart_schedule a *string as it may not be present.
* Adds a utils/ptr package with some useful methods.
This commit is contained in:
Cian Johnston
2022-06-02 11:23:34 +01:00
committed by GitHub
parent 51c420c90a
commit dcf03d8ba3
24 changed files with 287 additions and 148 deletions
+5 -8
View File
@@ -101,8 +101,7 @@ export interface CreateWorkspaceRequest {
readonly template_id: string
readonly name: string
readonly autostart_schedule?: string
// This is likely an enum in an external package ("time.Duration")
readonly ttl?: number
readonly ttl_ms?: number
readonly parameter_values?: CreateParameterRequest[]
}
@@ -301,13 +300,12 @@ export interface UpdateUserProfileRequest {
// From codersdk/workspaces.go:141:6
export interface UpdateWorkspaceAutostartRequest {
readonly schedule: string
readonly schedule?: string
}
// From codersdk/workspaces.go:161:6
export interface UpdateWorkspaceTTLRequest {
// This is likely an enum in an external package ("time.Duration")
readonly ttl?: number
readonly ttl_ms?: number
}
// From codersdk/files.go:16:6
@@ -372,9 +370,8 @@ export interface Workspace {
readonly latest_build: WorkspaceBuild
readonly outdated: boolean
readonly name: string
readonly autostart_schedule: string
// This is likely an enum in an external package ("time.Duration")
readonly ttl?: number
readonly autostart_schedule?: string
readonly ttl_ms?: number
}
// From codersdk/workspaceresources.go:31:6
@@ -19,6 +19,20 @@ export default {
const Template: Story<WorkspaceScheduleProps> = (args) => <WorkspaceSchedule {...args} />
export const NoScheduleNoTTL = Template.bind({})
NoScheduleNoTTL.args = {
workspace: {
...Mocks.MockWorkspace,
latest_build: {
...Mocks.MockWorkspaceBuild,
transition: "stop",
},
autostart_schedule: undefined,
ttl_ms: undefined,
},
}
export const NoTTL = Template.bind({})
NoTTL.args = {
workspace: {
@@ -29,7 +43,7 @@ NoTTL.args = {
// SEE: #1834
deadline: "0001-01-01T00:00:00Z",
},
ttl: undefined,
ttl_ms: undefined,
},
}
@@ -42,7 +56,7 @@ ShutdownSoon.args = {
deadline: dayjs().add(ONE, "hour").utc().format(),
transition: "start",
},
ttl: 2 * 60 * 60 * 1000 * 1_000_000, // 2 hours
ttl_ms: 2 * 60 * 60 * 1000, // 2 hours
},
}
@@ -56,7 +70,7 @@ ShutdownLong.args = {
deadline: dayjs().add(SEVEN, "days").utc().format(),
transition: "start",
},
ttl: 7 * 24 * 60 * 60 * 1000 * 1_000_000, // 7 days
ttl_ms: 7 * 24 * 60 * 60 * 1000, // 7 days
},
}
@@ -69,7 +83,7 @@ WorkspaceOffShort.args = {
...Mocks.MockWorkspaceBuild,
transition: "stop",
},
ttl: 2 * 60 * 60 * 1000 * 1_000_000, // 2 hours
ttl_ms: 2 * 60 * 60 * 1000, // 2 hours
},
}
@@ -82,6 +96,6 @@ WorkspaceOffLong.args = {
...Mocks.MockWorkspaceBuild,
transition: "stop",
},
ttl: 2 * 365 * 24 * 60 * 60 * 1000 * 1_000_000, // 2 years
ttl_ms: 2 * 365 * 24 * 60 * 60 * 1000, // 2 years
},
}
@@ -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) {
@@ -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
@@ -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`
}
},
@@ -123,7 +123,7 @@ describe("WorkspaceSchedulePage", () => {
ttl: 0,
},
{
ttl: undefined,
ttl_ms: undefined,
},
],
[
@@ -133,7 +133,7 @@ describe("WorkspaceSchedulePage", () => {
ttl: 2,
},
{
ttl: 7_200_000_000_000,
ttl_ms: 7_200_000,
},
],
[
@@ -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) => {
@@ -157,8 +157,8 @@ describe("WorkspaceSchedulePage", () => {
[
{
...Mocks.MockWorkspace,
autostart_schedule: "",
ttl: undefined,
autostart_schedule: undefined,
ttl_ms: undefined,
},
{
sunday: false,
@@ -179,7 +179,7 @@ describe("WorkspaceSchedulePage", () => {
{
...Mocks.MockWorkspace,
autostart_schedule: "",
ttl: 7_200_000_000_000,
ttl_ms: 7_200_000,
},
{
sunday: false,
@@ -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,
@@ -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,
@@ -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 {
+1 -1
View File
@@ -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, // 2 hours as milliseconds
latest_build: MockWorkspaceBuild,
}