Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/plausible/stats/sql/expression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Plausible.Stats.SQL.Expression do
"""
timeSlots(
toTimeZone(greatest(?, ?), ?),
toUInt32(timeDiff(greatest(?, ?), least(?, ?))),
toUInt32(timeDiff(greatest(?, ?), least(if(timeDiff(?, ?) < 1800, ?, ?), ?))),
toUInt32(?)
)
""",
Expand All @@ -43,6 +43,9 @@ defmodule Plausible.Stats.SQL.Expression do
s.start,
^unquote(first),
s.timestamp,
^unquote(query).now,
^unquote(query).now,
s.timestamp,
^unquote(last),
^unquote(period_in_seconds)
)
Expand Down
25 changes: 25 additions & 0 deletions test/plausible/stats/query/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@ defmodule Plausible.Stats.QueryTest do
end
end

describe "active session smearing on time:minute graphs" do
test "active sessions (last event within 30 min) are smeared through current time, not just last event",
%{site: site} do
now = ~U[2024-01-01 12:00:00Z]

populate_stats(site, [
# Visitor had an event 5 minutes ago - session is still active (within 30 min timeout)
build(:pageview, user_id: @user_id, timestamp: ~N[2024-01-01 11:55:00])
])

{:ok, query} =
QueryBuilder.build(site, %ParsedQueryParams{
metrics: [:visitors],
input_date_range: :realtime_30m,
dimensions: ["time:minute"],
now: now
})

%Stats.QueryResult{results: results} = Stats.query(site, query)

# The current minute (12:00) should show the visitor since their session is still active
assert %{dimensions: ["2024-01-01 12:00:00"], metrics: [1]} in results
end
end

describe "session smearing respects query date range boundaries" do
test "time:hour does not include buckets from outside the query range",
%{site: site} do
Expand Down
Loading