diff --git a/cmd/WatchYourLAN/main.go b/cmd/WatchYourLAN/main.go index 5700376..f8b0114 100644 --- a/cmd/WatchYourLAN/main.go +++ b/cmd/WatchYourLAN/main.go @@ -7,6 +7,8 @@ import ( // _ "net/http/pprof" "github.com/aceberg/WatchYourLAN/internal/conf" + "github.com/aceberg/WatchYourLAN/internal/gdb" + "github.com/aceberg/WatchYourLAN/internal/routines" "github.com/aceberg/WatchYourLAN/internal/web" ) @@ -30,5 +32,10 @@ func main() { // Generate AppConfig conf.Generate(*dirPtr, *nodePtr) - web.Gui() // webgui.go + gdb.Start() + + routines.ScanRestart() + routines.HistoryTrim() + + web.Gui() } diff --git a/go.mod b/go.mod index 457db5f..ad4bbc5 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/aceberg/WatchYourLAN go 1.24.5 require ( - github.com/aceberg/gorm-sqlite v0.0.0-20250723202739-1dbc05378285 + github.com/aceberg/gorm-sqlite v0.0.0-20250723203925-9af3950b56af github.com/containrrr/shoutrrr v0.8.0 github.com/gin-gonic/gin v1.10.1 github.com/influxdata/influxdb-client-go/v2 v2.14.0 diff --git a/go.sum b/go.sum index bcc73d9..09695d2 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= -github.com/aceberg/gorm-sqlite v0.0.0-20250723202739-1dbc05378285 h1:svuY2NUI8ipveiNvy65cbzgAFUbRDuMrahQo5QUN6s8= -github.com/aceberg/gorm-sqlite v0.0.0-20250723202739-1dbc05378285/go.mod h1:3g//0BOzaOv9dO8G9j7JZOJA6qIURpPaht9oDY7LKpA= +github.com/aceberg/gorm-sqlite v0.0.0-20250723203925-9af3950b56af h1:Y/PE5LSSwLmVw2juDC+U20agHCO5HdAsGE4Efpd0WVg= +github.com/aceberg/gorm-sqlite v0.0.0-20250723203925-9af3950b56af/go.mod h1:3g//0BOzaOv9dO8G9j7JZOJA6qIURpPaht9oDY7LKpA= github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= diff --git a/internal/gdb/edit.go b/internal/gdb/edit.go index 85f5073..81e9687 100644 --- a/internal/gdb/edit.go +++ b/internal/gdb/edit.go @@ -30,12 +30,14 @@ func Delete(table string, id int) { check.IfError(result.Error) } -// DeleteList - delete a list of hosts from History -func DeleteList(ids []int) { +// DeleteOldHistory - delete a list of hosts from History +func DeleteOldHistory(date string) int64 { tab := db.Table("history") - result := tab.Where("ID IN ?", ids).Delete(&models.Host{}) + result := tab.Where("DATE < ?", date).Delete(&models.Host{}) check.IfError(result.Error) + + return result.RowsAffected } // Clear - delete all hosts from table diff --git a/internal/gdb/start.go b/internal/gdb/start.go index bec6af9..4f13b85 100644 --- a/internal/gdb/start.go +++ b/internal/gdb/start.go @@ -58,6 +58,7 @@ func Start() { // sqlDB, _ := db.DB() // sqlDB.SetMaxOpenConns(1) // only one writer at a time // sqlDB.SetMaxIdleConns(1) + // sqlDB.SetConnMaxLifetime(time.Minute * 1) } // Migrate the schema diff --git a/internal/routines/routines-upd.go b/internal/routines/restart-scan.go similarity index 75% rename from internal/routines/routines-upd.go rename to internal/routines/restart-scan.go index e166108..80a1e4e 100644 --- a/internal/routines/routines-upd.go +++ b/internal/routines/restart-scan.go @@ -4,30 +4,20 @@ import ( "log/slog" "github.com/aceberg/WatchYourLAN/internal/conf" - "github.com/aceberg/WatchYourLAN/internal/gdb" - "github.com/aceberg/WatchYourLAN/internal/models" ) var ( - allHosts []models.Host - histHosts []models.Host - quitScan = make(chan bool) ) -// Update - start or update routines -func Update() { - - slog.Debug("Restarting scan routine") +// ScanRestart - start or update routines +func ScanRestart() { close(quitScan) + slog.Debug("Restarting scan routine") setLogLevel() - gdb.Start() - - allHosts = gdb.Select("now") - quitScan = make(chan bool) go startScan(quitScan) // scan-routine.go } diff --git a/internal/routines/scan-routine.go b/internal/routines/scan-routine.go index 9c34b17..5548a99 100644 --- a/internal/routines/scan-routine.go +++ b/internal/routines/scan-routine.go @@ -31,12 +31,11 @@ func startScan(quit chan bool) { foundHosts = arp.Scan(conf.AppConfig.Ifaces, conf.AppConfig.ArpArgs, conf.AppConfig.ArpStrs) compareHosts(foundHosts) - allHosts = gdb.Select("now") lastDate = time.Now() } - time.Sleep(time.Duration(1) * time.Second) + time.Sleep(time.Duration(1) * time.Minute) } } } @@ -49,6 +48,7 @@ func compareHosts(foundHosts []models.Host) { foundHostsMap[fHost.Mac] = fHost } + allHosts := gdb.Select("now") for _, aHost := range allHosts { fHost, exists := foundHostsMap[aHost.Mac] @@ -67,10 +67,8 @@ func compareHosts(foundHosts []models.Host) { gdb.Update("now", aHost) aHost.Date = time.Now().Format("2006-01-02 15:04:05") - histHosts = append(histHosts, aHost) - if conf.AppConfig.HistInDB { - gdb.Update("history", aHost) - } + gdb.Update("history", aHost) + if conf.AppConfig.InfluxEnable { influx.Add(conf.AppConfig, aHost) } diff --git a/internal/routines/trim-history.go b/internal/routines/trim-history.go index 288e587..998f28a 100644 --- a/internal/routines/trim-history.go +++ b/internal/routines/trim-history.go @@ -6,54 +6,22 @@ import ( "github.com/aceberg/WatchYourLAN/internal/conf" "github.com/aceberg/WatchYourLAN/internal/gdb" - "github.com/aceberg/WatchYourLAN/internal/models" ) // HistoryTrim - routine for History func HistoryTrim() { - go func() { for { - trimHistory() + hours := conf.AppConfig.TrimHist + nowMinus := time.Now().Add(-time.Duration(hours) * time.Hour) + date := nowMinus.Format("2006-01-02 15:04:05") + + slog.Info("Removing all History before", "date", date) + + n := gdb.DeleteOldHistory(date) + slog.Info("Removed records from History", "n", n) time.Sleep(time.Duration(1) * time.Hour) // Every hour } }() } - -func trimHistory() { - - hours := conf.AppConfig.TrimHist - - nowStr := time.Now().Format("2006-01-02 15:04:05") // This needed so all time is - now, _ := time.Parse("2006-01-02 15:04:05", nowStr) // in one format - nowMinus := now.Add(-time.Duration(hours) * time.Hour) - - if conf.AppConfig.HistInDB { - histHosts = gdb.Select("history") - } - - slog.Info("Removing all History before", "date", nowMinus.Format("2006-01-02 15:04:05")) - - n := 0 - newHistHosts := []models.Host{} - ids := []int{} - - for _, hist := range histHosts { - date, _ := time.Parse("2006-01-02 15:04:05", hist.Date) - - if date.Before(nowMinus) { - n = n + 1 - if conf.AppConfig.HistInDB { - ids = append(ids, hist.ID) - } - } else { - newHistHosts = append(newHistHosts, hist) - } - } - - gdb.DeleteList(ids) - histHosts = newHistHosts - - slog.Info("Removed records from History", "n", n) -} diff --git a/internal/web/webgui.go b/internal/web/webgui.go index f2aef23..119b18a 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -10,7 +10,6 @@ import ( "github.com/aceberg/WatchYourLAN/internal/check" "github.com/aceberg/WatchYourLAN/internal/conf" "github.com/aceberg/WatchYourLAN/internal/prometheus" - "github.com/aceberg/WatchYourLAN/internal/routines" "github.com/gin-gonic/gin" ) @@ -27,9 +26,6 @@ var pubFS embed.FS // Gui - start web server func Gui() { - routines.Update() - routines.HistoryTrim() - slog.Info("Config dir", "path", conf.AppConfig.DirPath) address := conf.AppConfig.Host + ":" + conf.AppConfig.Port