Skip to content

feat(site): add download logs option #13466

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 27 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
051217a
Download agent logs
BrunoQuaresma Jun 3, 2024
c9af899
Download workspace logs
BrunoQuaresma Jun 3, 2024
e479cf2
Use Logs instead of Agent Logs
BrunoQuaresma Jun 3, 2024
9ccc9e1
Centralize items list
BrunoQuaresma Jun 3, 2024
128f938
Init files with build logs
BrunoQuaresma Jun 3, 2024
4d5c9cb
Add tests for the download logs dialog
BrunoQuaresma Jun 4, 2024
11b528a
Add test to verify if download dialog is opening
BrunoQuaresma Jun 4, 2024
efcee2b
Add tests to download agent logs
BrunoQuaresma Jun 4, 2024
895e942
Fix package.json
BrunoQuaresma Jun 4, 2024
9ee7fd4
Commit Asher suggestions
BrunoQuaresma Jun 4, 2024
a71529a
Fix open download logs story
BrunoQuaresma Jun 5, 2024
f82a4e4
Fix dropdown arrow on light theme
BrunoQuaresma Jun 5, 2024
967fc0b
Enable agent logs only when showing
BrunoQuaresma Jun 5, 2024
73af195
Use web socket decorator and remove specific props for storybook
BrunoQuaresma Jun 5, 2024
f145ead
Refactor useAgentLogs to optimize loading
BrunoQuaresma Jun 5, 2024
5baa052
Add prefetch back
BrunoQuaresma Jun 5, 2024
40d2a65
Only fetch logs when clicking on download
BrunoQuaresma Jun 5, 2024
35368ee
Add useAgentLogs test
BrunoQuaresma Jun 5, 2024
10b4b71
Use Michael refactoring of useAgentLogs
BrunoQuaresma Jun 6, 2024
8da2d28
Improve naming
BrunoQuaresma Jun 6, 2024
b54c72c
Improve naming
BrunoQuaresma Jun 6, 2024
83f60dd
Fix hook usage
BrunoQuaresma Jun 6, 2024
6f85490
allow pkg
sreya Jun 6, 2024
270e04e
Rollback useAgentLogs implementation
BrunoQuaresma Jun 6, 2024
a6ab9fd
Add decorators to fix storybook
BrunoQuaresma Jun 6, 2024
c1f281c
Merge branch 'bq/download-logs' of https://github.com/coder/coder int…
BrunoQuaresma Jun 6, 2024
fab7d56
Apply Asher suggestions
BrunoQuaresma Jun 7, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ jobs:
uses: actions/dependency-review-action@v4.3.2
with:
allow-licenses: Apache-2.0, BSD-2-Clause, BSD-3-Clause, CC0-1.0, ISC, MIT, MIT-0, MPL-2.0
allow-dependencies-licenses: "pkg:golang/github.com/coder/wgtunnel@0.1.13-0.20240522110300-ade90dfb2da0"
allow-dependencies-licenses: "pkg:golang/github.com/coder/wgtunnel@0.1.13-0.20240522110300-ade90dfb2da0, pkg:npm/pako@1.0.11"
license-check: true
vulnerability-check: false
- name: "Report"
Expand Down
3 changes: 3 additions & 0 deletions site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@mui/system": "5.14.0",
"@mui/utils": "5.14.11",
"@tanstack/react-query-devtools": "4.35.3",
"@types/file-saver": "2.0.7",
"ansi-to-html": "0.7.2",
"axios": "1.6.0",
"canvas": "2.11.0",
Expand All @@ -58,8 +59,10 @@
"date-fns": "2.30.0",
"dayjs": "1.11.4",
"emoji-mart": "5.4.0",
"file-saver": "2.0.5",
"formik": "2.4.1",
"front-matter": "4.0.2",
"jszip": "3.10.1",
"lodash": "4.17.21",
"monaco-editor": "0.44.0",
"pretty-bytes": "6.1.0",
Expand Down
50 changes: 44 additions & 6 deletions site/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,10 +1142,9 @@ class ApiMethods {

getWorkspaceBuildLogs = async (
buildId: string,
before: Date,
): Promise<TypesGen.ProvisionerJobLog[]> => {
const response = await this.axios.get<TypesGen.ProvisionerJobLog[]>(
`/api/v2/workspacebuilds/${buildId}/logs?before=${before.getTime()}`,
`/api/v2/workspacebuilds/${buildId}/logs`,
);

return response.data;
Expand Down
4 changes: 2 additions & 2 deletions site/src/api/queries/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { UseQueryOptions, QueryKey } from "react-query";
import type { MetadataState, MetadataValue } from "hooks/useEmbeddedMetadata";

const disabledFetchOptions = {
export const disabledRefetchOptions = {
cacheTime: Infinity,
staleTime: Infinity,
refetchOnMount: false,
Expand Down Expand Up @@ -62,7 +62,7 @@ export function cachedQuery<

// Make sure the disabled options are always serialized last, so that no
// one using this function can accidentally override the values
...(metadata.available ? disabledFetchOptions : {}),
...(metadata.available ? disabledRefetchOptions : {}),
};

return newOptions as FormattedQueryOptionsResult<
Expand Down
30 changes: 30 additions & 0 deletions site/src/api/queries/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
WorkspacesRequest,
WorkspacesResponse,
} from "api/typesGenerated";
import { disabledRefetchOptions } from "./util";
import { workspaceBuildsKey } from "./workspaceBuilds";

export const workspaceByOwnerAndNameKey = (owner: string, name: string) => [
Expand Down Expand Up @@ -283,3 +284,32 @@ export const toggleFavorite = (
},
};
};

export const buildLogsKey = (workspaceId: string) => [
"workspaces",
workspaceId,
"logs",
];

export const buildLogs = (workspace: Workspace) => {
return {
queryKey: buildLogsKey(workspace.id),
queryFn: () => API.getWorkspaceBuildLogs(workspace.latest_build.id),
};
};

export const agentLogsKey = (workspaceId: string, agentId: string) => [
"workspaces",
workspaceId,
"agents",
agentId,
"logs",
];

export const agentLogs = (workspaceId: string, agentId: string) => {
return {
queryKey: agentLogsKey(workspaceId, agentId),
queryFn: () => API.getWorkspaceAgentLogs(agentId),
...disabledRefetchOptions,
};
};
6 changes: 3 additions & 3 deletions site/src/components/DropdownArrow/DropdownArrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export const DropdownArrow: FC<ArrowProps> = ({
};

const styles = {
base: (theme) => ({
color: theme.palette.primary.contrastText,
base: {
color: "currentcolor",
width: 16,
height: 16,
}),
},

withMargin: {
marginLeft: 8,
Expand Down
67 changes: 1 addition & 66 deletions site/src/modules/resources/AgentLogs/AgentLogs.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import type { Interpolation, Theme } from "@emotion/react";
import Tooltip from "@mui/material/Tooltip";
import {
type ComponentProps,
forwardRef,
useEffect,
useMemo,
useState,
} from "react";
import { type ComponentProps, forwardRef, useMemo } from "react";
import { FixedSizeList as List } from "react-window";
import { watchWorkspaceAgentLogs } from "api/api";
import type { WorkspaceAgentLogSource } from "api/typesGenerated";
import {
AGENT_LOG_LINE_HEIGHT,
Expand Down Expand Up @@ -179,64 +172,6 @@ export const AgentLogs = forwardRef<List, AgentLogsProps>(
},
);

export const useAgentLogs = (
agentId: string,
options?: { enabled?: boolean; initialData?: LineWithID[] },
) => {
const initialData = options?.initialData;
const enabled = options?.enabled === undefined ? true : options.enabled;
const [logs, setLogs] = useState<LineWithID[] | undefined>(initialData);

useEffect(() => {
if (!enabled) {
setLogs([]);
return;
}

const socket = watchWorkspaceAgentLogs(agentId, {
// Get all logs
after: 0,
onMessage: (logs) => {
// Prevent new logs getting added when a connection is not open
if (socket.readyState !== WebSocket.OPEN) {
return;
}

setLogs((previousLogs) => {
const newLogs: LineWithID[] = logs.map((log) => ({
id: log.id,
level: log.level || "info",
output: log.output,
time: log.created_at,
sourceId: log.source_id,
}));

if (!previousLogs) {
return newLogs;
}

return [...previousLogs, ...newLogs];
});
},
onError: (error) => {
// For some reason Firefox and Safari throw an error when a websocket
// connection is close in the middle of a message and because of that we
// can't safely show to the users an error message since most of the
// time they are just internal stuff. This does not happen to Chrome at
// all and I tried to find better way to "soft close" a WS connection on
// those browsers without success.
console.error(error);
},
});

return () => {
socket.close();
};
}, [agentId, enabled]);

return logs;
};

// These colors were picked at random. Feel free
// to add more, adjust, or change! Users will not
// depend on these colors.
Expand Down
Loading
Loading