Skip to content

Commit b0592b2

Browse files
committed
trying to optimise count in map view even more
1 parent d266e57 commit b0592b2

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

app/controllers/xronos_data_controller.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ def index
3939
# HTML (/data)
4040
# ---------------------------
4141
format.html do
42+
if unfiltered_request?
43+
# Unfiltered case: we only need one count,
44+
# and filtered == total. Cache it.
45+
base_relation = @data.everything.except(:select, :includes, :order)
46+
47+
@total_count = @filtered_count =
48+
Rails.cache.fetch(cache_key_for("total_count"), expires_in: 10.minutes) do
49+
base_relation.distinct.count(:id)
50+
end
51+
else
52+
# Filtered case: we need both numbers
53+
filtered_relation = @data.xrons.except(:select, :includes, :order)
54+
total_relation = @data.everything.except(:select, :includes, :order)
55+
56+
@filtered_count = filtered_relation.distinct.count(:id)
57+
@total_count = Rails.cache.fetch(cache_key_for("total_count"), expires_in: 10.minutes) do
58+
total_relation.distinct.count(:id)
59+
end
60+
end
61+
62+
# your existing includes + pagination
4263
xrons_relation = @data.xrons.includes(
4364
sample: [
4465
:material,
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<% if @data.filters.any? %>
2-
<span class="<%= @data.xrons.any? ? "text-success" : "text-danger" %>">
3-
Showing <%= @data.xrons.count %> of <%= @data.everything.count %> records
2+
<span class="<%= @xrons.any? ? "text-success" : "text-danger" %>">
3+
Showing <%= number_with_delimiter(@filtered_count) %>
4+
of <%= number_with_delimiter(@total_count) %> records
45
(<%= link_to "clear filters", :data %>)
56
</span>
67
<% else %>
7-
<span class="text-dark">Showing <%= @data.everything.count %> records</span>
8-
<% end %>
8+
<span class="text-dark">
9+
Showing <%= number_with_delimiter(@total_count) %> records
10+
</span>
11+
<% end %>

app/views/xronos_data/index.html.erb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<% content_for :title, "Browse data" %>
2-
<% record_count = @pagy&.count || @xrons.size %>
32
<% content_for :meta_description,
4-
"Browse and download #{record_count} chronometric records from XRONOS, including radiocarbon dates, typological classifications, and dendrochronological dates. Filter by countries, specific archaeological site(s), site types, sample materials, sample taxa, radiocarbon laboratories, or laboratory IDs." %>
3+
"Browse and download #{@total_count} chronometric records from XRONOS, including radiocarbon dates, typological classifications, and dendrochronological dates. Filter by countries, specific archaeological site(s), site types, sample materials, sample taxa, radiocarbon laboratories, or laboratory IDs." %>
54

65
<div class="px-1 py-2 border-bottom shadow-sm">
76
<%= render "toolbar" %>

0 commit comments

Comments
 (0)