Skip to content

Commit 3517f36

Browse files
committed
refactor: use ExecutionStep model to get for-each steps
1 parent 589644f commit 3517f36

File tree

1 file changed

+5
-48
lines changed

1 file changed

+5
-48
lines changed

packages/backend/src/graphql/queries/get-execution-steps.ts

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
TOOLBOX_APP_KEY,
66
} from '@/apps/toolbox/common/constants'
77
import paginate from '@/helpers/pagination'
8+
import ExecutionStep from '@/models/execution-step'
89

910
import type { QueryResolvers } from '../__generated__/types.generated'
1011

@@ -55,60 +56,16 @@ const getExecutionSteps: QueryResolvers['getExecutionSteps'] = async (
5556
// NOTE: use a separate query for for-each
5657
// as there are multiple execution steps with the same step_id in the same execution
5758
if (hasForEach) {
58-
const forEachExecutionSteps = execution
59-
.$relatedQuery('executionSteps')
60-
.with('latest_steps', (builder) => {
61-
/**
62-
* NOTE: there is a known issue with knex where 'groupBy' are placed at the end of the 'unionAll' query.
63-
* the workaround is to unionAll both queries with 'true' to wrap the subequery.
64-
*/
65-
builder
66-
.unionAll((qb) => {
67-
qb.select(
68-
'step_id',
69-
raw('max(created_at) as max_created_at'),
70-
raw('min(created_at) as min_created_at'),
71-
)
72-
.from('execution_steps')
73-
.groupBy('step_id')
74-
.where('execution_id', '=', execution.id)
75-
.where(raw("metadata = '{}'::jsonb"))
76-
.withSoftDeleted()
77-
}, true)
78-
.unionAll((qb) => {
79-
qb.select(
80-
'step_id',
81-
raw('max(created_at) as max_created_at'),
82-
raw('min(created_at) as min_created_at'),
83-
)
84-
.from('execution_steps')
85-
.groupBy('step_id', raw("metadata->>'iteration'"))
86-
.where('execution_id', '=', execution.id)
87-
.where(raw("metadata != '{}'::jsonb"))
88-
.withSoftDeleted()
89-
}, true)
90-
.withSoftDeleted()
91-
})
92-
.join('latest_steps', (builder) => {
93-
builder
94-
.on('execution_steps.step_id', '=', 'latest_steps.step_id')
95-
.andOn(
96-
'execution_steps.created_at',
97-
'=',
98-
'latest_steps.max_created_at',
99-
)
100-
})
101-
.select('execution_steps.*', 'min_created_at')
102-
.withSoftDeleted()
103-
.orderBy('min_created_at', 'asc')
59+
const forEachExecutionSteps = await ExecutionStep.getForEachExecutionSteps(
60+
execution.id,
61+
)
10462

105-
const allRecords = await forEachExecutionSteps
10663
return {
10764
pageInfo: {
10865
currentPage: 1,
10966
totalCount: 100,
11067
},
111-
edges: allRecords.map((record) => ({
68+
edges: forEachExecutionSteps.map((record) => ({
11269
node: record,
11370
})),
11471
}

0 commit comments

Comments
 (0)