Daily Analytics (#896)

* Send analytics daily

* Clean up error handling, add uptime to analytics

* Better analytics scheduling

* Even better logic for scheduling the analytics (hopefully)

* Some cleanup

* Switch to minutes for uptime, remove duplicate event on startup
This commit is contained in:
Matt
2025-07-15 04:24:19 -04:00
committed by GitHub
parent bd79ee3227
commit 90bb6ed1fe
2 changed files with 34 additions and 6 deletions

View File

@@ -6,8 +6,10 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/pressly/goose/v3"
"github.com/sysadminsmedia/homebox/backend/internal/sys/analytics"
"net/http"
"strings"
"time"
"github.com/hay-kot/httpkit/errchain"
"github.com/hay-kot/httpkit/graceful"
@@ -98,7 +100,6 @@ func run(cfg *config.Config) error {
// =========================================================================
// Initialize Database & Repos
setupStorageDir(cfg)
if strings.ToLower(cfg.Database.Driver) == "postgres" {
@@ -196,5 +197,28 @@ func run(cfg *config.Config) error {
// Start Reoccurring Tasks
registerRecurringTasks(app, cfg, runner)
// Send analytics if enabled at around midnight UTC
if cfg.Options.AllowAnalytics {
analyticsTime := time.Second
runner.AddPlugin(NewTask("send-analytics", analyticsTime, func(ctx context.Context) {
for {
now := time.Now().UTC()
nextMidnight := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, time.UTC)
dur := time.Until(nextMidnight)
analyticsTime = dur
select {
case <-ctx.Done():
return
case <-time.After(dur):
log.Debug().Msg("running send analytics")
err := analytics.Send(version, build())
if err != nil {
log.Error().Err(err).Msg("failed to send analytics")
}
}
}
}))
}
return runner.Start(context.Background())
}