History fixes

This commit is contained in:
aceberg 2023-10-08 13:07:44 +07:00
parent 3403857442
commit 4ebd2271bf
4 changed files with 17 additions and 15 deletions

View file

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

View file

@ -10,7 +10,7 @@
<th>IP</th>
<th>Mac</th>
<th>Hardware</th>
<th>First/Last seen</th>
<th>Time</th>
<th>Known</th>
<th>State</th>
</tr>

View file

@ -101,22 +101,16 @@
<div class="row">
<table class="table table-striped">
<tr>
<th>Host ID</th>
<th>Name</th>
<th>IP</th>
<th>Mac</th>
<th>Hardware</th>
<th>First/Last seen</th>
<th>Time</th>
<th>Known</th>
<th>State</th>
</tr>
{{ range .Hist }}
<tr>
<td><a href="/host?id={{ .Host }}">{{ .Host }}</a></td>
<td>{{ .Name }}</td>
<td><a href="http://{{ .IP }}" target="_blank">{{ .IP }}</a></td>
<td><a href="/host?id={{ .Host }}">{{ .Mac }}</a></td>
<td>{{ .Hw }}</td>
<td>{{ .Date }}</td>
<td>
{{ if eq .Known 1 }}

View file

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