From 5323e450ef056a5d1b81c822bfadcaa91e98c275 Mon Sep 17 00:00:00 2001 From: "Tomi P. Hakala" Date: Fri, 20 Dec 2024 22:40:25 +0200 Subject: [PATCH] refactor: optimize query for retrieving top bird data - 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. --- internal/datastore/interfaces.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/datastore/interfaces.go b/internal/datastore/interfaces.go index 14a33c3..97af0d4 100644 --- a/internal/datastore/interfaces.go +++ b/internal/datastore/interfaces.go @@ -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 }