Skip to content

chore: switch ssh session stats based on experiment #13637

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 8 commits into from
Jun 25, 2024
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
add support for web terminal
  • Loading branch information
f0ssel committed Jun 23, 2024
commit 61648cbca9078d66e86d9c99f3385f7b6ec1ad55
10 changes: 10 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import userAgentParser from "ua-parser-js";
import { delay } from "../utils/delay";
import * as TypesGen from "./typesGenerated";
import { PostWorkspaceUsageRequest } from "./typesGenerated";

Check failure on line 27 in site/src/api/api.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 27 in site/src/api/api.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

const getMissingParameters = (
oldBuildParameters: TypesGen.WorkspaceBuildParameter[],
Expand Down Expand Up @@ -1879,6 +1880,15 @@
throw error;
}
};

postWorkspaceUsage = async (workspaceID: string, options: PostWorkspaceUsageRequest) => {
const response = await this.axios.post(
`/api/v2/workspaces/${workspaceID}/usage`,
options,
);

return response.data;
};
}

// This is a hard coded CSRF token/cookie pair for local development. In prod,
Expand Down
34 changes: 34 additions & 0 deletions site/src/api/queries/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type {
CreateWorkspaceRequest,
ProvisionerLogLevel,
UsageAppName,
Workspace,
WorkspaceBuild,
WorkspaceBuildParameter,
Expand All @@ -16,6 +17,7 @@
} from "api/typesGenerated";
import { disabledRefetchOptions } from "./util";
import { workspaceBuildsKey } from "./workspaceBuilds";
import { Terminal } from "xterm";

Check failure on line 20 in site/src/api/queries/workspaces.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 20 in site/src/api/queries/workspaces.ts

View workflow job for this annotation

GitHub Actions / lint

`xterm` import should occur before import of `api/api`

Check failure on line 20 in site/src/api/queries/workspaces.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 20 in site/src/api/queries/workspaces.ts

View workflow job for this annotation

GitHub Actions / lint

`xterm` import should occur before import of `api/api`

export const workspaceByOwnerAndNameKey = (owner: string, name: string) => [
"workspace",
Expand Down Expand Up @@ -313,3 +315,35 @@
...disabledRefetchOptions,
};
};

// workspace usage options
export interface WorkspaceUsageOptions {
usageApp: UsageAppName;
terminal: Terminal | undefined;
workspaceId: string | undefined;
agentId: string | undefined;
}

export const workspaceUsage = (options: WorkspaceUsageOptions) => {
return {
queryKey: [
"workspaces", options.workspaceId,
"agents", options.agentId,
"usage", options.usageApp,
],
enabled: options.terminal !== undefined && options.workspaceId !== undefined && options.agentId !== undefined,
queryFn: () => {
if (options.terminal === undefined || options.workspaceId === undefined || options.agentId === undefined) {
return Promise.reject();
}

return API.postWorkspaceUsage(options.workspaceId, {
agent_id: options.agentId,
app_name: options.usageApp,
})
},
// ...disabledRefetchOptions,
refetchInterval: 60000,
refetchIntervalInBackground: true,
};
}
12 changes: 11 additions & 1 deletion site/src/pages/TerminalPage/TerminalPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Unicode11Addon } from "xterm-addon-unicode11";
import { WebLinksAddon } from "xterm-addon-web-links";
import { WebglAddon } from "xterm-addon-webgl";
import { deploymentConfig } from "api/queries/deployment";
import { workspaceByOwnerAndName } from "api/queries/workspaces";
import { workspaceByOwnerAndName, workspaceUsage } from "api/queries/workspaces";
import { useProxy } from "contexts/ProxyContext";
import { ThemeOverride } from "contexts/ThemeProvider";
import themes from "theme";
Expand Down Expand Up @@ -67,6 +67,16 @@ const TerminalPage: FC = () => {
const config = useQuery(deploymentConfig());
const renderer = config.data?.config.web_terminal_renderer;

// Periodically report workspace usage.
useQuery(
workspaceUsage({
usageApp: "reconnecting-pty",
terminal,
workspaceId: workspace.data?.id,
agentId: workspaceAgent?.id,
})
);

// handleWebLink handles opening of URLs in the terminal!
const handleWebLink = useCallback(
(uri: string) => {
Expand Down
Loading