Skip to content

Commit 6be0b06

Browse files
authored
Stop ordering by name and pathname in steps_query window definitions (#6285)
* Stop ordering by name and pathname in steps_query window definitions * Add explicit ordering by timestamp * Ensure consistent ordering between windows via row number enumeration * Remove explicit ordering by timestamp from `steps_query` * Filter out entries with 0 visitors (likely artifact or window optimisation)
1 parent 031e96d commit 6be0b06

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

extra/lib/plausible/stats/exploration.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,15 @@ defmodule Plausible.Stats.Exploration do
269269
),
270270
on: true,
271271
hints: "ARRAY",
272+
where: selected_as(:visitors) > 0,
272273
select: %{
273274
name: m.name,
274275
pathname: m.pathname,
275-
visitors: fragment("if(?, ?, ?)", is_wildcard, m.wildcard_visitors, m.exact_visitors),
276+
visitors:
277+
selected_as(
278+
fragment("if(?, ?, ?)", is_wildcard, m.wildcard_visitors, m.exact_visitors),
279+
:visitors
280+
),
276281
includes_subpaths: fragment("CAST(?, 'Bool')", is_wildcard),
277282
subpaths_count: fragment("if(?, ?, 0)", is_wildcard, m.subpaths_count)
278283
}
@@ -408,20 +413,19 @@ defmodule Plausible.Stats.Exploration do
408413
end
409414

410415
defp steps_query(query, steps, direction) when is_integer(steps) do
411-
event_ordering = [asc: :timestamp, asc: :name, asc: :pathname]
412-
413416
q_pairs =
414417
from(e in query,
415418
windows: [
416419
session_window: [
417420
partition_by: e.user_id,
418-
order_by: ^event_ordering
421+
order_by: [asc: e.timestamp]
419422
]
420423
],
421424
select: %{
422425
site_id: e.site_id,
423426
user_id: e.user_id,
424427
_sample_factor: e._sample_factor,
428+
row_number: row_number() |> over(:session_window),
425429
name: e.name,
426430
pathname:
427431
fragment("if(? = '/', ?, trimRight(?, '/'))", e.pathname, e.pathname, e.pathname),
@@ -433,7 +437,9 @@ defmodule Plausible.Stats.Exploration do
433437

434438
q_steps =
435439
from(e in subquery(q_pairs),
436-
windows: [step_window: [partition_by: e.user_id, order_by: ^event_ordering]],
440+
windows: [
441+
step_window: [partition_by: e.user_id, order_by: [asc: e.timestamp, asc: e.row_number]]
442+
],
437443
select: %{
438444
user_id: e.user_id,
439445
_sample_factor: e._sample_factor,

0 commit comments

Comments
 (0)