Fix weekly interval bug (#3686)

* Fix a bug

This commit fixes a bug where the timeseries weekly interval query for a
month returned 0 visitors for the first week.

This happened because the start of the month defining the first week was
missing the UTC -> site.timezone conversion.

* Update test/plausible_web/controllers/api/stats_controller/main_graph_test.exs

Remove dot from test description

Co-authored-by: Karl-Aksel Puulmann <macobo@users.noreply.github.com>

---------

Co-authored-by: Karl-Aksel Puulmann <macobo@users.noreply.github.com>
This commit is contained in:
RobertJoonas 2024-01-15 14:39:27 +00:00 committed by GitHub
parent eaa7020230
commit 7536f0285c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -92,7 +92,7 @@ defmodule Plausible.Stats.Fragments do
quote do
weekstart_not_before(
to_timezone(unquote(date), unquote(timezone)),
unquote(not_before)
to_timezone(unquote(not_before), unquote(timezone))
)
end
end

View File

@ -688,6 +688,31 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
}
end
test "returns stats for the first week of the month when site timezone is ahead of UTC", %{
conn: conn,
site: site
} do
site =
site
|> Plausible.Site.changeset(%{timezone: "Europe/Copenhagen"})
|> Plausible.Repo.update!()
populate_stats(site, [
build(:pageview, timestamp: ~N[2023-03-01 12:00:00])
])
conn =
get(
conn,
"/api/stats/#{site.domain}/main-graph?period=month&metric=visitors&date=2023-03-01&interval=week"
)
%{"labels" => labels, "plot" => plot} = json_response(conn, 200)
assert List.first(plot) == 1
assert List.first(labels) == "2023-03-01"
end
test "shows half-perfect week-split month on week scale with full week indicators", %{
conn: conn,
site: site