commit c6bcb6f3f9cac1f26785337dfb29f3773a6bc1b2
parent ef059e0a3fa10d5d815c1f2617b45d2646b6fd14
Author: vh <vetle.haflan@gmail.com>
Date: Wed, 29 Apr 2020 17:11:24 +0200
Limit number of events for a service if MaxNumberEvents is > 0
Closes #8
Diffstat:
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/internal/events/events.go b/internal/events/events.go
@@ -31,7 +31,7 @@ type Event struct {
}
// GetAll returns all events in the database
-func GetAll() ([]*Event) {
+func GetAll() []*Event {
events := []*Event{}
db := database.GetDB()
err := db.View(func(tx *bbolt.Tx) error {
diff --git a/internal/http/events.go b/internal/http/events.go
@@ -74,6 +74,25 @@ func reportEvent(w http.ResponseWriter, r *http.Request) {
err = events.Add(event)
check(err)
log.Printf("New event registered, id = %v\n", event.ID)
+
+ if service.MaxNumberEvents < 1 {
+ return
+ }
+ // Find whether MaxNumberEvents is reached and delete the first event if so
+ var numEvents, firstEventID uint64
+ es := events.GetAll()
+ for _, e := range es {
+ if e.Service == service.ID {
+ if firstEventID == 0 {
+ firstEventID = e.ID
+ }
+ numEvents++
+ }
+ }
+ if numEvents > service.MaxNumberEvents {
+ events.Delete(firstEventID)
+ log.Printf("MaxNumberEvents reached for service %v. Deleting first event", service.ID)
+ }
}
// For later reference: