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 Nov 22, 2023
1 parent af0b97e commit 0cd61b1
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ All notable changes to this project will be documented in this file.
- Improved the Goal Settings page (search, autcompletion etc.)
- Log mailer errors plausible/analytics#3336
- Allow custom event timeseries in stats API plausible/analytics#3505
- 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
7 changes: 1 addition & 6 deletions lib/plausible/stats/timeseries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ defmodule Plausible.Stats.Timeseries do

@full_day_in_hours 23
defp buckets(%Query{interval: "hour"} = 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)
end
n_buckets = Timex.diff(query.date_range.last, query.date_range.first, :hours) + @full_day_in_hours

Enum.map(0..n_buckets, fn step ->
query.date_range.first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,217 @@ 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",
"2021-09-13 01:00:00",
"2021-09-13 02:00:00",
"2021-09-13 03:00:00",
"2021-09-13 04:00:00",
"2021-09-13 05:00:00",
"2021-09-13 06:00:00",
"2021-09-13 07:00:00",
"2021-09-13 08:00:00",
"2021-09-13 09:00:00",
"2021-09-13 10:00:00",
"2021-09-13 11:00:00",
"2021-09-13 12:00:00",
"2021-09-13 13:00:00",
"2021-09-13 14:00:00",
"2021-09-13 15:00:00",
"2021-09-13 16:00:00",
"2021-09-13 17:00:00",
"2021-09-13 18:00:00",
"2021-09-13 19:00:00",
"2021-09-13 20:00:00",
"2021-09-13 21:00:00",
"2021-09-13 22:00:00",
"2021-09-13 23:00:00"
]

assert full_intervals == nil
end
end

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

0 comments on commit 0cd61b1

Please sign in to comment.