perf: N+1 Fix in GetRiskSummary — 231 Queries auf 1 reduziert

risk-summary Endpoint von ~7s auf <0.5s.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-05-08 00:48:21 +02:00
parent 1b5c6bd340
commit c4532049d8
@@ -367,12 +367,19 @@ func (s *Store) GetRiskSummary(ctx context.Context, projectID uuid.UUID) (*RiskS
return summary, nil return summary, nil
} }
// Batch: get all latest assessments in one query
assessmentMap, err := s.GetLatestAssessmentsByProject(ctx, projectID)
if err != nil {
return nil, fmt.Errorf("get risk summary - batch assessments: %w", err)
}
highestRisk := RiskLevelNegligible highestRisk := RiskLevelNegligible
for _, h := range hazards { for _, h := range hazards {
latest, err := s.GetLatestAssessment(ctx, h.ID) ra, ok := assessmentMap[h.ID]
if err != nil { var latest *RiskAssessment
return nil, fmt.Errorf("get risk summary - get assessment for hazard %s: %w", h.ID, err) if ok {
latest = &ra
} }
if latest == nil { if latest == nil {
// Hazard without assessment counts as unassessed; consider it not acceptable // Hazard without assessment counts as unassessed; consider it not acceptable