A continuation of HomeBox the inventory and organization system built for the Home User
Mk/simplified analytics (#596)
Matt Kilgore5e2e8b66d89f
diff --git a/backend/app/api/main.go b/backend/app/api/main.goindex bc16d508..094777a6 100644--- a/backend/app/api/main.go+++ b/backend/app/api/main.go@@ -3,10 +3,9 @@ package mainimport ("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())}// =========================================================================diff --git a/backend/internal/sys/analytics/analytics.go b/backend/internal/sys/analytics/analytics.gonew file mode 100644index 00000000..8b88d348--- /dev/null+++ b/backend/internal/sys/analytics/analytics.go@@ -0,0 +1,68 @@+// Package analytics provides analytics function that sends data to a remote server.+package analytics++import (+ "bytes"+ "encoding/json"+ "github.com/shirou/gopsutil/v4/host"+ "net/http"+ "time"++ "github.com/rs/zerolog/log"+)++type Data struct {+ Domain string `json:"domain"`+ Name string `json:"name"`+ URL string `json:"url"`+ Props map[string]interface{} `json:"props"`+}++func Send(version, buildInfo string) {+ hostData, _ := host.Info()+ analytics := Data{+ Domain: "homebox.software",+ URL: "https://homebox.software/stats",+ Name: "stats",+ Props: map[string]interface{}{+ "version": version + "/" + buildInfo,+ "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")+ return+ }+ 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")+ return+ }++ req.Header.Set("Content-Type", "application/json")+ req.Header.Set("User-Agent", "Homebox/"+version+"/"+buildInfo+" (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")+ return+ }++ defer func() {+ err := res.Body.Close()+ if err != nil {+ log.Error().Err(err).Msg("failed to close response body")+ }+ }()+}