Skip to content

Commit 50eef62

Browse files
authored
Fix breakdown comparisons breaking after pagination (#4951)
#4697 introduced some comparisons fixes which changed how comparisons interacted with main queries. Sadly this broke under pagination and due to lack of coverage wasn't caught. The bug can also be tested manually on localhost: - Change assets/js/dashboard/hooks/api-client.ts LIMIT to e.g. 3 - Open any modal, click load more - Check tooltips Before the fix this would have displayed 0 values, after the fix this works as expected.
1 parent 937d36f commit 50eef62

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ All notable changes to this project will be documented in this file.
2424
- Fix typo on login screen
2525
- Fix Direct / None details modal not opening
2626
- Fix year over year comparisons being offset by a day for leap years
27+
- Breakdown modals now display correct comparison values instead of 0 after pagination
2728

2829
## v2.1.4 - 2024-10-08
2930

lib/plausible/stats/comparisons.ex

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,15 @@ defmodule Plausible.Stats.Comparisons do
7575
defp add_query_filters(query, []), do: query
7676

7777
defp add_query_filters(query, [filter]) do
78-
Query.add_filter(query, [:ignore_in_totals_query, filter])
78+
query
79+
|> Query.add_filter([:ignore_in_totals_query, filter])
80+
|> Query.set(pagination: nil)
7981
end
8082

8183
defp add_query_filters(query, filters) do
82-
Query.add_filter(query, [:ignore_in_totals_query, [:or, filters]])
84+
query
85+
|> Query.add_filter([:ignore_in_totals_query, [:or, filters]])
86+
|> Query.set(pagination: nil)
8387
end
8488

8589
defp build_comparison_filter(%{dimensions: dimension_labels}, query) do

test/plausible_web/controllers/api/external_stats_controller/query_comparisons_test.exs

+32-5
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryComparisonsTest do
125125
build(:pageview, browser: "Chrome", timestamp: ~N[2021-01-01 00:00:00]),
126126
build(:pageview, browser: "Chrome", timestamp: ~N[2021-01-07 00:00:00]),
127127
build(:pageview, browser: "Chrome", timestamp: ~N[2021-01-07 00:00:00]),
128-
build(:pageview, browser: "Firefox", timestamp: ~N[2021-01-07 00:00:00])
128+
build(:pageview, browser: "Chrome", timestamp: ~N[2021-01-07 00:00:00]),
129+
build(:pageview, browser: "Firefox", timestamp: ~N[2021-01-07 00:00:00]),
130+
build(:pageview, browser: "Firefox", timestamp: ~N[2021-01-07 00:00:00]),
131+
build(:pageview, browser: "Safari", timestamp: ~N[2021-01-08 00:00:00])
129132
])
130133

131134
conn =
@@ -143,20 +146,44 @@ defmodule PlausibleWeb.Api.ExternalStatsController.QueryComparisonsTest do
143146
assert json_response(conn, 200)["results"] == [
144147
%{
145148
"dimensions" => ["Chrome"],
146-
"metrics" => [2, 66.7],
149+
"metrics" => [3, 50.0],
147150
"comparison" => %{
148151
"dimensions" => ["Chrome"],
149152
"metrics" => [1, 12.5],
150-
"change" => [100, 434]
153+
"change" => [200, 300]
151154
}
152155
},
153156
%{
154157
"dimensions" => ["Firefox"],
155-
"metrics" => [1, 33.3],
158+
"metrics" => [2, 33.3],
156159
"comparison" => %{
157160
"dimensions" => ["Firefox"],
158161
"metrics" => [4, 50.0],
159-
"change" => [-75, -33]
162+
"change" => [-50, -33]
163+
}
164+
}
165+
]
166+
167+
conn2 =
168+
post(conn, "/api/v2/query-internal-test", %{
169+
"site_id" => site.domain,
170+
"metrics" => ["visitors", "percentage"],
171+
"date_range" => ["2021-01-07", "2021-01-13"],
172+
"dimensions" => ["visit:browser"],
173+
"include" => %{
174+
"comparisons" => %{"mode" => "previous_period"}
175+
},
176+
"pagination" => %{"limit" => 2, "offset" => 2}
177+
})
178+
179+
assert json_response(conn2, 200)["results"] == [
180+
%{
181+
"dimensions" => ["Safari"],
182+
"metrics" => [1, 16.7],
183+
"comparison" => %{
184+
"dimensions" => ["Safari"],
185+
"metrics" => [3, 37.5],
186+
"change" => [-67, -55]
160187
}
161188
}
162189
]

0 commit comments

Comments
 (0)