- Notifications
You must be signed in to change notification settings - Fork35
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
38a1652 wip: show agent metadata
rodrimaia641a978 wip: show agent metadata
rodrimaia3e3e11e wip: fix alignment
rodrimaia21cc7b6 wip: hide if there is no metadata
rodrimaiae58a26b Merge branch 'main' into show-agent-metadata
rodrimaia1a317be wip: show metadata on sidebar
rodrimaiaa3fbc8e wip: only refresh if data is different
rodrimaiaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletionpackage.json
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 |
|---|---|---|
| @@ -226,6 +226,7 @@ | ||
| "tar-fs": "^2.1.1", | ||
| "which": "^2.0.2", | ||
| "ws": "^8.11.0", | ||
| "yaml": "^1.10.0", | ||
| "zod": "^3.21.4" | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletionssrc/api-helper.ts
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
8 changes: 4 additions & 4 deletionssrc/commands.ts
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
5 changes: 2 additions & 3 deletionssrc/extension.ts
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
6 changes: 0 additions & 6 deletionssrc/remote.ts
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
187 changes: 136 additions & 51 deletionssrc/workspacesProvider.ts
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 |
|---|---|---|
| @@ -1,93 +1,178 @@ | ||
| import { getWorkspaces } from "coder/site/src/api/api" | ||
| import { Workspace, WorkspaceAgent } from "coder/site/src/api/typesGenerated" | ||
| import EventSource from "eventsource" | ||
| import * as path from "path" | ||
| import * as vscode from "vscode" | ||
| import { AgentMetadataEvent, AgentMetadataEventSchemaArray, extractAgents } from "./api-helper" | ||
| import { Storage } from "./storage" | ||
| export enum WorkspaceQuery { | ||
| Mine = "owner:me", | ||
| All = "", | ||
| } | ||
| export class WorkspaceProvider implements vscode.TreeDataProvider<vscode.TreeItem> { | ||
| private workspaces: WorkspaceTreeItem[] = [] | ||
| private agentMetadata: Record<WorkspaceAgent["id"], AgentMetadataEvent[]> = {} | ||
| constructor(private readonly getWorkspacesQuery: WorkspaceQuery, private readonly storage: Storage) { | ||
| getWorkspaces({ q: this.getWorkspacesQuery }) | ||
| .then((workspaces) => { | ||
| const workspacesTreeItem: WorkspaceTreeItem[] = [] | ||
| workspaces.workspaces.forEach((workspace) => { | ||
| const showMetadata = this.getWorkspacesQuery === WorkspaceQuery.Mine | ||
| if (showMetadata) { | ||
| const agents = extractAgents(workspace) | ||
| agents.forEach((agent) => this.monitorMetadata(agent.id)) // monitor metadata for all agents | ||
| } | ||
| const treeItem = new WorkspaceTreeItem( | ||
| workspace, | ||
| this.getWorkspacesQuery === WorkspaceQuery.All, | ||
| showMetadata, | ||
| ) | ||
| workspacesTreeItem.push(treeItem) | ||
| }) | ||
| return workspacesTreeItem | ||
| }) | ||
| .then((workspaces) => { | ||
| this.workspaces = workspaces | ||
| this.refresh() | ||
| }) | ||
| } | ||
| private _onDidChangeTreeData: vscode.EventEmitter<vscode.TreeItem | undefined | null | void> = | ||
| new vscode.EventEmitter<vscode.TreeItem | undefined | null | void>() | ||
| readonly onDidChangeTreeData: vscode.Event<vscode.TreeItem | undefined | null | void> = | ||
| this._onDidChangeTreeData.event | ||
| refresh(item: vscode.TreeItem | undefined | null | void): void { | ||
| this._onDidChangeTreeData.fire(item) | ||
| } | ||
| asyncgetTreeItem(element:vscode.TreeItem):Promise<vscode.TreeItem> { | ||
| return element | ||
| } | ||
| getChildren(element?:vscode.TreeItem): Thenable<vscode.TreeItem[]> { | ||
| if (element) { | ||
| if (element instanceof WorkspaceTreeItem) { | ||
| const agents = extractAgents(element.workspace) | ||
| const agentTreeItems = agents.map((agent) => new AgentTreeItem(agent, element.watchMetadata)) | ||
| return Promise.resolve(agentTreeItems) | ||
| } else if (element instanceof AgentTreeItem) { | ||
| const savedMetadata = this.agentMetadata[element.agent.id] || [] | ||
| return Promise.resolve(savedMetadata.map((metadata) => new AgentMetadataTreeItem(metadata))) | ||
| } | ||
| return Promise.resolve([]) | ||
| } | ||
| return Promise.resolve(this.workspaces) | ||
| } | ||
| async monitorMetadata(agentId: WorkspaceAgent["id"]): Promise<void> { | ||
| const agentMetadataURL = new URL(`${this.storage.getURL()}/api/v2/workspaceagents/${agentId}/watch-metadata`) | ||
| const agentMetadataEventSource = new EventSource(agentMetadataURL.toString(), { | ||
| headers: { | ||
| "Coder-Session-Token": await this.storage.getSessionToken(), | ||
| }, | ||
| }) | ||
| agentMetadataEventSource.addEventListener("data", (event) => { | ||
| try { | ||
| const dataEvent = JSON.parse(event.data) | ||
| const agentMetadata = AgentMetadataEventSchemaArray.parse(dataEvent) | ||
| if (agentMetadata.length === 0) { | ||
| agentMetadataEventSource.close() | ||
| } | ||
| const savedMetadata = this.agentMetadata[agentId] | ||
| if (JSON.stringify(savedMetadata) !== JSON.stringify(agentMetadata)) { | ||
| this.agentMetadata[agentId] = agentMetadata // overwrite existing metadata | ||
| this.refresh() | ||
| } | ||
| } catch (error) { | ||
| agentMetadataEventSource.close() | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| type CoderTreeItemType = "coderWorkspaceSingleAgent" | "coderWorkspaceMultipleAgents" | "coderAgent" | ||
| class AgentMetadataTreeItem extends vscode.TreeItem { | ||
| constructor(metadataEvent: AgentMetadataEvent) { | ||
| const label = | ||
| metadataEvent.description.display_name.trim() + ": " + metadataEvent.result.value.replace(/\n/g, "").trim() | ||
| super(label, vscode.TreeItemCollapsibleState.None) | ||
| this.tooltip = "Collected at " + metadataEvent.result.collected_at | ||
| this.contextValue = "coderAgentMetadata" | ||
| } | ||
| } | ||
| export class OpenableTreeItem extends vscode.TreeItem { | ||
| constructor( | ||
| label: string, | ||
| tooltip: string, | ||
| collapsibleState: vscode.TreeItemCollapsibleState, | ||
| public readonly workspaceOwner: string, | ||
| public readonly workspaceName: string, | ||
| public readonly workspaceAgent: string | undefined, | ||
| public readonly workspaceFolderPath: string | undefined, | ||
| contextValue: CoderTreeItemType, | ||
| ) { | ||
| super(label, collapsibleState) | ||
| this.contextValue = contextValue | ||
| this.tooltip = tooltip | ||
| } | ||
| iconPath = { | ||
| light: path.join(__filename, "..", "..", "media", "logo.svg"), | ||
| dark: path.join(__filename, "..", "..", "media", "logo.svg"), | ||
| } | ||
| } | ||
| class AgentTreeItem extends OpenableTreeItem { | ||
| constructor(public readonly agent: WorkspaceAgent, watchMetadata = false) { | ||
| const label = agent.name | ||
| const detail = `Status: ${agent.status}` | ||
| super( | ||
| label, | ||
| detail, | ||
| watchMetadata ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None, | ||
| "", | ||
| "", | ||
| agent.name, | ||
| agent.expanded_directory, | ||
| "coderAgent", | ||
| ) | ||
| } | ||
| } | ||
| export class WorkspaceTreeItem extends OpenableTreeItem { | ||
| constructor( | ||
| public readonly workspace: Workspace, | ||
| public readonly showOwner: boolean, | ||
| public readonly watchMetadata = false, | ||
| ) { | ||
| const status = | ||
| workspace.latest_build.status.substring(0, 1).toUpperCase() + workspace.latest_build.status.substring(1) | ||
| const label = showOwner ? `${workspace.owner_name} / ${workspace.name}` : workspace.name | ||
| const detail = `Template: ${workspace.template_display_name || workspace.template_name} • Status: ${status}` | ||
| const agents = extractAgents(workspace) | ||
| super( | ||
| label, | ||
| detail, | ||
| showOwner ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.Expanded, | ||
| workspace.owner_name, | ||
| workspace.name, | ||
| undefined, | ||
| agents[0]?.expanded_directory, | ||
| "coderWorkspaceMultipleAgents", | ||
| ) | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletionsyarn.lock
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.