1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00
wakapi/services/aggregation.go

152 lines
3.5 KiB
Go
Raw Normal View History

package services
import (
2019-10-10 17:47:19 +03:00
"log"
2019-10-11 10:06:34 +03:00
"runtime"
"time"
2019-10-11 10:06:34 +03:00
"github.com/jasonlvhit/gocron"
"github.com/jinzhu/gorm"
"github.com/n1try/wakapi/models"
)
2019-10-10 17:47:19 +03:00
const (
intervalDays int = 1 // TODO: Make configurable
2019-10-10 17:47:19 +03:00
)
type AggregationService struct {
Config *models.Config
Db *gorm.DB
UserService *UserService
SummaryService *SummaryService
HeartbeatService *HeartbeatService
}
type AggregationJob struct {
2019-10-10 17:47:19 +03:00
UserID string
From time.Time
To time.Time
}
2020-02-20 16:28:55 +03:00
func (srv *AggregationService) Init() {}
2019-10-11 10:06:34 +03:00
// Schedule a job to (re-)generate summaries every day shortly after midnight
// TODO: Make configurable
func (srv *AggregationService) Schedule() {
2019-10-10 17:47:19 +03:00
jobs := make(chan *AggregationJob)
summaries := make(chan *models.Summary)
2019-10-11 10:06:34 +03:00
defer close(jobs)
defer close(summaries)
2019-10-10 17:47:19 +03:00
2019-10-11 10:06:34 +03:00
for i := 0; i < runtime.NumCPU(); i++ {
2019-10-10 17:47:19 +03:00
go srv.summaryWorker(jobs, summaries)
}
2019-10-11 10:06:34 +03:00
for i := 0; i < int(srv.Config.DbMaxConn); i++ {
2019-10-10 17:47:19 +03:00
go srv.persistWorker(summaries)
}
// Run once initially
srv.trigger(jobs)
2019-10-11 10:06:34 +03:00
gocron.Every(1).Day().At("02:15").Do(srv.trigger, jobs)
<-gocron.Start()
}
2019-10-10 17:47:19 +03:00
func (srv *AggregationService) summaryWorker(jobs <-chan *AggregationJob, summaries chan<- *models.Summary) {
for job := range jobs {
if summary, err := srv.SummaryService.Construct(job.From, job.To, &models.User{ID: job.UserID}, true); err != nil {
2019-10-11 10:06:34 +03:00
log.Printf("Failed to generate summary (%v, %v, %s) %v.\n", job.From, job.To, job.UserID, err)
2019-10-10 17:47:19 +03:00
} else {
2019-10-11 10:06:34 +03:00
log.Printf("Successfully generated summary (%v, %v, %s).\n", job.From, job.To, job.UserID)
2019-10-10 17:47:19 +03:00
summaries <- summary
}
}
}
2019-10-10 17:47:19 +03:00
func (srv *AggregationService) persistWorker(summaries <-chan *models.Summary) {
for summary := range summaries {
if err := srv.SummaryService.Insert(summary); err != nil {
2019-10-11 10:06:34 +03:00
log.Printf("Failed to save summary (%v, %v, %s) %v.\n", summary.UserID, summary.FromTime, summary.ToTime, err)
2019-10-10 17:47:19 +03:00
}
}
}
2019-10-11 10:06:34 +03:00
func (srv *AggregationService) trigger(jobs chan<- *AggregationJob) error {
log.Println("Generating summaries.")
users, err := srv.UserService.GetAll()
if err != nil {
2019-10-11 10:06:34 +03:00
log.Println(err)
2019-10-10 17:47:19 +03:00
return err
}
latestSummaries, err := srv.SummaryService.GetLatestByUser()
if err != nil {
2019-10-11 10:06:34 +03:00
log.Println(err)
2019-10-10 17:47:19 +03:00
return err
}
userSummaryTimes := make(map[string]time.Time)
for _, s := range latestSummaries {
userSummaryTimes[s.UserID] = s.ToTime
}
2019-10-10 17:47:19 +03:00
missingUserIDs := make([]string, 0)
for _, u := range users {
if _, ok := userSummaryTimes[u.ID]; !ok {
2019-10-10 17:47:19 +03:00
missingUserIDs = append(missingUserIDs, u.ID)
}
}
2019-10-10 17:47:19 +03:00
firstHeartbeats, err := srv.HeartbeatService.GetFirstUserHeartbeats(missingUserIDs)
if err != nil {
2019-10-11 10:06:34 +03:00
log.Println(err)
2019-10-10 17:47:19 +03:00
return err
}
for id, t := range userSummaryTimes {
generateUserJobs(id, t, jobs)
}
for _, h := range firstHeartbeats {
generateUserJobs(h.UserID, time.Time(h.Time), jobs)
2019-10-10 17:47:19 +03:00
}
2019-10-10 17:47:19 +03:00
return nil
}
func generateUserJobs(userId string, lastAggregation time.Time, jobs chan<- *AggregationJob) {
var from, to time.Time
end := getStartOfToday().Add(-1 * time.Second)
if lastAggregation.Hour() == 0 {
from = lastAggregation
} else {
from = time.Date(
lastAggregation.Year(),
lastAggregation.Month(),
lastAggregation.Day()+intervalDays,
0, 0, 0, 0,
lastAggregation.Location(),
)
}
2019-10-10 17:47:19 +03:00
for from.Before(end) && to.Before(end) {
to = time.Date(
from.Year(),
from.Month(),
from.Day()+intervalDays,
0, 0, 0, 0,
from.Location(),
)
2019-10-10 17:47:19 +03:00
jobs <- &AggregationJob{userId, from, to}
from = to
}
}
func getStartOfToday() time.Time {
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 1, now.Location())
}