diff --git a/CHANGELOG.md b/CHANGELOG.md index d98a944..a4047a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. ## [v2.0.3] - 2024-09- ### Fixed - `ARP_STRS_JOINED` should be empty in config file +- Optimized History Trim ## [v2.0.2] - 2024-09-07 ### Added diff --git a/go.mod b/go.mod index 8f1fd89..a85297e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/aceberg/WatchYourLAN -go 1.22.6 +go 1.23.1 require ( github.com/containrrr/shoutrrr v0.8.0 @@ -9,7 +9,7 @@ require ( github.com/jmoiron/sqlx v1.4.0 github.com/lib/pq v1.10.9 github.com/spf13/viper v1.19.0 - modernc.org/sqlite v1.32.0 + modernc.org/sqlite v1.33.1 ) require ( diff --git a/go.sum b/go.sum index 20c6ce7..6ce9acf 100644 --- a/go.sum +++ b/go.sum @@ -204,8 +204,8 @@ modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4= modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= modernc.org/sortutil v1.2.0 h1:jQiD3PfS2REGJNzNCMMaLSp/wdMNieTbKX920Cqdgqc= modernc.org/sortutil v1.2.0/go.mod h1:TKU2s7kJMf1AE84OoiGppNHJwvB753OYfNl2WRb++Ss= -modernc.org/sqlite v1.32.0 h1:6BM4uGza7bWypsw4fdLRsLxut6bHe4c58VeqjRgST8s= -modernc.org/sqlite v1.32.0/go.mod h1:UqoylwmTb9F+IqXERT8bW9zzOWN8qwAIcLdzeBZs4hA= +modernc.org/sqlite v1.33.1 h1:trb6Z3YYoeM9eDL1O8do81kP+0ejv+YzgyFo+Gwy0nM= +modernc.org/sqlite v1.33.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k= modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA= modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0= modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= diff --git a/internal/db/edit.go b/internal/db/edit.go index 34a556e..13f1c99 100644 --- a/internal/db/edit.go +++ b/internal/db/edit.go @@ -67,6 +67,23 @@ func Delete(table string, id int) { dbExec(sqlStatement) } +// DeleteList - delete a list of hosts from History +func DeleteList(ids []int) { + if len(ids) > 0 { + idString := "" + + for _, id := range ids { + idString = idString + fmt.Sprintf("%d, ", id) + } + idString = idString[:len(idString)-2] + + sqlStatement := `DELETE FROM history WHERE "ID" IN (%s);` + sqlStatement = fmt.Sprintf(sqlStatement, idString) + + dbExec(sqlStatement) + } +} + // Clear - delete all hosts from table func Clear(table string) { sqlStatement := `DELETE FROM %s;` diff --git a/internal/notify/shout.go b/internal/notify/shout.go index 73c811e..aaa01de 100644 --- a/internal/notify/shout.go +++ b/internal/notify/shout.go @@ -10,7 +10,7 @@ func Shout(message string, url string) { if url != "" { err := shoutrrr.Send(url, message) if err != nil { - slog.Error("Notification failed (shoutrrr): ", err) + slog.Error("Notification failed (shoutrrr): ", "", err) } } } diff --git a/internal/web/trim-history.go b/internal/web/trim-history.go index 2d19fa8..db38b40 100644 --- a/internal/web/trim-history.go +++ b/internal/web/trim-history.go @@ -25,28 +25,31 @@ func trimHistory() { now, _ := time.Parse("2006-01-02 15:04:05", nowStr) // in one format nowMinus := now.Add(-time.Duration(hours) * time.Hour) - history := histHosts - if appConfig.HistInDB { - history = db.Select("history") + histHosts = db.Select("history") } slog.Info("Removing all History before", "date", nowMinus.Format("2006-01-02 15:04:05")) n := 0 - histHosts = []models.Host{} - for _, hist := range history { + 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 appConfig.HistInDB { - db.Delete("history", hist.ID) + ids = append(ids, hist.ID) } } else { - histHosts = append(histHosts, hist) + newHistHosts = append(newHistHosts, hist) } } + + db.DeleteList(ids) + histHosts = newHistHosts slog.Info("Removed records from History", "n", n) }