Performance optimization

This commit is contained in:
aceberg 2025-07-24 06:26:53 +07:00
parent 24afe6c96f
commit 6b2851264f
9 changed files with 32 additions and 70 deletions

View file

@ -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()
}

2
go.mod
View file

@ -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

4
go.sum
View file

@ -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=

View file

@ -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

View file

@ -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

View file

@ -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
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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