From d989a760e3ae68c9882e7f9f75a5b581f666065d Mon Sep 17 00:00:00 2001 From: AleksandrMatsko Date: Wed, 18 Dec 2024 12:31:12 +0700 Subject: [PATCH] refactor: change moira.TriggerCheck to dto.Trigger --- api/controller/triggers.go | 15 ++++---- api/controller/triggers_test.go | 64 ++++++++++++++++++++------------- api/dto/triggers.go | 2 +- api/handler/triggers_test.go | 14 ++++---- 4 files changed, 58 insertions(+), 37 deletions(-) diff --git a/api/controller/triggers.go b/api/controller/triggers.go index cc149e1d1..43f4dc25a 100644 --- a/api/controller/triggers.go +++ b/api/controller/triggers.go @@ -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, }) } diff --git a/api/controller/triggers_test.go b/api/controller/triggers_test.go index a2555ccaa..a6de1e579 100644 --- a/api/controller/triggers_test.go +++ b/api/controller/triggers_test.go @@ -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, }, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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"))) diff --git a/api/dto/triggers.go b/api/dto/triggers.go index 7596f701d..d0429b5be 100644 --- a/api/dto/triggers.go +++ b/api/dto/triggers.go @@ -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"` } diff --git a/api/handler/triggers_test.go b/api/handler/triggers_test.go index aaceff20a..c65101d77 100644 --- a/api/handler/triggers_test.go +++ b/api/handler/triggers_test.go @@ -909,7 +909,7 @@ func TestGetTriggerNoisiness(t *testing.T) { return request } - testTrigger := moira.TriggerCheck{ + testTriggerCheck := moira.TriggerCheck{ Trigger: moira.Trigger{ ID: "triggerID", }, @@ -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)) @@ -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,