ankane / groupdate

The simplest way to group temporal data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Range method for group_by_week returning last year (Dec 2019...to January 2020)

RailsCod3rFuture opened this issue · comments

Greetings Ankane,
I'm receiving a weird bug where I use group_by_week for a date column and then it returns the last month of last year if the date is on the first of the upcoming month. Take a look at the query below.

@facility.chart_records.group_by_week(:record_date, range: Date.new(2020,1, 1).beginning_of_month..Date.new(2020,1,1).end_of_month).sum(:column_name)

Why am I receiving
{Sun, 29 Dec 2019 => 0, Sun, 05 Jan 2020=>0, Sun, 12 Jan 2020=>0, Sun, 19 Jan 2020=>0, Sun, 26 Jan 2020 =>0}

Shouldn't it only be {Jan 2020=>0....To Sun 26 Jan 2020} ??

Hey @RailsCod3rFuture, Jan 1, 2020 is a Wednesday, so the start of that week is Dec 29, 2019.

Is it possible to start at the beginning of the month for the week? I guess the developer that worked on the project before me, added some small hacks to get the window to thin for the weekly grouping

@RailsCod3rFuture You can change the first key in the hash that's returned from Groupdate to the first day of the month in your application.

Hey Ankane, its still returning the same results. Can you give me an example that works for you, please?

With this query: @facility.chart_records.group_by_week(:record_date, range: Date.new(2020,1, 1).beginning_of_month..Date.new(2020,1,1).end_of_month).sum(:column_name)

Also getting a similar issue, any updates on this?

I think I found the issue, when the Range has exclude_end set to false (default when creating a Range) Groupdate adds 1 day to the finish time:

You can get around this by building your Range as such: Range.new(start, end, true) (https://ruby-doc.org/core-2.6.6/Range.html#new-method)

I cant quite wrap my head around if this is expected behaviour, or a bug