diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ed59f16c4b4..b427eae44bb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,7 @@ All notable changes to this project will be documented in this file. - Allow running the container with arbitrary UID plausible/analytics#2986 - Fix `width=manual` in embedded dashboards plausible/analytics#3910 - Fix URL escaping when pipes are used in UTM tags plausible/analytics#3930 +- Fixed [Last 7 days with hourly resolution doesn't include today](https://github.com/plausible/analytics/issues/3342) ## v2.0.0 - 2023-07-12 diff --git a/lib/plausible/stats/timeseries.ex b/lib/plausible/stats/timeseries.ex index 2efa6418b674..f145438ce17a 100644 --- a/lib/plausible/stats/timeseries.ex +++ b/lib/plausible/stats/timeseries.ex @@ -103,11 +103,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 + 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 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 bb74bf531588..34ca93f7b7e6 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 @@ -912,6 +912,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