vikunja/pkg/metrics/active_users.go

146 lines
4.0 KiB
Go
Raw Normal View History

2020-02-07 16:27:45 +00:00
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
2020-12-23 15:41:52 +00:00
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2020-12-23 15:41:52 +00:00
// GNU Affero General Public Licensee for more details.
//
2020-12-23 15:41:52 +00:00
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package metrics
import (
"sync"
"time"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/modules/keyvalue"
2020-05-15 12:42:32 +00:00
"code.vikunja.io/web"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const secondsUntilInactive = 30
const activeUsersKey = `active_users`
const activeLinkSharesKey = `active_link_shares`
// ActiveAuthenticable defines an active user or link share
type ActiveAuthenticable struct {
ID int64
LastSeen time.Time
}
type activeUsersMap map[int64]*ActiveAuthenticable
2020-07-02 21:19:03 +00:00
type ActiveUsers struct {
2020-07-02 21:19:03 +00:00
users activeUsersMap
mutex *sync.Mutex
}
2020-05-15 12:42:32 +00:00
var activeUsers *ActiveUsers
2020-05-15 12:42:32 +00:00
type activeLinkSharesMap map[int64]*ActiveAuthenticable
type ActiveLinkShares struct {
shares activeLinkSharesMap
mutex *sync.Mutex
}
var activeLinkShares *ActiveLinkShares
func init() {
activeUsers = &ActiveUsers{
users: make(map[int64]*ActiveAuthenticable),
mutex: &sync.Mutex{},
}
activeLinkShares = &ActiveLinkShares{
shares: make(map[int64]*ActiveAuthenticable),
mutex: &sync.Mutex{},
}
}
2020-05-15 12:42:32 +00:00
func setupActiveUsersMetric() {
err := registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{
Name: "vikunja_active_users",
Help: "The number of shares active within the last 30 seconds",
}, func() float64 {
allActiveUsers := activeUsersMap{}
_, err := keyvalue.GetWithValue(activeUsersKey, &allActiveUsers)
if err != nil {
2019-07-20 18:12:10 +00:00
log.Error(err.Error())
return 0
}
if allActiveUsers == nil {
return 0
}
count := 0
2020-07-02 21:19:03 +00:00
for _, u := range allActiveUsers {
if time.Since(u.LastSeen) < secondsUntilInactive*time.Second {
count++
}
}
return float64(count)
}))
if err != nil {
log.Criticalf("Could not register metrics for currently active shares: %s", err)
}
}
func setupActiveLinkSharesMetric() {
err := registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{
Name: "vikunja_active_link_shares",
Help: "The number of link shares active within the last 30 seconds. Similar to vikunja_active_users.",
}, func() float64 {
allActiveLinkShares := activeLinkSharesMap{}
_, err := keyvalue.GetWithValue(activeLinkSharesKey, &allActiveLinkShares)
if err != nil {
log.Error(err.Error())
return 0
}
if allActiveLinkShares == nil {
return 0
}
count := 0
for _, u := range allActiveLinkShares {
if time.Since(u.LastSeen) < secondsUntilInactive*time.Second {
count++
}
}
return float64(count)
}))
if err != nil {
log.Criticalf("Could not register metrics for currently active link shares: %s", err)
}
}
// SetUserActive sets a user as active and pushes it to keyvalue
2020-05-15 12:42:32 +00:00
func SetUserActive(a web.Auth) (err error) {
activeUsers.mutex.Lock()
defer activeUsers.mutex.Unlock()
activeUsers.users[a.GetID()] = &ActiveAuthenticable{
ID: a.GetID(),
2020-05-15 12:42:32 +00:00
LastSeen: time.Now(),
}
return keyvalue.Put(activeUsersKey, activeUsers.users)
}
// SetLinkShareActive sets a user as active and pushes it to keyvalue
func SetLinkShareActive(a web.Auth) (err error) {
activeLinkShares.mutex.Lock()
defer activeLinkShares.mutex.Unlock()
activeLinkShares.shares[a.GetID()] = &ActiveAuthenticable{
ID: a.GetID(),
LastSeen: time.Now(),
}
return keyvalue.Put(activeLinkSharesKey, activeLinkShares.shares)
}