benwbrum / fromthepage

FromThePage is a wiki-like application for crowdsourcing transcription of handwritten documents.
http://fromthepage.com
GNU Affero General Public License v3.0
171 stars 51 forks source link

Admin "searches" view returns HTTP 500 when there are no searches with hits #4388

Open bencomp opened 1 month ago

bencomp commented 1 month ago

In our test environment I tried a single search that had no results. I then went to the admin dashboard, clicked the "Searches" tab and was presented with an internal server error, instead of the overview I had seen when there were no searches at all.

The logs show:

I, [2024-10-24T12:50:41.041167 #295]  INFO -- : Started GET "/admin/searches" for <IP> at 2024-10-24 12:50:41 +0000
I, [2024-10-24T12:50:41.043500 #295]  INFO -- : Processing by AdminController#searches as HTML
I, [2024-10-24T12:50:41.066908 #295]  INFO -- : Completed 500  in 23ms (ActiveRecord: 4.2ms | Allocations: 7768)
F, [2024-10-24T12:50:41.069929 #295] FATAL -- :   
NoMethodError (undefined method `round' for nil:NilClass):

app/controllers/admin_controller.rb:304:in `searches'
app/controllers/application_controller.rb:78:in `switch_locale'
app/controllers/application_controller.rb:33:in `check_deleted_articles'

https://github.com/benwbrum/fromthepage/blob/8408e0899541dabbb9ed4155857beb0253e8a9cd/app/controllers/admin_controller.rb#L299-L310

According to https://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-average, the .average method returns nil when there are no rows. In my case, I hadn't searched for a project, so it breaks on line 304. I guess that if I had only searched for a project but not for or within a collection, it would break on line 305.