mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-21 21:33:02 +01:00
Compare commits
7 Commits
main
...
mk/daily-a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e47a7ff6f2 | ||
|
|
57385f9ada | ||
|
|
94df40b718 | ||
|
|
c9cb1abdd9 | ||
|
|
76d7b3d6ca | ||
|
|
e5ba3bb10e | ||
|
|
a73d5887c7 |
@@ -6,8 +6,10 @@ import (
|
|||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
"github.com/pressly/goose/v3"
|
"github.com/pressly/goose/v3"
|
||||||
|
"github.com/sysadminsmedia/homebox/backend/internal/sys/analytics"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hay-kot/httpkit/errchain"
|
"github.com/hay-kot/httpkit/errchain"
|
||||||
"github.com/hay-kot/httpkit/graceful"
|
"github.com/hay-kot/httpkit/graceful"
|
||||||
@@ -98,7 +100,6 @@ func run(cfg *config.Config) error {
|
|||||||
|
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
// Initialize Database & Repos
|
// Initialize Database & Repos
|
||||||
|
|
||||||
setupStorageDir(cfg)
|
setupStorageDir(cfg)
|
||||||
|
|
||||||
if strings.ToLower(cfg.Database.Driver) == "postgres" {
|
if strings.ToLower(cfg.Database.Driver) == "postgres" {
|
||||||
@@ -196,5 +197,28 @@ func run(cfg *config.Config) error {
|
|||||||
// Start Reoccurring Tasks
|
// Start Reoccurring Tasks
|
||||||
registerRecurringTasks(app, cfg, runner)
|
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())
|
return runner.Start(context.Background())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var startTime = time.Now()
|
||||||
|
|
||||||
type Data struct {
|
type Data struct {
|
||||||
Domain string `json:"domain"`
|
Domain string `json:"domain"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -18,7 +20,7 @@ type Data struct {
|
|||||||
Props map[string]interface{} `json:"props"`
|
Props map[string]interface{} `json:"props"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Send(version, buildInfo string) {
|
func Send(version, buildInfo string) error {
|
||||||
hostData, _ := host.Info()
|
hostData, _ := host.Info()
|
||||||
analytics := Data{
|
analytics := Data{
|
||||||
Domain: "homebox.software",
|
Domain: "homebox.software",
|
||||||
@@ -32,22 +34,23 @@ func Send(version, buildInfo string) {
|
|||||||
"platform_version": hostData.PlatformVersion,
|
"platform_version": hostData.PlatformVersion,
|
||||||
"kernel_arch": hostData.KernelArch,
|
"kernel_arch": hostData.KernelArch,
|
||||||
"virt_type": hostData.VirtualizationSystem,
|
"virt_type": hostData.VirtualizationSystem,
|
||||||
|
"uptime_min": time.Since(startTime).Minutes(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
jsonBody, err := json.Marshal(analytics)
|
jsonBody, err := json.Marshal(analytics)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("failed to marshal analytics data")
|
log.Error().Err(err).Msg("failed to marshal analytics data")
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
bodyReader := bytes.NewReader(jsonBody)
|
bodyReader := bytes.NewReader(jsonBody)
|
||||||
req, err := http.NewRequest("POST", "https://a.sysadmins.zone/api/event", bodyReader)
|
req, err := http.NewRequest("POST", "https://a.sysadmins.zone/api/event", bodyReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("failed to create analytics request")
|
log.Error().Err(err).Msg("failed to create analytics request")
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("User-Agent", "Homebox/"+version+"/"+buildInfo+" (https://homebox.software)")
|
req.Header.Set("User-Agent", "Homebox/"+version+"/(https://homebox.software)")
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: 10 * time.Second,
|
Timeout: 10 * time.Second,
|
||||||
@@ -56,7 +59,7 @@ func Send(version, buildInfo string) {
|
|||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("failed to send analytics request")
|
log.Error().Err(err).Msg("failed to send analytics request")
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -65,4 +68,5 @@ func Send(version, buildInfo string) {
|
|||||||
log.Error().Err(err).Msg("failed to close response body")
|
log.Error().Err(err).Msg("failed to close response body")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user