History in DB switch
This commit is contained in:
parent
eaa0c27bd7
commit
0187d9b1bb
8 changed files with 59 additions and 13 deletions
|
|
@ -20,6 +20,7 @@ func Get(path string) (config models.Conf) {
|
|||
viper.SetDefault("IFACES", "")
|
||||
viper.SetDefault("TIMEOUT", 120)
|
||||
viper.SetDefault("TRIM_HIST", 48)
|
||||
viper.SetDefault("HIST_IN_DB", false)
|
||||
viper.SetDefault("SHOUTRRR_URL", "")
|
||||
|
||||
viper.SetDefault("USE_DB", "sqlite")
|
||||
|
|
@ -44,6 +45,7 @@ func Get(path string) (config models.Conf) {
|
|||
config.Ifaces = viper.Get("IFACES").(string)
|
||||
config.Timeout = viper.GetInt("TIMEOUT")
|
||||
config.TrimHist = viper.GetInt("TRIM_HIST")
|
||||
config.HistInDB = viper.GetBool("HIST_IN_DB")
|
||||
config.ShoutURL = viper.Get("SHOUTRRR_URL").(string)
|
||||
|
||||
config.UseDB = viper.Get("USE_DB").(string)
|
||||
|
|
@ -75,6 +77,7 @@ func Write(config models.Conf) {
|
|||
viper.Set("IFACES", config.Ifaces)
|
||||
viper.Set("TIMEOUT", config.Timeout)
|
||||
viper.Set("TRIM_HIST", config.TrimHist)
|
||||
viper.Set("HIST_IN_DB", config.HistInDB)
|
||||
viper.Set("SHOUTRRR_URL", config.ShoutURL)
|
||||
|
||||
viper.Set("USE_DB", config.UseDB)
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ type Conf struct {
|
|||
ArpArgs string
|
||||
Timeout int
|
||||
TrimHist int
|
||||
HistInDB bool
|
||||
ShoutURL string
|
||||
// PostgreSQL
|
||||
UseDB string
|
||||
|
|
|
|||
|
|
@ -21,7 +21,9 @@ func apiAll(c *gin.Context) {
|
|||
func apiHistory(c *gin.Context) {
|
||||
var hosts []models.Host
|
||||
|
||||
histHosts := db.Select("history")
|
||||
if appConfig.HistInDB {
|
||||
histHosts = db.Select("history")
|
||||
}
|
||||
|
||||
mac := c.Param("mac")
|
||||
|
||||
|
|
|
|||
|
|
@ -58,6 +58,13 @@ func saveSettingsHandler(c *gin.Context) {
|
|||
appConfig.Timeout, _ = strconv.Atoi(timeout)
|
||||
appConfig.TrimHist, _ = strconv.Atoi(trimHist)
|
||||
|
||||
histdb := c.PostForm("histdb")
|
||||
if histdb == "on" {
|
||||
appConfig.HistInDB = true
|
||||
} else {
|
||||
appConfig.HistInDB = false
|
||||
}
|
||||
|
||||
conf.Write(appConfig)
|
||||
|
||||
slog.Info("Writing new config to " + appConfig.ConfPath)
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ var (
|
|||
// appConfig - config for Web Gui
|
||||
appConfig models.Conf
|
||||
|
||||
allHosts []models.Host
|
||||
// histHosts []models.Host
|
||||
allHosts []models.Host
|
||||
histHosts []models.Host
|
||||
|
||||
quitScan chan bool
|
||||
)
|
||||
|
|
|
|||
|
|
@ -64,7 +64,10 @@ func compareHosts(foundHosts []models.Host) {
|
|||
db.Update("now", aHost)
|
||||
|
||||
aHost.Date = time.Now().Format("2006-01-02 15:04:05")
|
||||
db.Insert("history", aHost)
|
||||
histHosts = append(histHosts, aHost)
|
||||
if appConfig.HistInDB {
|
||||
db.Insert("history", aHost)
|
||||
}
|
||||
if appConfig.InfluxEnable {
|
||||
influx.Add(appConfig, aHost)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,10 +70,6 @@
|
|||
<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">
|
||||
|
|
@ -84,6 +80,22 @@
|
|||
<option value="error">error</option>
|
||||
</select></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Trim History (hours)</td>
|
||||
<td><input name="trim" type="number" class="form-control" value="{{ .Config.TrimHist }}"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Store History in DB</td>
|
||||
<td>
|
||||
<div class="form-check form-switch">
|
||||
{{ if .Config.HistInDB }}
|
||||
<input class="form-check-input" type="checkbox" name="histdb" checked>
|
||||
{{ else }}
|
||||
<input class="form-check-input" type="checkbox" name="histdb">
|
||||
{{ end }}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Use DB</td>
|
||||
<td><select name="usedb" class="form-select">
|
||||
|
|
@ -173,10 +185,11 @@
|
|||
<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>Trim History</b> - remove history after (hours)</p>
|
||||
<p>● <b>Store History in DB</b> - if off, the History will be stored only in memory and will be lost on app restart. Though, it will keep the app DB smaller and InfluxDB is recommended for long term History storage</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 <a href="https://github.com/aceberg" target="_blank">here</a></p>
|
||||
<p>● Commission you own app (Golang, HTML/JS, Flutter). Contact <a href="https://github.com/aceberg" target="_blank">here</a></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/aceberg/WatchYourLAN/internal/db"
|
||||
"github.com/aceberg/WatchYourLAN/internal/models"
|
||||
)
|
||||
|
||||
func trimHistoryRoutine() {
|
||||
|
|
@ -24,19 +25,35 @@ func trimHistory() {
|
|||
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")
|
||||
history := histHosts
|
||||
if appConfig.HistInDB {
|
||||
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 {
|
||||
for i, 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)
|
||||
if appConfig.HistInDB {
|
||||
db.Delete("history", hist.ID)
|
||||
} else {
|
||||
historyDel(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue