mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-21 21:33:02 +01:00
Clean up error handling, add uptime to analytics
This commit is contained in:
@@ -404,11 +404,11 @@ func run(cfg *config.Config) error {
|
|||||||
if cfg.Options.AllowAnalytics {
|
if cfg.Options.AllowAnalytics {
|
||||||
runner.AddPlugin(NewTask("send-analytics", time.Duration(24)*time.Hour, func(ctx context.Context) {
|
runner.AddPlugin(NewTask("send-analytics", time.Duration(24)*time.Hour, func(ctx context.Context) {
|
||||||
log.Debug().Msg("running send analytics")
|
log.Debug().Msg("running send analytics")
|
||||||
analytics.Send(version, build())
|
err := analytics.Send(version, build())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().
|
log.Error().
|
||||||
Err(err).
|
Err(err).
|
||||||
Msg("failed to send analytics")
|
Msg("failed to send scheduled analytics")
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,18 +34,19 @@ 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_sec": time.Since(startTime).Seconds(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
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")
|
||||||
@@ -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