Skip to content
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

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

Open
bencomp opened this issue Oct 24, 2024 · 0 comments
Open

Comments

@bencomp
Copy link
Contributor

bencomp commented Oct 24, 2024

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'

this_week = SearchAttempt.where('created_at > ?', 1.week.ago)
unless this_week.empty?
by_visit = this_week.joins(:visit).group('visits.id')
@find_a_project_searches_per_day = (this_week.where(search_type: 'findaproject').count / 7.0).round(2)
@collection_work_searches_per_day = (this_week.where.not(search_type: 'findaproject').count / 7.0).round(2)
@find_a_project_average_hits = this_week.where(search_type: 'findaproject').average(:hits).round(2)
@collection_work_average_hits = this_week.where.not(search_type: 'findaproject').average(:hits).round(2)
@clickthrough_rate = ((this_week.where('clicks > 0').count.to_f / this_week.count) * 100).round(1)
@clickthrough_rate_visit = ((by_visit.sum(:clicks).values.count{|c|c>0}.to_f / by_visit.length) * 100).round(1)
@contribution_rate = ((this_week.where('contributions > 0').count.to_f / this_week.count) * 100).round(1)
@contribution_rate_visit = ((by_visit.sum(:contributions).values.count{|c|c>0}.to_f / by_visit.length) * 100).round(1)
end

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant