Skip to content

fix(database): dedupe incident_issues pairs in Incident::stats_for#152

Merged
passcod merged 1 commit into
mainfrom
pr/incident-stats-dedupe
May 20, 2026
Merged

fix(database): dedupe incident_issues pairs in Incident::stats_for#152
passcod merged 1 commit into
mainfrom
pr/incident-stats-dedupe

Conversation

@passcod
Copy link
Copy Markdown
Member

@passcod passcod commented May 20, 2026

Summary

incident_issues is keyed on (incident_id, issue_id, joined_at), so an issue that leaves and rejoins the same incident produces multiple rows for the same pair. Incident::stats_for was counting those rows directly:

  • issue_count = count_star() over incident_issues → counted rejoin rows, not distinct issues.
  • event_count joined events to incident_issues on issue_id and count_star()'d, so every event row was multiplied by the number of rejoin rows for its (incident, issue) pair.
  • note_count's issue_notes half had the same multiplier.

On a real incident this surfaced as 9021 issues and 59M events when sum(occurrences) from events was 48288 — i.e. wildly impossible numbers.

The fix pulls distinct (incident_id, issue_id) pairs first, then counts events / issue_notes per issue and sums back per incident. incident_notes is unaffected and still runs concurrently with the other queries.

incident_issues is keyed on (incident_id, issue_id, joined_at), so an
issue that leaves and rejoins the same incident produces multiple rows
for the same pair. The previous count_star() queries multiplied the
issue count by the rejoin count and multiplied events/issue_notes by
the rejoin factor on top.

Pull distinct (incident_id, issue_id) pairs first, then count events
and notes per issue, summing back per incident.
@passcod passcod added this pull request to the merge queue May 20, 2026
Merged via the queue into main with commit 87cc014 May 20, 2026
3 checks passed
@passcod passcod deleted the pr/incident-stats-dedupe branch May 20, 2026 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant