Repository navigation
[Fix] Preserve the incident interval when extracting components - #136
Conversation
ExtractComponentsToNewIncident started the new incident at the extraction time, so the downtime the moved components had already spent in the source incident was dropped from both. Closed-incident extraction additionally lost the recorded end date. The new incident now inherits the source start date and, when the source is closed, its end date, both taken from the row re-read inside the transaction. The moved from/to statuses keep the extraction timestamp: they are audit records, not the outage interval.
There was a problem hiding this comment.
Summary
The fix is sound. Previously ExtractComponentsToNewIncident (internal/db/db.go:971) created the new incident with StartDate = now and EndDate = nil, silently dropping the downtime the moved components had already accumulated. The new code moves the construction of inc inside the transaction and inherits StartDate (and EndDate for a closed source) from the re-read incOld row, while appendExtractionStatuses (db.go:1048) still stamps the "moved from/to" statuses with timeNow — matching the stated intent. Taking the interval from the in-transaction re-read (not the caller's copy) is the right choice, consistent with the existing fresh-read semantics tested by TestExtractComponentsUsesFreshIncidentState.
The two new DB tests (tests/extract_tx_test.go:181, :251) cover the open and closed source cases, check both in-memory and stored rows, and use microsecond truncation to match Postgres timestamp precision. Test helpers (incidentStatusWithText, incidentComponentIDs, newProbeComponent, tableCount) all exist.
Findings
Suggestion
- internal/db/db.go (~line 1004):
startDate := *incOld.StartDatedereferences unconditionally. If a row could ever exist with a NULLstart_date, this panics inside the transaction. If the ent schema guarantees non-null (which the existing ordering/filter code suggests), this is fine as-is — otherwise add a guard that returns an error whenincOld.StartDate == nil. - Closed-source extraction: the new incident is created closed (inherited
EndDatein the past) but withStatus: event.OutDatedSystem(i.e. "ongoing"), which was the pre-existing behavior for status. Now that the end date is inherited, a "ongoing" incident with a past end date is more visible. Consider whether extracting from a closed incident should instead reuse the source's status.
Note
- Verification: the PR description states the
./testsDB-backed suite could not be executed locally (needs Docker), so the new interval tests have not actually run yet. CI (go-test,go-test-acc,build) was still queued at review time — please make sure the acceptance suite passes on CI before merging.
Verdict
No correctness, security or data-loss issues found in the change. Approving; the two suggestions above are optional.
ExtractComponentsToNewIncidentcreated the new incident at the extraction time, so the downtime the moved components had already accumulated was dropped from both incidents, and a closed source also lost its end date.The new incident now inherits the source start date and, for a closed source, its end date, taken from the row re-read inside the transaction. The
moved from/tostatuses keep the extraction timestamp.Verified with
go test ./internal/... -count 1and thego-test-accjob, which runsgo test ./tests/... -count 1against Postgres and covers extraction from open and closed incidents.Closes #111