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:
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user