From 950e72be9b491d19e2ef74ad8035469301a18cfa Mon Sep 17 00:00:00 2001 From: Aloento <11802769+Aloento@users.noreply.github.com> Date: Tue, 1 Dec 2026 12:38:21 +0100 Subject: [PATCH] [Fix] Preserve the incident interval when extracting components 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. --- internal/db/db.go | 37 +++++---- tests/extract_tx_test.go | 161 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 13 deletions(-) diff --git a/internal/db/db.go b/internal/db/db.go index fb8262b..efd63b8 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -977,18 +977,7 @@ func (db *DB) ExtractComponentsToNewIncident( timeNow := time.Now().UTC() - inc := &Incident{ - Text: &text, - Description: description, - StartDate: &timeNow, - EndDate: nil, - Impact: &impact, - Statuses: []IncidentStatus{}, - Status: event.OutDatedSystem, - System: false, - Type: event.TypeIncident, - Components: comp, - } + var inc *Incident err := db.WithTx(ctx, func(tx *Tx) error { // The caller loaded the source incident before this transaction, so lock @@ -1007,7 +996,29 @@ func (db *DB) ExtractComponentsToNewIncident( return readErr } comp = freshComps - inc.Components = comp + + // Extraction splits the source incident, it does not start a new outage: + // the moved components keep the source interval. Only the moved from/to + // statuses below are stamped with the extraction time. + startDate := *incOld.StartDate + var endDate *time.Time + if incOld.EndDate != nil { + end := *incOld.EndDate + endDate = &end + } + + inc = &Incident{ + Text: &text, + Description: description, + StartDate: &startDate, + EndDate: endDate, + Impact: &impact, + Statuses: []IncidentStatus{}, + Status: event.OutDatedSystem, + System: false, + Type: event.TypeIncident, + Components: comp, + } if _, saveErr := db.SaveIncidentTx(ctx, tx, inc); saveErr != nil { return saveErr diff --git a/tests/extract_tx_test.go b/tests/extract_tx_test.go index 2a652db..3105ab6 100644 --- a/tests/extract_tx_test.go +++ b/tests/extract_tx_test.go @@ -2,6 +2,7 @@ package tests import ( "context" + "strings" "testing" "time" @@ -174,6 +175,166 @@ func TestExtractComponentsAbortsOnSoftDeletedComponent(t *testing.T) { "incident_id = $1 AND component_id = $2", oldID, comp.ID)) } +// TestExtractComponentsKeepsOpenIncidentInterval extracts a component from an +// open incident and checks that the new incident keeps the source interval: the +// downtime between the original start and the extraction must not be dropped. +func TestExtractComponentsKeepsOpenIncidentInterval(t *testing.T) { + ctx := context.Background() + d, _ := newNotifDB(t) + + compA := newProbeComponent("extract probe "+uuid.NewString(), "EU-DE") + compB := newProbeComponent("extract probe "+uuid.NewString(), "EU-DE") + for _, c := range []*db.Component{compA, compB} { + _, err := d.SaveComponent(ctx, c) + require.NoError(t, err) + } + + start := time.Now().UTC().Add(-2 * time.Hour).Truncate(time.Microsecond) + impact := 2 + oldText := "source incident " + uuid.NewString() + + oldID, err := d.SaveIncident(ctx, &db.Incident{ + Text: &oldText, + StartDate: &start, + Impact: &impact, + Status: event.OutDatedSystem, + Type: event.TypeIncident, + Components: []db.Component{*compA, *compB}, + }) + require.NoError(t, err) + + incOld, err := d.GetIncident(ctx, int(oldID)) + require.NoError(t, err) + require.Len(t, incOld.Components, 2) + + extractedAt := time.Now().UTC() + newText := "extracted incident " + uuid.NewString() + description := "open interval probe" + inc, err := d.ExtractComponentsToNewIncident( + ctx, []db.Component{*compA}, incOld, impact, newText, &description, + ) + require.NoError(t, err) + require.NotNil(t, inc.StartDate) + assert.Equal(t, start, inc.StartDate.Truncate(time.Microsecond), + "the extracted incident must inherit the source start") + assert.Nil(t, inc.EndDate, + "extraction from an open incident must leave the extracted incident open") + + newStored, err := d.GetIncident(ctx, int(inc.ID)) + require.NoError(t, err) + require.NotNil(t, newStored.StartDate) + assert.Equal(t, start, newStored.StartDate.Truncate(time.Microsecond)) + assert.Nil(t, newStored.EndDate) + assert.ElementsMatch(t, []uint{compA.ID}, incidentComponentIDs(newStored)) + + oldStored, err := d.GetIncident(ctx, int(oldID)) + require.NoError(t, err) + require.NotNil(t, oldStored.StartDate) + assert.Equal(t, start, oldStored.StartDate.Truncate(time.Microsecond)) + assert.ElementsMatch(t, []uint{compB.ID}, incidentComponentIDs(oldStored), + "the extracted component must be removed from the source incident") + + movedFrom := incidentStatusWithText(t, newStored, "moved from") + assert.WithinDuration(t, extractedAt, movedFrom.Timestamp, time.Minute, + "the moved from status must stay at the extraction time") + assert.NotEqual(t, start, movedFrom.Timestamp.UTC().Truncate(time.Microsecond)) + + movedTo := incidentStatusWithText(t, oldStored, "moved to") + assert.WithinDuration(t, extractedAt, movedTo.Timestamp, time.Minute, + "the moved to status must stay at the extraction time") + assert.NotEqual(t, start, movedTo.Timestamp.UTC().Truncate(time.Microsecond)) +} + +// TestExtractComponentsKeepsClosedIncidentInterval extracts from a closed +// incident and checks that the new incident covers the same closed interval, so +// the moved components keep the recorded downtime. +func TestExtractComponentsKeepsClosedIncidentInterval(t *testing.T) { + ctx := context.Background() + d, _ := newNotifDB(t) + + compA := newProbeComponent("extract probe "+uuid.NewString(), "EU-FR") + compB := newProbeComponent("extract probe "+uuid.NewString(), "EU-FR") + for _, c := range []*db.Component{compA, compB} { + _, err := d.SaveComponent(ctx, c) + require.NoError(t, err) + } + + start := time.Now().UTC().Add(-3 * time.Hour).Truncate(time.Microsecond) + end := start.Add(45 * time.Minute) + impact := 2 + oldText := "source incident " + uuid.NewString() + + oldID, err := d.SaveIncident(ctx, &db.Incident{ + Text: &oldText, + StartDate: &start, + EndDate: &end, + Impact: &impact, + Status: event.OutDatedSystem, + Type: event.TypeIncident, + Components: []db.Component{*compA, *compB}, + }) + require.NoError(t, err) + + incOld, err := d.GetIncident(ctx, int(oldID)) + require.NoError(t, err) + require.NotNil(t, incOld.EndDate) + + extractedAt := time.Now().UTC() + newText := "extracted incident " + uuid.NewString() + description := "closed interval probe" + inc, err := d.ExtractComponentsToNewIncident( + ctx, []db.Component{*compA}, incOld, impact, newText, &description, + ) + require.NoError(t, err) + require.NotNil(t, inc.StartDate) + require.NotNil(t, inc.EndDate) + assert.Equal(t, start, inc.StartDate.Truncate(time.Microsecond), + "the extracted incident must inherit the source start") + assert.Equal(t, end, inc.EndDate.Truncate(time.Microsecond), + "the extracted incident must inherit the source end") + assert.Equal(t, event.OutDatedSystem, inc.Status, + "closed extraction keeps the stored SYSTEM status; the API normalizes it") + + newStored, err := d.GetIncident(ctx, int(inc.ID)) + require.NoError(t, err) + require.NotNil(t, newStored.StartDate) + require.NotNil(t, newStored.EndDate) + assert.Equal(t, start, newStored.StartDate.Truncate(time.Microsecond)) + assert.Equal(t, end, newStored.EndDate.Truncate(time.Microsecond)) + assert.ElementsMatch(t, []uint{compA.ID}, incidentComponentIDs(newStored)) + + oldStored, err := d.GetIncident(ctx, int(oldID)) + require.NoError(t, err) + require.NotNil(t, oldStored.EndDate) + assert.Equal(t, end, oldStored.EndDate.Truncate(time.Microsecond), + "the source incident must keep its end date") + assert.ElementsMatch(t, []uint{compB.ID}, incidentComponentIDs(oldStored), + "the extracted component must be removed from the source incident") + + movedFrom := incidentStatusWithText(t, newStored, "moved from") + assert.WithinDuration(t, extractedAt, movedFrom.Timestamp, time.Minute, + "the moved from status must stay at the extraction time") + assert.NotEqual(t, start, movedFrom.Timestamp.UTC().Truncate(time.Microsecond)) + + movedTo := incidentStatusWithText(t, oldStored, "moved to") + assert.WithinDuration(t, extractedAt, movedTo.Timestamp, time.Minute, + "the moved to status must stay at the extraction time") + assert.NotEqual(t, start, movedTo.Timestamp.UTC().Truncate(time.Microsecond)) +} + +func incidentStatusWithText(t *testing.T, inc *db.Incident, substr string) db.IncidentStatus { + t.Helper() + + for _, status := range inc.Statuses { + if strings.Contains(status.Text, substr) { + return status + } + } + t.Fatalf("no status containing %q on incident %d", substr, inc.ID) + + return db.IncidentStatus{} +} + func incidentComponentIDs(inc *db.Incident) []uint { ids := make([]uint, len(inc.Components)) for i := range inc.Components {