added db prune job, remove data that doesn't change

This commit is contained in:
BroodjeAap 2022-12-19 21:57:30 +00:00
parent 3cf87150a7
commit 2b410431b4
2 changed files with 30 additions and 2 deletions

29
main.go
View file

@ -144,6 +144,11 @@ func (web *Web) initCronJobs() {
log.Println("Started CronJob for WatchID", cronFilter.WatchID, "with schedule:", cronFilter.Var1)
web.cronWatch[cronFilter.ID] = entryID
}
if viper.IsSet("database.prune") {
pruneSchedule := viper.GetString("database.prune")
web.cron.AddFunc(pruneSchedule, web.pruneDB)
log.Println("Started DB prune cronjob:", pruneSchedule)
}
web.cron.Start()
}
@ -157,6 +162,30 @@ func (web *Web) initNotifiers() {
}
}
func (web *Web) pruneDB() {
log.Println("Starting database pruning")
var storeNames []string
web.db.Model(&FilterOutput{}).Distinct().Pluck("name", &storeNames)
for _, storeName := range storeNames {
log.Println("Pruning:", storeName)
var values []FilterOutput
web.db.Model(&FilterOutput{}).Order("time asc").Find(&values, fmt.Sprintf("name = '%s'", storeName))
IDs := make([]uint, 0, len(values))
for i := range values {
if i > len(values)-3 {
break
}
a := values[i]
b := values[i+1]
c := values[i+2]
if a.Value == b.Value && b.Value == c.Value {
IDs = append(IDs, b.ID)
}
}
web.db.Delete(&FilterOutput{}, IDs)
}
}
func (web *Web) notify(notifierKey string, message string) {
if notifierKey == "All" {
for _, notifier := range web.notifiers {

View file

@ -9,5 +9,4 @@
- etch?
- trusted proxies in conf?
- log things to db for cron runs
- comments
- db prune job
- comments