Skip to content

Commit

Permalink
Merge aee2f49 into 563be5a
Browse files Browse the repository at this point in the history
  • Loading branch information
titusjaka committed Mar 21, 2018
2 parents 563be5a + aee2f49 commit 6aae53c
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 6 deletions.
8 changes: 8 additions & 0 deletions api/controller/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ func GetTriggerEvents(database moira.Database, triggerID string, page int64, siz
}
return eventsList, nil
}

// DeleteAllEvents deletes all notification events
func DeleteAllEvents(database moira.Database) *api.ErrorResponse {
if err := database.RemoveAllNotificationEvents(); err != nil {
return api.ErrorInternalServer(err)
}
return nil
}
19 changes: 19 additions & 0 deletions api/controller/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,22 @@ func TestGetEvents(t *testing.T) {
So(list, ShouldBeNil)
})
}

func TestDeleteAllNotificationEvents(t *testing.T) {
mockCtrl := gomock.NewController(t)
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)
defer mockCtrl.Finish()

Convey("Success", t, func() {
dataBase.EXPECT().RemoveAllNotificationEvents().Return(nil)
err := DeleteAllEvents(dataBase)
So(err, ShouldBeNil)
})

Convey("Error delete", t, func() {
expected := fmt.Errorf("oooops! Can not get notifications")
dataBase.EXPECT().RemoveAllNotificationEvents().Return(expected)
err := DeleteAllEvents(dataBase)
So(err, ShouldResemble, api.ErrorInternalServer(expected))
})
}
8 changes: 8 additions & 0 deletions api/controller/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ func DeleteNotification(database moira.Database, notificationKey string) (*dto.N
}
return &dto.NotificationDeleteResponse{Result: result}, nil
}

// DeleteAllNotifications removes all notifications
func DeleteAllNotifications(database moira.Database) *api.ErrorResponse {
if err := database.RemoveAllNotifications(); err != nil {
return api.ErrorInternalServer(err)
}
return nil
}
18 changes: 18 additions & 0 deletions api/controller/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,21 @@ func TestDeleteNotification(t *testing.T) {
So(actual, ShouldBeNil)
})
}

func TestDeleteAllNotifications(t *testing.T) {
mockCtrl := gomock.NewController(t)
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)

Convey("Success", t, func() {
dataBase.EXPECT().RemoveAllNotifications().Return(nil)
err := DeleteAllNotifications(dataBase)
So(err, ShouldBeNil)
})

Convey("Error delete", t, func() {
expected := fmt.Errorf("Oooops! Can not get notifications")
dataBase.EXPECT().RemoveAllNotifications().Return(expected)
err := DeleteAllNotifications(dataBase)
So(err, ShouldResemble, api.ErrorInternalServer(expected))
})
}
6 changes: 6 additions & 0 deletions api/handler/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ func event(router chi.Router) {
render.Render(writer, request, api.ErrorRender(err))
}
})
router.Delete("/all", deleteAllEvents)
}
func deleteAllEvents(writer http.ResponseWriter, request *http.Request) {
if errorResponse := controller.DeleteAllEvents(database); errorResponse != nil {
render.Render(writer, request, errorResponse)
}
}
7 changes: 7 additions & 0 deletions api/handler/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func notification(router chi.Router) {
router.Get("/", getNotification)
router.Delete("/", deleteNotification)
router.Delete("/all", deleteAllNotifications)
}

func getNotification(writer http.ResponseWriter, request *http.Request) {
Expand Down Expand Up @@ -51,3 +52,9 @@ func deleteNotification(writer http.ResponseWriter, request *http.Request) {
render.Render(writer, request, api.ErrorRender(err))
}
}

func deleteAllNotifications(writer http.ResponseWriter, request *http.Request) {
if errorResponse := controller.DeleteAllNotifications(database); errorResponse != nil {
render.Render(writer, request, errorResponse)
}
}
12 changes: 12 additions & 0 deletions database/redis/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ func (connector *DbConnector) GetNotifications(start, end int64) ([]*moira.Sched
return notifications, total, nil
}

// RemoveAllNotifications delete all notifications
func (connector *DbConnector) RemoveAllNotifications() error {
c := connector.pool.Get()
defer c.Close()

if _, err := c.Do("DEL", notifierNotificationsKey); err != nil {
return fmt.Errorf("failed to remove %s: %s", notifierNotificationsKey, err.Error())
}

return nil
}

// RemoveNotification delete notifications by key = timestamp + contactID + subID
func (connector *DbConnector) RemoveNotification(notificationKey string) (int64, error) {
c := connector.pool.Get()
Expand Down
24 changes: 18 additions & 6 deletions database/redis/notification_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func (connector *DbConnector) PushNotificationEvent(event *moira.NotificationEve
c := connector.pool.Get()
defer c.Close()
c.Send("MULTI")
c.Send("LPUSH", eventsListKey, eventBytes)
c.Send("LPUSH", notificationEventsList, eventBytes)
if event.TriggerID != "" {
c.Send("ZADD", triggerEventsKey(event.TriggerID), event.Timestamp, eventBytes)
c.Send("ZREMRANGEBYSCORE", triggerEventsKey(event.TriggerID), "-inf", time.Now().Unix()-eventsTTL)
}
if ui {
c.Send("LPUSH", eventsUIListKey, eventBytes)
c.Send("LTRIM", eventsUIListKey, 0, 100)
c.Send("LPUSH", notificationEventsUIList, eventBytes)
c.Send("LTRIM", notificationEventsUIList, 0, 100)
}
_, err = c.Do("EXEC")
if err != nil {
Expand All @@ -74,7 +74,7 @@ func (connector *DbConnector) FetchNotificationEvent() (moira.NotificationEvent,

var event moira.NotificationEvent

rawRes, err := c.Do("BRPOP", eventsListKey, 1)
rawRes, err := c.Do("BRPOP", notificationEventsList, 1)
if err != nil {
return event, fmt.Errorf("Failed to fetch event: %s", err.Error())
}
Expand All @@ -95,8 +95,20 @@ func (connector *DbConnector) FetchNotificationEvent() (moira.NotificationEvent,
return event, nil
}

var eventsListKey = "moira-trigger-events"
var eventsUIListKey = "moira-trigger-events-ui"
// RemoveAllNotificationEvents removes all notification events from database
func (connector *DbConnector) RemoveAllNotificationEvents() error {
c := connector.pool.Get()
defer c.Close()

if _, err := c.Do("DEL", notificationEventsList); err != nil {
return fmt.Errorf("failed to remove %s: %s", notificationEventsList, err.Error())
}

return nil
}

var notificationEventsList = "moira-trigger-events"
var notificationEventsUIList = "moira-trigger-events-ui"

func triggerEventsKey(triggerID string) string {
return fmt.Sprintf("moira-trigger-events:%s", triggerID)
Expand Down
20 changes: 20 additions & 0 deletions database/redis/notification_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,26 @@ func TestNotificationEvents(t *testing.T) {
So(err, ShouldBeNil)
So(actual, ShouldResemble, make([]*moira.NotificationEvent, 0))
})

Convey("Test removing notification events", func() {
Convey("Should remove all notifications", func() {
err := dataBase.PushNotificationEvent(&notificationEvent, true)
So(err, ShouldBeNil)

err = dataBase.PushNotificationEvent(&notificationEvent1, true)
So(err, ShouldBeNil)

err = dataBase.PushNotificationEvent(&notificationEvent2, true)
So(err, ShouldBeNil)

err = dataBase.RemoveAllNotificationEvents()
So(err, ShouldBeNil)

actual, err := dataBase.FetchNotificationEvent()
So(err, ShouldResemble, database.ErrNil)
So(actual, ShouldResemble, moira.NotificationEvent{})
})
})
})
}

Expand Down
40 changes: 40 additions & 0 deletions database/redis/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,46 @@ func TestScheduledNotification(t *testing.T) {
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})
})

Convey("Test remove all notifications", func() {
now := time.Now().Unix()
id1 := "id1"
notification1 := moira.ScheduledNotification{
Contact: moira.ContactData{ID: id1},
Event: moira.NotificationEvent{SubscriptionID: &id1},
SendFail: 1,
Timestamp: now,
}
notification2 := moira.ScheduledNotification{
Contact: moira.ContactData{ID: id1},
Event: moira.NotificationEvent{SubscriptionID: &id1},
SendFail: 2,
Timestamp: now,
}
notification3 := moira.ScheduledNotification{
Contact: moira.ContactData{ID: id1},
Event: moira.NotificationEvent{SubscriptionID: &id1},
SendFail: 3,
Timestamp: now + 3600,
}
addNotifications(dataBase, []moira.ScheduledNotification{notification1, notification2, notification3})
actual, total, err := dataBase.GetNotifications(0, -1)
So(err, ShouldBeNil)
So(total, ShouldEqual, 3)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notification1, &notification2, &notification3})

err = dataBase.RemoveAllNotifications()
So(err, ShouldBeNil)

actual, total, err = dataBase.GetNotifications(0, -1)
So(err, ShouldBeNil)
So(total, ShouldEqual, 0)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})

actual, err = dataBase.FetchNotifications(now + 3600)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})
})
})
}

Expand Down
2 changes: 2 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Database interface {
PushNotificationEvent(event *NotificationEvent, ui bool) error
GetNotificationEventCount(triggerID string, from int64) int64
FetchNotificationEvent() (NotificationEvent, error)
RemoveAllNotificationEvents() error

// ContactData storing
GetContact(contactID string) (ContactData, error)
Expand All @@ -66,6 +67,7 @@ type Database interface {
// ScheduledNotification storing
GetNotifications(start, end int64) ([]*ScheduledNotification, int64, error)
RemoveNotification(notificationKey string) (int64, error)
RemoveAllNotifications() error
FetchNotifications(to int64) ([]*ScheduledNotification, error)
AddNotification(notification *ScheduledNotification) error
AddNotifications(notification []*ScheduledNotification, timestamp int64) error
Expand Down
24 changes: 24 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.

0 comments on commit 6aae53c

Please sign in to comment.