2021-01-17 11:24:09 +03:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2021-01-30 13:17:37 +03:00
|
|
|
"github.com/emvi/logbuch"
|
2022-12-01 17:31:19 +03:00
|
|
|
"github.com/muety/artifex/v2"
|
2021-01-17 11:24:09 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
2022-11-20 12:10:24 +03:00
|
|
|
"github.com/muety/wakapi/utils"
|
2022-12-01 15:46:06 +03:00
|
|
|
"go.uber.org/atomic"
|
2021-01-17 11:24:09 +03:00
|
|
|
"strconv"
|
2022-11-20 12:10:24 +03:00
|
|
|
"sync"
|
2021-01-17 11:24:09 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
)
|
|
|
|
|
2022-12-01 15:46:06 +03:00
|
|
|
const (
|
|
|
|
countUsersEvery = 1 * time.Hour
|
|
|
|
)
|
|
|
|
|
|
|
|
var countLock = sync.Mutex{}
|
|
|
|
|
2021-01-17 11:24:09 +03:00
|
|
|
type MiscService struct {
|
|
|
|
config *config.Config
|
|
|
|
userService IUserService
|
|
|
|
summaryService ISummaryService
|
|
|
|
keyValueService IKeyValueService
|
2022-11-20 12:10:24 +03:00
|
|
|
queueDefault *artifex.Dispatcher
|
|
|
|
queueWorkers *artifex.Dispatcher
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMiscService(userService IUserService, summaryService ISummaryService, keyValueService IKeyValueService) *MiscService {
|
|
|
|
return &MiscService{
|
|
|
|
config: config.Get(),
|
|
|
|
userService: userService,
|
|
|
|
summaryService: summaryService,
|
|
|
|
keyValueService: keyValueService,
|
2022-11-20 12:10:24 +03:00
|
|
|
queueDefault: config.GetDefaultQueue(),
|
|
|
|
queueWorkers: config.GetQueue(config.QueueProcessing),
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *MiscService) ScheduleCountTotalTime() {
|
2022-11-20 12:12:34 +03:00
|
|
|
logbuch.Info("scheduling total time counting")
|
2022-12-01 15:46:06 +03:00
|
|
|
if _, err := srv.queueDefault.DispatchEvery(srv.CountTotalTime, countUsersEvery); err != nil {
|
2022-11-20 12:10:24 +03:00
|
|
|
config.Log().Error("failed to schedule user counting jobs, %v", err)
|
|
|
|
}
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
|
|
|
|
2022-11-20 12:10:24 +03:00
|
|
|
func (srv *MiscService) CountTotalTime() {
|
2022-12-01 15:46:06 +03:00
|
|
|
logbuch.Info("counting users total time")
|
|
|
|
if ok := countLock.TryLock(); !ok {
|
|
|
|
config.Log().Warn("couldn't acquire lock for counting users total time, job is still pending")
|
|
|
|
}
|
|
|
|
defer countLock.Unlock()
|
|
|
|
|
2021-04-30 18:19:17 +03:00
|
|
|
users, err := srv.userService.GetAll()
|
|
|
|
if err != nil {
|
2022-11-20 12:12:34 +03:00
|
|
|
config.Log().Error("failed to fetch users for time counting, %v", err)
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
|
|
|
|
2022-12-01 15:46:06 +03:00
|
|
|
var totalTime = atomic.NewDuration(0)
|
2022-11-20 12:10:24 +03:00
|
|
|
var pendingJobs sync.WaitGroup
|
|
|
|
pendingJobs.Add(len(users))
|
2021-01-17 11:24:09 +03:00
|
|
|
|
2021-04-30 18:19:17 +03:00
|
|
|
for _, u := range users {
|
2022-12-01 16:13:52 +03:00
|
|
|
user := *u
|
2022-11-20 12:10:24 +03:00
|
|
|
if err := srv.queueWorkers.Dispatch(func() {
|
|
|
|
defer pendingJobs.Done()
|
2022-12-01 16:13:52 +03:00
|
|
|
totalTime.Add(srv.countUserTotalTime(user.ID))
|
2022-11-20 12:10:24 +03:00
|
|
|
}); err != nil {
|
2022-12-01 16:13:52 +03:00
|
|
|
config.Log().Error("failed to enqueue counting job for user '%s'", user.ID)
|
2022-11-20 12:10:24 +03:00
|
|
|
pendingJobs.Done()
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:19:17 +03:00
|
|
|
// persist
|
2022-11-20 12:10:24 +03:00
|
|
|
go func(wg *sync.WaitGroup) {
|
2022-12-01 15:46:06 +03:00
|
|
|
if !utils.WaitTimeout(&pendingJobs, 2*countUsersEvery) {
|
2022-11-20 12:10:24 +03:00
|
|
|
if err := srv.keyValueService.PutString(&models.KeyStringValue{
|
|
|
|
Key: config.KeyLatestTotalTime,
|
2022-12-01 15:46:06 +03:00
|
|
|
Value: totalTime.Load().String(),
|
2022-11-20 12:10:24 +03:00
|
|
|
}); err != nil {
|
2022-11-20 12:12:34 +03:00
|
|
|
config.Log().Error("failed to save total time count: %v", err)
|
2022-11-20 12:10:24 +03:00
|
|
|
}
|
2021-04-30 18:19:17 +03:00
|
|
|
|
2022-11-20 12:10:24 +03:00
|
|
|
if err := srv.keyValueService.PutString(&models.KeyStringValue{
|
|
|
|
Key: config.KeyLatestTotalUsers,
|
|
|
|
Value: strconv.Itoa(len(users)),
|
|
|
|
}); err != nil {
|
2022-11-20 12:12:34 +03:00
|
|
|
config.Log().Error("failed to save total users count: %v", err)
|
2021-04-30 18:19:17 +03:00
|
|
|
}
|
2022-11-20 12:10:24 +03:00
|
|
|
} else {
|
|
|
|
config.Log().Error("waiting for user counting jobs timed out")
|
2021-04-30 18:19:17 +03:00
|
|
|
}
|
2022-11-20 12:10:24 +03:00
|
|
|
}(&pendingJobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *MiscService) countUserTotalTime(userId string) time.Duration {
|
|
|
|
result, err := srv.summaryService.Aliased(time.Time{}, time.Now(), &models.User{ID: userId}, srv.summaryService.Retrieve, nil, false)
|
|
|
|
if err != nil {
|
|
|
|
config.Log().Error("failed to count total for user %s: %v", userId, err)
|
|
|
|
return 0
|
2021-04-30 18:19:17 +03:00
|
|
|
}
|
2022-11-20 12:10:24 +03:00
|
|
|
return result.TotalTime()
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|