-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathclickhouseRunsRepository.server.ts
More file actions
335 lines (285 loc) · 9.75 KB
/
clickhouseRunsRepository.server.ts
File metadata and controls
335 lines (285 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { type ClickhouseQueryBuilder } from "@internal/clickhouse";
import { RunId } from "@trigger.dev/core/v3/isomorphic";
import {
type FilterRunsOptions,
type IRunsRepository,
type ListRunsOptions,
type RunListInputOptions,
type RunsRepositoryOptions,
type TagListOptions,
convertRunListInputOptionsToFilterRunsOptions,
} from "./runsRepository.server";
import parseDuration from "parse-duration";
export class ClickHouseRunsRepository implements IRunsRepository {
constructor(private readonly options: RunsRepositoryOptions) {}
get name() {
return "clickhouse";
}
async listRunIds(options: ListRunsOptions) {
const queryBuilder = this.options.clickhouse.taskRuns.queryBuilder();
applyRunFiltersToQueryBuilder(
queryBuilder,
await convertRunListInputOptionsToFilterRunsOptions(options, this.options.prisma)
);
if (options.page.cursor) {
if (options.page.direction === "forward" || !options.page.direction) {
queryBuilder
.where("run_id < {runId: String}", { runId: options.page.cursor })
.orderBy("created_at DESC, run_id DESC")
.limit(options.page.size + 1);
} else {
queryBuilder
.where("run_id > {runId: String}", { runId: options.page.cursor })
.orderBy("created_at ASC, run_id ASC")
.limit(options.page.size + 1);
}
} else {
// Initial page - no cursor provided
queryBuilder.orderBy("created_at DESC, run_id DESC").limit(options.page.size + 1);
}
const [queryError, result] = await queryBuilder.execute();
if (queryError) {
throw queryError;
}
const runIds = result.map((row) => row.run_id);
return runIds;
}
async listFriendlyRunIds(options: ListRunsOptions) {
// First get internal IDs from ClickHouse
const internalIds = await this.listRunIds(options);
if (internalIds.length === 0) {
return [];
}
// Then get friendly IDs from Prisma
const runs = await this.options.prisma.taskRun.findMany({
where: {
id: {
in: internalIds,
},
},
select: {
friendlyId: true,
},
});
return runs.map((run) => run.friendlyId);
}
async listRuns(options: ListRunsOptions) {
const runIds = await this.listRunIds(options);
// If there are more runs than the page size, we need to fetch the next page
const hasMore = runIds.length > options.page.size;
let nextCursor: string | null = null;
let previousCursor: string | null = null;
//get cursors for next and previous pages
const direction = options.page.direction ?? "forward";
switch (direction) {
case "forward": {
previousCursor = options.page.cursor ? runIds.at(0) ?? null : null;
if (hasMore) {
// The next cursor should be the last run ID from this page
nextCursor = runIds[options.page.size - 1];
}
break;
}
case "backward": {
const reversedRunIds = [...runIds].reverse();
if (hasMore) {
previousCursor = reversedRunIds.at(1) ?? null;
nextCursor = reversedRunIds.at(options.page.size) ?? null;
} else {
// Use the last item (oldest run) as the forward cursor.
// We can't use a fixed index (pageSize - 1) because the result set
// may have fewer items than pageSize (e.g., when new runs were created
// while the user was browsing, shifting page boundaries).
nextCursor = reversedRunIds.at(reversedRunIds.length - 1) ?? null;
}
break;
}
}
const runIdsToReturn =
options.page.direction === "backward" && hasMore
? runIds.slice(1, options.page.size + 1)
: runIds.slice(0, options.page.size);
let runs = await this.options.prisma.taskRun.findMany({
where: {
id: {
in: runIdsToReturn,
},
},
orderBy: {
id: "desc",
},
select: {
id: true,
friendlyId: true,
taskIdentifier: true,
taskVersion: true,
runtimeEnvironmentId: true,
status: true,
createdAt: true,
startedAt: true,
lockedAt: true,
delayUntil: true,
updatedAt: true,
completedAt: true,
isTest: true,
spanId: true,
idempotencyKey: true,
ttl: true,
expiredAt: true,
costInCents: true,
baseCostInCents: true,
usageDurationMs: true,
runTags: true,
depth: true,
rootTaskRunId: true,
batchId: true,
metadata: true,
metadataType: true,
machinePreset: true,
queue: true,
},
});
// ClickHouse is slightly delayed, so we're going to do in-memory status filtering too
if (options.statuses && options.statuses.length > 0) {
runs = runs.filter((run) => options.statuses!.includes(run.status));
}
return {
runs,
pagination: {
nextCursor,
previousCursor,
},
};
}
async countRuns(options: RunListInputOptions) {
const queryBuilder = this.options.clickhouse.taskRuns.countQueryBuilder();
applyRunFiltersToQueryBuilder(
queryBuilder,
await convertRunListInputOptionsToFilterRunsOptions(options, this.options.prisma)
);
const [queryError, result] = await queryBuilder.execute();
if (queryError) {
throw queryError;
}
if (result.length === 0) {
throw new Error("No count rows returned");
}
return result[0].count;
}
async listTags(options: TagListOptions) {
const queryBuilder = this.options.clickhouse.taskRuns
.tagQueryBuilder()
.where("organization_id = {organizationId: String}", {
organizationId: options.organizationId,
})
.where("project_id = {projectId: String}", {
projectId: options.projectId,
})
.where("environment_id = {environmentId: String}", {
environmentId: options.environmentId,
});
const periodMs = options.period ? parseDuration(options.period) ?? undefined : undefined;
if (periodMs) {
queryBuilder.where("created_at >= fromUnixTimestamp64Milli({period: Int64})", {
period: new Date(Date.now() - periodMs).getTime(),
});
}
if (options.from) {
queryBuilder.where("created_at >= fromUnixTimestamp64Milli({from: Int64})", {
from: options.from,
});
}
if (options.to) {
queryBuilder.where("created_at <= fromUnixTimestamp64Milli({to: Int64})", { to: options.to });
}
// Filter by query (case-insensitive contains search)
if (options.query && options.query.trim().length > 0) {
queryBuilder.where("positionCaseInsensitiveUTF8(tag, {query: String}) > 0", {
query: options.query,
});
}
// Add ordering and pagination
queryBuilder.orderBy("tag ASC").limit(options.limit);
const [queryError, result] = await queryBuilder.execute();
if (queryError) {
throw queryError;
}
return {
tags: result.map((row) => row.tag),
};
}
}
function applyRunFiltersToQueryBuilder<T>(
queryBuilder: ClickhouseQueryBuilder<T>,
options: FilterRunsOptions
) {
queryBuilder
.where("organization_id = {organizationId: String}", {
organizationId: options.organizationId,
})
.where("project_id = {projectId: String}", {
projectId: options.projectId,
})
.where("environment_id = {environmentId: String}", {
environmentId: options.environmentId,
});
if (options.tasks && options.tasks.length > 0) {
queryBuilder.where("task_identifier IN {tasks: Array(String)}", { tasks: options.tasks });
}
if (options.versions && options.versions.length > 0) {
queryBuilder.where("task_version IN {versions: Array(String)}", {
versions: options.versions,
});
}
if (options.statuses && options.statuses.length > 0) {
queryBuilder.where("status IN {statuses: Array(String)}", { statuses: options.statuses });
}
if (options.tags && options.tags.length > 0) {
queryBuilder.where("hasAny(tags, {tags: Array(String)})", { tags: options.tags });
}
if (options.scheduleId) {
queryBuilder.where("schedule_id = {scheduleId: String}", { scheduleId: options.scheduleId });
}
// Period is a number of milliseconds duration
if (options.period) {
queryBuilder.where("created_at >= fromUnixTimestamp64Milli({period: Int64})", {
period: new Date(Date.now() - options.period).getTime(),
});
}
if (options.from) {
queryBuilder.where("created_at >= fromUnixTimestamp64Milli({from: Int64})", {
from: options.from,
});
}
if (options.to) {
queryBuilder.where("created_at <= fromUnixTimestamp64Milli({to: Int64})", { to: options.to });
}
if (typeof options.isTest === "boolean") {
queryBuilder.where("is_test = {isTest: Boolean}", { isTest: options.isTest });
}
if (options.rootOnly) {
queryBuilder.where("root_run_id = ''");
}
if (options.batchId) {
queryBuilder.where("batch_id = {batchId: String}", { batchId: options.batchId });
}
if (options.bulkId) {
queryBuilder.where("hasAny(bulk_action_group_ids, {bulkActionGroupIds: Array(String)})", {
bulkActionGroupIds: [options.bulkId],
});
}
if (options.runId && options.runId.length > 0) {
// it's important that in the query it's "runIds", otherwise it clashes with the cursor which is called "runId"
queryBuilder.where("friendly_id IN {runIds: Array(String)}", {
runIds: options.runId.map((runId) => RunId.toFriendlyId(runId)),
});
}
if (options.queues && options.queues.length > 0) {
queryBuilder.where("queue IN {queues: Array(String)}", { queues: options.queues });
}
if (options.machines && options.machines.length > 0) {
queryBuilder.where("machine_preset IN {machines: Array(String)}", {
machines: options.machines,
});
}
}