Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions server/src/db/repositories/RiskRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand All @@ -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})`,
Expand All @@ -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);
}
}

Expand Down
Loading