Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra day is included when supplying inclusive range #270

Closed
ycp3 opened this issue Apr 18, 2023 · 1 comment
Closed

Extra day is included when supplying inclusive range #270

ycp3 opened this issue Apr 18, 2023 · 1 comment

Comments

@ycp3
Copy link

ycp3 commented Apr 18, 2023

Hello,

It seems that when passing a Range with range.exclude_end? == false to the group_by_xxxx methods an extra day is included causing unwanted time periods in the resulting hash.

We supply the range: argument in the first place because we want the hash full of zeroed out time periods in the case of no data instead of the empty hash that is returned when there is no range supplied.

Reproduction:
note: actual model name changed to "comment" in below examples

range = DateTime.now.all_month
Comment.group_by_month(:created_at, range: range).count
# Comment Count (12.4ms)  SELECT COUNT(*) AS "count_all", DATE_TRUNC('month', "comments"."created_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date AS "date_trunc_month_comments_created_at_timestamptz_at_tim" FROM "comments" WHERE ("comments"."created_at" >= '2023-04-01 00:00:00' AND "comments"."created_at" < '2023-05-01 23:59:59.999999') GROUP BY DATE_TRUNC('month', "comments"."created_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date
# => {Thu, 01 Dec 2022=>2557, Sun, 01 Jan 2023=>23}
range = DateTime.now.all_day
Comment.group_by_day(:created_at, range: range).count
# Comment Count (11.2ms)  SELECT COUNT(*) AS "count_all", DATE_TRUNC('day', "comments"."created_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date AS "date_trunc_day_comments_created_at_timestamptz_at_time_" FROM "comments" WHERE ("comments"."created_at" >= '2023-04-18 00:00:00' AND "comments"."created_at" < '2023-04-19 23:59:59.999999') GROUP BY DATE_TRUNC('day', "comments"."created_at"::timestamptz AT TIME ZONE 'Etc/UTC')::date
# => {Mon, 12 Dec 2022=>133, Tue, 13 Dec 2022=>218}
range = DateTime.now.all_day
Comment.group_by_hour(:created_at, range: range).count.count
# Comment Count (11.0ms)  SELECT COUNT(*) AS "count_all", DATE_TRUNC('hour', "comments"."created_at"::timestamptz AT TIME ZONE 'Etc/UTC') AT TIME ZONE 'Etc/UTC' AS "date_trunc_hour_comments_created_at_timestamptz_at_time" FROM "actual_locations" WHERE ("comments"."created_at" >= '2023-04-18 00:00:00' AND "comments"."created_at" < '2023-04-19 23:59:59.999999') GROUP BY DATE_TRUNC('hour', "comments"."created_at"::timestamptz AT TIME ZONE 'Etc/UTC') AT TIME ZONE 'Etc/UTC'
# => 48
@ankane ankane closed this as completed in 0018a73 Apr 18, 2023
@ankane
Copy link
Owner

ankane commented Apr 18, 2023

Hi @ycp3, thanks for the great report! Fixed in the commit above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants