diff --git a/internal/web/public/js/hist-html.js b/internal/web/public/js/hist-html.js index c05b547..9844014 100644 --- a/internal/web/public/js/hist-html.js +++ b/internal/web/public/js/hist-html.js @@ -1,8 +1,6 @@ function getHistHTML(hist) { - hist = sortHistByDate(hist); - let html = '', col, title; for (let h of hist) { @@ -23,7 +21,7 @@ function getHistHTML(hist) { function sortHistByDate(hist) { - hist.sort((a, b) => (a.Date > b.Date)); + hist.sort((a, b) => (a.Date < b.Date ? 1 : -1)); return hist; } \ No newline at end of file diff --git a/internal/web/public/js/history.js b/internal/web/public/js/history.js index a776216..cda0ed5 100644 --- a/internal/web/public/js/history.js +++ b/internal/web/public/js/history.js @@ -26,6 +26,8 @@ async function loadHistory() { for (let a of addrsArray) { url = '/api/history/'+a.Mac; hist = await (await fetch(url)).json(); + hist = sortHistByDate(hist); + if (show > 0) { hist = hist.slice(0, show); } diff --git a/internal/web/public/js/host.js b/internal/web/public/js/host.js index bf64ccd..560ac71 100644 --- a/internal/web/public/js/host.js +++ b/internal/web/public/js/host.js @@ -21,6 +21,7 @@ async function loadHistory(m) { const url = '/api/history/'+mac; let hist = await (await fetch(url)).json(); + hist = sortHistByDate(hist); if (show > 0) { hist = hist.slice(0, show); } diff --git a/internal/web/trim-history.go b/internal/web/trim-history.go index e88b61d..2d19fa8 100644 --- a/internal/web/trim-history.go +++ b/internal/web/trim-history.go @@ -26,6 +26,7 @@ func trimHistory() { nowMinus := now.Add(-time.Duration(hours) * time.Hour) history := histHosts + if appConfig.HistInDB { history = db.Select("history") } @@ -33,27 +34,19 @@ func trimHistory() { slog.Info("Removing all History before", "date", nowMinus.Format("2006-01-02 15:04:05")) n := 0 - for i, hist := range history { + histHosts = []models.Host{} + for _, hist := range history { 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) - } else { - historyDel(i) } + } else { + histHosts = append(histHosts, hist) } } slog.Info("Removed records from History", "n", n) } - -func historyDel(i int) { - - l := len(histHosts) - - histHosts[i] = histHosts[l-1] - histHosts[l-1] = models.Host{} - histHosts = histHosts[:l-1] -}