- Notifications
You must be signed in to change notification settings - Fork928
feat: workspace view for schedules#991
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -54,14 +54,18 @@ export interface CreateWorkspaceRequest { | ||
template_id: string | ||
} | ||
/** | ||
* @remarks Keep in sync with codersdk/workspaces.go | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. nice | ||
export interface Workspace { | ||
id: string | ||
created_at: string | ||
updated_at: string | ||
owner_id: string | ||
template_id: string | ||
name: string | ||
autostart_schedule: string | ||
autostop_schedule: string | ||
} | ||
export interface APIKeyResponse { | ||
@@ -74,3 +78,11 @@ export interface UserAgent { | ||
readonly ip_address: string | ||
readonly os: string | ||
} | ||
export interface WorkspaceAutostartRequest { | ||
schedule: string | ||
} | ||
export interface WorkspaceAutostopRequest { | ||
schedule: string | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Story } from "@storybook/react" | ||
import React from "react" | ||
import { MockWorkspaceAutostartEnabled } from "../../test_helpers" | ||
import { WorkspaceSchedule, WorkspaceScheduleProps } from "./WorkspaceSchedule" | ||
export default { | ||
title: "Workspaces/WorkspaceSchedule", | ||
component: WorkspaceSchedule, | ||
} | ||
const Template: Story<WorkspaceScheduleProps> = (args) => <WorkspaceSchedule {...args} /> | ||
export const Example = Template.bind({}) | ||
Example.args = { | ||
autostart: MockWorkspaceAutostartEnabled.schedule, | ||
autostop: "", | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Box from "@material-ui/core/Box" | ||
import Typography from "@material-ui/core/Typography" | ||
import cronstrue from "cronstrue" | ||
import React from "react" | ||
import { expandScheduleCronString, extractTimezone } from "../../util/schedule" | ||
import { WorkspaceSection } from "./WorkspaceSection" | ||
const Language = { | ||
autoStartLabel: (schedule: string): string => { | ||
const prefix = "Workspace start" | ||
if (schedule) { | ||
return `${prefix} (${extractTimezone(schedule)})` | ||
} else { | ||
return prefix | ||
} | ||
}, | ||
autoStopLabel: (schedule: string): string => { | ||
const prefix = "Workspace shutdown" | ||
if (schedule) { | ||
return `${prefix} (${extractTimezone(schedule)})` | ||
} else { | ||
return prefix | ||
} | ||
}, | ||
cronHumanDisplay: (schedule: string): string => { | ||
if (schedule) { | ||
return cronstrue.toString(expandScheduleCronString(schedule), { throwExceptionOnParseError: false }) | ||
} | ||
return "Manual" | ||
}, | ||
} | ||
export interface WorkspaceScheduleProps { | ||
autostart: string | ||
autostop: string | ||
} | ||
/** | ||
* WorkspaceSchedule displays a workspace schedule in a human-readable format | ||
* | ||
* @remarks Visual Component | ||
*/ | ||
export const WorkspaceSchedule: React.FC<WorkspaceScheduleProps> = ({ autostart, autostop }) => { | ||
return ( | ||
<WorkspaceSection title="Workspace schedule"> | ||
<Box mt={2}> | ||
<Typography variant="h6">{Language.autoStartLabel(autostart)}</Typography> | ||
<Typography>{Language.cronHumanDisplay(autostart)}</Typography> | ||
</Box> | ||
<Box mt={2}> | ||
<Typography variant="h6">{Language.autoStopLabel(autostop)}</Typography> | ||
<Typography>{Language.cronHumanDisplay(autostop)}</Typography> | ||
</Box> | ||
</WorkspaceSection> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -44,4 +44,10 @@ export const handlers = [ | ||
rest.get("/api/v2/workspaces/:workspaceId", async (req, res, ctx) => { | ||
return res(ctx.status(200), ctx.json(M.MockWorkspace)) | ||
}), | ||
rest.put("/api/v2/workspaces/:workspaceId/autostart", async (req, res, ctx) => { | ||
return res(ctx.status(200)) | ||
}), | ||
rest.put("/api/v2/workspaces/:workspaceId/autostop", async (req, res, ctx) => { | ||
return res(ctx.status(200)) | ||
}), | ||
greyscaled marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { expandScheduleCronString, extractTimezone, stripTimezone } from "./schedule" | ||
describe("util/schedule", () => { | ||
describe("stripTimezone", () => { | ||
it.each<[string, string]>([ | ||
["CRON_TZ=Canada/Eastern 30 9 1-5", "30 9 1-5"], | ||
["CRON_TZ=America/Central 0 8 1,2,4,5", "0 8 1,2,4,5"], | ||
["30 9 1-5", "30 9 1-5"], | ||
])(`stripTimezone(%p) returns %p`, (input, expected) => { | ||
expect(stripTimezone(input)).toBe(expected) | ||
}) | ||
}) | ||
describe("extractTimezone", () => { | ||
it.each<[string, string]>([ | ||
["CRON_TZ=Canada/Eastern 30 9 1-5", "Canada/Eastern"], | ||
["CRON_TZ=America/Central 0 8 1,2,4,5", "America/Central"], | ||
["30 9 1-5", "UTC"], | ||
])(`extractTimezone(%p) returns %p`, (input, expected) => { | ||
expect(extractTimezone(input)).toBe(expected) | ||
}) | ||
}) | ||
describe("expandScheduleCronString", () => { | ||
it.each<[string, string]>([ | ||
["CRON_TZ=Canada/Eastern 30 9 1-5", "30 9 * * 1-5"], | ||
["CRON_TZ=America/Central 0 8 1,2,4,5", "0 8 * * 1,2,4,5"], | ||
["30 9 1-5", "30 9 * * 1-5"], | ||
])(`expandScheduleCronString(%p) returns %p`, (input, expected) => { | ||
expect(expandScheduleCronString(input)).toBe(expected) | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @fileoverview Client-side counterpart of the coderd/autostart/schedule Go | ||
* package. This package is a variation on crontab that uses minute, hour and | ||
* day of week. | ||
*/ | ||
/** | ||
* DEFAULT_TIMEZONE is the default timezone that crontab assumes unless one is | ||
* specified. | ||
*/ | ||
const DEFAULT_TIMEZONE = "UTC" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 👍 | ||
/** | ||
* stripTimezone strips a leading timezone from a schedule string | ||
*/ | ||
export const stripTimezone = (raw: string): string => { | ||
return raw.replace(/CRON_TZ=\S*\s/, "") | ||
} | ||
/** | ||
* extractTimezone returns a leading timezone from a schedule string if one is | ||
* specified; otherwise DEFAULT_TIMEZONE | ||
*/ | ||
export const extractTimezone = (raw: string): string => { | ||
const matches = raw.match(/CRON_TZ=\S*\s/g) | ||
if (matches && matches.length) { | ||
return matches[0].replace(/CRON_TZ=/, "").trim() | ||
} else { | ||
return DEFAULT_TIMEZONE | ||
} | ||
} | ||
/** | ||
* expandScheduleCronString ensures a Schedule is expanded to a valid 5-value | ||
* cron string by inserting '*' in month and day positions. If there is a | ||
* leading timezone, it is removed. | ||
* | ||
* @example | ||
* expandScheduleCronString("30 9 1-5") // -> "30 9 * * 1-5" | ||
*/ | ||
export const expandScheduleCronString = (schedule: string): string => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. (non-blocking): Would it simplify the frontend code to alter the backend to support a full valid 5-value cron string? ContributorAuthor
| ||
const prepared = stripTimezone(schedule).trim() | ||
const parts = prepared.split(" ") | ||
while (parts.length < 5) { | ||
// insert '*' in the second to last position | ||
// ie [a, b, c] --> [a, b, *, c] | ||
parts.splice(parts.length - 1, 0, "*") | ||
} | ||
return parts.join(" ") | ||
} |