Skip to content

feat: show agent metadata #92

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 7 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
"tar-fs": "^2.1.1",
"which": "^2.0.2",
"ws": "^8.11.0",
"yaml": "^1.10.0"
"yaml": "^1.10.0",
"zod": "^3.21.4"
}
}
77 changes: 71 additions & 6 deletions src/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import prettyBytes from "pretty-bytes"
import * as semver from "semver"
import * as vscode from "vscode"
import * as ws from "ws"
import { z } from "zod"
import { SSHConfig, SSHValues, defaultSSHConfigResponse, mergeSSHConfigValues } from "./sshConfig"
import { sshSupportsSetEnv } from "./sshSupport"
import { Storage } from "./storage"
Expand Down Expand Up @@ -280,16 +281,15 @@ export class Remote {
"Coder-Session-Token": await this.storage.getSessionToken(),
},
})
eventSource.addEventListener("open", () => {
// TODO: Add debug output that we began watching here!
})
eventSource.addEventListener("error", () => {
// TODO: Add debug output that we got an error here!
})

const workspaceUpdatedStatus = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 999)
disposables.push(workspaceUpdatedStatus)

const agentMetadataStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 999)
disposables.push(agentMetadataStatusBarItem)

await this.populateAgentMetadataStatusBarItem(agent, agentMetadataStatusBarItem)

let hasShownOutdatedNotification = false
const refreshWorkspaceUpdatedStatus = (newWorkspace: Workspace) => {
// If the newly gotten workspace was updated, then we show a notification
Expand Down Expand Up @@ -439,6 +439,71 @@ export class Remote {
}
}

private async populateAgentMetadataStatusBarItem(
agent: WorkspaceAgent,
agentMetadataStatusBarItem: vscode.StatusBarItem,
) {
const agentMetadataURL = new URL(`${this.storage.getURL()}/api/v2/workspaceagents/${agent?.id}/watch-metadata`)
const agentMetadataEventSource = new EventSource(agentMetadataURL.toString(), {
headers: {
"Coder-Session-Token": await this.storage.getSessionToken(),
},
})

agentMetadataEventSource.addEventListener("error", () => {
agentMetadataStatusBarItem.hide()
})

agentMetadataEventSource.addEventListener("open", () => {
agentMetadataStatusBarItem.show()
})

agentMetadataEventSource.addEventListener("data", (event) => {
try {
const AgentMetadataEventSchema = z
.object({
result: z.object({
collected_at: z.string(),
age: z.number(),
value: z.string(),
error: z.string(),
}),
description: z.object({
display_name: z.string(),
key: z.string(),
script: z.string(),
interval: z.number(),
timeout: z.number(),
}),
})
.array()

const dataEvent = JSON.parse(event.data)
const agentMetadata = AgentMetadataEventSchema.parse(dataEvent)

if (agentMetadata.length === 0) {
agentMetadataStatusBarItem.hide()
agentMetadataEventSource.close()
}

agentMetadataStatusBarItem.text = "$(symbol-variable) Metadata"

const tooltipData = agentMetadata.map((agentMetadata) => {
return [agentMetadata.description.display_name.trim(), agentMetadata.result.value.replace("\n", "").trim()]
})

const tooltipMarkdown = new vscode.MarkdownString(
"| | | " + "\n" + "|:--- | ---: |" + "\n" + tooltipData.map((row) => `| ${row[0]} | ${row[1]} |`).join("\n"),
)

agentMetadataStatusBarItem.tooltip = tooltipMarkdown
} catch (error) {
agentMetadataStatusBarItem.hide()
agentMetadataEventSource.close()
}
})
}

// updateSSHConfig updates the SSH configuration with a wildcard that handles
// all Coder entries.
private async updateSSHConfig() {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5761,3 +5761,8 @@ yocto-queue@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251"
integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==

zod@^3.21.4:
version "3.21.4"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.21.4.tgz#10882231d992519f0a10b5dd58a38c9dabbb64db"
integrity sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==