From 694dab9295cca449776faf31ebc086a09fbb67df Mon Sep 17 00:00:00 2001 From: kolaente Date: Mon, 1 Feb 2021 21:43:32 +0100 Subject: [PATCH] Cleanup --- pkg/metrics/metrics.go | 20 -------------------- pkg/models/listeners.go | 25 +++++++++---------------- pkg/user/listeners.go | 4 ++-- 3 files changed, 11 insertions(+), 38 deletions(-) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 69899d890..c4cb51973 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -17,7 +17,6 @@ package metrics import ( - "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/keyvalue" "github.com/prometheus/client_golang/prometheus" @@ -142,22 +141,3 @@ func GetCount(key string) (count int64, err error) { func SetCount(count int64, key string) error { return keyvalue.Put(key, count) } - -// UpdateCount updates a count with a given amount -func UpdateCount(update int64, key string) { - if !config.ServiceEnableMetrics.GetBool() { - return - } - if update > 0 { - err := keyvalue.IncrBy(key, update) - if err != nil { - log.Error(err.Error()) - } - } - if update < 0 { - err := keyvalue.DecrBy(key, update*-1) - if err != nil { - log.Error(err.Error()) - } - } -} diff --git a/pkg/models/listeners.go b/pkg/models/listeners.go index 0874f869b..665894125 100644 --- a/pkg/models/listeners.go +++ b/pkg/models/listeners.go @@ -19,6 +19,7 @@ package models import ( "code.vikunja.io/api/pkg/events" "code.vikunja.io/api/pkg/metrics" + "code.vikunja.io/api/pkg/modules/keyvalue" "github.com/ThreeDotsLabs/watermill/message" ) @@ -47,8 +48,7 @@ func (s *IncreaseTaskCounter) Name() string { // Hanlde is executed when the event IncreaseTaskCounter listens on is fired func (s *IncreaseTaskCounter) Handle(payload message.Payload) (err error) { - metrics.UpdateCount(1, metrics.TaskCountKey) - return nil + return keyvalue.IncrBy(metrics.TaskCountKey, 1) } // DecreaseTaskCounter represents a listener @@ -62,8 +62,7 @@ func (s *DecreaseTaskCounter) Name() string { // Hanlde is executed when the event DecreaseTaskCounter listens on is fired func (s *DecreaseTaskCounter) Handle(payload message.Payload) (err error) { - metrics.UpdateCount(-1, metrics.TaskCountKey) - return nil + return keyvalue.DecrBy(metrics.TaskCountKey, 1) } /////// @@ -77,8 +76,7 @@ func (s *IncreaseListCounter) Name() string { } func (s *IncreaseListCounter) Handle(payload message.Payload) (err error) { - metrics.UpdateCount(1, metrics.ListCountKey) - return nil + return keyvalue.IncrBy(metrics.ListCountKey, 1) } type DecreaseListCounter struct { @@ -89,8 +87,7 @@ func (s *DecreaseListCounter) Name() string { } func (s *DecreaseListCounter) Handle(payload message.Payload) (err error) { - metrics.UpdateCount(-1, metrics.ListCountKey) - return nil + return keyvalue.DecrBy(metrics.ListCountKey, 1) } ////// @@ -107,8 +104,7 @@ func (s *IncreaseNamespaceCounter) Name() string { // 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 + return keyvalue.IncrBy(metrics.NamespaceCountKey, 1) } // DecreaseNamespaceCounter represents a listener @@ -122,8 +118,7 @@ func (s *DecreaseNamespaceCounter) Name() string { // 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 + return keyvalue.DecrBy(metrics.NamespaceCountKey, 1) } /////// @@ -140,8 +135,7 @@ func (s *IncreaseTeamCounter) Name() string { // Hanlde is executed when the event IncreaseTeamCounter listens on is fired func (s *IncreaseTeamCounter) Handle(payload message.Payload) (err error) { - metrics.UpdateCount(1, metrics.TeamCountKey) - return nil + return keyvalue.IncrBy(metrics.TeamCountKey, 1) } // DecreaseTeamCounter represents a listener @@ -155,6 +149,5 @@ func (s *DecreaseTeamCounter) Name() string { // Hanlde is executed when the event DecreaseTeamCounter listens on is fired func (s *DecreaseTeamCounter) Handle(payload message.Payload) (err error) { - metrics.UpdateCount(-1, metrics.TeamCountKey) - return nil + return keyvalue.DecrBy(metrics.TeamCountKey, 1) } diff --git a/pkg/user/listeners.go b/pkg/user/listeners.go index 479211982..36592385e 100644 --- a/pkg/user/listeners.go +++ b/pkg/user/listeners.go @@ -19,6 +19,7 @@ package user import ( "code.vikunja.io/api/pkg/events" "code.vikunja.io/api/pkg/metrics" + "code.vikunja.io/api/pkg/modules/keyvalue" "github.com/ThreeDotsLabs/watermill/message" ) @@ -40,6 +41,5 @@ func (s *IncreaseUserCounter) Name() string { // Hanlde is executed when the event IncreaseUserCounter listens on is fired func (s *IncreaseUserCounter) Handle(payload message.Payload) (err error) { - metrics.UpdateCount(1, metrics.UserCountKey) - return nil + return keyvalue.IncrBy(metrics.UserCountKey, 1) }