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

124 lines
3.4 KiB
Go
Raw Normal View History

2021-04-30 15:07:14 +03:00
package services
import (
"github.com/duke-git/lancet/v2/slice"
2021-04-30 15:07:14 +03:00
"github.com/emvi/logbuch"
2021-04-30 17:20:08 +03:00
"github.com/leandro-lugaresi/hub"
2022-12-01 17:31:19 +03:00
"github.com/muety/artifex/v2"
2021-04-30 15:07:14 +03:00
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"math/rand"
2021-04-30 15:07:14 +03:00
"time"
)
// delay between evey report generation task (to throttle email sending frequency)
2022-12-01 12:57:51 +03:00
const reportDelay = 10 * time.Second
2021-04-30 15:07:14 +03:00
// past time range to cover in the report
const reportRange = 7 * 24 * time.Hour
2021-04-30 15:07:14 +03:00
type ReportService struct {
config *config.Config
eventBus *hub.Hub
summaryService ISummaryService
userService IUserService
mailService IMailService
rand *rand.Rand
queueDefault *artifex.Dispatcher
queueWorkers *artifex.Dispatcher
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{
config: config.Get(),
eventBus: config.EventBus(),
summaryService: summaryService,
userService: userService,
mailService: mailService,
rand: rand.New(rand.NewSource(time.Now().Unix())),
queueDefault: config.GetDefaultQueue(),
queueWorkers: config.GetQueue(config.QueueReports),
2021-04-30 15:07:14 +03:00
}
2021-04-30 17:20:08 +03:00
return srv
2021-04-30 15:07:14 +03:00
}
func (srv *ReportService) Schedule() {
logbuch.Info("scheduling report generation")
2021-04-30 15:07:14 +03:00
2022-12-01 12:57:51 +03:00
scheduleUserReport := func(u *models.User) {
if err := srv.queueWorkers.Dispatch(func() {
t0 := time.Now()
if err := srv.SendReport(u, reportRange); err != nil {
config.Log().Error("failed to generate report for '%s', %v", u.ID, err)
}
2022-12-01 12:57:51 +03:00
// make the job take at least reportDelay seconds
if diff := reportDelay - time.Now().Sub(t0); diff > 0 {
logbuch.Debug("waiting for %v before sending next report", diff)
time.Sleep(diff)
}
}); err != nil {
config.Log().Error("failed to dispatch report generation job for user '%s', %v", u.ID, err)
}
}
_, err := srv.queueDefault.DispatchCron(func() {
// fetch all users with reports enabled
users, err := srv.userService.GetAllByReports(true)
if err != nil {
config.Log().Error("failed to get users for report generation, %v", err)
return
}
2021-04-30 15:07:14 +03:00
// filter users who have their email set
users = slice.Filter[*models.User](users, func(i int, u *models.User) bool {
return u.Email != ""
})
// schedule jobs, throttled by one job per x seconds
logbuch.Info("scheduling report generation for %d users", len(users))
2022-12-01 12:57:51 +03:00
for _, u := range users {
scheduleUserReport(u)
}
}, srv.config.App.GetWeeklyReportCron())
2021-04-30 17:20:08 +03:00
if err != nil {
config.Log().Error("failed to dispatch report generation jobs, %v", err)
}
2021-04-30 15:07:14 +03:00
}
func (srv *ReportService) SendReport(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")
return nil
2021-04-30 17:20:08 +03:00
}
logbuch.Info("generating report for '%s'", user.ID)
2021-04-30 17:20:08 +03:00
2021-04-30 15:07:14 +03:00
end := time.Now().In(user.TZ())
start := time.Now().Add(-1 * duration)
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)
2021-04-30 15:07:14 +03:00
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)
2021-04-30 15:07:14 +03:00
return err
}
logbuch.Info("sent report to user '%s'", user.ID)
return nil
}