Skip to content

Commit f72b151

Browse files
ukutahtaerosol
andauthored
Reduce imported opts queries (#6106)
* Reduce database queries for imported_opts in sparkline graphs * Conditionally preload imports in query builder * Extract Imported.schema_supports_interval?/1 * Fix test failures * Don't join sessions for regular site cards (#6109) --------- Co-authored-by: Adam Rutkowski <hq@mtod.org>
1 parent 5a26631 commit f72b151

4 files changed

Lines changed: 48 additions & 13 deletions

File tree

lib/plausible/stats/imported/imported.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ defmodule Plausible.Stats.Imported do
2525
length(Imported.Base.decide_tables(query)) > 0
2626
end
2727

28+
def schema_supports_interval?(query) do
29+
"time:minute" not in query.dimensions and
30+
"time:hour" not in query.dimensions
31+
end
32+
2833
def merge_imported_country_suggestions(native_q, _site, %Plausible.Stats.Query{
2934
include_imported: false
3035
}) do

lib/plausible/stats/query.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ defmodule Plausible.Stats.Query do
151151
requested? = query.include.imports
152152

153153
query =
154-
if site do
154+
if site && Imported.schema_supports_interval?(query) do
155+
site = Plausible.Repo.preload(site, :completed_imports)
156+
155157
struct!(query,
156158
imports_exist: Plausible.Imported.any_completed_imports?(site),
157159
imports_in_range: get_imports_in_range(site, query)
@@ -188,18 +190,18 @@ defmodule Plausible.Stats.Query do
188190
end
189191

190192
@spec get_skip_imported_reason(t()) ::
191-
nil | :no_imported_data | :out_of_range | :unsupported_query
193+
nil | :no_imported_data | :out_of_range | :unsupported_interval | :unsupported_query
192194
def get_skip_imported_reason(query) do
193195
cond do
196+
not Imported.schema_supports_interval?(query) ->
197+
:unsupported_interval
198+
194199
not query.imports_exist ->
195200
:no_imported_data
196201

197202
query.imports_in_range == [] ->
198203
:out_of_range
199204

200-
"time:minute" in query.dimensions or "time:hour" in query.dimensions ->
201-
:unsupported_interval
202-
203205
not Imported.schema_supports_query?(query) ->
204206
:unsupported_query
205207

lib/plausible/stats/sparkline.ex

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,42 @@ defmodule Plausible.Stats.Sparkline do
7373
end
7474

7575
defp query_24h_stats(view_or_site, now) do
76+
if Plausible.Sites.regular?(view_or_site) do
77+
query_24h_stats_regular(view_or_site, now)
78+
else
79+
query_24h_stats_consolidated(view_or_site, now)
80+
end
81+
end
82+
83+
defp query_24h_stats_regular(site, now) do
7684
stats_query =
77-
QueryBuilder.build!(view_or_site,
85+
QueryBuilder.build!(site,
86+
now: DateTime.from_naive!(now, "Etc/UTC"),
87+
input_date_range: :"24h",
88+
metrics: [:visitors],
89+
include: [compare: :previous_period]
90+
)
91+
92+
%Stats.QueryResult{
93+
results: [
94+
%{
95+
metrics: [visitors],
96+
comparison: %{
97+
change: [visitors_change]
98+
}
99+
}
100+
]
101+
} = Stats.query(site, stats_query)
102+
103+
%{
104+
visitors: visitors,
105+
visitors_change: visitors_change
106+
}
107+
end
108+
109+
defp query_24h_stats_consolidated(view, now) do
110+
stats_query =
111+
QueryBuilder.build!(view,
78112
now: DateTime.from_naive!(now, "Etc/UTC"),
79113
input_date_range: :"24h",
80114
metrics: [:visitors, :visits, :pageviews, :views_per_visit],
@@ -90,7 +124,7 @@ defmodule Plausible.Stats.Sparkline do
90124
}
91125
}
92126
]
93-
} = Stats.query(view_or_site, stats_query)
127+
} = Stats.query(view, stats_query)
94128

95129
%{
96130
visitors: visitors,

test/plausible/stats/sparkline_test.exs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ defmodule Plausible.Stats.SparklineTest do
3636
assert %{
3737
^domain => %{
3838
visitors: 0,
39-
pageviews: 0,
40-
pageviews_change: 0,
41-
views_per_visit: +0.0,
42-
views_per_visit_change: 0,
4339
visitors_change: 0,
44-
visits: 0,
45-
visits_change: 0,
4640
intervals: intervals
4741
}
4842
} =

0 commit comments

Comments
 (0)