@@ -6,51 +6,22 @@ import {
66 workflowDeploymentVersion ,
77 workflowExecutionLogs ,
88} from '@sim/db/schema'
9- import {
10- and ,
11- desc ,
12- eq ,
13- gt ,
14- gte ,
15- inArray ,
16- isNotNull ,
17- isNull ,
18- lt ,
19- lte ,
20- ne ,
21- or ,
22- type SQL ,
23- sql ,
24- } from 'drizzle-orm'
9+ import { and , desc , eq , isNotNull , isNull , or , type SQL , sql } from 'drizzle-orm'
2510import { type NextRequest , NextResponse } from 'next/server'
2611import { z } from 'zod'
2712import { getSession } from '@/lib/auth'
2813import { generateRequestId } from '@/lib/core/utils/request'
2914import { createLogger } from '@/lib/logs/console/logger'
15+ import { buildFilterConditions , LogFilterParamsSchema } from '@/lib/logs/filters'
3016
3117const logger = createLogger ( 'LogsAPI' )
3218
3319export const revalidate = 0
3420
35- const QueryParamsSchema = z . object ( {
21+ const QueryParamsSchema = LogFilterParamsSchema . extend ( {
3622 details : z . enum ( [ 'basic' , 'full' ] ) . optional ( ) . default ( 'basic' ) ,
3723 limit : z . coerce . number ( ) . optional ( ) . default ( 100 ) ,
3824 offset : z . coerce . number ( ) . optional ( ) . default ( 0 ) ,
39- level : z . string ( ) . optional ( ) ,
40- workflowIds : z . string ( ) . optional ( ) ,
41- folderIds : z . string ( ) . optional ( ) ,
42- triggers : z . string ( ) . optional ( ) ,
43- startDate : z . string ( ) . optional ( ) ,
44- endDate : z . string ( ) . optional ( ) ,
45- search : z . string ( ) . optional ( ) ,
46- workflowName : z . string ( ) . optional ( ) ,
47- folderName : z . string ( ) . optional ( ) ,
48- executionId : z . string ( ) . optional ( ) ,
49- costOperator : z . enum ( [ '=' , '>' , '<' , '>=' , '<=' , '!=' ] ) . optional ( ) ,
50- costValue : z . coerce . number ( ) . optional ( ) ,
51- durationOperator : z . enum ( [ '=' , '>' , '<' , '>=' , '<=' , '!=' ] ) . optional ( ) ,
52- durationValue : z . coerce . number ( ) . optional ( ) ,
53- workspaceId : z . string ( ) ,
5425} )
5526
5627export async function GET ( request : NextRequest ) {
@@ -197,102 +168,11 @@ export async function GET(request: NextRequest) {
197168 }
198169 }
199170
200- if ( params . workflowIds ) {
201- const workflowIds = params . workflowIds . split ( ',' ) . filter ( Boolean )
202- if ( workflowIds . length > 0 ) {
203- conditions = and ( conditions , inArray ( workflow . id , workflowIds ) )
204- }
205- }
206-
207- if ( params . folderIds ) {
208- const folderIds = params . folderIds . split ( ',' ) . filter ( Boolean )
209- if ( folderIds . length > 0 ) {
210- conditions = and ( conditions , inArray ( workflow . folderId , folderIds ) )
211- }
212- }
213-
214- if ( params . triggers ) {
215- const triggers = params . triggers . split ( ',' ) . filter ( Boolean )
216- if ( triggers . length > 0 && ! triggers . includes ( 'all' ) ) {
217- conditions = and ( conditions , inArray ( workflowExecutionLogs . trigger , triggers ) )
218- }
219- }
220-
221- if ( params . startDate ) {
222- conditions = and (
223- conditions ,
224- gte ( workflowExecutionLogs . startedAt , new Date ( params . startDate ) )
225- )
226- }
227- if ( params . endDate ) {
228- conditions = and ( conditions , lte ( workflowExecutionLogs . startedAt , new Date ( params . endDate ) ) )
229- }
230-
231- if ( params . search ) {
232- const searchTerm = `%${ params . search } %`
233- conditions = and ( conditions , sql `${ workflowExecutionLogs . executionId } ILIKE ${ searchTerm } ` )
234- }
235-
236- if ( params . workflowName ) {
237- const nameTerm = `%${ params . workflowName } %`
238- conditions = and ( conditions , sql `${ workflow . name } ILIKE ${ nameTerm } ` )
239- }
240-
241- if ( params . folderName ) {
242- const folderTerm = `%${ params . folderName } %`
243- conditions = and ( conditions , sql `${ workflow . name } ILIKE ${ folderTerm } ` )
244- }
245-
246- if ( params . executionId ) {
247- conditions = and ( conditions , eq ( workflowExecutionLogs . executionId , params . executionId ) )
248- }
249-
250- if ( params . costOperator && params . costValue !== undefined ) {
251- const costField = sql `(${ workflowExecutionLogs . cost } ->>'total')::numeric`
252- switch ( params . costOperator ) {
253- case '=' :
254- conditions = and ( conditions , sql `${ costField } = ${ params . costValue } ` )
255- break
256- case '>' :
257- conditions = and ( conditions , sql `${ costField } > ${ params . costValue } ` )
258- break
259- case '<' :
260- conditions = and ( conditions , sql `${ costField } < ${ params . costValue } ` )
261- break
262- case '>=' :
263- conditions = and ( conditions , sql `${ costField } >= ${ params . costValue } ` )
264- break
265- case '<=' :
266- conditions = and ( conditions , sql `${ costField } <= ${ params . costValue } ` )
267- break
268- case '!=' :
269- conditions = and ( conditions , sql `${ costField } != ${ params . costValue } ` )
270- break
271- }
272- }
273-
274- if ( params . durationOperator && params . durationValue !== undefined ) {
275- const durationField = workflowExecutionLogs . totalDurationMs
276- switch ( params . durationOperator ) {
277- case '=' :
278- conditions = and ( conditions , eq ( durationField , params . durationValue ) )
279- break
280- case '>' :
281- conditions = and ( conditions , gt ( durationField , params . durationValue ) )
282- break
283- case '<' :
284- conditions = and ( conditions , lt ( durationField , params . durationValue ) )
285- break
286- case '>=' :
287- conditions = and ( conditions , gte ( durationField , params . durationValue ) )
288- break
289- case '<=' :
290- conditions = and ( conditions , lte ( durationField , params . durationValue ) )
291- break
292- case '!=' :
293- conditions = and ( conditions , ne ( durationField , params . durationValue ) )
294- break
295- }
171+ // Apply common filters (workflowIds, folderIds, triggers, dates, search, cost, duration)
172+ // Level filtering is handled above with advanced running/pending state logic
173+ const commonFilters = buildFilterConditions ( params , { useSimpleLevelFilter : false } )
174+ if ( commonFilters ) {
175+ conditions = and ( conditions , commonFilters )
296176 }
297177
298178 const logs = await baseQuery
0 commit comments