diff --git a/internal/scan/compare.go b/internal/scan/compare.go index 5b6f22c..1b126fb 100644 --- a/internal/scan/compare.go +++ b/internal/scan/compare.go @@ -3,6 +3,7 @@ package scan import ( "fmt" "log" + "time" "github.com/aceberg/WatchYourLAN/internal/db" "github.com/aceberg/WatchYourLAN/internal/models" @@ -31,6 +32,8 @@ func hostsCompare() { } else if oneHost.Now == 1 { oneHost.Now = 0 + oneHost.Date = time.Now().Format("2006-01-02 15:04:05") + db.Update(appConfig.DbPath, oneHost) histAdd(oneHost, 0) diff --git a/internal/web/templates/history.html b/internal/web/templates/history.html index 5e289b0..a6f1709 100644 --- a/internal/web/templates/history.html +++ b/internal/web/templates/history.html @@ -10,7 +10,7 @@ IP Mac Hardware - First/Last seen + Time Known State diff --git a/internal/web/templates/host.html b/internal/web/templates/host.html index 381b8ff..d0a73ec 100644 --- a/internal/web/templates/host.html +++ b/internal/web/templates/host.html @@ -101,22 +101,16 @@
- - - - + {{ range .Hist }} - - -
Host ID Name IPMacHardwareFirst/Last seenTime Known State
{{ .Host }} {{ .Name }} {{ .IP }}{{ .Mac }}{{ .Hw }} {{ .Date }} {{ if eq .Known 1 }} diff --git a/internal/web/trim-history.go b/internal/web/trim-history.go index 82fbb30..0b3f383 100644 --- a/internal/web/trim-history.go +++ b/internal/web/trim-history.go @@ -1,7 +1,7 @@ package web import ( - // "log" + "log" "strconv" "time" @@ -13,25 +13,30 @@ func trimHistoryRoutine() { for { trimHistory() - time.Sleep(time.Duration(1) * time.Hour) + time.Sleep(time.Duration(1) * time.Hour) // Every hour } } func trimHistory() { days, _ := strconv.Atoi(AppConfig.HistDays) - now := time.Now() + + 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(days) * 24 * time.Hour) + history := db.SelectHist(AppConfig.DbPath) + if AppConfig.LogLevel != "short" { + log.Println("INFO: removing all history before", nowMinus.Format("2006-01-02 15:04:05")) + } + for _, hist := range history { date, _ := time.Parse("2006-01-02 15:04:05", hist.Date) - datePlus := date.Add(time.Duration(days) * 24 * time.Hour) - if now.After(datePlus) { + if date.Before(nowMinus) { db.DeleteHist(AppConfig.DbPath, hist.ID) - - // log.Println("REMOVED DATE =", hist.Date) } } }