Skip to content

Commit 952c254

Browse files
fix: fix duplicated agent logs (#17806)
Fix #16355
1 parent 2c49fd9 commit 952c254

15 files changed

+136
-307
lines changed

site/src/api/api.ts

+6-24
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@ export const watchBuildLogsByTemplateVersionId = (
221221

222222
export const watchWorkspaceAgentLogs = (
223223
agentId: string,
224-
{ after, onMessage, onDone, onError }: WatchWorkspaceAgentLogsOptions,
224+
params?: WatchWorkspaceAgentLogsParams,
225225
) => {
226226
const searchParams = new URLSearchParams({
227227
follow: "true",
228-
after: after.toString(),
228+
after: params?.after?.toString() ?? "",
229229
});
230230

231231
/**
@@ -237,32 +237,14 @@ export const watchWorkspaceAgentLogs = (
237237
searchParams.set("no_compression", "");
238238
}
239239

240-
const socket = createWebSocket(
241-
`/api/v2/workspaceagents/${agentId}/logs`,
240+
return new OneWayWebSocket<TypesGen.WorkspaceAgentLog[]>({
241+
apiRoute: `/api/v2/workspaceagents/${agentId}/logs`,
242242
searchParams,
243-
);
244-
245-
socket.addEventListener("message", (event) => {
246-
const logs = JSON.parse(event.data) as TypesGen.WorkspaceAgentLog[];
247-
onMessage(logs);
248-
});
249-
250-
socket.addEventListener("error", () => {
251-
onError(new Error("socket errored"));
252243
});
253-
254-
socket.addEventListener("close", () => {
255-
onDone?.();
256-
});
257-
258-
return socket;
259244
};
260245

261-
type WatchWorkspaceAgentLogsOptions = {
262-
after: number;
263-
onMessage: (logs: TypesGen.WorkspaceAgentLog[]) => void;
264-
onDone?: () => void;
265-
onError: (error: Error) => void;
246+
type WatchWorkspaceAgentLogsParams = {
247+
after?: number;
266248
};
267249

268250
type WatchBuildLogsByBuildIdOptions = {

site/src/api/queries/workspaces.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
ProvisionerLogLevel,
66
UsageAppName,
77
Workspace,
8+
WorkspaceAgentLog,
89
WorkspaceBuild,
910
WorkspaceBuildParameter,
1011
WorkspacesRequest,
@@ -20,6 +21,7 @@ import type {
2021
QueryClient,
2122
QueryOptions,
2223
UseMutationOptions,
24+
UseQueryOptions,
2325
} from "react-query";
2426
import { checkAuthorization } from "./authCheck";
2527
import { disabledRefetchOptions } from "./util";
@@ -342,20 +344,14 @@ export const buildLogs = (workspace: Workspace) => {
342344
};
343345
};
344346

345-
export const agentLogsKey = (workspaceId: string, agentId: string) => [
346-
"workspaces",
347-
workspaceId,
348-
"agents",
349-
agentId,
350-
"logs",
351-
];
347+
export const agentLogsKey = (agentId: string) => ["agents", agentId, "logs"];
352348

353-
export const agentLogs = (workspaceId: string, agentId: string) => {
349+
export const agentLogs = (agentId: string) => {
354350
return {
355-
queryKey: agentLogsKey(workspaceId, agentId),
351+
queryKey: agentLogsKey(agentId),
356352
queryFn: () => API.getWorkspaceAgentLogs(agentId),
357353
...disabledRefetchOptions,
358-
};
354+
} satisfies UseQueryOptions<WorkspaceAgentLog[]>;
359355
};
360356

361357
// workspace usage options

site/src/modules/resources/AgentLogs/useAgentLogs.test.tsx

-142
This file was deleted.

site/src/modules/resources/AgentLogs/useAgentLogs.ts

-95
This file was deleted.

site/src/modules/resources/AgentRow.tsx

+2-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import { AgentDevcontainerCard } from "./AgentDevcontainerCard";
3131
import { AgentLatency } from "./AgentLatency";
3232
import { AGENT_LOG_LINE_HEIGHT } from "./AgentLogs/AgentLogLine";
3333
import { AgentLogs } from "./AgentLogs/AgentLogs";
34-
import { useAgentLogs } from "./AgentLogs/useAgentLogs";
3534
import { AgentMetadata } from "./AgentMetadata";
3635
import { AgentStatus } from "./AgentStatus";
3736
import { AgentVersion } from "./AgentVersion";
@@ -41,6 +40,7 @@ import { PortForwardButton } from "./PortForwardButton";
4140
import { AgentSSHButton } from "./SSHButton/SSHButton";
4241
import { TerminalLink } from "./TerminalLink/TerminalLink";
4342
import { VSCodeDesktopButton } from "./VSCodeDesktopButton/VSCodeDesktopButton";
43+
import { useAgentLogs } from "./useAgentLogs";
4444

4545
export interface AgentRowProps {
4646
agent: WorkspaceAgent;
@@ -89,12 +89,7 @@ export const AgentRow: FC<AgentRowProps> = ({
8989
["starting", "start_timeout"].includes(agent.lifecycle_state) &&
9090
hasStartupFeatures,
9191
);
92-
const agentLogs = useAgentLogs({
93-
workspaceId: workspace.id,
94-
agentId: agent.id,
95-
agentLifeCycleState: agent.lifecycle_state,
96-
enabled: showLogs,
97-
});
92+
const agentLogs = useAgentLogs(agent, showLogs);
9893
const logListRef = useRef<List>(null);
9994
const logListDivRef = useRef<HTMLDivElement>(null);
10095
const startupLogs = useMemo(() => {

site/src/modules/resources/DownloadAgentLogsButton.stories.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const meta: Meta<typeof DownloadAgentLogsButton> = {
1515
parameters: {
1616
queries: [
1717
{
18-
key: agentLogsKey(MockWorkspace.id, MockWorkspaceAgent.id),
18+
key: agentLogsKey(MockWorkspaceAgent.id),
1919
data: generateLogs(5),
2020
},
2121
],

site/src/modules/resources/DownloadAgentLogsButton.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const DownloadAgentLogsButton: FC<DownloadAgentLogsButtonProps> = ({
2323
const [isDownloading, setIsDownloading] = useState(false);
2424

2525
const fetchLogs = async () => {
26-
const queryOpts = agentLogs(workspaceId, agent.id);
26+
const queryOpts = agentLogs(agent.id);
2727
let logs = queryClient.getQueryData<WorkspaceAgentLog[]>(
2828
queryOpts.queryKey,
2929
);

0 commit comments

Comments
 (0)