@@ -8,7 +8,8 @@ defmodule Plausible.Stats.Comparisons do
88 """
99
1010 alias Plausible.Stats
11- alias Plausible.Stats . { Query , DateTimeRange , Time }
11+ alias Plausible.Stats . { DateTimeRange , Query , Time }
12+ alias Plausible.Times
1213
1314 @ spec get_comparison_utc_time_range ( Stats.Query . t ( ) ) :: DateTimeRange . t ( )
1415 @ doc """
@@ -53,8 +54,8 @@ defmodule Plausible.Stats.Comparisons do
5354 DateTimeRange . new! ( from , to )
5455
5556 _ ->
56- # For 24h period, work directly with datetime ranges to preserve time precision
57- if source_query . input_date_range == :"24h" do
57+ # For 24h period and today , work directly with datetime ranges to preserve time precision
58+ if use_datetime_for_comparison? ( source_query ) do
5859 get_comparison_datetime_range ( source_query )
5960 else
6061 comparison_date_range = get_comparison_date_range ( source_query )
@@ -70,6 +71,17 @@ defmodule Plausible.Stats.Comparisons do
7071 DateTimeRange . to_timezone ( datetime_range , "Etc/UTC" )
7172 end
7273
74+ defp use_datetime_for_comparison? ( query ) do
75+ if query . input_date_range == :day do
76+ today_from_now = Times . to_date ( query . now , query . timezone )
77+ day_from_range = Times . to_date ( query . utc_time_range . first , query . timezone )
78+
79+ Date . compare ( today_from_now , day_from_range ) == :eq
80+ else
81+ query . input_date_range == :"24h"
82+ end
83+ end
84+
7385 def get_comparison_query (
7486 % Query { comparison_utc_time_range: % DateTimeRange { } = comparison_range } = source_query
7587 ) do
@@ -120,38 +132,34 @@ defmodule Plausible.Stats.Comparisons do
120132 end
121133 end
122134
123- # For 24h period , shift the datetime range directly to preserve time precision
135+ # For 24h and today periods , shift the datetime range directly to preserve time precision
124136 defp get_comparison_datetime_range (
125137 % Query {
126- input_date_range: :"24h" ,
127- include: % { compare: :previous_period , compare_match_day_of_week: true }
138+ input_date_range: input_range ,
139+ include: % { compare: :previous_period } = include
128140 } = source_query
129- ) do
130- days_back = 7
131- comparison_start = DateTime . shift ( source_query . utc_time_range . first , day: - days_back )
132- comparison_end = DateTime . shift ( source_query . utc_time_range . last , day: - days_back )
133-
134- DateTimeRange . new! ( comparison_start , comparison_end )
135- end
141+ )
142+ when input_range in [ :"24h" , :day ] do
143+ offset =
144+ if include . compare_match_day_of_week do
145+ [ day: - 7 ]
146+ else
147+ [ hour: - 24 ]
148+ end
136149
137- defp get_comparison_datetime_range (
138- % Query {
139- input_date_range: :"24h" ,
140- include: % { compare: :previous_period }
141- } = source_query
142- ) do
143- comparison_start = DateTime . shift ( source_query . utc_time_range . first , hour: - 24 )
144- comparison_end = DateTime . shift ( source_query . utc_time_range . last , hour: - 24 )
150+ comparison_start = DateTime . shift ( source_query . utc_time_range . first , offset )
151+ comparison_end = DateTime . shift ( source_query . utc_time_range . last , offset )
145152
146153 DateTimeRange . new! ( comparison_start , comparison_end )
147154 end
148155
149156 defp get_comparison_datetime_range (
150157 % Query {
151- input_date_range: :"24h" ,
158+ input_date_range: input_range ,
152159 include: % { compare: :year_over_year }
153160 } = source_query
154- ) do
161+ )
162+ when input_range in [ :"24h" , :day ] do
155163 comparison_start = DateTime . shift ( source_query . utc_time_range . first , year: - 1 )
156164 comparison_end = DateTime . shift ( source_query . utc_time_range . last , year: - 1 )
157165
0 commit comments