Optimized History Trim
This commit is contained in:
parent
ee850d2576
commit
2934878b5a
6 changed files with 33 additions and 12 deletions
|
|
@ -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
|
||||
|
|
|
|||
4
go.mod
4
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 (
|
||||
|
|
|
|||
4
go.sum
4
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=
|
||||
|
|
|
|||
|
|
@ -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;`
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue