Skip to content

Commit 382a71c

Browse files
committed
The task stats need org id and project id too
1 parent 5039c0e commit 382a71c

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import {
2-
PrismaClientOrTransaction,
3-
RuntimeEnvironmentType,
2+
type PrismaClientOrTransaction,
3+
type RuntimeEnvironmentType,
44
type TaskTriggerSource,
55
} from "@trigger.dev/database";
66
import { $replica } from "~/db.server";
77
import { clickhouseClient } from "~/services/clickhouseInstance.server";
88
import {
9-
AverageDurations,
9+
type AverageDurations,
1010
ClickHouseEnvironmentMetricsRepository,
11-
CurrentRunningStats,
12-
DailyTaskActivity,
13-
EnvironmentMetricsRepository,
11+
type CurrentRunningStats,
12+
type DailyTaskActivity,
13+
type EnvironmentMetricsRepository,
1414
PostgrestEnvironmentMetricsRepository,
1515
} from "~/services/environmentMetricsRepository.server";
1616
import { singleton } from "~/utils/singleton";
@@ -32,9 +32,13 @@ export class TaskListPresenter {
3232
) {}
3333

3434
public async call({
35+
organizationId,
36+
projectId,
3537
environmentId,
3638
environmentType,
3739
}: {
40+
organizationId: string;
41+
projectId: string;
3842
environmentId: string;
3943
environmentType: RuntimeEnvironmentType;
4044
}) {
@@ -76,18 +80,24 @@ export class TaskListPresenter {
7680
// IMPORTANT: Don't await these, we want to return the promises
7781
// so we can defer the loading of the data
7882
const activity = this.environmentMetricsRepository.getDailyTaskActivity({
83+
organizationId,
84+
projectId,
7985
environmentId,
8086
days: 6, // This actually means 7 days, because we want to show the current day too
8187
tasks: slugs,
8288
});
8389

8490
const runningStats = this.environmentMetricsRepository.getCurrentRunningStats({
91+
organizationId,
92+
projectId,
8593
environmentId,
8694
days: 6,
8795
tasks: slugs,
8896
});
8997

9098
const durations = this.environmentMetricsRepository.getAverageDurations({
99+
organizationId,
100+
projectId,
91101
environmentId,
92102
days: 6,
93103
tasks: slugs,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
125125

126126
try {
127127
const { tasks, activity, runningStats, durations } = await taskListPresenter.call({
128+
organizationId: project.organizationId,
129+
projectId: project.id,
128130
environmentId: environment.id,
129131
environmentType: environment.type,
130132
});

apps/webapp/app/services/environmentMetricsRepository.server.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,24 @@ export type AverageDurations = Record<string, number>;
1010

1111
export interface EnvironmentMetricsRepository {
1212
getDailyTaskActivity(options: {
13+
organizationId: string;
14+
projectId: string;
1315
environmentId: string;
1416
days: number;
1517
tasks: string[];
1618
}): Promise<DailyTaskActivity>;
1719

1820
getCurrentRunningStats(options: {
21+
organizationId: string;
22+
projectId: string;
1923
environmentId: string;
2024
days: number;
2125
tasks: string[];
2226
}): Promise<CurrentRunningStats>;
2327

2428
getAverageDurations(options: {
29+
organizationId: string;
30+
projectId: string;
2531
environmentId: string;
2632
days: number;
2733
tasks: string[];
@@ -177,10 +183,14 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
177183
constructor(private readonly options: ClickHouseEnvironmentMetricsRepositoryOptions) {}
178184

179185
public async getDailyTaskActivity({
186+
organizationId,
187+
projectId,
180188
environmentId,
181189
days,
182190
tasks,
183191
}: {
192+
organizationId: string;
193+
projectId: string;
184194
environmentId: string;
185195
days: number;
186196
tasks: string[];
@@ -190,6 +200,8 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
190200
}
191201

192202
const [queryError, activity] = await this.options.clickhouse.taskRuns.getTaskActivity({
203+
organizationId,
204+
projectId,
193205
environmentId,
194206
days,
195207
});
@@ -210,10 +222,14 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
210222
}
211223

212224
public async getCurrentRunningStats({
225+
organizationId,
226+
projectId,
213227
environmentId,
214228
days,
215229
tasks,
216230
}: {
231+
organizationId: string;
232+
projectId: string;
217233
environmentId: string;
218234
days: number;
219235
tasks: string[];
@@ -223,6 +239,8 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
223239
}
224240

225241
const [queryError, stats] = await this.options.clickhouse.taskRuns.getCurrentRunningStats({
242+
organizationId,
243+
projectId,
226244
environmentId,
227245
days,
228246
});
@@ -242,10 +260,14 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
242260
}
243261

244262
public async getAverageDurations({
263+
organizationId,
264+
projectId,
245265
environmentId,
246266
days,
247267
tasks,
248268
}: {
269+
organizationId: string;
270+
projectId: string;
249271
environmentId: string;
250272
days: number;
251273
tasks: string[];
@@ -255,6 +277,8 @@ export class ClickHouseEnvironmentMetricsRepository implements EnvironmentMetric
255277
}
256278

257279
const [queryError, durations] = await this.options.clickhouse.taskRuns.getAverageDurations({
280+
organizationId,
281+
projectId,
258282
environmentId,
259283
days,
260284
});

apps/webapp/test/runsRepository.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ describe("RunsRepository", () => {
7272
page: { size: 10 },
7373
projectId: project.id,
7474
environmentId: runtimeEnvironment.id,
75+
organizationId: organization.id,
7576
});
7677

7778
expect(runs).toHaveLength(1);
@@ -180,6 +181,7 @@ describe("RunsRepository", () => {
180181
page: { size: 10 },
181182
projectId: project.id,
182183
environmentId: runtimeEnvironment.id,
184+
organizationId: organization.id,
183185
tasks: ["task-1", "task-2"],
184186
});
185187

@@ -290,6 +292,7 @@ describe("RunsRepository", () => {
290292
page: { size: 10 },
291293
projectId: project.id,
292294
environmentId: runtimeEnvironment.id,
295+
organizationId: organization.id,
293296
versions: ["1.0.0", "3.0.0"],
294297
});
295298

@@ -400,6 +403,7 @@ describe("RunsRepository", () => {
400403
page: { size: 10 },
401404
projectId: project.id,
402405
environmentId: runtimeEnvironment.id,
406+
organizationId: organization.id,
403407
statuses: ["PENDING", "COMPLETED_SUCCESSFULLY"],
404408
});
405409

@@ -510,6 +514,7 @@ describe("RunsRepository", () => {
510514
page: { size: 10 },
511515
projectId: project.id,
512516
environmentId: runtimeEnvironment.id,
517+
organizationId: organization.id,
513518
tags: ["urgent"],
514519
});
515520

@@ -619,6 +624,7 @@ describe("RunsRepository", () => {
619624
page: { size: 10 },
620625
projectId: project.id,
621626
environmentId: runtimeEnvironment.id,
627+
organizationId: organization.id,
622628
scheduleId: "schedule_1",
623629
});
624630

@@ -712,6 +718,7 @@ describe("RunsRepository", () => {
712718
page: { size: 10 },
713719
projectId: project.id,
714720
environmentId: runtimeEnvironment.id,
721+
organizationId: organization.id,
715722
isTest: true,
716723
});
717724

@@ -723,6 +730,7 @@ describe("RunsRepository", () => {
723730
page: { size: 10 },
724731
projectId: project.id,
725732
environmentId: runtimeEnvironment.id,
733+
organizationId: organization.id,
726734
isTest: false,
727735
});
728736

@@ -816,6 +824,7 @@ describe("RunsRepository", () => {
816824
page: { size: 10 },
817825
projectId: project.id,
818826
environmentId: runtimeEnvironment.id,
827+
organizationId: organization.id,
819828
rootOnly: true,
820829
});
821830

@@ -945,6 +954,7 @@ describe("RunsRepository", () => {
945954
page: { size: 10 },
946955
projectId: project.id,
947956
environmentId: runtimeEnvironment.id,
957+
organizationId: organization.id,
948958
batchId: batchRun1.id,
949959
});
950960

@@ -1052,6 +1062,7 @@ describe("RunsRepository", () => {
10521062
page: { size: 10 },
10531063
projectId: project.id,
10541064
environmentId: runtimeEnvironment.id,
1065+
organizationId: organization.id,
10551066
runFriendlyIds: ["run_abc", "run_xyz"],
10561067
});
10571068

@@ -1159,6 +1170,7 @@ describe("RunsRepository", () => {
11591170
page: { size: 10 },
11601171
projectId: project.id,
11611172
environmentId: runtimeEnvironment.id,
1173+
organizationId: organization.id,
11621174
runIds: [run1.id, run3.id],
11631175
});
11641176

@@ -1273,6 +1285,7 @@ describe("RunsRepository", () => {
12731285
page: { size: 10 },
12741286
projectId: project.id,
12751287
environmentId: runtimeEnvironment.id,
1288+
organizationId: organization.id,
12761289
from: yesterday.getTime(),
12771290
to: now.getTime(),
12781291
});
@@ -1393,6 +1406,7 @@ describe("RunsRepository", () => {
13931406
page: { size: 10 },
13941407
projectId: project.id,
13951408
environmentId: runtimeEnvironment.id,
1409+
organizationId: organization.id,
13961410
tasks: ["task-1"],
13971411
versions: ["1.0.0"],
13981412
statuses: ["COMPLETED_SUCCESSFULLY"],
@@ -1476,6 +1490,7 @@ describe("RunsRepository", () => {
14761490
page: { size: 2 },
14771491
projectId: project.id,
14781492
environmentId: runtimeEnvironment.id,
1493+
organizationId: organization.id,
14791494
});
14801495

14811496
expect(firstPage.runs).toHaveLength(2);
@@ -1491,6 +1506,7 @@ describe("RunsRepository", () => {
14911506
},
14921507
projectId: project.id,
14931508
environmentId: runtimeEnvironment.id,
1509+
organizationId: organization.id,
14941510
});
14951511

14961512
expect(secondPage.runs).toHaveLength(2);

internal-packages/clickhouse/src/taskRuns.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ export const TaskActivityQueryResult = z.object({
113113
export type TaskActivityQueryResult = z.infer<typeof TaskActivityQueryResult>;
114114

115115
export const TaskActivityQueryParams = z.object({
116+
organizationId: z.string(),
117+
projectId: z.string(),
116118
environmentId: z.string(),
117119
days: z.number().int(),
118120
});
@@ -128,7 +130,9 @@ export function getTaskActivityQueryBuilder(ch: ClickhouseReader, settings?: Cli
128130
count() as count
129131
FROM trigger_dev.task_runs_v2 FINAL
130132
WHERE
131-
environment_id = {environmentId: String}
133+
organization_id = {organizationId: String}
134+
AND project_id = {projectId: String}
135+
AND environment_id = {environmentId: String}
132136
AND created_at >= today() - {days: Int64}
133137
AND _is_deleted = 0
134138
GROUP BY
@@ -155,6 +159,8 @@ export const CurrentRunningStatsQueryResult = z.object({
155159
export type CurrentRunningStatsQueryResult = z.infer<typeof CurrentRunningStatsQueryResult>;
156160

157161
export const CurrentRunningStatsQueryParams = z.object({
162+
organizationId: z.string(),
163+
projectId: z.string(),
158164
environmentId: z.string(),
159165
days: z.number().int(),
160166
});
@@ -169,7 +175,9 @@ export function getCurrentRunningStats(ch: ClickhouseReader, settings?: ClickHou
169175
count() as count
170176
FROM trigger_dev.task_runs_v2 FINAL
171177
WHERE
172-
environment_id = {environmentId: String}
178+
organization_id = {organizationId: String}
179+
AND project_id = {projectId: String}
180+
AND environment_id = {environmentId: String}
173181
AND status IN ('PENDING', 'WAITING_FOR_DEPLOY', 'WAITING_TO_RESUME', 'QUEUED', 'EXECUTING')
174182
AND _is_deleted = 0
175183
AND created_at >= now() - INTERVAL {days: Int64} DAY
@@ -193,6 +201,8 @@ export const AverageDurationsQueryResult = z.object({
193201
export type AverageDurationsQueryResult = z.infer<typeof AverageDurationsQueryResult>;
194202

195203
export const AverageDurationsQueryParams = z.object({
204+
organizationId: z.string(),
205+
projectId: z.string(),
196206
environmentId: z.string(),
197207
days: z.number().int(),
198208
});
@@ -206,7 +216,9 @@ export function getAverageDurations(ch: ClickhouseReader, settings?: ClickHouseS
206216
avg(toUnixTimestamp(completed_at) - toUnixTimestamp(started_at)) as duration
207217
FROM trigger_dev.task_runs_v2 FINAL
208218
WHERE
209-
environment_id = {environmentId: String}
219+
organization_id = {organizationId: String}
220+
AND project_id = {projectId: String}
221+
AND environment_id = {environmentId: String}
210222
AND created_at >= today() - {days: Int64}
211223
AND status IN ('COMPLETED_SUCCESSFULLY', 'COMPLETED_WITH_ERRORS')
212224
AND started_at IS NOT NULL

0 commit comments

Comments
 (0)