2021-01-17 11:24:09 +03:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2021-01-30 13:17:37 +03:00
|
|
|
"github.com/emvi/logbuch"
|
2021-01-17 11:24:09 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-co-op/gocron"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MiscService struct {
|
|
|
|
config *config.Config
|
|
|
|
userService IUserService
|
|
|
|
summaryService ISummaryService
|
|
|
|
keyValueService IKeyValueService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMiscService(userService IUserService, summaryService ISummaryService, keyValueService IKeyValueService) *MiscService {
|
|
|
|
return &MiscService{
|
|
|
|
config: config.Get(),
|
|
|
|
userService: userService,
|
|
|
|
summaryService: summaryService,
|
|
|
|
keyValueService: keyValueService,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CountTotalTimeJob struct {
|
|
|
|
UserID string
|
|
|
|
NumJobs int
|
|
|
|
}
|
|
|
|
|
|
|
|
type CountTotalTimeResult struct {
|
|
|
|
UserId string
|
|
|
|
Total time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *MiscService) ScheduleCountTotalTime() {
|
|
|
|
// Run once initially
|
|
|
|
if err := srv.runCountTotalTime(); err != nil {
|
2021-04-16 16:59:39 +03:00
|
|
|
logbuch.Fatal("failed to run CountTotalTimeJob: %v", err)
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s := gocron.NewScheduler(time.Local)
|
2021-02-12 20:37:30 +03:00
|
|
|
s.Every(1).Hour().Do(srv.runCountTotalTime)
|
2021-01-17 11:24:09 +03:00
|
|
|
s.StartBlocking()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *MiscService) runCountTotalTime() error {
|
2021-04-30 18:19:17 +03:00
|
|
|
users, err := srv.userService.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
|
|
|
|
2021-04-30 18:19:17 +03:00
|
|
|
jobs := make(chan *CountTotalTimeJob, len(users))
|
|
|
|
results := make(chan *CountTotalTimeResult, len(users))
|
2021-01-17 11:24:09 +03:00
|
|
|
|
2021-04-30 18:19:17 +03:00
|
|
|
for _, u := range users {
|
|
|
|
jobs <- &CountTotalTimeJob{
|
|
|
|
UserID: u.ID,
|
|
|
|
NumJobs: len(users),
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-30 18:19:17 +03:00
|
|
|
close(jobs)
|
2021-01-17 11:24:09 +03:00
|
|
|
|
2021-04-30 18:19:17 +03:00
|
|
|
for i := 0; i < runtime.NumCPU(); i++ {
|
|
|
|
go srv.countTotalTimeWorker(jobs, results)
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
|
|
|
|
2021-04-30 18:19:17 +03:00
|
|
|
// persist
|
|
|
|
var i int
|
2021-01-17 11:24:09 +03:00
|
|
|
var total time.Duration
|
2021-04-30 18:19:17 +03:00
|
|
|
for i = 0; i < len(users); i++ {
|
|
|
|
result := <-results
|
2021-01-17 11:24:09 +03:00
|
|
|
total += result.Total
|
|
|
|
}
|
2021-04-30 18:19:17 +03:00
|
|
|
close(results)
|
2021-01-17 11:24:09 +03:00
|
|
|
|
|
|
|
if err := srv.keyValueService.PutString(&models.KeyStringValue{
|
|
|
|
Key: config.KeyLatestTotalTime,
|
|
|
|
Value: total.String(),
|
|
|
|
}); err != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Error("failed to save total time count: %v", err)
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.keyValueService.PutString(&models.KeyStringValue{
|
|
|
|
Key: config.KeyLatestTotalUsers,
|
2021-04-30 18:19:17 +03:00
|
|
|
Value: strconv.Itoa(i),
|
2021-01-17 11:24:09 +03:00
|
|
|
}); err != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Error("failed to save total users count: %v", err)
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|
2021-04-30 18:19:17 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *MiscService) countTotalTimeWorker(jobs <-chan *CountTotalTimeJob, results chan<- *CountTotalTimeResult) {
|
|
|
|
for job := range jobs {
|
|
|
|
if result, err := srv.summaryService.Aliased(time.Time{}, time.Now(), &models.User{ID: job.UserID}, srv.summaryService.Retrieve, false); err != nil {
|
|
|
|
config.Log().Error("failed to count total for user %s: %v", job.UserID, err)
|
|
|
|
} else {
|
|
|
|
results <- &CountTotalTimeResult{
|
|
|
|
UserId: job.UserID,
|
|
|
|
Total: result.TotalTime(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 11:24:09 +03:00
|
|
|
}
|