From 5e5d12c8e2d46321be96363ef5d724a4a4850c84 Mon Sep 17 00:00:00 2001 From: Matt Kilgore Date: Fri, 4 Oct 2024 16:21:18 -0400 Subject: [PATCH] feat: Initial Analytics stuff --- backend/app/api/main.go | 42 ++++++++++++++++++++++++++ backend/go.mod | 1 + backend/go.sum | 4 +++ backend/internal/sys/config/conf.go | 1 + frontend/components/Form/TextField.vue | 1 - 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/backend/app/api/main.go b/backend/app/api/main.go index 6247fe6ad..0c9408127 100644 --- a/backend/app/api/main.go +++ b/backend/app/api/main.go @@ -3,7 +3,9 @@ package main import ( "bytes" "context" + "encoding/json" "fmt" + "github.com/shirou/gopsutil/v4/host" "net/http" "os" "path/filepath" @@ -19,6 +21,7 @@ import ( "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/rs/zerolog/pkgerrors" + _ "github.com/shirou/gopsutil/v4/host" "github.com/sysadminsmedia/homebox/backend/internal/core/currencies" "github.com/sysadminsmedia/homebox/backend/internal/core/services" "github.com/sysadminsmedia/homebox/backend/internal/core/services/reporting/eventbus" @@ -72,6 +75,45 @@ func run(cfg *config.Config) error { app := new(cfg) 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", + Name: "stats", + Props: map[string]interface{}{ + "os": hostData.OS, + "platform": hostData.Platform, + "platform_family": hostData.PlatformFamily, + "platform_version": hostData.PlatformVersion, + "kernel_arch": hostData.KernelArch, + }, + } + 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) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.284") + client := &http.Client{ + Timeout: 10 * time.Second, + } + res, err := client.Do(req) + if err != nil { + return err + } + log.Info().Msgf("Analytics Response: %v", res) + log.Info().Msgf("Analytics Response: %v", json.NewEncoder(os.Stdout).Encode(analytics)) + } + // ========================================================================= // Initialize Database & Repos diff --git a/backend/go.mod b/backend/go.mod index b2f2d597b..ba70fe03a 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -22,6 +22,7 @@ require ( github.com/swaggo/swag v1.16.3 github.com/yeqown/go-qrcode/v2 v2.2.4 github.com/yeqown/go-qrcode/writer/standard v1.2.4 + github.com/shirou/gopsutil/v4 v4.24.9 golang.org/x/crypto v0.28.0 modernc.org/sqlite v1.33.1 ) diff --git a/backend/go.sum b/backend/go.sum index f9a83abcd..bdee680eb 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -148,6 +148,10 @@ github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= +github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/shirou/gopsutil/v4 v4.24.9 h1:KIV+/HaHD5ka5f570RZq+2SaeFsb/pq+fp2DGNWYoOI= +github.com/shirou/gopsutil/v4 v4.24.9/go.mod h1:3fkaHNeYsUFCGZ8+9vZVWtbyM1k2eRnlL+bWO8Bxa/Q= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= diff --git a/backend/internal/sys/config/conf.go b/backend/internal/sys/config/conf.go index 8b7b23c3c..e3c4b830a 100644 --- a/backend/internal/sys/config/conf.go +++ b/backend/internal/sys/config/conf.go @@ -32,6 +32,7 @@ type Options struct { AllowRegistration bool `yaml:"disable_registration" conf:"default:true"` AutoIncrementAssetID bool `yaml:"auto_increment_asset_id" conf:"default:true"` CurrencyConfig string `yaml:"currencies"` + AllowAnalytics bool `yaml:"allow_analytics" conf:"default:true"` } type DebugConf struct { diff --git a/frontend/components/Form/TextField.vue b/frontend/components/Form/TextField.vue index b3db86a33..d1aa2e46b 100644 --- a/frontend/components/Form/TextField.vue +++ b/frontend/components/Form/TextField.vue @@ -12,7 +12,6 @@ {{ typeof value === "string" && (maxLength || minLength) ? `${value.length}/${maxLength}` : "" }} - {{ value }}