From 95e2f2a4de4c91cbaccc4ea71d587ca127821e0c Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:57:23 +0700 Subject: [PATCH] Search index page --- internal/conf/getconfig.go | 10 ++++ internal/models/models.go | 32 +++++++----- internal/web/config.go | 3 ++ internal/web/public/js/filter-search.js | 68 +++++++++++++++++++++++++ internal/web/public/js/filter.js | 32 ------------ internal/web/templates/config.html | 14 +++++ internal/web/templates/index.html | 15 ++++-- internal/web/webgui.go | 2 +- 8 files changed, 125 insertions(+), 51 deletions(-) create mode 100644 internal/web/public/js/filter-search.js delete mode 100644 internal/web/public/js/filter.js diff --git a/internal/conf/getconfig.go b/internal/conf/getconfig.go index 1f18597..0044c9c 100644 --- a/internal/conf/getconfig.go +++ b/internal/conf/getconfig.go @@ -21,6 +21,10 @@ func Get(path string) (config models.Conf) { viper.SetDefault("TIMEOUT", 60) viper.SetDefault("TRIM_HIST", 48) viper.SetDefault("SHOUTRRR_URL", "") + + viper.SetDefault("USE_DB", "sqlite") + viper.SetDefault("PG_CONNECT", "") + viper.SetDefault("INFLUX_ENABLE", false) viper.SetConfigFile(path) @@ -42,6 +46,9 @@ func Get(path string) (config models.Conf) { config.TrimHist = viper.GetInt("TRIM_HIST") config.ShoutURL = viper.Get("SHOUTRRR_URL").(string) + config.UseDB = viper.Get("USE_DB").(string) + config.PGConnect = viper.Get("PG_CONNECT").(string) + config.InfluxEnable = viper.GetBool("INFLUX_ENABLE") config.InfluxSkipTLS = viper.GetBool("INFLUX_SKIP_TLS") config.InfluxAddr, _ = viper.Get("INFLUX_ADDR").(string) @@ -70,6 +77,9 @@ func Write(config models.Conf) { viper.Set("TRIM_HIST", config.TrimHist) viper.Set("SHOUTRRR_URL", config.ShoutURL) + viper.Set("USE_DB", config.UseDB) + viper.Set("PG_CONNECT", config.PGConnect) + viper.Set("influx_enable", config.InfluxEnable) viper.Set("influx_skip_tls", config.InfluxSkipTLS) viper.Set("influx_addr", config.InfluxAddr) diff --git a/internal/models/models.go b/internal/models/models.go index a68376f..b1c67fa 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -2,20 +2,24 @@ package models // Conf - app config type Conf struct { - Host string - Port string - Theme string - Color string - DirPath string - ConfPath string - DBPath string - NodePath string - LogLevel string - Ifaces string - ArpArgs string - Timeout int - TrimHist int - ShoutURL string + Host string + Port string + Theme string + Color string + DirPath string + ConfPath string + DBPath string + NodePath string + LogLevel string + Ifaces string + ArpArgs string + Timeout int + TrimHist int + ShoutURL string + // PostgreSQL + UseDB string + PGConnect string + // InfluxDB InfluxEnable bool InfluxAddr string InfluxToken string diff --git a/internal/web/config.go b/internal/web/config.go index 9ad4c26..d24a85e 100644 --- a/internal/web/config.go +++ b/internal/web/config.go @@ -40,6 +40,9 @@ func saveConfigHandler(c *gin.Context) { appConfig.Ifaces = c.PostForm("ifaces") appConfig.ShoutURL = c.PostForm("shout") + appConfig.UseDB = c.PostForm("usedb") + appConfig.PGConnect = c.PostForm("pgconnect") + timeout := c.PostForm("timeout") trimHist := c.PostForm("trim") appConfig.Timeout, _ = strconv.Atoi(timeout) diff --git a/internal/web/public/js/filter-search.js b/internal/web/public/js/filter-search.js new file mode 100644 index 0000000..7287df7 --- /dev/null +++ b/internal/web/public/js/filter-search.js @@ -0,0 +1,68 @@ +let oldFilter = ''; +let bkpArray; + +function filterFunc(field, value) { + + if (oldFilter == field) { + addrsArray = bkpArray; + } + + switch (field) { + case 'iface': + addrsArray = addrsArray.filter((item) => item.Iface == value); + break; + case 'known': + addrsArray = addrsArray.filter((item) => item.Known == value); + break; + case 'line': + addrsArray = addrsArray.filter((item) => item.Now == value); + break; + default: + console.log("Filter error"); + } + + oldFilter = field; + + displayArrayData(addrsArray); +} + +function resetFilter() { + addrsArray = bkpArray; + displayArrayData(addrsArray); +} + +function searchFunc() { + const s = document.getElementById('search').value; + + if (s != "") { + + const sl = s.toLowerCase(); + + let newArray = []; + + for (let item of addrsArray) { + + if (searchItem(item, sl)) { + newArray.push(item); + } + } + addrsArray = newArray; + } else { + addrsArray = bkpArray; + } + + displayArrayData(addrsArray); +} + +function searchItem(item, sl) { + + const name = item.Name.toLowerCase(); + const hw = item.Hw.toLowerCase(); + const mac = item.Mac.toLowerCase(); + + if ((name.includes(sl)) || (item.Iface.includes(sl)) || (item.IP.includes(sl)) || (mac.includes(sl)) || (hw.includes(sl)) || (item.Date.includes(sl))) { + return true; + } else { + return false; + } +} \ No newline at end of file diff --git a/internal/web/public/js/filter.js b/internal/web/public/js/filter.js deleted file mode 100644 index f29aa7e..0000000 --- a/internal/web/public/js/filter.js +++ /dev/null @@ -1,32 +0,0 @@ -let oldFilter = ''; -let bkpArray; - -function filterFunc(field, value) { - - if (oldFilter == field) { - addrsArray = bkpArray; - } - - switch (field) { - case 'iface': - addrsArray = addrsArray.filter((item) => item.Iface == value); - break; - case 'known': - addrsArray = addrsArray.filter((item) => item.Known == value); - break; - case 'line': - addrsArray = addrsArray.filter((item) => item.Now == value); - break; - default: - console.log("Filter error"); - } - - oldFilter = field; - - displayArrayData(addrsArray); -} - -function resetFilter() { - addrsArray = bkpArray; - displayArrayData(addrsArray); -} \ No newline at end of file diff --git a/internal/web/templates/config.html b/internal/web/templates/config.html index 006843d..5d4f1c0 100644 --- a/internal/web/templates/config.html +++ b/internal/web/templates/config.html @@ -44,6 +44,20 @@ + + Use DB + + + + PG Connect URL + + + + Log level + + +
diff --git a/internal/web/webgui.go b/internal/web/webgui.go index 5b332fa..cbea95b 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -27,7 +27,7 @@ func Gui(dirPath, nodePath string) { quitScan = make(chan bool) updateRoutines() // routines-upd.go - slog.Info("config", "path", appConfig.DirPath) + slog.Info("Config dir", "path", appConfig.DirPath) address := appConfig.Host + ":" + appConfig.Port