From 0187d9b1bb58cab915bd7e043ec6890426547662 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Fri, 30 Aug 2024 00:09:33 +0700 Subject: [PATCH] History in DB switch --- internal/conf/getconfig.go | 3 +++ internal/models/models.go | 1 + internal/web/api.go | 4 +++- internal/web/config.go | 7 +++++++ internal/web/const-var.go | 4 ++-- internal/web/scan-routine.go | 5 ++++- internal/web/templates/config.html | 25 +++++++++++++++++++------ internal/web/trim-history.go | 23 ++++++++++++++++++++--- 8 files changed, 59 insertions(+), 13 deletions(-) diff --git a/internal/conf/getconfig.go b/internal/conf/getconfig.go index 8318f15..e5953cb 100644 --- a/internal/conf/getconfig.go +++ b/internal/conf/getconfig.go @@ -20,6 +20,7 @@ func Get(path string) (config models.Conf) { viper.SetDefault("IFACES", "") viper.SetDefault("TIMEOUT", 120) viper.SetDefault("TRIM_HIST", 48) + viper.SetDefault("HIST_IN_DB", false) viper.SetDefault("SHOUTRRR_URL", "") viper.SetDefault("USE_DB", "sqlite") @@ -44,6 +45,7 @@ func Get(path string) (config models.Conf) { config.Ifaces = viper.Get("IFACES").(string) config.Timeout = viper.GetInt("TIMEOUT") config.TrimHist = viper.GetInt("TRIM_HIST") + config.HistInDB = viper.GetBool("HIST_IN_DB") config.ShoutURL = viper.Get("SHOUTRRR_URL").(string) config.UseDB = viper.Get("USE_DB").(string) @@ -75,6 +77,7 @@ func Write(config models.Conf) { viper.Set("IFACES", config.Ifaces) viper.Set("TIMEOUT", config.Timeout) viper.Set("TRIM_HIST", config.TrimHist) + viper.Set("HIST_IN_DB", config.HistInDB) viper.Set("SHOUTRRR_URL", config.ShoutURL) viper.Set("USE_DB", config.UseDB) diff --git a/internal/models/models.go b/internal/models/models.go index b1c67fa..d5405bd 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -15,6 +15,7 @@ type Conf struct { ArpArgs string Timeout int TrimHist int + HistInDB bool ShoutURL string // PostgreSQL UseDB string diff --git a/internal/web/api.go b/internal/web/api.go index 8745b1f..6f0ec6f 100644 --- a/internal/web/api.go +++ b/internal/web/api.go @@ -21,7 +21,9 @@ func apiAll(c *gin.Context) { func apiHistory(c *gin.Context) { var hosts []models.Host - histHosts := db.Select("history") + if appConfig.HistInDB { + histHosts = db.Select("history") + } mac := c.Param("mac") diff --git a/internal/web/config.go b/internal/web/config.go index a539f0f..2312224 100644 --- a/internal/web/config.go +++ b/internal/web/config.go @@ -58,6 +58,13 @@ func saveSettingsHandler(c *gin.Context) { appConfig.Timeout, _ = strconv.Atoi(timeout) appConfig.TrimHist, _ = strconv.Atoi(trimHist) + histdb := c.PostForm("histdb") + if histdb == "on" { + appConfig.HistInDB = true + } else { + appConfig.HistInDB = false + } + conf.Write(appConfig) slog.Info("Writing new config to " + appConfig.ConfPath) diff --git a/internal/web/const-var.go b/internal/web/const-var.go index e8cbf92..7cc526e 100644 --- a/internal/web/const-var.go +++ b/internal/web/const-var.go @@ -10,8 +10,8 @@ var ( // appConfig - config for Web Gui appConfig models.Conf - allHosts []models.Host - // histHosts []models.Host + allHosts []models.Host + histHosts []models.Host quitScan chan bool ) diff --git a/internal/web/scan-routine.go b/internal/web/scan-routine.go index ca82e94..d3b0c22 100644 --- a/internal/web/scan-routine.go +++ b/internal/web/scan-routine.go @@ -64,7 +64,10 @@ func compareHosts(foundHosts []models.Host) { db.Update("now", aHost) aHost.Date = time.Now().Format("2006-01-02 15:04:05") - db.Insert("history", aHost) + histHosts = append(histHosts, aHost) + if appConfig.HistInDB { + db.Insert("history", aHost) + } if appConfig.InfluxEnable { influx.Add(appConfig, aHost) } diff --git a/internal/web/templates/config.html b/internal/web/templates/config.html index 1355e6f..b73762e 100644 --- a/internal/web/templates/config.html +++ b/internal/web/templates/config.html @@ -70,10 +70,6 @@ Args for arp-scan - - Trim History (hours) - - Log level + + Trim History (hours) + + + + Store History in DB + +
+ {{ if .Config.HistInDB }} + + {{ else }} + + {{ end }} +
+ + Use DB