mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-24 06:28:34 +01:00
Mk/simplified analytics (#596)
* chore: move analytics to it's own seperate file and function. * chore: fix linting
This commit is contained in:
@@ -3,10 +3,9 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/shirou/gopsutil/v4/host"
|
||||
"github.com/sysadminsmedia/homebox/backend/internal/sys/analytics"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -92,49 +91,7 @@ func run(cfg *config.Config) error {
|
||||
app.setupLogger()
|
||||
|
||||
if cfg.Options.AllowAnalytics {
|
||||
type analyticsData struct {
|
||||
Domain string `json:"domain"`
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
Props map[string]interface{} `json:"props"`
|
||||
}
|
||||
hostData, _ := host.Info()
|
||||
analytics := analyticsData{
|
||||
Domain: "homebox.software",
|
||||
URL: "https://homebox.software/stats",
|
||||
Name: "stats",
|
||||
Props: map[string]interface{}{
|
||||
"version": version + "/" + build(),
|
||||
"os": hostData.OS,
|
||||
"platform": hostData.Platform,
|
||||
"platform_family": hostData.PlatformFamily,
|
||||
"platform_version": hostData.PlatformVersion,
|
||||
"kernel_arch": hostData.KernelArch,
|
||||
"virt_type": hostData.VirtualizationSystem,
|
||||
},
|
||||
}
|
||||
jsonBody, err := json.Marshal(analytics)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to marshal analytics data")
|
||||
}
|
||||
bodyReader := bytes.NewReader(jsonBody)
|
||||
req, err := http.NewRequest("POST", "https://a.sysadmins.zone/api/event", bodyReader)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to create analytics request")
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", "Homebox/"+version+"/"+build()+" (https://homebox.software)")
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to send analytics request")
|
||||
}
|
||||
err = res.Body.Close()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to send analytics request")
|
||||
}
|
||||
analytics.Send(version, build())
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
|
||||
Reference in New Issue
Block a user