2021-04-30 15:07:14 +03:00
|
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/emvi/logbuch"
|
|
|
|
|
"github.com/go-co-op/gocron"
|
2021-04-30 17:20:08 +03:00
|
|
|
|
"github.com/leandro-lugaresi/hub"
|
2021-04-30 15:07:14 +03:00
|
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
|
"github.com/muety/wakapi/models"
|
2021-05-04 22:04:11 +03:00
|
|
|
|
"math/rand"
|
2021-04-30 15:07:14 +03:00
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var reportLock = sync.Mutex{}
|
|
|
|
|
|
2021-05-04 22:04:11 +03:00
|
|
|
|
// range for random offset to add / subtract when scheduling a new job
|
|
|
|
|
// to avoid all mails being sent at once, but distributed over 2*offsetIntervalMin minutes
|
|
|
|
|
const offsetIntervalMin = 15
|
|
|
|
|
|
2021-04-30 15:07:14 +03:00
|
|
|
|
type ReportService struct {
|
2021-05-04 22:04:11 +03:00
|
|
|
|
config *config.Config
|
|
|
|
|
eventBus *hub.Hub
|
|
|
|
|
summaryService ISummaryService
|
|
|
|
|
userService IUserService
|
|
|
|
|
mailService IMailService
|
|
|
|
|
scheduler *gocron.Scheduler
|
|
|
|
|
rand *rand.Rand
|
2021-04-30 15:07:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewReportService(summaryService ISummaryService, userService IUserService, mailService IMailService) *ReportService {
|
2021-04-30 17:20:08 +03:00
|
|
|
|
srv := &ReportService{
|
2021-05-04 22:04:11 +03:00
|
|
|
|
config: config.Get(),
|
|
|
|
|
eventBus: config.EventBus(),
|
|
|
|
|
summaryService: summaryService,
|
|
|
|
|
userService: userService,
|
|
|
|
|
mailService: mailService,
|
|
|
|
|
scheduler: gocron.NewScheduler(time.Local),
|
|
|
|
|
rand: rand.New(rand.NewSource(time.Now().Unix())),
|
2021-04-30 15:07:14 +03:00
|
|
|
|
}
|
2021-04-30 17:20:08 +03:00
|
|
|
|
|
2021-05-04 22:04:11 +03:00
|
|
|
|
srv.scheduler.StartAsync()
|
|
|
|
|
|
2021-04-30 17:20:08 +03:00
|
|
|
|
sub := srv.eventBus.Subscribe(0, config.EventUserUpdate)
|
|
|
|
|
go func(sub *hub.Subscription) {
|
|
|
|
|
for m := range sub.Receiver {
|
|
|
|
|
srv.SyncSchedule(m.Fields[config.FieldPayload].(*models.User))
|
|
|
|
|
}
|
|
|
|
|
}(&sub)
|
|
|
|
|
|
|
|
|
|
return srv
|
2021-04-30 15:07:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (srv *ReportService) Schedule() {
|
|
|
|
|
logbuch.Info("initializing report service")
|
|
|
|
|
|
|
|
|
|
users, err := srv.userService.GetAllByReports(true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
config.Log().Fatal("%v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logbuch.Info("scheduling reports for %d users", len(users))
|
|
|
|
|
for _, u := range users {
|
2021-04-30 17:20:08 +03:00
|
|
|
|
srv.SyncSchedule(u)
|
2021-04-30 15:07:14 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 17:20:08 +03:00
|
|
|
|
// SyncSchedule syncs the currently active schedulers with the user's wish about whether or not to receive reports.
|
|
|
|
|
// Returns whether a scheduler is active after this operation has run.
|
|
|
|
|
func (srv *ReportService) SyncSchedule(u *models.User) bool {
|
2021-04-30 15:07:14 +03:00
|
|
|
|
reportLock.Lock()
|
|
|
|
|
defer reportLock.Unlock()
|
|
|
|
|
|
|
|
|
|
// unschedule
|
2021-05-04 22:04:11 +03:00
|
|
|
|
if !u.ReportsWeekly {
|
|
|
|
|
_ = srv.scheduler.RemoveByTag(u.ID)
|
2021-12-22 20:33:15 +03:00
|
|
|
|
logbuch.Info("disabled scheduled reports for user %s", u.ID)
|
2021-04-30 17:20:08 +03:00
|
|
|
|
return false
|
2021-04-30 15:07:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// schedule
|
2021-12-22 20:33:15 +03:00
|
|
|
|
if job := srv.getJobByTag(u.ID); job == nil && u.ReportsWeekly {
|
2021-05-04 22:04:11 +03:00
|
|
|
|
t, _ := time.ParseInLocation("15:04", srv.config.App.GetWeeklyReportTime(), u.TZ())
|
2021-12-22 20:33:15 +03:00
|
|
|
|
t = t.Add(time.Duration(srv.rand.Intn(offsetIntervalMin*60)) * time.Second)
|
|
|
|
|
if job, err := srv.scheduler.
|
|
|
|
|
SingletonMode().
|
2021-04-30 15:07:14 +03:00
|
|
|
|
Every(1).
|
|
|
|
|
Week().
|
|
|
|
|
Weekday(srv.config.App.GetWeeklyReportDay()).
|
2021-05-04 22:04:11 +03:00
|
|
|
|
At(t).
|
|
|
|
|
Tag(u.ID).
|
|
|
|
|
Do(srv.Run, u, 7*24*time.Hour); err != nil {
|
|
|
|
|
config.Log().Error("failed to schedule report job for user '%s' – %v", u.ID, err)
|
2021-12-22 20:33:15 +03:00
|
|
|
|
} else {
|
|
|
|
|
logbuch.Info("next report for user %s is scheduled for %v", u.ID, job.NextRun())
|
2021-05-04 22:04:11 +03:00
|
|
|
|
}
|
2021-04-30 15:07:14 +03:00
|
|
|
|
}
|
2021-04-30 17:20:08 +03:00
|
|
|
|
|
|
|
|
|
return u.ReportsWeekly
|
2021-04-30 15:07:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (srv *ReportService) Run(user *models.User, duration time.Duration) error {
|
2021-04-30 17:20:08 +03:00
|
|
|
|
if user.Email == "" {
|
|
|
|
|
logbuch.Warn("not generating report for '%s' as no e-mail address is set")
|
2021-12-22 20:33:15 +03:00
|
|
|
|
return nil
|
2021-04-30 17:20:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !srv.SyncSchedule(user) {
|
|
|
|
|
logbuch.Info("reports for user '%s' were turned off in the meanwhile since last report job ran")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 15:07:14 +03:00
|
|
|
|
end := time.Now().In(user.TZ())
|
|
|
|
|
start := time.Now().Add(-1 * duration)
|
|
|
|
|
|
2021-12-26 19:02:14 +03:00
|
|
|
|
summary, err := srv.summaryService.Aliased(start, end, user, srv.summaryService.Retrieve, nil, false)
|
2021-04-30 15:07:14 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
config.Log().Error("failed to generate report for '%s' – %v", user.ID, err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
report := &models.Report{
|
|
|
|
|
From: start,
|
|
|
|
|
To: end,
|
|
|
|
|
User: user,
|
|
|
|
|
Summary: summary,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := srv.mailService.SendReport(user, report); err != nil {
|
|
|
|
|
config.Log().Error("failed to send report for '%s' – %v", user.ID, err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logbuch.Info("sent report to user '%s'", user.ID)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-05-04 22:04:11 +03:00
|
|
|
|
|
|
|
|
|
func (srv *ReportService) getJobByTag(tag string) *gocron.Job {
|
|
|
|
|
for _, j := range srv.scheduler.Jobs() {
|
|
|
|
|
for _, t := range j.Tags() {
|
|
|
|
|
if t == tag {
|
|
|
|
|
return j
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|