Skip to content

Commit

Permalink
History fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Oct 8, 2023
1 parent 3403857 commit 4ebd227
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
3 changes: 3 additions & 0 deletions internal/scan/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scan
import (
"fmt"
"log"
"time"

"github.com/aceberg/WatchYourLAN/internal/db"
"github.com/aceberg/WatchYourLAN/internal/models"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/web/templates/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
8 changes: 1 addition & 7 deletions internal/web/templates/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
19 changes: 12 additions & 7 deletions internal/web/trim-history.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package web

import (
// "log"
"log"
"strconv"
"time"

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

0 comments on commit 4ebd227

Please sign in to comment.