Skip to content

Commit

Permalink
refactor: streamline species configuration handling and improve actio…
Browse files Browse the repository at this point in the history
…n processing

- Removed the deprecated species configuration loading logic, consolidating species settings into a new structure for better clarity and maintainability.
- Updated action processing to utilize the new species thresholds and action configurations, enhancing the filtering logic for species inclusion/exclusion.
- Introduced helper functions for better management of script parameters and action handling, ensuring a more modular approach.
- Improved logging for debugging purposes, providing clearer insights into species configuration usage during processing.
  • Loading branch information
tphakala committed Jan 7, 2025
1 parent 34fa16c commit 56b8ba0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 197 deletions.
42 changes: 29 additions & 13 deletions internal/analysis/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ func New(settings *conf.Settings, ds datastore.Interface, bn *birdnet.BirdNET, m
// Start the held detection flusher
p.pendingDetectionsFlusher()

// Load Species configs
p.Settings.Realtime.Species, _ = LoadSpeciesConfig(conf.SpeciesConfigCSV)

// Initialize BirdWeather client if enabled in settings
if settings.Realtime.Birdweather.Enabled {
var err error
Expand Down Expand Up @@ -270,11 +267,16 @@ func (p *Processor) processResults(item queue.Results) []Detections {
continue
}

// Match against location-based filter
//if !p.Settings.IsSpeciesIncluded(result.Species) {
if !p.Settings.IsSpeciesIncluded(scientificName) {
// Check include/exclude lists from new structure
if len(p.Settings.Realtime.Species.Include) > 0 {
if !contains(p.Settings.Realtime.Species.Include, speciesLowercase) {
continue
}
}

if contains(p.Settings.Realtime.Species.Exclude, speciesLowercase) {
if p.Settings.Debug {
log.Printf("Species not on included list: %s\n", commonName)
log.Printf("Species excluded: %s\n", commonName)
}
continue
}
Expand Down Expand Up @@ -327,13 +329,16 @@ func (p *Processor) handleHumanDetection(item queue.Results, speciesLowercase st

// getBaseConfidenceThreshold retrieves the confidence threshold for a species, using custom or global thresholds.
func (p *Processor) getBaseConfidenceThreshold(speciesLowercase string) float32 {
confidenceThreshold, exists := p.Settings.Realtime.Species.Threshold[speciesLowercase]
if !exists {
confidenceThreshold = float32(p.Settings.BirdNET.Threshold)
} else if p.Settings.Debug {
log.Printf("\nUsing confidence threshold of %.2f for %s\n", confidenceThreshold, speciesLowercase)
// Check if species has a custom threshold in the new structure
if threshold, exists := p.Settings.Realtime.Species.Thresholds[speciesLowercase]; exists {
if p.Settings.Debug {
log.Printf("\nUsing custom confidence threshold of %.2f for %s\n", threshold.Confidence, speciesLowercase)
}
return float32(threshold.Confidence)
}
return confidenceThreshold

// Fall back to global threshold
return float32(p.Settings.BirdNET.Threshold)
}

// generateClipName generates a clip name for the given scientific name and confidence.
Expand Down Expand Up @@ -474,3 +479,14 @@ func (p *Processor) pendingDetectionsFlusher() {
}
}()
}

// Helper function to check if a slice contains a string (case-insensitive)
func contains(slice []string, item string) bool {
item = strings.ToLower(item)
for _, s := range slice {
if strings.ToLower(s) == item {
return true
}
}
return false
}
165 changes: 0 additions & 165 deletions internal/analysis/processor/species_config.go

This file was deleted.

53 changes: 34 additions & 19 deletions internal/analysis/processor/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,49 @@ func (p *Processor) actionWorker() {

// getActionsForItem determines the actions to be taken for a given detection.
func (p *Processor) getActionsForItem(detection Detections) []Action {
// match lower case
speciesName := strings.ToLower(detection.Note.CommonName)
speciesConfig, exists := p.Settings.Realtime.Species.Actions[speciesName]

var actions []Action
if exists {
// Check if species has custom configuration
if speciesConfig, exists := p.Settings.Realtime.Species.Thresholds[speciesName]; exists {
if p.Settings.Debug {
log.Println("Species config exists for custom actions")
}
customActions := p.createActionsFromConfig(speciesConfig, detection)

// Determine whether to use only custom actions or combine with default actions
if speciesConfig.OnlyActions {
//log.Println("Only using custom actions for", speciesName)
actions = customActions
} else {
//log.Println("Using default actions with custom actions for", speciesName)
defaultActions := p.getDefaultActions(detection)
actions = append(defaultActions, customActions...)

var actions []Action

// Add custom actions from the new structure
for _, actionConfig := range speciesConfig.Actions {
switch actionConfig.Type {
case "ExecuteScript":
if len(actionConfig.Parameters) > 0 {
actions = append(actions, ExecuteScriptAction{
ScriptPath: actionConfig.Parameters[0],
Params: parseScriptParams(actionConfig.Parameters[1:], detection),
})
}
case "SendNotification":
// Add notification action handling
// ... implementation ...
}
}
} else {
if p.Settings.Debug {
log.Println("No species config found, using default actions for", speciesName)

// If OnlyActions is true, return only custom actions
if len(actions) > 0 {
return actions
}
actions = p.getDefaultActions(detection)
}

return actions
// Fall back to default actions if no custom actions or if custom actions should be combined
return p.getDefaultActions(detection)
}

// Helper function to parse script parameters
func parseScriptParams(params []string, detection Detections) map[string]interface{} {
scriptParams := make(map[string]interface{})
for _, param := range params {
scriptParams[param] = getNoteValueByName(detection.Note, param)
}
return scriptParams
}

// getDefaultActions returns the default actions to be taken for a given detection.
Expand Down

0 comments on commit 56b8ba0

Please sign in to comment.