diff --git a/server/src/db/repositories/RiskRepository.ts b/server/src/db/repositories/RiskRepository.ts index b682adc5cef..7c0d768f0b4 100644 --- a/server/src/db/repositories/RiskRepository.ts +++ b/server/src/db/repositories/RiskRepository.ts @@ -31,13 +31,15 @@ export class RiskRepository { ); const savedScore = scoreRows[0]; - const savedSignals: RiskSignal[] = []; // 2. Insert Risk Signals // BOLT OPTIMIZATION: Use batched inserts with chunking to reduce database round-trips. // This reduces latency from O(N) to O(N/chunkSize), typically O(1) for normal signal counts. if (input.signals && input.signals.length > 0) { const chunkSize = 100; + // BOLT OPTIMIZATION: Hoist default timestamp creation to avoid redundant object allocations. + const defaultDetectedAt = new Date(); + for (let i = 0; i < input.signals.length; i += chunkSize) { const chunk = input.signals.slice(i, i + chunkSize); const values: any[] = []; @@ -53,7 +55,7 @@ export class RiskRepository { sig.weight, sig.contributionScore, sig.description || null, - sig.detectedAt || new Date(), + sig.detectedAt || defaultDetectedAt, ); placeholders.push( `($${paramIndex}, $${paramIndex + 1}, $${paramIndex + 2}, $${paramIndex + 3}, $${paramIndex + 4}, $${paramIndex + 5}, $${paramIndex + 6}, $${paramIndex + 7})`, @@ -64,13 +66,10 @@ export class RiskRepository { const sql = ` INSERT INTO risk_signals ( risk_score_id, type, source, value, weight, contribution_score, description, detected_at - ) VALUES ${placeholders.join(', ')} - RETURNING *`; + ) VALUES ${placeholders.join(', ')}`; - const sigRows = await tx.query(sql, values); - for (const row of sigRows) { - savedSignals.push(this.mapSignal(row)); - } + // BOLT OPTIMIZATION: Removed RETURNING * and mapping loop as signals are not part of RiskScore return value. + await tx.query(sql, values); } }