2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2020-01-09 18:33:22 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
2018-12-12 23:50:35 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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.
|
2018-12-12 23:50:35 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01: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
|
|
|
|
// GNU General Public License for more details.
|
2018-12-12 23:50:35 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-12-12 23:50:35 +01:00
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-12-12 23:50:35 +01:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
2020-10-10 18:53:59 +02:00
|
|
|
"code.vikunja.io/api/pkg/modules/keyvalue"
|
2020-05-15 14:42:32 +02:00
|
|
|
"code.vikunja.io/web"
|
2018-12-12 23:50:35 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-07-02 23:19:03 +02:00
|
|
|
type activeUsersMap map[int64]*ActiveUser
|
|
|
|
|
2020-08-02 19:16:58 +02:00
|
|
|
// ActiveUsers is the type used to save active users
|
2020-07-02 21:16:39 +02:00
|
|
|
type ActiveUsers struct {
|
2020-07-02 23:19:03 +02:00
|
|
|
users activeUsersMap
|
2020-07-02 21:16:39 +02:00
|
|
|
mutex *sync.Mutex
|
|
|
|
}
|
2020-05-15 14:42:32 +02:00
|
|
|
|
|
|
|
// activeUsers holds a map with all active users
|
2020-07-02 21:16:39 +02:00
|
|
|
var activeUsers *ActiveUsers
|
2020-05-15 14:42:32 +02:00
|
|
|
|
2018-12-12 23:50:35 +01:00
|
|
|
func init() {
|
2020-07-02 21:16:39 +02:00
|
|
|
activeUsers = &ActiveUsers{
|
|
|
|
users: make(map[int64]*ActiveUser),
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
}
|
2020-05-15 14:42:32 +02:00
|
|
|
|
2018-12-12 23:50:35 +01:00
|
|
|
promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Name: "vikunja_active_users",
|
|
|
|
Help: "The currently active users on this node",
|
|
|
|
}, func() float64 {
|
|
|
|
|
2020-07-02 23:19:03 +02:00
|
|
|
allActiveUsers, err := getActiveUsers()
|
2018-12-12 23:50:35 +01:00
|
|
|
if err != nil {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Error(err.Error())
|
2018-12-12 23:50:35 +01:00
|
|
|
}
|
2020-07-02 21:16:39 +02:00
|
|
|
if allActiveUsers == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2018-12-12 23:50:35 +01:00
|
|
|
activeUsersCount := 0
|
2020-07-02 23:19:03 +02:00
|
|
|
for _, u := range allActiveUsers {
|
2018-12-12 23:50:35 +01:00
|
|
|
if time.Since(u.LastSeen) < SecondsUntilInactive*time.Second {
|
|
|
|
activeUsersCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return float64(activeUsersCount)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-15 14:42:32 +02:00
|
|
|
// SetUserActive sets a user as active and pushes it to redis
|
|
|
|
func SetUserActive(a web.Auth) (err error) {
|
2020-07-02 21:16:39 +02:00
|
|
|
activeUsers.mutex.Lock()
|
|
|
|
activeUsers.users[a.GetID()] = &ActiveUser{
|
2020-05-15 14:42:32 +02:00
|
|
|
UserID: a.GetID(),
|
|
|
|
LastSeen: time.Now(),
|
|
|
|
}
|
2020-07-02 21:16:39 +02:00
|
|
|
activeUsers.mutex.Unlock()
|
2020-05-15 14:42:32 +02:00
|
|
|
return PushActiveUsers()
|
|
|
|
}
|
|
|
|
|
2020-07-02 23:19:03 +02:00
|
|
|
// getActiveUsers returns the active users from redis
|
|
|
|
func getActiveUsers() (users activeUsersMap, err error) {
|
2020-10-10 18:53:59 +02:00
|
|
|
u, err := keyvalue.Get(ActiveUsersKey)
|
2018-12-12 23:50:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-10 18:53:59 +02:00
|
|
|
|
|
|
|
users = u.(activeUsersMap)
|
2018-12-12 23:50:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-15 14:42:32 +02:00
|
|
|
// PushActiveUsers pushed the content of the activeUsers map to redis
|
|
|
|
func PushActiveUsers() (err error) {
|
2020-07-02 21:16:39 +02:00
|
|
|
activeUsers.mutex.Lock()
|
|
|
|
defer activeUsers.mutex.Unlock()
|
2018-12-12 23:50:35 +01:00
|
|
|
|
2020-10-10 18:53:59 +02:00
|
|
|
return keyvalue.Put(ActiveUsersKey, activeUsers.users)
|
2018-12-12 23:50:35 +01:00
|
|
|
}
|