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" ;
47import { clickhouseClient } from "~/services/clickhouseInstance.server" ;
58import {
9+ AverageDurations ,
610 ClickHouseEnvironmentMetricsRepository ,
11+ CurrentRunningStats ,
712 DailyTaskActivity ,
813 EnvironmentMetricsRepository ,
914 PostgrestEnvironmentMetricsRepository ,
1015} from "~/services/environmentMetricsRepository.server" ;
1116import { singleton } from "~/utils/singleton" ;
17+ import { findCurrentWorkerFromEnvironment } from "~/v3/models/workerDeployment.server" ;
1218
1319export 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
0 commit comments