Skip to content

fix: display build timings when all timings are loaded #15728

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 5 commits into from
Dec 3, 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
Refetch timings until script timings are present
  • Loading branch information
BrunoQuaresma committed Dec 2, 2024
commit 2181bec72b5334e5e15dc575d2234860be02920b
2 changes: 0 additions & 2 deletions site/src/api/queries/workspaceBuilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ export const infiniteWorkspaceBuilds = (
// We use readyAgentsCount to invalidate the query when an agent connects
export const workspaceBuildTimings = (
workspaceBuildId: string,
readyAgentsCount: number,
) => {
return {
queryKey: [
"workspaceBuilds",
workspaceBuildId,
"timings",
{ readyAgentsCount },
Copy link
Member

Choose a reason for hiding this comment

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

What was the readyAgentCount value doing before? I see that we were only using it for the query key, and now it's gone

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was to refetch the query when a new agent was marked as ready but it was very buggy and a bad decision on my end. The current refetch logic makes more sense and it is easier to follow.

],
queryFn: () => API.workspaceBuildTimings(workspaceBuildId),
};
Expand Down
25 changes: 20 additions & 5 deletions site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,28 @@ export const WorkspaceReadyPage: FC<WorkspaceReadyPageProps> = ({
// Cancel build
const cancelBuildMutation = useMutation(cancelBuild(workspace, queryClient));

// Build Timings. Fetch build timings only when the build job is completed.
const readyAgents = workspace.latest_build.resources
.flatMap((r) => r.agents)
.filter((a) => a && a.lifecycle_state !== "starting");
// Workspace Timings.
const timingsQuery = useQuery({
...workspaceBuildTimings(workspace.latest_build.id, readyAgents.length),
...workspaceBuildTimings(workspace.latest_build.id),

// Fetch build timings only when the build job is completed.
enabled: Boolean(workspace.latest_build.job.completed_at),

// Sometimes, the timings can be fetched before the agent script timings are
// done or saved in the database so we need to conditionally refetch the
// timings. To refetch the timings, I found the best way was to compare the
// expected amount of script timings with the current amount of script
// timings returned in the response.
refetchInterval: (data) => {
const expectedScriptTimingsCount = workspace.latest_build.resources
.flatMap((r) => r.agents)
.flatMap((a) => a?.scripts)
.filter((s) => s !== undefined).length;
Copy link
Member

Choose a reason for hiding this comment

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

Tiny nit, but I want to say this can be cleaned up as:

const expectedScriptTimingsCount = workspace.latest_build.resources
  .flatMap((r) => r.agents)
  .flatMap((a) => a?.scripts ?? []).length;

const currentScriptTimingsCount = data?.agent_script_timings?.length ?? 0;
return expectedScriptTimingsCount === currentScriptTimingsCount
? false
: 1_000;
},
});

const runLastBuild = (
Expand Down