Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions internal/api/v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,15 @@ func shouldSkipIncident(inc *db.Incident) bool {
return inc.Type == event.TypeInformation || inc.Type == event.TypeMaintenance
}

// incidentStore is the subset of the database facade needed to move a
// component between incidents; tests replace it with a stub.
type incidentStore interface {
GetIncidentComponents(ctx context.Context, incidentID int) ([]db.Component, error)
MoveComponentFromOldToAnotherIncident(
ctx context.Context, comp *db.Component, incOld, incNew *db.Incident, closeOld bool,
) (*db.Incident, error)
}

// tryMoveComponentIfFound attempts to move a component if it's found in the given incident.
func tryMoveComponentIfFound(
ctx context.Context,
Expand All @@ -883,13 +892,33 @@ func tryMoveComponentIfFound(
inc *db.Incident,
incIn *db.Incident,
compResult *ProcessComponentResp,
) (bool, error) {
return moveComponentIfFound(ctx, dbInst, log, comp, inc, incIn, compResult)
}

func moveComponentIfFound(
ctx context.Context,
store incidentStore,
log *zap.Logger,
comp *db.Component,
inc *db.Incident,
incIn *db.Incident,
compResult *ProcessComponentResp,
) (bool, error) {
for _, incComp := range inc.Components {
if comp.ID == incComp.ID {
log.Info("found the component in the opened incident", zap.Any("component", comp), zap.Any("incident", inc))

closeInc := len(inc.Components) == 1
incident, err := dbInst.MoveComponentFromOldToAnotherIncident(ctx, comp, inc, incIn, closeInc)
// inc.Components is the snapshot fetched before this request created
// its incident; reload so the close decision reflects the current
// component list.
components, err := store.GetIncidentComponents(ctx, int(inc.ID))
if err != nil {
return false, err
}

closeInc := len(components) == 1
incident, err := store.MoveComponentFromOldToAnotherIncident(ctx, comp, inc, incIn, closeInc)
if err != nil {
return false, err
}
Expand Down
105 changes: 105 additions & 0 deletions internal/api/v2/v2_component_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package v2

import (
"context"
"encoding/json"
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/stackmon/otc-status-dashboard/internal/db"
)
Expand Down Expand Up @@ -47,3 +50,105 @@ func TestToAPIComponentMapsAttributes(t *testing.T) {
{Name: "type", Value: "cce"},
}, comp.Attributes)
}

type stubIncidentStore struct {
components []db.Component
getErr error
moveErr error
closeOld bool
moves int
}

func (s *stubIncidentStore) GetIncidentComponents(context.Context, int) ([]db.Component, error) {
if s.getErr != nil {
return nil, s.getErr
}
return s.components, nil
}

func (s *stubIncidentStore) MoveComponentFromOldToAnotherIncident(
_ context.Context, _ *db.Component, _, incNew *db.Incident, closeOld bool,
) (*db.Incident, error) {
s.moves++
s.closeOld = closeOld
if s.moveErr != nil {
return nil, s.moveErr
}
return incNew, nil
}

// The close decision must come from the component list re-read before the move,
// not from the opened-incident snapshot this request started with.
func TestMoveComponentIfFoundReloadsComponents(t *testing.T) {
t.Parallel()

tests := []struct {
name string
snapshot []db.Component
fresh []db.Component
wantClose bool
}{
{
name: "stale single component now has a peer",
snapshot: []db.Component{{ID: 7}},
fresh: []db.Component{{ID: 7}, {ID: 8}},
wantClose: false,
},
{
name: "component is the last one",
snapshot: []db.Component{{ID: 7}, {ID: 8}},
fresh: []db.Component{{ID: 7}},
wantClose: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

store := &stubIncidentStore{components: tc.fresh}
inc := &db.Incident{ID: 10, Components: tc.snapshot}
result := &ProcessComponentResp{ComponentID: 7}

moved, err := moveComponentIfFound(
context.Background(), store, zap.NewNop(), &db.Component{ID: 7}, inc, &db.Incident{ID: 20}, result,
)
require.NoError(t, err)
assert.True(t, moved)
require.Equal(t, 1, store.moves)
assert.Equal(t, tc.wantClose, store.closeOld)
assert.Equal(t, 20, result.IncidentID)
})
}
}

func TestMoveComponentIfFoundSkipsUnknownComponent(t *testing.T) {
t.Parallel()

store := &stubIncidentStore{components: []db.Component{{ID: 7}}}
inc := &db.Incident{ID: 10, Components: []db.Component{{ID: 7}}}

moved, err := moveComponentIfFound(
context.Background(), store, zap.NewNop(), &db.Component{ID: 8}, inc, &db.Incident{ID: 20},
&ProcessComponentResp{ComponentID: 8},
)
require.NoError(t, err)
assert.False(t, moved)
assert.Zero(t, store.moves)
}

func TestMoveComponentIfFoundStopsOnReloadError(t *testing.T) {
t.Parallel()

reloadErr := errors.New("reload failed")
store := &stubIncidentStore{getErr: reloadErr}
inc := &db.Incident{ID: 10, Components: []db.Component{{ID: 7}}}

moved, err := moveComponentIfFound(
context.Background(), store, zap.NewNop(), &db.Component{ID: 7}, inc, &db.Incident{ID: 20},
&ProcessComponentResp{ComponentID: 7},
)
require.ErrorIs(t, err, reloadErr)
assert.False(t, moved)
assert.Zero(t, store.moves)
}
23 changes: 23 additions & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,29 @@ func (db *DB) GetComponent(ctx context.Context, id int) (*Component, error) {
return &comp, nil
}

// GetIncidentComponents returns the components currently linked to the incident.
func (db *DB) GetIncidentComponents(ctx context.Context, incidentID int) ([]Component, error) {
e, err := db.e.Incident.Query().
Where(incident.IDEQ(incidentID)).
WithComponents(func(q *ent.ComponentQuery) {
q.Select(component.FieldID, component.FieldName)
}).
First(ctx)
if err != nil {
if ent.IsNotFound(err) {
return nil, ErrDBIncidentDSNotExist
}
return nil, err
}

components := make([]Component, 0, len(e.Edges.Components))
for _, c := range e.Edges.Components {
components = append(components, componentFromEnt(c))
}

return components, nil
}

func (db *DB) GetComponentsAsMap(ctx context.Context) (map[int]*Component, error) {
rows, err := db.e.Component.Query().All(ctx)
if err != nil {
Expand Down