@@ -85,38 +85,27 @@ class ExecutionStep extends Base {
8585 }
8686
8787 static async getForEachExecutionSteps ( executionId : string ) {
88+ // TESTING
8889 return ExecutionStep . query ( )
90+ . select ( 'execution_steps.*' , 'latest_steps.min_created_at' )
8991 . with ( 'latest_steps' , ( builder ) => {
90- /**
91- * NOTE: there is a known issue with knex where 'groupBy' are placed at the end of the 'unionAll' query.
92- * the workaround is to unionAll both queries with 'true' to wrap the subequery.
93- */
9492 builder
95- . unionAll ( ( qb ) => {
96- qb . select (
97- 'step_id' ,
98- raw ( 'max(created_at) as max_created_at' ) ,
99- raw ( 'min(created_at) as min_created_at' ) ,
100- )
101- . from ( 'execution_steps' )
102- . groupBy ( 'step_id' )
103- . where ( 'execution_id' , '=' , executionId )
104- . where ( raw ( "metadata = '{}'::jsonb" ) )
105- . withSoftDeleted ( )
106- } , true )
107- . unionAll ( ( qb ) => {
108- qb . select (
109- 'step_id' ,
110- raw ( 'max(created_at) as max_created_at' ) ,
111- raw ( 'min(created_at) as min_created_at' ) ,
112- )
113- . from ( 'execution_steps' )
114- . groupBy ( 'step_id' , raw ( "metadata->>'iteration'" ) )
115- . where ( 'execution_id' , '=' , executionId )
116- . where ( raw ( "metadata != '{}'::jsonb" ) )
117- . withSoftDeleted ( )
118- } , true )
119- . withSoftDeleted ( )
93+ . select (
94+ 'step_id' ,
95+ raw ( 'MAX(created_at) as max_created_at' ) ,
96+ raw ( 'MIN(created_at) as min_created_at' ) ,
97+ )
98+ . from ( 'execution_steps' )
99+ . where ( 'execution_id' , executionId )
100+ . groupBy ( 'step_id' )
101+ . groupBy (
102+ raw ( `
103+ CASE
104+ WHEN metadata = '{}'::jsonb THEN NULL
105+ ELSE metadata ->> 'iteration'
106+ END
107+ ` ) ,
108+ )
120109 } )
121110 . join ( 'latest_steps' , ( builder ) => {
122111 builder
@@ -127,9 +116,53 @@ class ExecutionStep extends Base {
127116 'latest_steps.max_created_at' ,
128117 )
129118 } )
130- . select ( 'execution_steps.*' , 'min_created_at' )
131- . withSoftDeleted ( )
132- . orderBy ( 'min_created_at' , 'asc' )
119+ . where ( 'execution_steps.execution_id' , executionId )
120+ . orderBy ( 'latest_steps.min_created_at' , 'asc' )
121+ // return ExecutionStep.query()
122+ // .with('latest_steps', (builder) => {
123+ // /**
124+ // * NOTE: there is a known issue with knex where 'groupBy' are placed at the end of the 'unionAll' query.
125+ // * the workaround is to unionAll both queries with 'true' to wrap the subequery.
126+ // */
127+ // builder
128+ // .unionAll((qb) => {
129+ // qb.select(
130+ // 'step_id',
131+ // raw('max(created_at) as max_created_at'),
132+ // raw('min(created_at) as min_created_at'),
133+ // )
134+ // .from('execution_steps')
135+ // .groupBy('step_id')
136+ // .where('execution_id', '=', executionId)
137+ // .where(raw("metadata = '{}'::jsonb"))
138+ // .withSoftDeleted()
139+ // }, true)
140+ // .unionAll((qb) => {
141+ // qb.select(
142+ // 'step_id',
143+ // raw('max(created_at) as max_created_at'),
144+ // raw('min(created_at) as min_created_at'),
145+ // )
146+ // .from('execution_steps')
147+ // .groupBy('step_id', raw("metadata->>'iteration'"))
148+ // .where('execution_id', '=', executionId)
149+ // .where(raw("metadata != '{}'::jsonb"))
150+ // .withSoftDeleted()
151+ // }, true)
152+ // .withSoftDeleted()
153+ // })
154+ // .join('latest_steps', (builder) => {
155+ // builder
156+ // .on('execution_steps.step_id', '=', 'latest_steps.step_id')
157+ // .andOn(
158+ // 'execution_steps.created_at',
159+ // '=',
160+ // 'latest_steps.max_created_at',
161+ // )
162+ // })
163+ // .select('execution_steps.*', 'min_created_at')
164+ // .withSoftDeleted()
165+ // .orderBy('min_created_at', 'asc')
133166 }
134167
135168 static async getForEachExecutionState ( executionId : string ) {
0 commit comments