bugfix: #316 Entity file change debouce timer now uses delay after initial event (#321)

This commit is contained in:
James Read 2024-05-26 18:01:37 +01:00 committed by GitHub
parent 046ffaecf4
commit dc7ff40da6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 50 additions and 16 deletions

View File

@ -89,7 +89,7 @@ func loadEntityFileJson(filename string, entityname string) {
data = append(data, d) data = append(data, d)
} }
updateEvmFromFile(entityname, data) updateSvFromFile(entityname, data)
} }
func loadEntityFileYaml(filename string, entityname string) { func loadEntityFileYaml(filename string, entityname string) {
@ -113,10 +113,12 @@ func loadEntityFileYaml(filename string, entityname string) {
log.Errorf("Unmarshal: %v", err) log.Errorf("Unmarshal: %v", err)
} }
updateEvmFromFile(entityname, data) updateSvFromFile(entityname, data)
} }
func updateEvmFromFile(entityname string, data []map[string]string) { func updateSvFromFile(entityname string, data []map[string]string) {
log.Debugf("updateSvFromFile: %+v", data)
count := len(data) count := len(data)
sv.RemoveKeysThatStartWith("entities." + entityname) sv.RemoveKeysThatStartWith("entities." + entityname)

View File

@ -5,18 +5,27 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"path/filepath" "path/filepath"
"time" "time"
"sync"
) )
var ( var (
debounceWriteLog map[string]time.Time debounceWriteLog map[string]*FsNotifyLogEntry
debounceWriteLogMutex = sync.Mutex{}
) )
func init() { func init() {
debounceWriteLog = make(map[string]time.Time) debounceWriteLog = make(map[string]*FsNotifyLogEntry)
}
type FsNotifyLogEntry struct {
callbackWrapper *time.Timer
callbackComplete bool
} }
const ( const (
debounceDelay = 300 debounceDelay = 300 * time.Millisecond
) )
type watchContext struct { type watchContext struct {
@ -117,22 +126,44 @@ func consumeEvent(ok bool, ctx *watchContext) bool {
func consumeRelevantEvents(ctx *watchContext) { func consumeRelevantEvents(ctx *watchContext) {
if ctx.event.Has(ctx.interestedEvent) { if ctx.event.Has(ctx.interestedEvent) {
log.Debugf("fsnotify write event: %v", ctx.event) log.Debugf("fsnotify event relevant: %v", ctx.event)
processDebounce(ctx) processDebounce(ctx)
} else { } else {
log.Debugf("fsnotify irrelevant event on file %v", ctx.event) log.Debugf("fsnotify event irrelevant: %v", ctx.event)
} }
} }
func processDebounce(ctx *watchContext) { func processDebounce(ctx *watchContext) {
entry, found := debounceWriteLog[ctx.filename] debounceWriteLogMutex.Lock()
if !found || time.Since(entry) < debounceDelay { logEntry, found := debounceWriteLog[ctx.filename]
debounceWriteLog[ctx.filename] = time.Now()
ctx.callback(ctx.event.Name) if !found {
} else { logEntry = &FsNotifyLogEntry{
log.Debugf("Supressing write event because it's within the debounce delay: %v", ctx.filename) callbackComplete: false,
callbackWrapper: nil,
}
debounceWriteLog[ctx.filename] = logEntry
} }
log.Infof("fsnotify event %+v", logEntry)
if logEntry.callbackComplete || logEntry.callbackWrapper == nil {
log.Debugf("fsnotify event callback queued within debounce delay: %v", ctx.filename)
logEntry.callbackComplete = false
logEntry.callbackWrapper = time.AfterFunc(debounceDelay, func() {
log.Debugf("fsnotify event callback being fired: %v", ctx.filename)
ctx.callback(ctx.event.Name)
logEntry.callbackComplete = true
})
} else {
log.Debugf("fsnotify event suppressed because it's within the debounce delay: %v", ctx.filename)
}
debounceWriteLogMutex.Unlock()
} }

View File

@ -173,7 +173,7 @@ function marshalDashboardStructureToHtml (json) {
const nav = document.getElementById('navigation-links') const nav = document.getElementById('navigation-links')
for (const dashboard of json.dashboards) { for (const dashboard of json.dashboards) {
const oldsection = document.querySelector('section[title="' + dashboard.title + '"]') const oldsection = document.querySelector('section[title="' + dashboard.title.replace(' ', '') + '"]')
if (oldsection != null) { if (oldsection != null) {
oldsection.remove() oldsection.remove()

View File

@ -134,7 +134,8 @@ function main () {
initMarshaller() initMarshaller()
setupLogSearchBox() setupLogSearchBox()
window.addEventListener('EventConfigChanged', fetchGetDashboardComponents) // For websocket window.addEventListener('EventConfigChanged', fetchGetDashboardComponents)
window.addEventListener('EventEntityChanged', fetchGetDashboardComponents)
window.fetch('webUiSettings.json').then(res => { window.fetch('webUiSettings.json').then(res => {
return res.json() return res.json()