
Fix lint Add docs for keyvalue config options Use keyvalue store to cache unsplash photo results Cleanup Use keyvalue store for upload avatar Use keyvalue store for initials avatar Fix initializing metrics Use keyvalue for metrics Add IncryBy and DecrBy methods to increase or decrease a value Fix lint Return custom error if a key does not exist Init keyvalue storage Follow the keyvalue storage setting for things like cache and other Add docs Add configuration of the storage backend Add redis keyvalue storage implementation Add doc comments Add methods to use storage through the package itself Add memory implementation for keyvalue store Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/674 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
// Vikunja is a to-do list application to facilitate your life.
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License 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
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package metrics
|
|
|
|
import (
|
|
"code.vikunja.io/api/pkg/log"
|
|
"code.vikunja.io/api/pkg/modules/keyvalue"
|
|
"code.vikunja.io/web"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// SecondsUntilInactive defines the seconds until a user is considered inactive
|
|
const SecondsUntilInactive = 60
|
|
|
|
// ActiveUsersKey is the key used to store active users in redis
|
|
const ActiveUsersKey = `activeusers`
|
|
|
|
// ActiveUser defines an active user
|
|
type ActiveUser struct {
|
|
UserID int64
|
|
LastSeen time.Time
|
|
}
|
|
|
|
type activeUsersMap map[int64]*ActiveUser
|
|
|
|
// ActiveUsers is the type used to save active users
|
|
type ActiveUsers struct {
|
|
users activeUsersMap
|
|
mutex *sync.Mutex
|
|
}
|
|
|
|
// activeUsers holds a map with all active users
|
|
var activeUsers *ActiveUsers
|
|
|
|
func init() {
|
|
activeUsers = &ActiveUsers{
|
|
users: make(map[int64]*ActiveUser),
|
|
mutex: &sync.Mutex{},
|
|
}
|
|
|
|
promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
|
Name: "vikunja_active_users",
|
|
Help: "The currently active users on this node",
|
|
}, func() float64 {
|
|
|
|
allActiveUsers, err := getActiveUsers()
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
}
|
|
if allActiveUsers == nil {
|
|
return 0
|
|
}
|
|
activeUsersCount := 0
|
|
for _, u := range allActiveUsers {
|
|
if time.Since(u.LastSeen) < SecondsUntilInactive*time.Second {
|
|
activeUsersCount++
|
|
}
|
|
}
|
|
return float64(activeUsersCount)
|
|
})
|
|
}
|
|
|
|
// SetUserActive sets a user as active and pushes it to redis
|
|
func SetUserActive(a web.Auth) (err error) {
|
|
activeUsers.mutex.Lock()
|
|
activeUsers.users[a.GetID()] = &ActiveUser{
|
|
UserID: a.GetID(),
|
|
LastSeen: time.Now(),
|
|
}
|
|
activeUsers.mutex.Unlock()
|
|
return PushActiveUsers()
|
|
}
|
|
|
|
// getActiveUsers returns the active users from redis
|
|
func getActiveUsers() (users activeUsersMap, err error) {
|
|
u, err := keyvalue.Get(ActiveUsersKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
users = u.(activeUsersMap)
|
|
return
|
|
}
|
|
|
|
// PushActiveUsers pushed the content of the activeUsers map to redis
|
|
func PushActiveUsers() (err error) {
|
|
activeUsers.mutex.Lock()
|
|
defer activeUsers.mutex.Unlock()
|
|
|
|
return keyvalue.Put(ActiveUsersKey, activeUsers.users)
|
|
}
|