1
- import { type ClickHouse , type ClickhouseQueryBuilder } from "@internal/clickhouse" ;
2
- import { type Tracer } from "@internal/tracing" ;
3
- import { type Logger , type LogLevel } from "@trigger.dev/core/logger" ;
4
- import { MachinePresetName } from "@trigger.dev/core/v3" ;
5
- import { BulkActionId , RunId } from "@trigger.dev/core/v3/isomorphic" ;
6
- import { TaskRunStatus } from "@trigger.dev/database" ;
7
- import parseDuration from "parse-duration" ;
8
- import { z } from "zod" ;
9
- import { timeFilters } from "~/components/runs/v3/SharedFilters" ;
10
- import { type PrismaClient } from "~/db.server" ;
11
-
12
- export type RunsRepositoryOptions = {
13
- clickhouse : ClickHouse ;
14
- prisma : PrismaClient ;
15
- logger ?: Logger ;
16
- logLevel ?: LogLevel ;
17
- tracer ?: Tracer ;
18
- } ;
19
-
20
- const RunStatus = z . enum ( Object . values ( TaskRunStatus ) as [ TaskRunStatus , ...TaskRunStatus [ ] ] ) ;
21
-
22
- const RunListInputOptionsSchema = z . object ( {
23
- organizationId : z . string ( ) ,
24
- projectId : z . string ( ) ,
25
- environmentId : z . string ( ) ,
26
- //filters
27
- tasks : z . array ( z . string ( ) ) . optional ( ) ,
28
- versions : z . array ( z . string ( ) ) . optional ( ) ,
29
- statuses : z . array ( RunStatus ) . optional ( ) ,
30
- tags : z . array ( z . string ( ) ) . optional ( ) ,
31
- scheduleId : z . string ( ) . optional ( ) ,
32
- period : z . string ( ) . optional ( ) ,
33
- from : z . number ( ) . optional ( ) ,
34
- to : z . number ( ) . optional ( ) ,
35
- isTest : z . boolean ( ) . optional ( ) ,
36
- rootOnly : z . boolean ( ) . optional ( ) ,
37
- batchId : z . string ( ) . optional ( ) ,
38
- runId : z . array ( z . string ( ) ) . optional ( ) ,
39
- bulkId : z . string ( ) . optional ( ) ,
40
- queues : z . array ( z . string ( ) ) . optional ( ) ,
41
- machines : MachinePresetName . array ( ) . optional ( ) ,
42
- } ) ;
43
-
44
- export type RunListInputOptions = z . infer < typeof RunListInputOptionsSchema > ;
45
- export type RunListInputFilters = Omit <
46
- RunListInputOptions ,
47
- "organizationId" | "projectId" | "environmentId"
48
- > ;
49
-
50
- export type ParsedRunFilters = RunListInputFilters & {
51
- cursor ?: string ;
52
- direction ?: "forward" | "backward" ;
53
- } ;
54
-
55
- type FilterRunsOptions = Omit < RunListInputOptions , "period" > & {
56
- period : number | undefined ;
57
- } ;
58
-
59
- type Pagination = {
60
- page : {
61
- size : number ;
62
- cursor ?: string ;
63
- direction ?: "forward" | "backward" ;
64
- } ;
65
- } ;
66
-
67
- export type ListRunsOptions = RunListInputOptions & Pagination ;
68
-
69
- export class RunsRepository {
1
+ import { type ClickhouseQueryBuilder } from "@internal/clickhouse" ;
2
+ import { RunId } from "@trigger.dev/core/v3/isomorphic" ;
3
+ import {
4
+ type FilterRunsOptions ,
5
+ type IRunsRepository ,
6
+ type ListRunsOptions ,
7
+ type RunListInputOptions ,
8
+ type RunsRepositoryOptions ,
9
+ convertRunListInputOptionsToFilterRunsOptions ,
10
+ } from "./runsRepository.server" ;
11
+
12
+ export class ClickHouseRunsRepository implements IRunsRepository {
70
13
constructor ( private readonly options : RunsRepositoryOptions ) { }
71
14
15
+ get name ( ) {
16
+ return "clickhouse" ;
17
+ }
18
+
72
19
async listRunIds ( options : ListRunsOptions ) {
73
20
const queryBuilder = this . options . clickhouse . taskRuns . queryBuilder ( ) ;
74
21
applyRunFiltersToQueryBuilder (
75
22
queryBuilder ,
76
- await this . # convertRunListInputOptionsToFilterRunsOptions( options )
23
+ await convertRunListInputOptionsToFilterRunsOptions ( options , this . options . prisma )
77
24
) ;
78
25
79
26
if ( options . page . cursor ) {
@@ -200,7 +147,7 @@ export class RunsRepository {
200
147
const queryBuilder = this . options . clickhouse . taskRuns . countQueryBuilder ( ) ;
201
148
applyRunFiltersToQueryBuilder (
202
149
queryBuilder ,
203
- await this . # convertRunListInputOptionsToFilterRunsOptions( options )
150
+ await convertRunListInputOptionsToFilterRunsOptions ( options , this . options . prisma )
204
151
) ;
205
152
206
153
const [ queryError , result ] = await queryBuilder . execute ( ) ;
@@ -215,73 +162,6 @@ export class RunsRepository {
215
162
216
163
return result [ 0 ] . count ;
217
164
}
218
-
219
- async #convertRunListInputOptionsToFilterRunsOptions(
220
- options : RunListInputOptions
221
- ) : Promise < FilterRunsOptions > {
222
- const convertedOptions : FilterRunsOptions = {
223
- ...options ,
224
- period : undefined ,
225
- } ;
226
-
227
- // Convert time period to ms
228
- const time = timeFilters ( {
229
- period : options . period ,
230
- from : options . from ,
231
- to : options . to ,
232
- } ) ;
233
- convertedOptions . period = time . period ? parseDuration ( time . period ) ?? undefined : undefined ;
234
-
235
- // batch friendlyId to id
236
- if ( options . batchId && options . batchId . startsWith ( "batch_" ) ) {
237
- const batch = await this . options . prisma . batchTaskRun . findFirst ( {
238
- select : {
239
- id : true ,
240
- } ,
241
- where : {
242
- friendlyId : options . batchId ,
243
- runtimeEnvironmentId : options . environmentId ,
244
- } ,
245
- } ) ;
246
-
247
- if ( batch ) {
248
- convertedOptions . batchId = batch . id ;
249
- }
250
- }
251
-
252
- // scheduleId can be a friendlyId
253
- if ( options . scheduleId && options . scheduleId . startsWith ( "sched_" ) ) {
254
- const schedule = await this . options . prisma . taskSchedule . findFirst ( {
255
- select : {
256
- id : true ,
257
- } ,
258
- where : {
259
- friendlyId : options . scheduleId ,
260
- projectId : options . projectId ,
261
- } ,
262
- } ) ;
263
-
264
- if ( schedule ) {
265
- convertedOptions . scheduleId = schedule ?. id ;
266
- }
267
- }
268
-
269
- if ( options . bulkId && options . bulkId . startsWith ( "bulk_" ) ) {
270
- convertedOptions . bulkId = BulkActionId . toId ( options . bulkId ) ;
271
- }
272
-
273
- if ( options . runId ) {
274
- //convert to friendlyId
275
- convertedOptions . runId = options . runId . map ( ( r ) => RunId . toFriendlyId ( r ) ) ;
276
- }
277
-
278
- // Show all runs if we are filtering by batchId or runId
279
- if ( options . batchId || options . runId ?. length || options . scheduleId || options . tasks ?. length ) {
280
- convertedOptions . rootOnly = false ;
281
- }
282
-
283
- return convertedOptions ;
284
- }
285
165
}
286
166
287
167
function applyRunFiltersToQueryBuilder < T > (
@@ -373,7 +253,3 @@ function applyRunFiltersToQueryBuilder<T>(
373
253
} ) ;
374
254
}
375
255
}
376
-
377
- export function parseRunListInputOptions ( data : any ) : RunListInputOptions {
378
- return RunListInputOptionsSchema . parse ( data ) ;
379
- }
0 commit comments