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

feat(site): add download logs option #13466

merged 27 commits into from
Jun 7, 2024

Conversation

BrunoQuaresma
Copy link
Collaborator

Close #12485

Screen.Recording.2024-06-03.at.15.31.41.mov

@BrunoQuaresma BrunoQuaresma requested a review from a team June 4, 2024 14:36
@BrunoQuaresma BrunoQuaresma self-assigned this Jun 4, 2024
@BrunoQuaresma BrunoQuaresma requested review from code-asher and removed request for a team June 4, 2024 14:36
Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very handy!

Copy link
Member

@Parkreiner Parkreiner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far! I feel like there might be some ways to tighten up useAgentLogs, but I'll come back to this tomorrow morning, when I'm not so tired

@Parkreiner
Copy link
Member

Parkreiner commented Jun 6, 2024

@BrunoQuaresma What do you think of this?

type UseAgentLogsOptions = Readonly<{
  workspaceId: string;
  agentId: string;
  agentLifeCycleState: WorkspaceAgentLifecycle;

  enabled?: boolean;
}>;

export function useAgentLogs(
  options: UseAgentLogsOptions,
): readonly WorkspaceAgentLog[] | undefined {
  const { workspaceId, agentId, agentLifeCycleState, enabled = true } = options;
  const queryClient = useQueryClient();
  const queryOptions = agentLogs(workspaceId, agentId);

  const { data } = useQuery({
    ...queryOptions,
    enabled,
    select: (logs) => {
      return {
        logs,
        lastLogId: logs.at(-1)?.id ?? 0,
      };
    },
  });

  const socketRef = useRef<WebSocket | null>(null);
  const lastInitializedAgentIdRef = useRef<string | null>(null);

  const addLogs = useEffectEvent((newLogs: WorkspaceAgentLog[]) => {
    queryClient.setQueryData(
      queryOptions.queryKey,
      (oldLogs: WorkspaceAgentLog[] = []) => [...oldLogs, ...newLogs],
    );
  });

  useEffect(() => {
    const isSameAgentId = agentId === lastInitializedAgentIdRef.current;
    if (!isSameAgentId) {
      socketRef.current?.close();
    }

    const cannotCreateSocket =
      agentLifeCycleState !== "starting" || data === undefined;
    if (cannotCreateSocket) {
      return;
    }

    const socket = watchWorkspaceAgentLogs(agentId, {
      after: data.lastLogId,
      onMessage: (newLogs) => {
        // Prevent new logs getting added when a connection is not open
        if (socket.readyState === WebSocket.OPEN) {
          addLogs(newLogs);;
        }
      },
      onError: (error) => {
        // Might be able to put more precise error handling here?
        console.error(error);
      },
    });

    socketRef.current = socket;
    lastInitializedAgentIdRef.current = agentId;
  }, [addLogs, agentId, agentLifeCycleState, data]);

  // The above effect is likely going to run a lot because we don't know when or
  // how agentLifeCycleState will change over time (it's a union of nine
  // values). The only way to ensure that we only close when we unmount is by
  // putting the logic into a separate effect with an empty dependency array
  useEffect(() => {
    const closeSocketOnUnmount = () => socketRef.current?.close();
    return closeSocketOnUnmount;
  }, []);

  return data?.logs;
}

@Parkreiner
Copy link
Member

Parkreiner commented Jun 6, 2024

@BrunoQuaresma Realized one other thing: if the query for the agent logs hook is only able to run once, is it a problem if the query doesn't come back in the starting state at first?

I don't know enough about lifecycle states, and I don't know if there's a flow chart for how the states can transition into each other anywhere

@BrunoQuaresma
Copy link
Collaborator Author

@BrunoQuaresma Realized one other thing: if the query for the agent logs hook is only able to run once, is it a problem if the query doesn't come back in the starting state at first?

Do you have a use case that could contextualize this better?

@BrunoQuaresma BrunoQuaresma requested a review from Parkreiner June 6, 2024 14:38
@Parkreiner
Copy link
Member

Parkreiner commented Jun 6, 2024

Do you have a use case that could contextualize this better?
@BrunoQuaresma To be honest, I'm not sure – I was kind of hoping you would know

Looking at the type definition for WorkspaceAgentLifecycle, I see that there's a ready state that sees like it could be a good candidate for the log functionality. But I don't know if that's actually true, or if there's some nuance that I'm missing. I would assume that from the names, a workspace transitions from starting to ready

My main worry was, if a workspace is already in the ready state, is it okay to skip generating logs for it? Or is it impossible for that to happen? The current logic only makes logs if we're specifically starting up

@BrunoQuaresma
Copy link
Collaborator Author

@Parkreiner I think I maybe answered this question here #13466 (comment)

@BrunoQuaresma
Copy link
Collaborator Author

@Parkreiner I had to roll back to the previous useAgentLogs implementation because it was causing a loop since the data was in the dependencies list. I also removed the select option to get the last agent log ID because it updates on every data change keeping it unstable.

@Parkreiner
Copy link
Member

@Parkreiner I had to roll back to the previous useAgentLogs implementation because it was causing a loop since the data was in the dependencies list. I also removed the select option to get the last agent log ID because it updates on every data change keeping it unstable.

@BrunoQuaresma Ah, right – forgot that we are mutating the cache, so the data is potentially going to be changing a lot

Copy link
Member

@Parkreiner Parkreiner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! And glad you caught that infinite loop issue

Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@BrunoQuaresma BrunoQuaresma merged commit 056a697 into main Jun 7, 2024
30 checks passed
@BrunoQuaresma BrunoQuaresma deleted the bq/download-logs branch June 7, 2024 13:03
@github-actions github-actions bot locked and limited conversation to collaborators Jun 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feat: Add a log download feature to workspace detail page
4 participants