Trim history

This commit is contained in:
aceberg 2024-08-28 23:30:25 +07:00
parent 4ddb03719f
commit d6ee3a059f
5 changed files with 109 additions and 38 deletions

View file

@ -35,10 +35,20 @@ func saveConfigHandler(c *gin.Context) {
appConfig.Theme = c.PostForm("theme")
appConfig.Color = c.PostForm("color")
appConfig.NodePath = c.PostForm("node")
appConfig.ShoutURL = c.PostForm("shout")
conf.Write(appConfig)
slog.Info("Writing new config to " + appConfig.ConfPath)
c.Redirect(http.StatusFound, "/config")
}
func saveSettingsHandler(c *gin.Context) {
appConfig.LogLevel = c.PostForm("log")
appConfig.ArpArgs = c.PostForm("arpargs")
appConfig.Ifaces = c.PostForm("ifaces")
appConfig.ShoutURL = c.PostForm("shout")
appConfig.UseDB = c.PostForm("usedb")
appConfig.PGConnect = c.PostForm("pgconnect")

View file

@ -7,7 +7,7 @@ import (
)
func updateRoutines() {
slog.Debug("Restarting go routines")
slog.Debug("Restarting scan routine")
close(quitScan)

View file

@ -5,7 +5,7 @@
<div class="row">
<div class="col-md mt-4">
<div class="card border-primary">
<div class="card-header">Config</div>
<div class="card-header">Basic config</div>
<div class="card-body table-responsive">
<table class="table table-borderless">
<form action="/config/" method="post">
@ -35,7 +35,7 @@
</select></td>
</tr>
<tr>
<td>Local node-bootstrap url</td>
<td>Local node-bootstrap URL</td>
<td><input name="node" type="text" class="form-control" value="{{ .Config.NodePath }}"></td>
</tr>
<tr>
@ -44,6 +44,46 @@
<textarea name="shout" class="form-control" style="width: 100%;" rows="3" wrap="soft">{{ .Config.ShoutURL }}</textarea>
</td>
</tr>
<tr>
<td><button type="submit" class="btn btn-primary">Save</button></td>
<td></td>
</tr>
</form>
</table>
</div>
</div>
<div class="card border-primary mt-4 mb-4">
<div class="card-header">Scan settings</div>
<div class="card-body table-responsive">
<table class="table table-borderless">
<form action="/config_settings/" method="post">
<tr>
<td>Interfaces</td>
<td><input name="ifaces" type="text" class="form-control" value="{{ .Config.Ifaces }}"></td>
</tr>
<tr>
<td>Timeout (seconds)</td>
<td><input name="timeout" type="number" class="form-control" value="{{ .Config.Timeout }}"></td>
</tr>
<tr>
<td>Args for arp-scan</td>
<td><input name="arpargs" type="text" class="form-control" value="{{ .Config.ArpArgs }}"></td>
</tr>
<tr>
<td>Trim History (hours)</td>
<td><input name="trim" type="number" class="form-control" value="{{ .Config.TrimHist }}"></td>
</tr>
<tr>
<td>Log level</td>
<td><select name="log" class="form-select">
<option selected>{{ .Config.LogLevel }}</option>
<option value="debug">debug</option>
<option value="info">info</option>
<option value="warn">warn</option>
<option value="error">error</option>
</select></td>
</tr>
<tr>
<td>Use DB</td>
<td><select name="usedb" class="form-select">
@ -58,33 +98,6 @@
<textarea name="pgconnect" class="form-control" style="width: 100%;" rows="3" wrap="soft">{{ .Config.PGConnect }}</textarea>
</td>
</tr>
<tr>
<td>Log level</td>
<td><select name="log" class="form-select">
<option selected>{{ .Config.LogLevel }}</option>
<option value="debug">debug</option>
<option value="info">info</option>
<option value="warn">warn</option>
<option value="error">error</option>
</select></td>
</tr>
<tr>
<tr>
<td>Interfaces</td>
<td><input name="ifaces" type="text" class="form-control" value="{{ .Config.Ifaces }}"></td>
</tr>
<tr>
<td>Timeout</td>
<td><input name="timeout" type="number" class="form-control" value="{{ .Config.Timeout }}"></td>
</tr>
<tr>
<td>Args for arp-scan</td>
<td><input name="arpargs" type="text" class="form-control" value="{{ .Config.ArpArgs }}"></td>
</tr>
<tr>
<td>Trim History (hours)</td>
<td><input name="trim" type="number" class="form-control" value="{{ .Config.TrimHist }}"></td>
</tr>
<tr>
<td><button type="submit" class="btn btn-primary">Save</button></td>
<td></td>
@ -150,16 +163,20 @@
</div>
</div>
<div class="card border-primary mt-4">
<div class="card border-primary mt-4 mb-4">
<div class="card-header">
About (<a href="https://github.com/aceberg/WatchYourLAN/releases/tag/{{ .Version }}" target="_blank">v{{ .Version }}</a>)
</div>
<div class="card-body">
<p>● After changing <b>Host</b> or <b>Port</b> the app must be restarted</p>
<p><b>Timeout (minutes)</b> - how often watched ports are scanned</p>
<p><b>Trim History</b> - how many port states are saved in memory and displayed on the <a href="/history/">History</a> page</p>
<p><b>Shoutrrr URL</b> provides notifications to Discord, Email, Gotify, Telegram and other services. <a href="https://containrrr.dev/shoutrrr/v0.8/" target="_blank">Link to documentation</a></p>
<p><b>Interfaces</b> - one or more, space separated</p>
<p><b>Timeout (seconds)</b> - time between scans</p>
<p><b>Args for arp-scan</b> - pass your own arguments to <code>arp-scan</code>. See <code>man arp-scan</code> for more. Enable <b>debug</b> log level to see resulting command. (Example: <code>-r 1</code>)</p>
<p><b>Trim History (hours)</b> - remove history after</p>
<p><b>PG Connect URL</b> - address to connect to PostgreSQL DB. (Example: <code>postgres://username:password@192.168.0.1:5432/dbname?sslmode=disable</code>). Full list of URL parameters <a href="https://pkg.go.dev/github.com/lib/pq#hdr-Connection_String_Parameters" target="_blank">here</a></p>
<p>● If you find this app useful, please, <a href="https://github.com/aceberg#donate" target="_blank">donate</a></p>
<p>● Commission you own app (Golang, HTML/JS). Contact and prices <a href="https://github.com/aceberg" target="_blank">here</a></p>
<p>● Commission you own app (Golang, HTML/JS). Contact <a href="https://github.com/aceberg" target="_blank">here</a></p>
</div>
</div>
</div>

View file

@ -0,0 +1,42 @@
package web
import (
"log/slog"
"time"
"github.com/aceberg/WatchYourLAN/internal/db"
)
func trimHistoryRoutine() {
for {
trimHistory()
time.Sleep(time.Duration(1) * time.Hour) // Every hour
}
}
func trimHistory() {
hours := appConfig.TrimHist
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(hours) * time.Hour)
history := db.Select("history")
slog.Info("Removing all History before", "date", nowMinus.Format("2006-01-02 15:04:05"))
n := 0
for _, hist := range history {
date, _ := time.Parse("2006-01-02 15:04:05", hist.Date)
if date.Before(nowMinus) {
n = n + 1
db.Delete("history", hist.ID)
}
}
slog.Info("Removed records from History", "n", n)
}

View file

@ -25,7 +25,8 @@ func Gui(dirPath, nodePath string) {
appConfig.NodePath = nodePath
quitScan = make(chan bool)
updateRoutines() // routines-upd.go
updateRoutines() // routines-upd.go
go trimHistoryRoutine() // trim-history.go
slog.Info("Config dir", "path", appConfig.DirPath)
@ -57,8 +58,9 @@ func Gui(dirPath, nodePath string) {
router.GET("/host/:id", hostHandler) // host.go
router.GET("/config/", configHandler) // config.go
router.POST("/config/", saveConfigHandler) // config.go
router.POST("/config_influx/", saveInfluxHandler) // config.go
router.POST("/config/", saveConfigHandler) // config.go
router.POST("/config_settings/", saveSettingsHandler) // config.go
router.POST("/config_influx/", saveInfluxHandler) // config.go
err := router.Run(address)
check.IfError(err)