2020-02-07 16:27:45 +00:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2023-09-01 06:32:28 +00:00
|
|
|
// Copyright 2018-present Vikunja and contributors. All rights reserved.
|
2018-12-12 22:50:35 +00:00
|
|
|
//
|
2019-12-04 19:39:56 +00:00
|
|
|
// 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
|
2019-12-04 19:39:56 +00:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-12-12 22:50:35 +00:00
|
|
|
//
|
2019-12-04 19:39:56 +00:00
|
|
|
// 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.
|
2018-12-12 22:50:35 +00:00
|
|
|
//
|
2020-12-23 15:41:52 +00:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 19:39:56 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-12-12 22:50:35 +00:00
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2020-10-11 20:10:03 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-12-12 22:50:35 +00:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
2020-10-10 16:53:59 +00:00
|
|
|
"code.vikunja.io/api/pkg/modules/keyvalue"
|
2020-05-15 12:42:32 +00:00
|
|
|
"code.vikunja.io/web"
|
2018-12-12 22:50:35 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
)
|
|
|
|
|
2023-12-03 14:12:43 +00:00
|
|
|
const secondsUntilInactive = 30
|
|
|
|
const activeUsersKey = `active_users`
|
|
|
|
const activeLinkSharesKey = `active_link_shares`
|
2018-12-12 22:50:35 +00:00
|
|
|
|
2023-12-03 14:12:43 +00:00
|
|
|
// ActiveAuthenticable defines an active user or link share
|
|
|
|
type ActiveAuthenticable struct {
|
|
|
|
ID int64
|
2018-12-12 22:50:35 +00:00
|
|
|
LastSeen time.Time
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:12:43 +00:00
|
|
|
type activeUsersMap map[int64]*ActiveAuthenticable
|
2020-07-02 21:19:03 +00:00
|
|
|
|
2020-07-02 19:16:39 +00:00
|
|
|
type ActiveUsers struct {
|
2020-07-02 21:19:03 +00:00
|
|
|
users activeUsersMap
|
2020-07-02 19:16:39 +00:00
|
|
|
mutex *sync.Mutex
|
|
|
|
}
|
2020-05-15 12:42:32 +00:00
|
|
|
|
2020-07-02 19:16:39 +00:00
|
|
|
var activeUsers *ActiveUsers
|
2020-05-15 12:42:32 +00:00
|
|
|
|
2023-12-03 14:12:43 +00:00
|
|
|
type activeLinkSharesMap map[int64]*ActiveAuthenticable
|
|
|
|
|
|
|
|
type ActiveLinkShares struct {
|
|
|
|
shares activeLinkSharesMap
|
|
|
|
mutex *sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var activeLinkShares *ActiveLinkShares
|
|
|
|
|
2018-12-12 22:50:35 +00:00
|
|
|
func init() {
|
2020-07-02 19:16:39 +00:00
|
|
|
activeUsers = &ActiveUsers{
|
2023-12-03 14:12:43 +00:00
|
|
|
users: make(map[int64]*ActiveAuthenticable),
|
2020-07-02 19:16:39 +00:00
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
}
|
2023-12-03 14:12:43 +00:00
|
|
|
activeLinkShares = &ActiveLinkShares{
|
|
|
|
shares: make(map[int64]*ActiveAuthenticable),
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
}
|
2022-11-09 20:12:17 +00:00
|
|
|
}
|
2020-05-15 12:42:32 +00:00
|
|
|
|
2022-11-09 20:12:17 +00:00
|
|
|
func setupActiveUsersMetric() {
|
|
|
|
err := registry.Register(promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
2018-12-12 22:50:35 +00:00
|
|
|
Name: "vikunja_active_users",
|
2023-12-03 14:12:43 +00:00
|
|
|
Help: "The number of shares active within the last 30 seconds",
|
2018-12-12 22:50:35 +00:00
|
|
|
}, func() float64 {
|
2023-12-03 14:12:43 +00:00
|
|
|
allActiveUsers := activeUsersMap{}
|
|
|
|
_, err := keyvalue.GetWithValue(activeUsersKey, &allActiveUsers)
|
2018-12-12 22:50:35 +00:00
|
|
|
if err != nil {
|
2019-07-20 18:12:10 +00:00
|
|
|
log.Error(err.Error())
|
2023-12-03 14:12:43 +00:00
|
|
|
return 0
|
2018-12-12 22:50:35 +00:00
|
|
|
}
|
2020-07-02 19:16:39 +00:00
|
|
|
if allActiveUsers == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2023-12-03 14:12:43 +00:00
|
|
|
count := 0
|
2020-07-02 21:19:03 +00:00
|
|
|
for _, u := range allActiveUsers {
|
2023-12-03 14:12:43 +00:00
|
|
|
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++
|
2018-12-12 22:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-03 14:12:43 +00:00
|
|
|
return float64(count)
|
2022-11-09 20:12:17 +00:00
|
|
|
}))
|
|
|
|
if err != nil {
|
2023-12-03 14:12:43 +00:00
|
|
|
log.Criticalf("Could not register metrics for currently active link shares: %s", err)
|
2022-11-09 20:12:17 +00:00
|
|
|
}
|
2018-12-12 22:50:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-03 14:12:43 +00:00
|
|
|
// 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) {
|
2020-07-02 19:16:39 +00:00
|
|
|
activeUsers.mutex.Lock()
|
2023-12-03 14:12:43 +00:00
|
|
|
defer activeUsers.mutex.Unlock()
|
|
|
|
activeUsers.users[a.GetID()] = &ActiveAuthenticable{
|
|
|
|
ID: a.GetID(),
|
2020-05-15 12:42:32 +00:00
|
|
|
LastSeen: time.Now(),
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:12:43 +00:00
|
|
|
return keyvalue.Put(activeUsersKey, activeUsers.users)
|
2018-12-12 22:50:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-03 14:12:43 +00:00
|
|
|
// 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(),
|
|
|
|
}
|
2018-12-12 22:50:35 +00:00
|
|
|
|
2023-12-03 14:12:43 +00:00
|
|
|
return keyvalue.Put(activeLinkSharesKey, activeLinkShares.shares)
|
2018-12-12 22:50:35 +00:00
|
|
|
}
|