Skip to content

Commit d1a2762

Browse files
committed
Use array_agg for the run list tags so pagination works and we get a single result for each run
1 parent 4b4554a commit d1a2762

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

apps/webapp/app/presenters/v3/RunListPresenter.server.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export class RunListPresenter extends BasePresenter {
153153
const periodMs = period ? parse(period) : undefined;
154154

155155
//get the runs with tags, there will be multiple rows for each run if it has multiple tags
156-
const runsWithTags = await this._replica.$queryRaw<
156+
const runs = await this._replica.$queryRaw<
157157
{
158158
id: string;
159159
number: BigInt;
@@ -174,7 +174,7 @@ export class RunListPresenter extends BasePresenter {
174174
expiredAt: Date | null;
175175
costInCents: number;
176176
usageDurationMs: BigInt;
177-
tagName: string | null;
177+
tags: string[];
178178
}[]
179179
>`
180180
SELECT
@@ -197,7 +197,7 @@ export class RunListPresenter extends BasePresenter {
197197
tr."expiredAt" AS "expiredAt",
198198
tr."costInCents" AS "costInCents",
199199
tr."usageDurationMs" AS "usageDurationMs",
200-
tag.name AS "tagName"
200+
array_remove(array_agg(tag.name), NULL) AS "tags"
201201
FROM
202202
${sqlDatabaseSchema}."TaskRun" tr
203203
LEFT JOIN
@@ -271,26 +271,12 @@ WHERE
271271
)`
272272
: Prisma.empty
273273
}
274+
GROUP BY
275+
tr.id, bw.version
274276
ORDER BY
275277
${direction === "forward" ? Prisma.sql`tr.id DESC` : Prisma.sql`tr.id ASC`}
276278
LIMIT ${pageSize + 1}`;
277279

278-
//flatten the tags into a single row per run. Needs to be an array and keep the order
279-
const runs = runsWithTags.reduce((acc, run) => {
280-
const existingRun = acc.find((r) => r.id === run.id);
281-
if (existingRun) {
282-
if (run.tagName) {
283-
existingRun.tags.push(run.tagName);
284-
}
285-
} else {
286-
acc.push({
287-
...run,
288-
tags: run.tagName ? [run.tagName] : [],
289-
});
290-
}
291-
return acc;
292-
}, [] as (Omit<(typeof runsWithTags)[number], "tagId" | "tagName"> & { tags: string[] })[]);
293-
294280
const hasMore = runs.length > pageSize;
295281

296282
//get cursors for next and previous pages

0 commit comments

Comments
 (0)