Skip to content

Commit

Permalink
refactor: optimize query for retrieving top bird data
Browse files Browse the repository at this point in the history
- Updated the SQL query in the GetTopBirdsData function to use MAX(scientific_name) for better accuracy in species reporting.
- Improved code readability by separating the query construction from the execution, enhancing maintainability.
  • Loading branch information
tphakala committed Dec 20, 2024
1 parent 2ed97ff commit 5323e45
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions internal/datastore/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,15 @@ func (ds *DataStore) GetTopBirdsData(selectedDate string, minConfidenceNormalize
// Get the number of species to report from the dashboard settings
reportCount := conf.Setting().Realtime.Dashboard.SummaryLimit

err := ds.DB.Table("notes").
Select("common_name", "scientific_name", "COUNT(*) as count").
// First, get the count and common names
query := ds.DB.Table("notes").
Select("common_name, MAX(scientific_name) as scientific_name, COUNT(*) as count").
Where("date = ? AND confidence >= ?", selectedDate, minConfidenceNormalized).
Group("common_name").
Order("count DESC").
Limit(reportCount).
Scan(&results).Error
Limit(reportCount)

err := query.Scan(&results).Error
return results, err
}

Expand Down

1 comment on commit 5323e45

@HWittingen
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to have solved the reported issue.

Please sign in to comment.