-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve configuration reload #378
Conversation
…ctionality - Added validateModelAndLabels method to ensure the number of labels matches the model's output size, improving error handling and debugging. - Implemented ReloadModel method to safely reload the BirdNET model and labels, including mutex locking for thread safety and cleanup of old interpreters upon failure. - Removed unused SpeciesListUpdated timestamp field to streamline the BirdNET struct. - Improved error messages for model and label loading failures, enhancing clarity during debugging.
- Updated RealtimeAnalysis to accept a notification channel for handling status updates. - Enhanced startControlMonitor to send notifications for range filter rebuild and model reload events, improving user feedback during real-time analysis. - Modified HTTP server and handlers to include the notification channel, ensuring consistent notification management across components.
…ions - Implemented checks to detect changes in BirdNET settings and send notifications for model reloads. - Added notifications for range filter rebuilds when related settings change. - Improved error handling and logging during settings updates, ensuring better user feedback and traceability. - Introduced functions to check for specific changes in BirdNET settings, enhancing configuration management.
WalkthroughThe pull request introduces a comprehensive notification mechanism across multiple components of the application. The changes focus on adding a new Changes
Sequence DiagramsequenceDiagram
participant Server
participant Handlers
participant Analysis
participant BirdNET
Server->>Handlers: Initialize with notificationChan
Handlers->>Analysis: Pass notificationChan
Analysis->>BirdNET: Trigger model operations
BirdNET-->>Analysis: Send notifications on success/failure
Analysis-->>Handlers: Forward notifications
Handlers-->>Server: Process notifications
Poem
Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Nitpick comments (3)
internal/httpcontroller/handlers/settings.go (1)
588-597
: Consider combining settings change checks for efficiencyIn the
rangeFilterSettingsChanged
function, you are individually checking for changes in species include/exclude lists, range filter settings, and latitude/longitude. To improve maintainability and efficiency, consider combining these checks into a single comparison of relevant substructures.For example, you could use:
return !reflect.DeepEqual(oldSettings.Realtime.Species, currentSettings.Realtime.Species) || !reflect.DeepEqual(oldSettings.BirdNET.RangeFilter, currentSettings.BirdNET.RangeFilter) || oldSettings.BirdNET.Latitude != currentSettings.BirdNET.Latitude || oldSettings.BirdNET.Longitude != currentSettings.BirdNET.Longitudeinternal/httpcontroller/server.go (1)
37-37
: Consider documenting the purpose of the notification channel.Add a comment explaining the role of
notificationChan
in handling real-time feedback for configuration changes.+ // notificationChan handles real-time feedback for configuration changes notificationChan chan handlers.Notification
internal/analysis/realtime.go (1)
367-379
: Refactor duplicate range filter rebuild logic.The range filter rebuild logic after model reload duplicates the earlier implementation. Consider extracting this into a helper function.
+func rebuildRangeFilter(notificationChan chan handlers.Notification) error { + if err := birdnet.BuildRangeFilter(bn); err != nil { + log.Printf("\033[31m❌ Error handling range filter rebuild: %v\033[0m", err) + select { + case notificationChan <- handlers.Notification{ + Message: fmt.Sprintf("Failed to rebuild range filter: %v", err), + Type: "error", + }: + case <-time.After(time.Second): + log.Printf("Warning: Notification channel blocked") + } + return err + } + log.Printf("\033[32m✅ Range filter rebuilt successfully\033[0m") + select { + case notificationChan <- handlers.Notification{ + Message: "Range filter rebuilt successfully", + Type: "success", + }: + case <-time.After(time.Second): + log.Printf("Warning: Notification channel blocked") + } + return nil +}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
cmd/realtime/realtime.go
(2 hunks)internal/analysis/realtime.go
(5 hunks)internal/birdnet/birdnet.go
(1 hunks)internal/httpcontroller/handlers/handlers.go
(3 hunks)internal/httpcontroller/handlers/settings.go
(2 hunks)internal/httpcontroller/server.go
(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: test-docker-image
🔇 Additional comments (4)
internal/httpcontroller/handlers/settings.go (1)
601-626
: Ensure all relevant BirdNET settings are monitored for changesThe
birdnetSettingsChanged
function checks for changes in several BirdNET settings. Please verify that all settings which require a model reload upon change are included. If any setting is omitted, changes to it may not trigger the necessary reload, potentially causing inconsistencies during runtime.cmd/realtime/realtime.go (1)
21-22
: Initialization ofnotificationChan
for real-time analysis looks goodThe addition of the
notificationChan
channel and its integration with theRealtimeAnalysis
function enhances the notification mechanism during real-time audio analysis. This change is implemented correctly.internal/httpcontroller/server.go (1)
58-58
: Review the buffer size and add channel cleanup.The buffer size of 10 might be too small if multiple operations trigger notifications simultaneously. Also, ensure proper cleanup of the channel during shutdown.
Consider:
- Increasing the buffer size to handle concurrent notifications.
- Adding cleanup logic in the shutdown process.
internal/httpcontroller/handlers/handlers.go (1)
33-33
: LGTM! Clean integration of notification channel.The changes properly integrate the notification channel into the Handlers struct and its constructor.
Also applies to: 76-76, 95-95
return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m", | ||
modelOutputSize, len(bn.Settings.BirdNET.Labels)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Avoid including ANSI color codes and emojis in error messages
Including ANSI color codes and emojis in error messages can lead to unreadable logs or issues in environments that do not support ANSI escape sequences. It is recommended to keep error messages plain to ensure they are properly displayed and logged across different systems.
Apply this diff to remove color codes and emojis from error messages:
- return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m",
+ return fmt.Errorf("label count mismatch: model expects %d classes but label file has %d labels",
modelOutputSize, len(bn.Settings.BirdNET.Labels))
...
- return fmt.Errorf("\033[31m❌ failed to reload model: %w\033[0m", err)
+ return fmt.Errorf("failed to reload model: %w", err)
...
- return fmt.Errorf("\033[31m❌ failed to reload meta model: %w\033[0m", err)
+ return fmt.Errorf("failed to reload meta model: %w", err)
...
- return fmt.Errorf("\033[31m❌ failed to reload labels: %w\033[0m", err)
+ return fmt.Errorf("failed to reload labels: %w", err)
...
- return fmt.Errorf("\033[31m❌ model validation failed: %w\033[0m", err)
+ return fmt.Errorf("model validation failed: %w", err)
Also applies to: 341-341, 354-354, 370-370, 386-386
func (bn *BirdNET) validateModelAndLabels() error { | ||
// Get the output tensor to check its dimensions | ||
outputTensor := bn.AnalysisInterpreter.GetOutputTensor(0) | ||
if outputTensor == nil { | ||
return fmt.Errorf("cannot get output tensor") | ||
} | ||
|
||
// Get the number of classes from the model's output tensor | ||
modelOutputSize := outputTensor.Dim(outputTensor.NumDims() - 1) | ||
|
||
// Compare with the number of labels | ||
if len(bn.Settings.BirdNET.Labels) != modelOutputSize { | ||
return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m", | ||
modelOutputSize, len(bn.Settings.BirdNET.Labels)) | ||
} | ||
|
||
bn.Debug("\033[32m✅ Model validation successful: %d labels match model output size\033[0m", modelOutputSize) | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure NumDims()
is greater than zero to prevent potential panic
In the validateModelAndLabels
method, calling outputTensor.Dim(outputTensor.NumDims() - 1)
without checking if NumDims()
is greater than zero could cause a panic if the tensor has zero dimensions. Please add a check to ensure NumDims()
is greater than zero before accessing tensor dimensions.
Apply this diff to fix the issue:
func (bn *BirdNET) validateModelAndLabels() error {
// Get the output tensor to check its dimensions
outputTensor := bn.AnalysisInterpreter.GetOutputTensor(0)
if outputTensor == nil {
return fmt.Errorf("cannot get output tensor")
}
+ if outputTensor.NumDims() == 0 {
+ return fmt.Errorf("output tensor has no dimensions")
+ }
// Get the number of classes from the model's output tensor
modelOutputSize := outputTensor.Dim(outputTensor.NumDims() - 1)
// Compare with the number of labels
if len(bn.Settings.BirdNET.Labels) != modelOutputSize {
return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m",
modelOutputSize, len(bn.Settings.BirdNET.Labels))
}
bn.Debug("\033[32m✅ Model validation successful: %d labels match model output size\033[0m", modelOutputSize)
return nil
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
func (bn *BirdNET) validateModelAndLabels() error { | |
// Get the output tensor to check its dimensions | |
outputTensor := bn.AnalysisInterpreter.GetOutputTensor(0) | |
if outputTensor == nil { | |
return fmt.Errorf("cannot get output tensor") | |
} | |
// Get the number of classes from the model's output tensor | |
modelOutputSize := outputTensor.Dim(outputTensor.NumDims() - 1) | |
// Compare with the number of labels | |
if len(bn.Settings.BirdNET.Labels) != modelOutputSize { | |
return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m", | |
modelOutputSize, len(bn.Settings.BirdNET.Labels)) | |
} | |
bn.Debug("\033[32m✅ Model validation successful: %d labels match model output size\033[0m", modelOutputSize) | |
return nil | |
} | |
func (bn *BirdNET) validateModelAndLabels() error { | |
// Get the output tensor to check its dimensions | |
outputTensor := bn.AnalysisInterpreter.GetOutputTensor(0) | |
if outputTensor == nil { | |
return fmt.Errorf("cannot get output tensor") | |
} | |
if outputTensor.NumDims() == 0 { | |
return fmt.Errorf("output tensor has no dimensions") | |
} | |
// Get the number of classes from the model's output tensor | |
modelOutputSize := outputTensor.Dim(outputTensor.NumDims() - 1) | |
// Compare with the number of labels | |
if len(bn.Settings.BirdNET.Labels) != modelOutputSize { | |
return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m", | |
modelOutputSize, len(bn.Settings.BirdNET.Labels)) | |
} | |
bn.Debug("\033[32m✅ Model validation successful: %d labels match model output size\033[0m", modelOutputSize) | |
return nil | |
} |
notificationChan <- handlers.Notification{ | ||
Message: fmt.Sprintf("Failed to rebuild range filter: %v", err), | ||
Type: "error", | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider error handling for channel operations.
The notification channel writes could block if the channel is full. Consider adding a select statement with a timeout.
- notificationChan <- handlers.Notification{
- Message: fmt.Sprintf("Failed to rebuild range filter: %v", err),
- Type: "error",
- }
+ select {
+ case notificationChan <- handlers.Notification{
+ Message: fmt.Sprintf("Failed to rebuild range filter: %v", err),
+ Type: "error",
+ }:
+ case <-time.After(time.Second):
+ log.Printf("Warning: Notification channel blocked")
+ }
Also applies to: 348-351
if err := bn.ReloadModel(); err != nil { | ||
log.Printf("\033[31m❌ Error reloading BirdNET model: %v\033[0m", err) | ||
notificationChan <- handlers.Notification{ | ||
Message: fmt.Sprintf("Failed to reload BirdNET model: %v", err), | ||
Type: "error", | ||
} | ||
} else { | ||
log.Printf("\033[32m✅ BirdNET model reloaded successfully\033[0m") | ||
notificationChan <- handlers.Notification{ | ||
Message: "BirdNET model reloaded successfully", | ||
Type: "success", | ||
} | ||
// Rebuild range filter after model reload |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add timeout handling for model reload operation.
The model reload operation could potentially be time-consuming. Consider adding a timeout to prevent blocking.
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ defer cancel()
+
+ errChan := make(chan error, 1)
+ go func() {
+ errChan <- bn.ReloadModel()
+ }()
+
+ select {
+ case err := <-errChan:
if err != nil {
// ... error handling ...
} else {
// ... success handling ...
}
+ case <-ctx.Done():
+ notificationChan <- handlers.Notification{
+ Message: "Model reload timed out after 30 seconds",
+ Type: "error",
+ }
+ }
Committable suggestion skipped: line range outside the PR's diff.
BirdNET model setting changes no longer require application restart