From 7536f0285cde47cabaedfd3c6519152f819eda36 Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Mon, 15 Jan 2024 14:39:27 +0000 Subject: [PATCH] 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 --------- Co-authored-by: Karl-Aksel Puulmann --- lib/plausible/stats/fragments.ex | 2 +- .../api/stats_controller/main_graph_test.exs | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/plausible/stats/fragments.ex b/lib/plausible/stats/fragments.ex index 19808288d..a4caea33b 100644 --- a/lib/plausible/stats/fragments.ex +++ b/lib/plausible/stats/fragments.ex @@ -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 diff --git a/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs b/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs index fa1b2bf11..2174943ef 100644 --- a/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs @@ -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