Skip to content

Commit

Permalink
Fix the timeseries for the 7d period with hour interval
Browse files Browse the repository at this point in the history
When bucketing on hour, the current day needs to be taken into account for as well.
The query executed on the database already contained the correct results, but they were omitted because too few buckets were selected
  • Loading branch information
krisbuist committed Sep 22, 2023
1 parent 2c9c159 commit 0b4d92f
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ All notable changes to this project will be documented in this file.
- Update bot detection (matomo 6.1.4, ua_inspector 3.4.0)
- Improved the Goal Settings page (search, autcompletion etc.)
- Log mailer errors plausible/analytics#3336
- Fixed [Last 7 days with hourly resolution doesn't include today](https://github.com/plausible/analytics/issues/3342)

## v2.0.0 - 2023-07-12

Expand Down
10 changes: 5 additions & 5 deletions lib/plausible/stats/timeseries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ defmodule Plausible.Stats.Timeseries do
end

@full_day_in_hours 23
defp buckets(%Query{interval: "hour"} = query) do
defp buckets(%Query{interval: "hour", period: period} = query) do
n_buckets =
if query.date_range.first == query.date_range.last do
@full_day_in_hours
else
Timex.diff(query.date_range.last, query.date_range.first, :hours)
cond do
query.date_range.first == query.date_range.last -> @full_day_in_hours
# period == "7d" -> Timex.diff(query.date_range.last |> Timex.shift(days: 1), query.date_range.first, :hours)
true -> Timex.diff(query.date_range.last, query.date_range.first, :hours)
end

Enum.map(0..n_buckets, fn step ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,194 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
"2021-12-01" => false
}
end

test "shows the current day for 7d period with days interval", %{
conn: conn,
site: site
} do
conn =
get(
conn,
"/api/stats/#{site.domain}/main-graph?period=7d&metric=visitors&date=2021-09-13"
)

assert %{"labels" => labels, "full_intervals" => full_intervals} = json_response(conn, 200)

assert labels == [
"2021-09-07",
"2021-09-08",
"2021-09-09",
"2021-09-10",
"2021-09-11",
"2021-09-12",
"2021-09-13"
]

assert full_intervals == nil
end

test "shows the current day for 7d period with hour interval", %{
conn: conn,
site: site
} do
conn =
get(
conn,
"/api/stats/#{site.domain}/main-graph?period=7d&metric=visitors&interval=hour&date=2021-09-13"
)

assert %{"labels" => labels, "full_intervals" => full_intervals} = json_response(conn, 200)

assert labels == [
"2021-09-07 00:00:00",
"2021-09-07 01:00:00",
"2021-09-07 02:00:00",
"2021-09-07 03:00:00",
"2021-09-07 04:00:00",
"2021-09-07 05:00:00",
"2021-09-07 06:00:00",
"2021-09-07 07:00:00",
"2021-09-07 08:00:00",
"2021-09-07 09:00:00",
"2021-09-07 10:00:00",
"2021-09-07 11:00:00",
"2021-09-07 12:00:00",
"2021-09-07 13:00:00",
"2021-09-07 14:00:00",
"2021-09-07 15:00:00",
"2021-09-07 16:00:00",
"2021-09-07 17:00:00",
"2021-09-07 18:00:00",
"2021-09-07 19:00:00",
"2021-09-07 20:00:00",
"2021-09-07 21:00:00",
"2021-09-07 22:00:00",
"2021-09-07 23:00:00",
"2021-09-08 00:00:00",
"2021-09-08 01:00:00",
"2021-09-08 02:00:00",
"2021-09-08 03:00:00",
"2021-09-08 04:00:00",
"2021-09-08 05:00:00",
"2021-09-08 06:00:00",
"2021-09-08 07:00:00",
"2021-09-08 08:00:00",
"2021-09-08 09:00:00",
"2021-09-08 10:00:00",
"2021-09-08 11:00:00",
"2021-09-08 12:00:00",
"2021-09-08 13:00:00",
"2021-09-08 14:00:00",
"2021-09-08 15:00:00",
"2021-09-08 16:00:00",
"2021-09-08 17:00:00",
"2021-09-08 18:00:00",
"2021-09-08 19:00:00",
"2021-09-08 20:00:00",
"2021-09-08 21:00:00",
"2021-09-08 22:00:00",
"2021-09-08 23:00:00",
"2021-09-09 00:00:00",
"2021-09-09 01:00:00",
"2021-09-09 02:00:00",
"2021-09-09 03:00:00",
"2021-09-09 04:00:00",
"2021-09-09 05:00:00",
"2021-09-09 06:00:00",
"2021-09-09 07:00:00",
"2021-09-09 08:00:00",
"2021-09-09 09:00:00",
"2021-09-09 10:00:00",
"2021-09-09 11:00:00",
"2021-09-09 12:00:00",
"2021-09-09 13:00:00",
"2021-09-09 14:00:00",
"2021-09-09 15:00:00",
"2021-09-09 16:00:00",
"2021-09-09 17:00:00",
"2021-09-09 18:00:00",
"2021-09-09 19:00:00",
"2021-09-09 20:00:00",
"2021-09-09 21:00:00",
"2021-09-09 22:00:00",
"2021-09-09 23:00:00",
"2021-09-10 00:00:00",
"2021-09-10 01:00:00",
"2021-09-10 02:00:00",
"2021-09-10 03:00:00",
"2021-09-10 04:00:00",
"2021-09-10 05:00:00",
"2021-09-10 06:00:00",
"2021-09-10 07:00:00",
"2021-09-10 08:00:00",
"2021-09-10 09:00:00",
"2021-09-10 10:00:00",
"2021-09-10 11:00:00",
"2021-09-10 12:00:00",
"2021-09-10 13:00:00",
"2021-09-10 14:00:00",
"2021-09-10 15:00:00",
"2021-09-10 16:00:00",
"2021-09-10 17:00:00",
"2021-09-10 18:00:00",
"2021-09-10 19:00:00",
"2021-09-10 20:00:00",
"2021-09-10 21:00:00",
"2021-09-10 22:00:00",
"2021-09-10 23:00:00",
"2021-09-11 00:00:00",
"2021-09-11 01:00:00",
"2021-09-11 02:00:00",
"2021-09-11 03:00:00",
"2021-09-11 04:00:00",
"2021-09-11 05:00:00",
"2021-09-11 06:00:00",
"2021-09-11 07:00:00",
"2021-09-11 08:00:00",
"2021-09-11 09:00:00",
"2021-09-11 10:00:00",
"2021-09-11 11:00:00",
"2021-09-11 12:00:00",
"2021-09-11 13:00:00",
"2021-09-11 14:00:00",
"2021-09-11 15:00:00",
"2021-09-11 16:00:00",
"2021-09-11 17:00:00",
"2021-09-11 18:00:00",
"2021-09-11 19:00:00",
"2021-09-11 20:00:00",
"2021-09-11 21:00:00",
"2021-09-11 22:00:00",
"2021-09-11 23:00:00",
"2021-09-12 00:00:00",
"2021-09-12 01:00:00",
"2021-09-12 02:00:00",
"2021-09-12 03:00:00",
"2021-09-12 04:00:00",
"2021-09-12 05:00:00",
"2021-09-12 06:00:00",
"2021-09-12 07:00:00",
"2021-09-12 08:00:00",
"2021-09-12 09:00:00",
"2021-09-12 10:00:00",
"2021-09-12 11:00:00",
"2021-09-12 12:00:00",
"2021-09-12 13:00:00",
"2021-09-12 14:00:00",
"2021-09-12 15:00:00",
"2021-09-12 16:00:00",
"2021-09-12 17:00:00",
"2021-09-12 18:00:00",
"2021-09-12 19:00:00",
"2021-09-12 20:00:00",
"2021-09-12 21:00:00",
"2021-09-12 22:00:00",
"2021-09-12 23:00:00",
"2021-09-13 00:00:00"
]

assert full_intervals == nil
end
end

describe "GET /api/stats/main-graph - comparisons" do
Expand Down

0 comments on commit 0b4d92f

Please sign in to comment.