Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add pager deletion #623

Merged
merged 1 commit into from
Apr 14, 2021
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
15 changes: 15 additions & 0 deletions api/controller/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ func SearchTriggers(database moira.Database, searcher moira.Searcher, page int64
return &triggersList, nil
}

func DeleteTriggersPager(dataBase moira.Database, pagerID string) (dto.TriggersSearchResultDeleteResponse, *api.ErrorResponse) {
exists, err := dataBase.IsTriggersSearchResultsExist(pagerID)
if err != nil {
return dto.TriggersSearchResultDeleteResponse{}, api.ErrorInternalServer(err)
}
if !exists {
return dto.TriggersSearchResultDeleteResponse{}, api.ErrorNotFound(fmt.Sprintf("pager with id %s not found", pagerID))
}
err = dataBase.DeleteTriggersSearchResults(pagerID)
if err != nil {
return dto.TriggersSearchResultDeleteResponse{}, api.ErrorInternalServer(err)
}
return dto.TriggersSearchResultDeleteResponse{PagerID: pagerID}, nil
}

func triggerExists(dataBase moira.Database, triggerID string) (bool, error) {
_, err := dataBase.GetTrigger(triggerID)
if err == database.ErrNil {
Expand Down
42 changes: 42 additions & 0 deletions api/controller/triggers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -741,3 +742,44 @@ var triggerChecks = []moira.TriggerCheck{
}

var testHighlightsMap = map[string]string{"testField": "testHighlight"}

func TestDeleteTriggersPager(t *testing.T) {
Convey("DeleteTriggersPager", t, func() {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)
const pagerID = "pagerID"

Convey("Pager exists", func() {
dataBase.EXPECT().IsTriggersSearchResultsExist(pagerID).Return(true, nil)
dataBase.EXPECT().DeleteTriggersSearchResults(pagerID).Return(nil)
response, err := DeleteTriggersPager(dataBase, pagerID)
So(err, ShouldBeNil)
So(response, ShouldResemble, dto.TriggersSearchResultDeleteResponse{PagerID: pagerID})
})

Convey("Pager is not exist", func() {
dataBase.EXPECT().IsTriggersSearchResultsExist(pagerID).Return(false, nil)
response, err := DeleteTriggersPager(dataBase, pagerID)
So(err, ShouldResemble, api.ErrorNotFound("pager with id pagerID not found"))
So(response, ShouldResemble, dto.TriggersSearchResultDeleteResponse{})
})

Convey("Error while checking pager existence", func() {
errReturning := errors.New("example error")
dataBase.EXPECT().IsTriggersSearchResultsExist(pagerID).Return(false, errReturning)
response, err := DeleteTriggersPager(dataBase, pagerID)
So(err, ShouldResemble, api.ErrorInternalServer(errReturning))
So(response, ShouldResemble, dto.TriggersSearchResultDeleteResponse{})
})

Convey("Error while deleting pager", func() {
errReturning := errors.New("example error")
dataBase.EXPECT().IsTriggersSearchResultsExist(pagerID).Return(true, nil)
dataBase.EXPECT().DeleteTriggersSearchResults(pagerID).Return(errReturning)
response, err := DeleteTriggersPager(dataBase, pagerID)
So(err, ShouldResemble, api.ErrorInternalServer(errReturning))
So(response, ShouldResemble, dto.TriggersSearchResultDeleteResponse{})
})
})
}
8 changes: 8 additions & 0 deletions api/dto/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,11 @@ type TriggerDump struct {
Trigger moira.Trigger `json:"trigger"`
Metrics []PatternMetrics `json:"metrics"`
}

type TriggersSearchResultDeleteResponse struct {
PagerID string `json:"pager_id"`
}

func (TriggersSearchResultDeleteResponse) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}
16 changes: 16 additions & 0 deletions api/handler/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func triggers(metricSourceProvider *metricSource.SourceProvider, searcher moira.
router.Put("/check", triggerCheck)
router.Route("/{triggerId}", trigger)
router.With(middleware.Paginate(0, 10)).With(middleware.Pager(false, "")).Get("/search", searchTriggers)
router.With(middleware.Pager(false, "")).Delete("/search/pager", deletePager)
// ToDo: DEPRECATED method. Remove in Moira 2.6
router.With(middleware.Paginate(0, 10)).With(middleware.Pager(false, "")).Get("/page", searchTriggers)
}
Expand Down Expand Up @@ -144,6 +145,21 @@ func searchTriggers(writer http.ResponseWriter, request *http.Request) {
}
}

func deletePager(writer http.ResponseWriter, request *http.Request) {
pagerID := middleware.GetPagerID(request)

response, errorResponse := controller.DeleteTriggersPager(database, pagerID)
if errorResponse != nil {
render.Render(writer, request, errorResponse) //nolint
return
}

if err := render.Render(writer, request, response); err != nil {
render.Render(writer, request, api.ErrorRender(err)) //nolint
return
}
}

func getRequestTags(request *http.Request) []string {
var filterTags []string
i := 0
Expand Down
33 changes: 31 additions & 2 deletions database/redis/triggers_search_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func (connector *DbConnector) GetTriggersSearchResults(searchResultsID string, p

resultsID := triggersSearchResultsKey(searchResultsID)

c.Send("MULTI") //nolint
c.Send("MULTI") //nolint
c.Send("LRANGE", resultsID, from, to) //nolint
c.Send("LLEN", resultsID) //nolint
c.Send("LLEN", resultsID) //nolint
response, err := redis.Values(c.Do("EXEC"))
if err != nil {
return nil, 0, fmt.Errorf("failed to EXEC: %w", err)
Expand All @@ -66,6 +66,35 @@ func (connector *DbConnector) GetTriggersSearchResults(searchResultsID string, p
return reply.SearchResults(response[0], response[1], nil)
}

// IsTriggersSearchResultsExist is a function that checks if there exists pager for triggers search by it's ID
func (connector *DbConnector) IsTriggersSearchResultsExist(pagerID string) (bool, error) {
c := connector.pool.Get()
defer c.Close()

pagerIDKey := triggersSearchResultsKey(pagerID)
reply, err := c.Do("EXISTS", pagerIDKey)

result, err := redis.Bool(reply, err)
if err != nil {
return false, fmt.Errorf("failed to check if pager exists: %w", err)
}
return result, nil
}

// DeleteTriggersSearchResults is a function that checks if there exists pager for triggers search by it's ID
func (connector *DbConnector) DeleteTriggersSearchResults(pagerID string) error {
c := connector.pool.Get()
defer c.Close()

pagerIDKey := triggersSearchResultsKey(pagerID)
_, err := c.Do("DEL", pagerIDKey)

if err != nil {
return fmt.Errorf("failed to check if pager exists: %w", err)
}
return nil
}

func triggersSearchResultsKey(searchResultsID string) string {
return fmt.Sprintf("moira-triggersSearchResults:%s", searchResultsID)
}
15 changes: 15 additions & 0 deletions database/redis/triggers_search_results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ func TestTriggersSearchResultsStoring(t *testing.T) {
So(actual, ShouldHaveLength, 3)
So(actual, ShouldResemble, searchResults[3:6])
So(total, ShouldResemble, int64(8))

actualExists, err := dataBase.IsTriggersSearchResultsExist(searchResultsID)
So(err, ShouldBeNil)
So(actualExists, ShouldBeTrue)

actualExists, err = dataBase.IsTriggersSearchResultsExist("nonexistentPagerID")
So(err, ShouldBeNil)
So(actualExists, ShouldBeFalse)

err = dataBase.DeleteTriggersSearchResults(searchResultsID)
So(err, ShouldBeNil)

actualExists, err = dataBase.IsTriggersSearchResultsExist(searchResultsID)
So(err, ShouldBeNil)
So(actualExists, ShouldBeFalse)
})
}

Expand Down
4 changes: 3 additions & 1 deletion interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ type Database interface {
GetPatternTriggerIDs(pattern string) ([]string, error)
RemovePatternTriggerIDs(pattern string) error

// SearchResult storing
// SearchResult AKA pager storing
GetTriggersSearchResults(searchResultsID string, page, size int64) ([]*SearchResult, int64, error)
SaveTriggersSearchResults(searchResultsID string, searchResults []*SearchResult) error
IsTriggersSearchResultsExist(pagerID string) (bool, error)
DeleteTriggersSearchResults(pagerID string) error

// Throttling
GetTriggerThrottling(triggerID string) (time.Time, time.Time)
Expand Down
29 changes: 29 additions & 0 deletions mock/moira-alert/database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.