Skip to content

Commit

Permalink
refactor: change moira.TriggerCheck to dto.Trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko committed Dec 18, 2024
1 parent 3971aef commit d989a76
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 37 deletions.
15 changes: 9 additions & 6 deletions api/controller/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,23 @@ func GetTriggerNoisiness(
}
}

triggerChecks, err := getTriggerChecks(database, onlyTriggerIDs(triggerIDsWithEventsCount))
triggers, err := getTriggerChecks(database, onlyTriggerIDs(triggerIDsWithEventsCount))
if err != nil {
return nil, api.ErrorInternalServer(err)
}

if len(triggerChecks) != len(triggerIDsWithEventsCount) {
if len(triggers) != len(triggerIDsWithEventsCount) {
return nil, api.ErrorInternalServer(fmt.Errorf("failed to fetch triggers for such range"))
}

resDto.List = make([]dto.TriggerNoisiness, 0, len(triggerChecks))
for i := range triggerChecks {
resDto.List = make([]dto.TriggerNoisiness, 0, len(triggers))
for i := range triggers {
resDto.List = append(resDto.List, dto.TriggerNoisiness{
TriggerCheck: triggerChecks[i],
EventsCount: triggerIDsWithEventsCount[i].eventsCount,
Trigger: dto.Trigger{
TriggerModel: dto.CreateTriggerModel(&triggers[i].Trigger),
Throttling: triggers[i].Throttling,
},
EventsCount: triggerIDsWithEventsCount[i].eventsCount,
})
}

Expand Down
64 changes: 40 additions & 24 deletions api/controller/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,13 +1233,13 @@ func TestGetTriggerNoisiness(t *testing.T) {
triggerID2 = "second-trigger"
)

trigger1 := moira.TriggerCheck{
triggerCheck1 := moira.TriggerCheck{
Trigger: moira.Trigger{
ID: triggerID1,
},
}

trigger2 := moira.TriggerCheck{
triggerCheck2 := moira.TriggerCheck{
Trigger: moira.Trigger{
ID: triggerID2,
},
Expand All @@ -1250,19 +1250,23 @@ func TestGetTriggerNoisiness(t *testing.T) {
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(0))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(0))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID1, triggerID2}).
Return([]*moira.TriggerCheck{&trigger1, &trigger2}, nil)
Return([]*moira.TriggerCheck{&triggerCheck1, &triggerCheck2}, nil)

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, zeroPage, allEventsSize, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: trigger1,
EventsCount: 0,
Trigger: dto.Trigger{
TriggerModel: dto.CreateTriggerModel(&triggerCheck1.Trigger),
},
EventsCount: 0,
},
{
TriggerCheck: trigger2,
EventsCount: 0,
Trigger: dto.Trigger{
TriggerModel: dto.CreateTriggerModel(&triggerCheck2.Trigger),
},
EventsCount: 0,
},
},
Page: zeroPage,
Expand All @@ -1277,19 +1281,23 @@ func TestGetTriggerNoisiness(t *testing.T) {
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID1, triggerID2}).
Return([]*moira.TriggerCheck{&trigger1, &trigger2}, nil)
Return([]*moira.TriggerCheck{&triggerCheck1, &triggerCheck2}, nil)

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, zeroPage, allEventsSize, defaultFrom, defaultTo, api.AscSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: trigger1,
EventsCount: 1,
Trigger: dto.Trigger{
TriggerModel: dto.CreateTriggerModel(&triggerCheck1.Trigger),
},
EventsCount: 1,
},
{
TriggerCheck: trigger2,
EventsCount: 2,
Trigger: dto.Trigger{
TriggerModel: dto.CreateTriggerModel(&triggerCheck2.Trigger),
},
EventsCount: 2,
},
},
Page: zeroPage,
Expand All @@ -1303,19 +1311,23 @@ func TestGetTriggerNoisiness(t *testing.T) {
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID2, triggerID1}).
Return([]*moira.TriggerCheck{&trigger2, &trigger1}, nil)
Return([]*moira.TriggerCheck{&triggerCheck2, &triggerCheck1}, nil)

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, zeroPage, allEventsSize, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: trigger2,
EventsCount: 2,
Trigger: dto.Trigger{
TriggerModel: dto.CreateTriggerModel(&triggerCheck2.Trigger),
},
EventsCount: 2,
},
{
TriggerCheck: trigger1,
EventsCount: 1,
Trigger: dto.Trigger{
TriggerModel: dto.CreateTriggerModel(&triggerCheck1.Trigger),
},
EventsCount: 1,
},
},
Page: zeroPage,
Expand Down Expand Up @@ -1345,15 +1357,17 @@ func TestGetTriggerNoisiness(t *testing.T) {
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID2}).
Return([]*moira.TriggerCheck{&trigger2}, nil)
Return([]*moira.TriggerCheck{&triggerCheck2}, nil)

triggerNoisinessList, err = GetTriggerNoisiness(dataBase, zeroPage, 1, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: trigger2,
EventsCount: 2,
Trigger: dto.Trigger{
TriggerModel: dto.CreateTriggerModel(&triggerCheck2.Trigger),
},
EventsCount: 2,
},
},
Page: zeroPage,
Expand All @@ -1365,15 +1379,17 @@ func TestGetTriggerNoisiness(t *testing.T) {
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID1}).
Return([]*moira.TriggerCheck{&trigger1}, nil)
Return([]*moira.TriggerCheck{&triggerCheck1}, nil)

triggerNoisinessList, err = GetTriggerNoisiness(dataBase, 1, 1, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: trigger1,
EventsCount: 1,
Trigger: dto.Trigger{
TriggerModel: dto.CreateTriggerModel(&triggerCheck1.Trigger),
},
EventsCount: 1,
},
},
Page: 1,
Expand Down Expand Up @@ -1433,7 +1449,7 @@ func TestGetTriggerNoisiness(t *testing.T) {
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID2, triggerID1}).
Return([]*moira.TriggerCheck{&trigger2}, nil)
Return([]*moira.TriggerCheck{&triggerCheck2}, nil)

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, zeroPage, allEventsSize, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldResemble, api.ErrorInternalServer(fmt.Errorf("failed to fetch triggers for such range")))
Expand Down
2 changes: 1 addition & 1 deletion api/dto/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func (TriggersSearchResultDeleteResponse) Render(http.ResponseWriter, *http.Requ

// TriggerNoisiness represents TriggerCheck with amount of events for this trigger.
type TriggerNoisiness struct {
moira.TriggerCheck
Trigger
// EventsCount for the trigger.
EventsCount int64 `json:"events_count"`
}
Expand Down
14 changes: 8 additions & 6 deletions api/handler/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ func TestGetTriggerNoisiness(t *testing.T) {
return request
}

testTrigger := moira.TriggerCheck{
testTriggerCheck := moira.TriggerCheck{
Trigger: moira.Trigger{
ID: "triggerID",
},
Expand All @@ -924,9 +924,9 @@ func TestGetTriggerNoisiness(t *testing.T) {
Convey("with ok", func() {
responseWriter := httptest.NewRecorder()

mockDB.EXPECT().GetAllTriggerIDs().Return([]string{testTrigger.ID}, nil)
mockDB.EXPECT().GetNotificationEventCount(testTrigger.ID, from, to).Return(int64(1))
mockDB.EXPECT().GetTriggerChecks([]string{testTrigger.ID}).Return([]*moira.TriggerCheck{&testTrigger}, nil)
mockDB.EXPECT().GetAllTriggerIDs().Return([]string{testTriggerCheck.ID}, nil)
mockDB.EXPECT().GetNotificationEventCount(testTriggerCheck.ID, from, to).Return(int64(1))
mockDB.EXPECT().GetTriggerChecks([]string{testTriggerCheck.ID}).Return([]*moira.TriggerCheck{&testTriggerCheck}, nil)

getTriggerNoisiness(responseWriter, getRequestTriggerNoisiness(from, to))

Expand All @@ -944,8 +944,10 @@ func TestGetTriggerNoisiness(t *testing.T) {
So(&gotDTO, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: testTrigger,
EventsCount: 1,
Trigger: dto.Trigger{
TriggerModel: dto.CreateTriggerModel(&testTriggerCheck.Trigger),
},
EventsCount: 1,
},
},
Page: 0,
Expand Down

0 comments on commit d989a76

Please sign in to comment.