Trim history
This commit is contained in:
parent
4ddb03719f
commit
d6ee3a059f
5 changed files with 109 additions and 38 deletions
|
|
@ -35,10 +35,20 @@ func saveConfigHandler(c *gin.Context) {
|
||||||
appConfig.Theme = c.PostForm("theme")
|
appConfig.Theme = c.PostForm("theme")
|
||||||
appConfig.Color = c.PostForm("color")
|
appConfig.Color = c.PostForm("color")
|
||||||
appConfig.NodePath = c.PostForm("node")
|
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.LogLevel = c.PostForm("log")
|
||||||
appConfig.ArpArgs = c.PostForm("arpargs")
|
appConfig.ArpArgs = c.PostForm("arpargs")
|
||||||
appConfig.Ifaces = c.PostForm("ifaces")
|
appConfig.Ifaces = c.PostForm("ifaces")
|
||||||
appConfig.ShoutURL = c.PostForm("shout")
|
|
||||||
|
|
||||||
appConfig.UseDB = c.PostForm("usedb")
|
appConfig.UseDB = c.PostForm("usedb")
|
||||||
appConfig.PGConnect = c.PostForm("pgconnect")
|
appConfig.PGConnect = c.PostForm("pgconnect")
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func updateRoutines() {
|
func updateRoutines() {
|
||||||
slog.Debug("Restarting go routines")
|
slog.Debug("Restarting scan routine")
|
||||||
|
|
||||||
close(quitScan)
|
close(quitScan)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md mt-4">
|
<div class="col-md mt-4">
|
||||||
<div class="card border-primary">
|
<div class="card border-primary">
|
||||||
<div class="card-header">Config</div>
|
<div class="card-header">Basic config</div>
|
||||||
<div class="card-body table-responsive">
|
<div class="card-body table-responsive">
|
||||||
<table class="table table-borderless">
|
<table class="table table-borderless">
|
||||||
<form action="/config/" method="post">
|
<form action="/config/" method="post">
|
||||||
|
|
@ -35,7 +35,7 @@
|
||||||
</select></td>
|
</select></td>
|
||||||
</tr>
|
</tr>
|
||||||
<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>
|
<td><input name="node" type="text" class="form-control" value="{{ .Config.NodePath }}"></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -44,6 +44,46 @@
|
||||||
<textarea name="shout" class="form-control" style="width: 100%;" rows="3" wrap="soft">{{ .Config.ShoutURL }}</textarea>
|
<textarea name="shout" class="form-control" style="width: 100%;" rows="3" wrap="soft">{{ .Config.ShoutURL }}</textarea>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</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>
|
<tr>
|
||||||
<td>Use DB</td>
|
<td>Use DB</td>
|
||||||
<td><select name="usedb" class="form-select">
|
<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>
|
<textarea name="pgconnect" class="form-control" style="width: 100%;" rows="3" wrap="soft">{{ .Config.PGConnect }}</textarea>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</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>
|
<tr>
|
||||||
<td><button type="submit" class="btn btn-primary">Save</button></td>
|
<td><button type="submit" class="btn btn-primary">Save</button></td>
|
||||||
<td></td>
|
<td></td>
|
||||||
|
|
@ -150,16 +163,20 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card border-primary mt-4">
|
<div class="card border-primary mt-4 mb-4">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
About (<a href="https://github.com/aceberg/WatchYourLAN/releases/tag/{{ .Version }}" target="_blank">v{{ .Version }}</a>)
|
About (<a href="https://github.com/aceberg/WatchYourLAN/releases/tag/{{ .Version }}" target="_blank">v{{ .Version }}</a>)
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<p>● After changing <b>Host</b> or <b>Port</b> the app must be restarted</p>
|
<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>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>Trim History</b> - how many port states are saved in memory and displayed on the <a href="/history/">History</a> page</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>● 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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
42
internal/web/trim-history.go
Normal file
42
internal/web/trim-history.go
Normal 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)
|
||||||
|
}
|
||||||
|
|
@ -25,7 +25,8 @@ func Gui(dirPath, nodePath string) {
|
||||||
appConfig.NodePath = nodePath
|
appConfig.NodePath = nodePath
|
||||||
|
|
||||||
quitScan = make(chan bool)
|
quitScan = make(chan bool)
|
||||||
updateRoutines() // routines-upd.go
|
updateRoutines() // routines-upd.go
|
||||||
|
go trimHistoryRoutine() // trim-history.go
|
||||||
|
|
||||||
slog.Info("Config dir", "path", appConfig.DirPath)
|
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("/host/:id", hostHandler) // host.go
|
||||||
router.GET("/config/", configHandler) // config.go
|
router.GET("/config/", configHandler) // config.go
|
||||||
|
|
||||||
router.POST("/config/", saveConfigHandler) // config.go
|
router.POST("/config/", saveConfigHandler) // config.go
|
||||||
router.POST("/config_influx/", saveInfluxHandler) // config.go
|
router.POST("/config_settings/", saveSettingsHandler) // config.go
|
||||||
|
router.POST("/config_influx/", saveInfluxHandler) // config.go
|
||||||
|
|
||||||
err := router.Run(address)
|
err := router.Run(address)
|
||||||
check.IfError(err)
|
check.IfError(err)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue