| 23 | } |
| 24 | |
| 25 | export function buildLogFilters(filters: LogFilters): SQL<unknown> { |
| 26 | const conditions: SQL<unknown>[] = [] |
| 27 | |
| 28 | conditions.push(eq(workflowExecutionLogs.workspaceId, filters.workspaceId)) |
| 29 | |
| 30 | // Cursor-based pagination |
| 31 | if (filters.cursor) { |
| 32 | const cursorDate = new Date(filters.cursor.startedAt) |
| 33 | if (filters.order === 'desc') { |
| 34 | conditions.push( |
| 35 | sql`(${workflowExecutionLogs.startedAt}, ${workflowExecutionLogs.id}) < (${cursorDate}, ${filters.cursor.id})` |
| 36 | ) |
| 37 | } else { |
| 38 | conditions.push( |
| 39 | sql`(${workflowExecutionLogs.startedAt}, ${workflowExecutionLogs.id}) > (${cursorDate}, ${filters.cursor.id})` |
| 40 | ) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Workflow IDs filter |
| 45 | if (filters.workflowIds && filters.workflowIds.length > 0) { |
| 46 | conditions.push(inArray(workflow.id, filters.workflowIds)) |
| 47 | } |
| 48 | |
| 49 | // Folder IDs filter |
| 50 | if (filters.folderIds && filters.folderIds.length > 0) { |
| 51 | conditions.push(inArray(workflow.folderId, filters.folderIds)) |
| 52 | } |
| 53 | |
| 54 | // Triggers filter |
| 55 | if (filters.triggers && filters.triggers.length > 0 && !filters.triggers.includes('all')) { |
| 56 | conditions.push(inArray(workflowExecutionLogs.trigger, filters.triggers)) |
| 57 | } |
| 58 | |
| 59 | // Level filter |
| 60 | if (filters.level) { |
| 61 | conditions.push(eq(workflowExecutionLogs.level, filters.level)) |
| 62 | } |
| 63 | |
| 64 | // Date range filters |
| 65 | if (filters.startDate) { |
| 66 | conditions.push(gte(workflowExecutionLogs.startedAt, filters.startDate)) |
| 67 | } |
| 68 | |
| 69 | if (filters.endDate) { |
| 70 | conditions.push(lte(workflowExecutionLogs.startedAt, filters.endDate)) |
| 71 | } |
| 72 | |
| 73 | // Search filter (execution ID) |
| 74 | if (filters.executionId) { |
| 75 | conditions.push(eq(workflowExecutionLogs.executionId, filters.executionId)) |
| 76 | } |
| 77 | |
| 78 | // Duration filters |
| 79 | if (filters.minDurationMs !== undefined) { |
| 80 | conditions.push(gte(workflowExecutionLogs.totalDurationMs, filters.minDurationMs)) |
| 81 | } |
| 82 | |