Increase and decrease the list metrics counter with events

This commit is contained in:
kolaente 2021-02-01 21:05:53 +01:00
parent 3f21b3610e
commit 83f48dd175
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 53 additions and 6 deletions

View File

@ -155,7 +155,7 @@ func UpdateCount(update int64, key string) {
}
}
if update < 0 {
err := keyvalue.DecrBy(key, update)
err := keyvalue.DecrBy(key, update*-1)
if err != nil {
log.Error(err.Error())
}

View File

@ -106,6 +106,15 @@ func (l *ListUpdatedEvent) TopicName() string {
return "list.updated"
}
type ListDeletedEvent struct {
List *List
Doer *user.User
}
func (t *ListDeletedEvent) TopicName() string {
return "list.deleted"
}
////////////////////
// Sharing Events //
////////////////////

View File

@ -25,7 +25,6 @@ import (
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/metrics"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
"xorm.io/builder"
@ -493,7 +492,6 @@ func CreateOrUpdateList(s *xorm.Session, list *List, auth web.Auth) (err error)
if list.ID == 0 {
_, err = s.Insert(list)
metrics.UpdateCount(1, metrics.ListCountKey)
} else {
// We need to specify the cols we want to update here to be able to un-archive lists
colsToUpdate := []string{
@ -643,11 +641,21 @@ func (l *List) Delete(s *xorm.Session, a web.Auth) (err error) {
if err != nil {
return
}
metrics.UpdateCount(-1, metrics.ListCountKey)
// Delete all todotasks on that list
// Delete all tasks on that list
_, err = s.Where("list_id = ?", l.ID).Delete(&Task{})
return
if err != nil {
return
}
doer, err := user.GetFromAuth(a)
if err != nil {
return err
}
return events.Publish(&ListDeletedEvent{
List: l,
Doer: doer,
})
}
// SetListBackground sets a background file as list background in the db

View File

@ -17,6 +17,7 @@
package models
import (
"code.vikunja.io/api/pkg/metrics"
"encoding/json"
"code.vikunja.io/api/pkg/events"
@ -25,6 +26,8 @@ import (
func RegisterListeners() {
events.RegisterListener((&TaskCreatedEvent{}).TopicName(), &SendTaskCreatedNotification{})
events.RegisterListener((&ListCreatedEvent{}).TopicName(), &IncreaseListCounter{})
events.RegisterListener((&ListDeletedEvent{}).TopicName(), &DecreaseListCounter{})
}
type SendTaskCreatedNotification struct {
@ -43,3 +46,30 @@ func (s *SendTaskCreatedNotification) Handle(payload message.Payload) (err error
return nil
}
///////
// List Event Listeners
type IncreaseListCounter struct {
}
func (s *IncreaseListCounter) Name() string {
return "increase.list.counter"
}
func (s *IncreaseListCounter) Handle(payload message.Payload) (err error) {
metrics.UpdateCount(1, metrics.ListCountKey)
return nil
}
type DecreaseListCounter struct {
}
func (s *DecreaseListCounter) Name() string {
return "decrease.list.counter"
}
func (s *DecreaseListCounter) Handle(payload message.Payload) (err error) {
metrics.UpdateCount(-1, metrics.ListCountKey)
return nil
}