-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add VS code notifications for workspace actions #111
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f8b5031
feat: add VS code notifications for autostop
a810f98
feat: add VS code notifications for workspace actions
Kira-Pilot 196ce2e
resetting boolean invert
Kira-Pilot e9f3ad0
fix lint
Kira-Pilot 867b841
check job
Kira-Pilot 4baaccf
added notifications for workspace deletion
Kira-Pilot 752bbd2
fix lint
Kira-Pilot 4d9d3e2
writing to coder output channel when requests fail
Kira-Pilot 1c65a2f
updating wasNotified for upcoming deletions
Kira-Pilot 7ad0d71
added nifty TS fix
Kira-Pilot 9727783
fixed casing
Kira-Pilot ac0ce3a
update Coder dependency
Kira-Pilot dd1aa9c
fix lint
Kira-Pilot 7aca734
fixed relative path importing for coder dependency
Kira-Pilot 2d9e7f6
Update src/WorkspaceAction.ts
Kira-Pilot 53704d0
simplify build status gate
Kira-Pilot 450d054
capture all errors
Kira-Pilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import axios from "axios" | ||
import { getWorkspaces } from "coder/site/src/api/api" | ||
import { Workspace, WorkspacesResponse, WorkspaceBuild } from "coder/site/src/api/typesGenerated" | ||
import { formatDistanceToNowStrict } from "date-fns" | ||
import * as vscode from "vscode" | ||
import { Storage } from "./storage" | ||
|
||
interface NotifiedWorkspace { | ||
workspace: Workspace | ||
wasNotified: boolean | ||
impendingActionDeadline: string | ||
} | ||
|
||
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>> | ||
|
||
type WorkspaceWithDeadline = Workspace & { latest_build: WithRequired<WorkspaceBuild, "deadline"> } | ||
type WorkspaceWithDeletingAt = WithRequired<Workspace, "deleting_at"> | ||
|
||
export class WorkspaceAction { | ||
// We use this same interval in the Dashboard to poll for updates on the Workspaces page. | ||
#POLL_INTERVAL: number = 1000 * 5 | ||
#fetchWorkspacesInterval?: ReturnType<typeof setInterval> | ||
|
||
#ownedWorkspaces: Workspace[] = [] | ||
#workspacesApproachingAutostop: NotifiedWorkspace[] = [] | ||
#workspacesApproachingDeletion: NotifiedWorkspace[] = [] | ||
|
||
private constructor( | ||
private readonly vscodeProposed: typeof vscode, | ||
private readonly storage: Storage, | ||
ownedWorkspaces: Workspace[], | ||
) { | ||
this.#ownedWorkspaces = ownedWorkspaces | ||
|
||
// seed initial lists | ||
this.updateNotificationLists() | ||
|
||
this.notifyAll() | ||
|
||
// set up polling so we get current workspaces data | ||
this.pollGetWorkspaces() | ||
} | ||
|
||
static async init(vscodeProposed: typeof vscode, storage: Storage) { | ||
// fetch all workspaces owned by the user and set initial public class fields | ||
let ownedWorkspacesResponse: WorkspacesResponse | ||
try { | ||
ownedWorkspacesResponse = await getWorkspaces({ q: "owner:me" }) | ||
} catch (error) { | ||
let status | ||
if (axios.isAxiosError(error)) { | ||
status = error.response?.status | ||
} | ||
if (status !== 401) { | ||
storage.writeToCoderOutputChannel( | ||
`Failed to fetch owned workspaces. Some workspace notifications may be missing: ${error}`, | ||
) | ||
} | ||
|
||
ownedWorkspacesResponse = { workspaces: [], count: 0 } | ||
} | ||
return new WorkspaceAction(vscodeProposed, storage, ownedWorkspacesResponse.workspaces) | ||
} | ||
|
||
updateNotificationLists() { | ||
this.#workspacesApproachingAutostop = this.#ownedWorkspaces | ||
.filter(this.filterWorkspacesImpendingAutostop) | ||
.map((workspace) => | ||
this.transformWorkspaceObjects(workspace, this.#workspacesApproachingAutostop, workspace.latest_build.deadline), | ||
) | ||
|
||
this.#workspacesApproachingDeletion = this.#ownedWorkspaces | ||
.filter(this.filterWorkspacesImpendingDeletion) | ||
.map((workspace) => | ||
this.transformWorkspaceObjects(workspace, this.#workspacesApproachingDeletion, workspace.deleting_at), | ||
) | ||
} | ||
|
||
filterWorkspacesImpendingAutostop(workspace: Workspace): workspace is WorkspaceWithDeadline { | ||
// a workspace is eligible for autostop if the workspace is running and it has a deadline | ||
if (workspace.latest_build.status !== "running" || !workspace.latest_build.deadline) { | ||
return false | ||
} | ||
|
||
const hourMilli = 1000 * 60 * 60 | ||
// return workspaces with a deadline that is in 1 hr or less | ||
return Math.abs(new Date().getTime() - new Date(workspace.latest_build.deadline).getTime()) <= hourMilli | ||
} | ||
|
||
filterWorkspacesImpendingDeletion(workspace: Workspace): workspace is WorkspaceWithDeletingAt { | ||
if (!workspace.deleting_at) { | ||
return false | ||
} | ||
|
||
const dayMilli = 1000 * 60 * 60 * 24 | ||
|
||
// return workspaces with a deleting_at that is 24 hrs or less | ||
return Math.abs(new Date().getTime() - new Date(workspace.deleting_at).getTime()) <= dayMilli | ||
} | ||
|
||
transformWorkspaceObjects(workspace: Workspace, workspaceList: NotifiedWorkspace[], deadlineField: string) { | ||
const wasNotified = workspaceList.find((nw) => nw.workspace.id === workspace.id)?.wasNotified ?? false | ||
const impendingActionDeadline = formatDistanceToNowStrict(new Date(deadlineField)) | ||
return { workspace, wasNotified, impendingActionDeadline } | ||
} | ||
|
||
async pollGetWorkspaces() { | ||
let errorCount = 0 | ||
this.#fetchWorkspacesInterval = setInterval(async () => { | ||
try { | ||
const workspacesResult = await getWorkspaces({ q: "owner:me" }) | ||
this.#ownedWorkspaces = workspacesResult.workspaces | ||
this.updateNotificationLists() | ||
this.notifyAll() | ||
} catch (error) { | ||
code-asher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
errorCount++ | ||
|
||
let status | ||
if (axios.isAxiosError(error)) { | ||
status = error.response?.status | ||
} | ||
if (status !== 401) { | ||
this.storage.writeToCoderOutputChannel( | ||
`Failed to poll owned workspaces. Some workspace notifications may be missing: ${error}`, | ||
) | ||
} | ||
if (errorCount === 3) { | ||
clearInterval(this.#fetchWorkspacesInterval) | ||
} | ||
} | ||
}, this.#POLL_INTERVAL) | ||
} | ||
|
||
notifyAll() { | ||
this.notifyImpendingAutostop() | ||
this.notifyImpendingDeletion() | ||
} | ||
|
||
notifyImpendingAutostop() { | ||
this.#workspacesApproachingAutostop?.forEach((notifiedWorkspace: NotifiedWorkspace) => { | ||
if (notifiedWorkspace.wasNotified) { | ||
// don't message the user; we've already messaged | ||
return | ||
} | ||
|
||
// we display individual notifications for each workspace as VS Code | ||
// intentionally strips new lines from the message text | ||
// https://github.com/Microsoft/vscode/issues/48900 | ||
this.vscodeProposed.window.showInformationMessage( | ||
`${notifiedWorkspace.workspace.name} is scheduled to shut down in ${notifiedWorkspace.impendingActionDeadline}.`, | ||
) | ||
notifiedWorkspace.wasNotified = true | ||
}) | ||
} | ||
|
||
notifyImpendingDeletion() { | ||
this.#workspacesApproachingDeletion?.forEach((notifiedWorkspace: NotifiedWorkspace) => { | ||
if (notifiedWorkspace.wasNotified) { | ||
// don't message the user; we've already messaged | ||
return | ||
} | ||
|
||
// we display individual notifications for each workspace as VS Code | ||
// intentionally strips new lines from the message text | ||
// https://github.com/Microsoft/vscode/issues/48900 | ||
this.vscodeProposed.window.showInformationMessage( | ||
`${notifiedWorkspace.workspace.name} is scheduled for deletion in ${notifiedWorkspace.impendingActionDeadline}.`, | ||
) | ||
notifiedWorkspace.wasNotified = true | ||
}) | ||
} | ||
|
||
cleanupWorkspaceActions() { | ||
clearInterval(this.#fetchWorkspacesInterval) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.