Skip to content

Commit bde4675

Browse files
committed
Speed up loading of recent_responses table in admin dashboard
1 parent 1b58586 commit bde4675

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

app/controllers/admin/site_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def index
1212
@response_groups = Submission.group('date(created_at)').count.sort.last(@days_since.days)
1313
@user_groups = User.group('date(created_at)').count.sort.last(@days_since.days)
1414
@inactive_user_groups = User.where(inactive: true).group('date(updated_at)').count.sort.last(@days_since.days)
15-
todays_submissions = Submission.where('created_at > ?', Time.zone.now - @days_since.days)
1615

1716
# Add in 0 count days to fetched analytics
1817
@dates.each do |date|
@@ -24,8 +23,11 @@ def index
2423
@inactive_user_groups = @inactive_user_groups.sort
2524
@response_groups = @response_groups.sort
2625

27-
form_ids = todays_submissions.collect(&:form_id).uniq
28-
@recent_forms = Form.includes(:organization).find(form_ids)
26+
@recent_forms = Form.includes(:organization)
27+
.joins(:submissions)
28+
.where("submissions.created_at > ?", Time.zone.now - @days_since.days)
29+
.select("forms.*", "count(distinct submissions.id) as recent_submissions_count")
30+
.group("forms.id")
2931
end
3032

3133
def a11_v2_collections; end

app/views/components/admin/_recent_responses.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<td>
2424
<%= link_to form.name, admin_form_path(form) %>
2525
</td>
26-
<td data-sort-value="<%= c = form.submissions.where("created_at > ?", Time.now - days_since.days).count %>">
26+
<td data-sort-value="<%= c = form.recent_submissions_count %>">
2727
<%= number_with_delimiter(c) %>
2828
</td>
2929
</tr>

0 commit comments

Comments
 (0)