Skip to content

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 17 commits into from
Jun 27, 2023
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
added notifications for workspace deletion
  • Loading branch information
Kira-Pilot committed Jun 22, 2023
commit 4baaccfd7581099727bf2edcd839e230fa23c4ab
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
},
"dependencies": {
"axios": "0.26.1",
"date-fns": "^2.30.0",
"eventsource": "^2.0.2",
"find-process": "^1.4.7",
"fs-extra": "^11.1.0",
Expand All @@ -246,4 +247,4 @@
"yaml": "^1.10.0",
"zod": "^3.21.4"
}
}
}
52 changes: 36 additions & 16 deletions src/WorkspaceAction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { getWorkspaces } from "coder/site/src/api/api"
import { Workspace, WorkspacesResponse } from "coder/site/src/api/typesGenerated"
import * as vscode from "vscode"
import { formatDistanceToNowStrict } from "date-fns"

interface NotifiedWorkspace {
workspace: Workspace
wasNotified: boolean
impendingActionDeadline: string
}

export class WorkspaceAction {
Expand All @@ -20,6 +22,8 @@ export class WorkspaceAction {
// seed initial lists
this.seedNotificationLists()

this.notifyAll()

// set up polling so we get current workspaces data
this.pollGetWorkspaces()
}
Expand All @@ -32,25 +36,20 @@ export class WorkspaceAction {
} catch (error) {
ownedWorkspacesResponse = { workspaces: [], count: 0 }
}

return new WorkspaceAction(vscodeProposed, ownedWorkspacesResponse.workspaces)
}

seedNotificationLists() {
this.#workspacesApproachingAutostop = this.#ownedWorkspaces
.filter(this.filterImpendingAutostopWorkspaces)
.map((workspace: Workspace) => {
const wasNotified =
this.#workspacesApproachingAutostop.find((wn) => wn.workspace.id === workspace.id)?.wasNotified ?? false
return { workspace, wasNotified }
})

// NOTE: this feature is currently in-progess; however, we're including scaffolding for it
// to exemplify the class pattern used for Workspace Actions
this.#workspacesApproachingDeletion = []
.filter(this.filterWorkspacesImpendingAutostop)
.map((workspace: Workspace) => this.transformWorkspaceObjects(workspace, workspace.latest_build.deadline))

this.#workspacesApproachingDeletion = this.#ownedWorkspaces
.filter(this.filterWorkspacesImpendingDeletion)
.map((workspace: Workspace) => this.transformWorkspaceObjects(workspace, workspace.deleting_at))
}

filterImpendingAutostopWorkspaces(workspace: Workspace) {
filterWorkspacesImpendingAutostop(workspace: Workspace) {
// a workspace is eligible for autostop if the last build was successful,
// and the workspace is started,
// and it has a deadline
Expand All @@ -62,9 +61,30 @@ export class WorkspaceAction {
return false
}

const hoursMilli = 1000 * 60 * 60
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()) <= hoursMilli
return Math.abs(new Date().getTime() - new Date(workspace.latest_build.deadline).getTime()) <= hourMilli
}

filterWorkspacesImpendingDeletion(workspace: Workspace) {
if (!workspace.deleting_at) {
return
}

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, deadlineField?: string) {
// the below line is to satisfy TS; we should always pass a deadlineField, e.g
// workspace,deleting_at or workspace.latest_build.deadline
if (!deadlineField) return { workspace, wasNotified: true, impendingActionDeadline: "" }
const wasNotified =
this.#workspacesApproachingAutostop.find((wn) => wn.workspace.id === workspace.id)?.wasNotified ?? false
const impendingActionDeadline = formatDistanceToNowStrict(new Date(deadlineField))
return { workspace, wasNotified, impendingActionDeadline }
}

async pollGetWorkspaces() {
Expand Down Expand Up @@ -100,7 +120,7 @@ export class WorkspaceAction {
// 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 1 hour.`,
`${notifiedWorkspace.workspace.name} is scheduled to shut down in ${notifiedWorkspace.impendingActionDeadline}.`,
)
notifiedWorkspace.wasNotified = true
})
Expand All @@ -117,7 +137,7 @@ export class WorkspaceAction {
// 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.`,
`${notifiedWorkspace.workspace.name} is scheduled for deletion in ${notifiedWorkspace.impendingActionDeadline}.`,
)
notifiedWorkspace.wasNotified = true
})
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.20.13.tgz#ddf1eb5a813588d2fb1692b70c6fce75b945c088"
integrity sha512-gFDLKMfpiXCsjt4za2JA9oTMn70CeseCehb11kRZgvd7+F67Hih3OHOK24cRrWECJ/ljfPGac6ygXAs/C8kIvw==

"@babel/runtime@^7.21.0":
version "7.22.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.5.tgz#8564dd588182ce0047d55d7a75e93921107b57ec"
integrity sha512-ecjvYlnAaZ/KVneE/OdKYBYfgXV3Ptu6zQWmgEF7vwKhQnvVS6bjMD2XYgj+SNvQ1GfK/pjgokfPkC/2CO8CuA==
dependencies:
regenerator-runtime "^0.13.11"

"@babel/template@^7.18.10", "@babel/template@^7.20.7":
version "7.20.7"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.20.7.tgz#a15090c2839a83b02aa996c0b4994005841fd5a8"
Expand Down Expand Up @@ -1530,6 +1537,13 @@ css-what@^6.1.0:
resolved "https://registry.yarnpkg.com/css-what/-/css-what-6.1.0.tgz#fb5effcf76f1ddea2c81bdfaa4de44e79bac70f4"
integrity sha512-HTUrgRJ7r4dsZKU6GjmpfRK1O76h97Z8MfS1G0FozR+oF2kG6Vfe8JE6zwrkbxigziPHinCJ+gCPjA9EaBDtRw==

date-fns@^2.30.0:
version "2.30.0"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.30.0.tgz#f367e644839ff57894ec6ac480de40cae4b0f4d0"
integrity sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==
dependencies:
"@babel/runtime" "^7.21.0"

dayjs@^1.11.7:
version "1.11.7"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.7.tgz#4b296922642f70999544d1144a2c25730fce63e2"
Expand Down Expand Up @@ -3887,6 +3901,11 @@ rechoir@^0.8.0:
dependencies:
resolve "^1.20.0"

regenerator-runtime@^0.13.11:
version "0.13.11"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==

regexp.prototype.flags@^1.4.3:
version "1.4.3"
resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.4.3.tgz#87cab30f80f66660181a3bb7bf5981a872b367ac"
Expand Down