Increase and decrease the namespace metrics counter with events
This commit is contained in:
parent
35f9a47e4a
commit
56c712e6b6
|
@ -88,7 +88,7 @@ func InitMetrics() {
|
||||||
|
|
||||||
// Register total Namespaces count metric
|
// Register total Namespaces count metric
|
||||||
err = registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
err = registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
||||||
Name: "vikunja_namespcae_count",
|
Name: "vikunja_namespace_count",
|
||||||
Help: "The total number of namespaces on this instance",
|
Help: "The total number of namespaces on this instance",
|
||||||
}, func() float64 {
|
}, func() float64 {
|
||||||
count, _ := GetCount(NamespaceCountKey)
|
count, _ := GetCount(NamespaceCountKey)
|
||||||
|
|
|
@ -84,6 +84,17 @@ func (n *NamespaceUpdatedEvent) TopicName() string {
|
||||||
return "namespace.updated"
|
return "namespace.updated"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NamespaceDeletedEvent represents a NamespaceDeletedEvent event
|
||||||
|
type NamespaceDeletedEvent struct {
|
||||||
|
Namespace *Namespace
|
||||||
|
Doer *user.User
|
||||||
|
}
|
||||||
|
|
||||||
|
// TopicName defines the name for NamespaceDeletedEvent
|
||||||
|
func (t *NamespaceDeletedEvent) TopicName() string {
|
||||||
|
return "namespace.deleted"
|
||||||
|
}
|
||||||
|
|
||||||
/////////////////
|
/////////////////
|
||||||
// List Events //
|
// List Events //
|
||||||
/////////////////
|
/////////////////
|
||||||
|
|
|
@ -28,6 +28,8 @@ func RegisterListeners() {
|
||||||
events.RegisterListener((&TaskCreatedEvent{}).TopicName(), &SendTaskCreatedNotification{})
|
events.RegisterListener((&TaskCreatedEvent{}).TopicName(), &SendTaskCreatedNotification{})
|
||||||
events.RegisterListener((&ListCreatedEvent{}).TopicName(), &IncreaseListCounter{})
|
events.RegisterListener((&ListCreatedEvent{}).TopicName(), &IncreaseListCounter{})
|
||||||
events.RegisterListener((&ListDeletedEvent{}).TopicName(), &DecreaseListCounter{})
|
events.RegisterListener((&ListDeletedEvent{}).TopicName(), &DecreaseListCounter{})
|
||||||
|
events.RegisterListener((&NamespaceCreatedEvent{}).TopicName(), &IncreaseNamespaceCounter{})
|
||||||
|
events.RegisterListener((&NamespaceDeletedEvent{}).TopicName(), &DecreaseNamespaceCounter{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type SendTaskCreatedNotification struct {
|
type SendTaskCreatedNotification struct {
|
||||||
|
@ -54,7 +56,7 @@ type IncreaseListCounter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *IncreaseListCounter) Name() string {
|
func (s *IncreaseListCounter) Name() string {
|
||||||
return "increase.list.counter"
|
return "list.counter.increase"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *IncreaseListCounter) Handle(payload message.Payload) (err error) {
|
func (s *IncreaseListCounter) Handle(payload message.Payload) (err error) {
|
||||||
|
@ -66,10 +68,43 @@ type DecreaseListCounter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DecreaseListCounter) Name() string {
|
func (s *DecreaseListCounter) Name() string {
|
||||||
return "decrease.list.counter"
|
return "list.counter.decrease"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DecreaseListCounter) Handle(payload message.Payload) (err error) {
|
func (s *DecreaseListCounter) Handle(payload message.Payload) (err error) {
|
||||||
metrics.UpdateCount(-1, metrics.ListCountKey)
|
metrics.UpdateCount(-1, metrics.ListCountKey)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////
|
||||||
|
// Namespace events
|
||||||
|
|
||||||
|
// IncreaseNamespaceCounter represents a listener
|
||||||
|
type IncreaseNamespaceCounter struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name defines the name for the IncreaseNamespaceCounter listener
|
||||||
|
func (s *IncreaseNamespaceCounter) Name() string {
|
||||||
|
return "namespace.counter.increase"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hanlde is executed when the event IncreaseNamespaceCounter listens on is fired
|
||||||
|
func (s *IncreaseNamespaceCounter) Handle(payload message.Payload) (err error) {
|
||||||
|
metrics.UpdateCount(1, metrics.NamespaceCountKey)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecreaseNamespaceCounter represents a listener
|
||||||
|
type DecreaseNamespaceCounter struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name defines the name for the DecreaseNamespaceCounter listener
|
||||||
|
func (s *DecreaseNamespaceCounter) Name() string {
|
||||||
|
return "namespace.counter.decrease"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hanlde is executed when the event DecreaseNamespaceCounter listens on is fired
|
||||||
|
func (s *DecreaseNamespaceCounter) Handle(payload message.Payload) (err error) {
|
||||||
|
metrics.UpdateCount(-1, metrics.NamespaceCountKey)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
"code.vikunja.io/api/pkg/metrics"
|
|
||||||
"code.vikunja.io/api/pkg/user"
|
"code.vikunja.io/api/pkg/user"
|
||||||
"code.vikunja.io/web"
|
"code.vikunja.io/web"
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
|
@ -487,7 +486,6 @@ func (n *Namespace) Create(s *xorm.Session, a web.Auth) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
metrics.UpdateCount(1, metrics.NamespaceCountKey)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -552,9 +550,14 @@ func (n *Namespace) Delete(s *xorm.Session, a web.Auth) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
metrics.UpdateCount(-1, metrics.NamespaceCountKey)
|
doer, err := user.GetFromAuth(a)
|
||||||
|
if err != nil {
|
||||||
return
|
return err
|
||||||
|
}
|
||||||
|
return events.Publish(&NamespaceDeletedEvent{
|
||||||
|
Namespace: n,
|
||||||
|
Doer: doer,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update implements the update method via the interface
|
// Update implements the update method via the interface
|
||||||
|
|
Loading…
Reference in New Issue