From 29050373bb4e6440cb3e06a4a642cf2c5e34713f Mon Sep 17 00:00:00 2001 From: "Tomi P. Hakala" Date: Wed, 15 Jan 2025 17:19:55 +0200 Subject: [PATCH] feat: introduce configurable event tracking intervals - Added a new EventTrackerConfig struct to hold various configuration intervals for event tracking. - Modified the NewEventTracker function to accept a time.Duration parameter, allowing for dynamic configuration of event handler intervals. - Updated the EventTracker initialization in the processor to utilize the new configuration based on settings. These changes enhance the flexibility and customization of event tracking behavior in the application. --- internal/analysis/processor/eventtracker.go | 22 +++++++++++++++------ internal/analysis/processor/processor.go | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/internal/analysis/processor/eventtracker.go b/internal/analysis/processor/eventtracker.go index fd4f9402..9b6883b7 100644 --- a/internal/analysis/processor/eventtracker.go +++ b/internal/analysis/processor/eventtracker.go @@ -71,14 +71,24 @@ type EventTracker struct { Mutex sync.Mutex // Mutex to ensure thread-safe access } -// NewEventTracker initializes a new EventTracker with default event handlers. -func NewEventTracker() *EventTracker { +// Add this new struct to hold configuration +type EventTrackerConfig struct { + DatabaseSaveInterval time.Duration + LogToFileInterval time.Duration + NotificationInterval time.Duration + BirdWeatherSubmitInterval time.Duration + MQTTPublishInterval time.Duration +} + +// Modify NewEventTracker to accept configuration +func NewEventTracker(interval time.Duration) *EventTracker { return &EventTracker{ Handlers: map[EventType]*EventHandler{ - DatabaseSave: NewEventHandler(15*time.Second, StandardEventBehavior), - LogToFile: NewEventHandler(15*time.Second, StandardEventBehavior), - SendNotification: NewEventHandler(60*time.Minute, StandardEventBehavior), - BirdWeatherSubmit: NewEventHandler(15*time.Second, StandardEventBehavior), + DatabaseSave: NewEventHandler(interval, StandardEventBehavior), + LogToFile: NewEventHandler(interval, StandardEventBehavior), + SendNotification: NewEventHandler(interval, StandardEventBehavior), + BirdWeatherSubmit: NewEventHandler(interval, StandardEventBehavior), + MQTTPublish: NewEventHandler(interval, StandardEventBehavior), }, } } diff --git a/internal/analysis/processor/processor.go b/internal/analysis/processor/processor.go index 6bee9aa7..70866392 100644 --- a/internal/analysis/processor/processor.go +++ b/internal/analysis/processor/processor.go @@ -82,7 +82,7 @@ func New(settings *conf.Settings, ds datastore.Interface, bn *birdnet.BirdNET, m Ds: ds, Bn: bn, BirdImageCache: birdImageCache, - EventTracker: NewEventTracker(), + EventTracker: NewEventTracker(time.Duration(settings.Realtime.Interval) * time.Second), Metrics: metrics, LastDogDetection: make(map[string]time.Time), LastHumanDetection: make(map[string]time.Time),