( workflowId: string, currentDurationMs: number, spikePercent: number, windowHours: number )
| 75 | } |
| 76 | |
| 77 | async function checkLatencySpike( |
| 78 | workflowId: string, |
| 79 | currentDurationMs: number, |
| 80 | spikePercent: number, |
| 81 | windowHours: number |
| 82 | ): Promise<boolean> { |
| 83 | const windowStart = new Date(Date.now() - windowHours * 60 * 60 * 1000) |
| 84 | |
| 85 | const result = await db |
| 86 | .select({ |
| 87 | avgDuration: avg(workflowExecutionLogs.totalDurationMs), |
| 88 | count: count(), |
| 89 | }) |
| 90 | .from(workflowExecutionLogs) |
| 91 | .where( |
| 92 | and( |
| 93 | eq(workflowExecutionLogs.workflowId, workflowId), |
| 94 | gte(workflowExecutionLogs.startedAt, windowStart), |
| 95 | excludeSimExecutionsCondition() |
| 96 | ) |
| 97 | ) |
| 98 | |
| 99 | const avgDuration = result[0]?.avgDuration |
| 100 | const execCount = result[0]?.count || 0 |
| 101 | |
| 102 | if (!avgDuration || execCount < SIM_MIN_EXECUTIONS_FOR_RATE_RULES) return false |
| 103 | |
| 104 | const avgMs = Number(avgDuration) |
| 105 | const threshold = avgMs * (1 + spikePercent / 100) |
| 106 | |
| 107 | return currentDurationMs > threshold |
| 108 | } |
| 109 | |
| 110 | async function checkErrorCount( |
| 111 | workflowId: string, |
no test coverage detected