Skip to content

Commit be70f25

Browse files
committed
Improve the query to get the latest tasks for the task list presenter
1 parent 30813f1 commit be70f25

File tree

2 files changed

+57
-59
lines changed

2 files changed

+57
-59
lines changed

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

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import { CURRENT_DEPLOYMENT_LABEL } from "@trigger.dev/core/v3/isomorphic";
2-
import { PrismaClientOrTransaction, type TaskTriggerSource } from "@trigger.dev/database";
3-
import { $replica, sqlDatabaseSchema } from "~/db.server";
1+
import {
2+
PrismaClientOrTransaction,
3+
RuntimeEnvironmentType,
4+
type TaskTriggerSource,
5+
} from "@trigger.dev/database";
6+
import { $replica } from "~/db.server";
47
import { clickhouseClient } from "~/services/clickhouseInstance.server";
58
import {
9+
AverageDurations,
610
ClickHouseEnvironmentMetricsRepository,
11+
CurrentRunningStats,
712
DailyTaskActivity,
813
EnvironmentMetricsRepository,
914
PostgrestEnvironmentMetricsRepository,
1015
} from "~/services/environmentMetricsRepository.server";
1116
import { singleton } from "~/utils/singleton";
17+
import { findCurrentWorkerFromEnvironment } from "~/v3/models/workerDeployment.server";
1218

1319
export type TaskListItem = {
1420
slug: string;
@@ -25,77 +31,69 @@ export class TaskListPresenter {
2531
private readonly _replica: PrismaClientOrTransaction
2632
) {}
2733

28-
public async call({ environmentId, projectId }: { environmentId: string; projectId: string }) {
29-
const tasks = await this._replica.$queryRaw<
34+
public async call({
35+
environmentId,
36+
environmentType,
37+
}: {
38+
environmentId: string;
39+
environmentType: RuntimeEnvironmentType;
40+
}) {
41+
const currentWorker = await findCurrentWorkerFromEnvironment(
3042
{
31-
id: string;
32-
slug: string;
33-
filePath: string;
34-
createdAt: Date;
35-
triggerSource: TaskTriggerSource;
36-
}[]
37-
>`
38-
WITH non_dev_workers AS (
39-
SELECT wd."workerId" AS id
40-
FROM ${sqlDatabaseSchema}."WorkerDeploymentPromotion" wdp
41-
INNER JOIN ${sqlDatabaseSchema}."WorkerDeployment" wd
42-
ON wd.id = wdp."deploymentId"
43-
WHERE wdp."environmentId" = ${environmentId}
44-
AND wdp."label" = ${CURRENT_DEPLOYMENT_LABEL}
45-
),
46-
workers AS (
47-
SELECT DISTINCT ON ("runtimeEnvironmentId") id, "runtimeEnvironmentId", version
48-
FROM ${sqlDatabaseSchema}."BackgroundWorker"
49-
WHERE "runtimeEnvironmentId" = ${environmentId}
50-
OR id IN (SELECT id FROM non_dev_workers)
51-
ORDER BY "runtimeEnvironmentId", "createdAt" DESC
52-
)
53-
SELECT tasks.id, slug, "filePath", "triggerSource", tasks."runtimeEnvironmentId", tasks."createdAt"
54-
FROM workers
55-
JOIN ${sqlDatabaseSchema}."BackgroundWorkerTask" tasks ON tasks."workerId" = workers.id
56-
ORDER BY slug ASC;`;
57-
58-
//then get the activity for each task
59-
const activity = this.#getActivity(
60-
tasks.map((t) => t.slug),
61-
environmentId
43+
id: environmentId,
44+
type: environmentType,
45+
},
46+
this._replica
6247
);
6348

64-
const runningStats = this.#getRunningStats(
65-
tasks.map((t) => t.slug),
66-
environmentId
67-
);
49+
if (!currentWorker) {
50+
return {
51+
tasks: [],
52+
activity: Promise.resolve({} as DailyTaskActivity),
53+
runningStats: Promise.resolve({} as CurrentRunningStats),
54+
durations: Promise.resolve({} as AverageDurations),
55+
};
56+
}
6857

69-
const durations = this.#getAverageDurations(
70-
tasks.map((t) => t.slug),
71-
environmentId
72-
);
58+
const tasks = await this._replica.backgroundWorkerTask.findMany({
59+
where: {
60+
workerId: currentWorker.id,
61+
},
62+
select: {
63+
id: true,
64+
slug: true,
65+
filePath: true,
66+
triggerSource: true,
67+
createdAt: true,
68+
},
69+
orderBy: {
70+
slug: "asc",
71+
},
72+
});
7373

74-
return { tasks, activity, runningStats, durations };
75-
}
74+
const slugs = tasks.map((t) => t.slug);
7675

77-
async #getActivity(tasks: string[], environmentId: string) {
78-
return this.environmentMetricsRepository.getDailyTaskActivity({
76+
// IMPORTANT: Don't await these, we want to return the promises
77+
// so we can defer the loading of the data
78+
const activity = this.environmentMetricsRepository.getDailyTaskActivity({
7979
environmentId,
80-
days: 6,
81-
tasks,
80+
days: 6, // This actually means 7 days, because we want to show the current day too
81+
tasks: slugs,
8282
});
83-
}
8483

85-
async #getRunningStats(tasks: string[], environmentId: string) {
86-
return this.environmentMetricsRepository.getCurrentRunningStats({
84+
const runningStats = this.environmentMetricsRepository.getCurrentRunningStats({
8785
environmentId,
8886
days: 6,
89-
tasks,
87+
tasks: slugs,
9088
});
91-
}
9289

93-
async #getAverageDurations(tasks: string[], environmentId: string) {
94-
return this.environmentMetricsRepository.getAverageDurations({
90+
const durations = this.environmentMetricsRepository.getAverageDurations({
9591
environmentId,
9692
days: 6,
97-
tasks,
93+
tasks: slugs,
9894
});
95+
96+
return { tasks, activity, runningStats, durations };
9997
}
10098
}
10199

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam._index/route.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
126126
try {
127127
const { tasks, activity, runningStats, durations } = await taskListPresenter.call({
128128
environmentId: environment.id,
129-
projectId: project.id,
129+
environmentType: environment.type,
130130
});
131131

132132
const usefulLinksPreference = await getUsefulLinksPreference(request);

0 commit comments

Comments
 (0)