Code Monkey home page Code Monkey logo

groupdate's Introduction

Groupdate

The simplest way to group by:

  • day
  • week
  • hour of the day
  • and more (complete list below)

🎉 Time zones - including daylight saving time - supported!! the best part

🍰 Get the entire series - the other best part

Supports PostgreSQL, MySQL, and Redshift, plus arrays and hashes (and limited support for SQLite)

💘 Goes hand in hand with Chartkick

Build Status

Installation

Add this line to your application’s Gemfile:

gem "groupdate"

For MySQL and SQLite, also follow these instructions.

Getting Started

User.group_by_day(:created_at).count
# {
#   Sat, 24 May 2020 => 50,
#   Sun, 25 May 2020 => 100,
#   Mon, 26 May 2020 => 34
# }

Results are returned in ascending order by default, so no need to sort.

You can group by:

  • second
  • minute
  • hour
  • day
  • week
  • month
  • quarter
  • year

and

  • minute_of_hour
  • hour_of_day
  • day_of_week (Sunday = 0, Monday = 1, etc)
  • day_of_month
  • day_of_year
  • month_of_year

Use it anywhere you can use group. Works with count, sum, minimum, maximum, and average. For median and percentile, check out ActiveMedian.

Time Zones

The default time zone is Time.zone. Change this with:

Groupdate.time_zone = "Pacific Time (US & Canada)"

or

User.group_by_week(:created_at, time_zone: "Pacific Time (US & Canada)").count
# {
#   Sun, 08 Mar 2020 => 70,
#   Sun, 15 Mar 2020 => 54,
#   Sun, 22 Mar 2020 => 80
# }

Time zone objects also work. To see a list of available time zones in Rails, run rake time:zones:all.

Week Start

Weeks start on Sunday by default. Change this with:

Groupdate.week_start = :monday

or

User.group_by_week(:created_at, week_start: :monday).count

Day Start

You can change the hour days start with:

Groupdate.day_start = 2 # 2 am - 2 am

or

User.group_by_day(:created_at, day_start: 2).count

Time Range

To get a specific time range, use:

User.group_by_day(:created_at, range: 2.weeks.ago.midnight..Time.now).count

To expand the range to the start and end of the time period, use:

User.group_by_day(:created_at, range: 2.weeks.ago..Time.now, expand_range: true).count

To get the most recent time periods, use:

User.group_by_week(:created_at, last: 8).count # last 8 weeks

To exclude the current period, use:

User.group_by_week(:created_at, last: 8, current: false).count

Order

You can order in descending order with:

User.group_by_day(:created_at, reverse: true).count

Keys

Keys are returned as date or time objects for the start of the period.

To get keys in a different format, use:

User.group_by_month(:created_at, format: "%b %Y").count
# {
#   "Jan 2020" => 10
#   "Feb 2020" => 12
# }

or

User.group_by_hour_of_day(:created_at, format: "%-l %P").count
# {
#    "12 am" => 15,
#    "1 am"  => 11
#    ...
# }

Takes a String, which is passed to strftime, or a Symbol, which is looked up by I18n.localize in i18n scope 'time.formats', or a Proc. You can pass a locale with the locale option.

Series

The entire series is returned by default. To exclude points without data, use:

User.group_by_day(:created_at, series: false).count

Or change the default value with:

User.group_by_day(:created_at, default_value: "missing").count

Dynamic Grouping

User.group_by_period(:day, :created_at).count

Limit groupings with the permit option.

User.group_by_period(params[:period], :created_at, permit: ["day", "week"]).count

Raises an ArgumentError for unpermitted periods.

Custom Duration

To group by a specific number of minutes or seconds, use:

User.group_by_minute(:created_at, n: 10).count # 10 minutes

Date Columns

If grouping on date columns which don’t need time zone conversion, use:

User.group_by_week(:created_on, time_zone: false).count

Default Scopes

If you use Postgres and have a default scope that uses order, you may get a column must appear in the GROUP BY clause error (just like with Active Record’s group method). Remove the order scope with:

User.unscope(:order).group_by_day(:count).count

Arrays and Hashes

users.group_by_day { |u| u.created_at } # or group_by_day(&:created_at)

Supports the same options as above

users.group_by_day(time_zone: time_zone) { |u| u.created_at }

Get the entire series with:

users.group_by_day(series: true) { |u| u.created_at }

Count

users.group_by_day { |u| u.created_at }.to_h { |k, v| [k, v.count] }

Additional Instructions

For MySQL

Time zone support must be installed on the server.

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql

You can confirm it worked with:

SELECT CONVERT_TZ(NOW(), '+00:00', 'Pacific/Honolulu');

It should return the time instead of NULL.

For SQLite

Groupdate has limited support for SQLite.

  • No time zone support
  • No day_start option
  • No group_by_quarter method

If your application’s time zone is set to something other than Etc/UTC (the default), create an initializer with:

Groupdate.time_zone = false

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development and testing, check out the Contributing Guide.

groupdate's People

Contributors

ankane avatar askl56 avatar bjacobso avatar buhrmi avatar caulfield avatar codesnik avatar danandreasson avatar edestecd avatar hqm42 avatar hyfen avatar kavu avatar leonelgalan avatar mccallumjack avatar mechanicles avatar mochnatiy avatar pherris avatar rmm5t avatar robinator avatar thedanotto avatar tompesman avatar trestrantham avatar wspurgin avatar zorab47 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

groupdate's Issues

undefined method `group_by_day'

I added the gem to my Gemfile (and bundle installed) and I keep getting this error when I try Question.group_by_day(:creation_date).count. I even tried adding require 'groupdate' to my controller and still no luck

Different return types per DB

Currently, MySQL and Postgres are returning different date types. Queries against MySQL database return Time keys, whereas Postgres queries are returning String keys.

MySQL example:

irb(main):007:0> User.group_by_week(:created_at).count
=> {"2013-06-09 00:00:00+00"=>7, "2013-05-26 00:00:00+00"=>1, "2013-06-16 00:00:00+00"=>1, "2013-06-02 00:00:00+00"=>2, "2013-05-05 00:00:00+00"=>1}

irb(main):008:0> User.group_by_week(:created_at).count.keys.first.class
=> String

Postgres example:

Examples:

>> User.group_by_week(:created_at).count
=> {2013-05-05 00:00:00 UTC=>1, 2013-05-26 00:00:00 UTC=>1, 2013-06-02 00:00:00 UTC=>2, 2013-06-09 00:00:00 UTC=>7, 2013-06-16 00:00:00 UTC=>1}

>> User.group_by_week(:created_at).count.keys.first.class
=> Time

Custom week start

How difficult would it be to modify the week ranges? I'd love to be able to generate date groups around a Monday - Sunday week, rather than Sunday - Saturday.

Sweet gem! Thanks!

Decouple Ruby and JS code

You should decouple Ruby and JS code so that other web languages (PHP, Python, ...) can build similar libraries based on the same JS.

MySQL 5.6 group_by_day and group_by_week return nil

I have a production database on a mysql 5.5 server that returns all of my group_by_* calls correctly. I recently upgraded the development mysql server to 5.6 and the same code is now failing with the same data. I made sure the timezone setting was correct.

Is there a reason group_by_month lists date as 1st of the month?

e.g. the key for November 2013, would be:

Fri, 01 Nov 2013 00:00:00 UTC +00:00

Why this date? This is the start of the month, and were it an accurate key, wouldn't include anything in November. I assumed it would be something like:

Sat, 30 Nov 2013 23:59:59 UTC +00:00

All 'day' values are null

I've been trying to use this awesome sounding library all afternoon, to no avail.

When using it in console, it returns nil, with a warning:

RuntimeError: Be sure to install time zone support - https://github.com/ankane/groupdate#for-mysql
from /Users/stevehull/.rvm/gems/ruby-1.9.3-p448/gems/groupdate-2.2.1/lib/groupdate/magic.rb:119:in `rescue in perform'

I followed the recommendation and loaded the timezones. They didn't all load, but the 'America/Los_Angeles' zone loaded just fine so I figure I'm good to go.

Here's the query it constructed:

SELECT COUNT(*) AS count_all, DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(`created_at`, INTERVAL 0 HOUR), '+00:00', 'America/Los_Angeles'), '%Y-%m-%d 00:00:00'), 'America/Los_Angeles', '+00:00'), INTERVAL 0 HOUR) AS day FROM `listing_invites` WHERE (`created_at` IS NOT NULL) GROUP BY DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(`created_at`, INTERVAL 0 HOUR), '+00:00', 'America/Los_Angeles'), '%Y-%m-%d 00:00:00'), 'America/Los_Angeles', '+00:00'), INTERVAL 0 HOUR);

And there results it returned:

count_all   day
20  NULL

And when I try a simple CONVERT_TZ myself:

select convert_tz(created_at, '+00:00', 'America/Los_Angeles') from listing_invites;

It works fine.

Strange turning to bizarre... if I add some returns in the constructed query above, it runs fine:

SELECT
  COUNT(*) AS count_all,
  DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(`created_at`, INTERVAL 0 HOUR), '+00:00', 'America/Los_Angeles'), '%Y-%m-%d 00:00:00'), 'America/Los_Angeles', '+00:00'), INTERVAL 0 HOUR) AS day
FROM `listing_invites`
WHERE (`created_at` IS NOT NULL)
GROUP BY DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(`created_at`, INTERVAL 0 HOUR), '+00:00', 'America/Los_Angeles'), '%Y-%m-%d 00:00:00'), 'America/Los_Angeles', '+00:00'), INTERVAL 0 HOUR);

And it returns:

count_all   day
2   2014-04-17 07:00:00
2   2014-04-18 07:00:00
1   2014-04-22 07:00:00
1   2014-04-28 07:00:00
14  2014-05-27 07:00:00

.....

wat

groupdate is having Date formatting issue on hash keys that are Date class.

Take a look at:
https://gist.github.com/StephenOTT/6614278

Run the code with and without require 'groupdate'

When you run with groupdate you get a output of:

{Wed, 01 Dec 2010=>1, Mon, 01 Aug 2011=>8, Thu, 01 Sep 2011=>58, Sat, 01 Oct 2011=>48, Tue, 01 Nov 2011=>46, Thu, 01 Dec 2011=>47, Sun, 01 Jan 2012=>70, Wed, 01 Feb 2012=>95, Thu, 01 Mar 2012=>68, Sun, 01 Apr 2012=>50, Tue, 01 May 2012=>77, Fri, 01 Jun 2012=>129, Sun, 01 Jul 2012=>84, Wed, 01 Aug 2012=>70, Sat, 01 Sep 2012=>102, Mon, 01 Oct 2012=>53, Thu, 01 Nov 2012=>75, Sat, 01 Dec 2012=>58, Tue, 01 Jan 2013=>242, Fri, 01 Feb 2013=>266, Fri, 01 Mar 2013=>253, Mon, 01 Apr 2013=>449, Wed, 01 May 2013=>609, Sat, 01 Jun 2013=>561, Mon, 01 Jul 2013=>595, Thu, 01 Aug 2013=>736, Sun, 01 Sep 2013=>67}

When you run without groupdate you get:
{#<Date: 2010-12-01 ((2455532j,0s,0n),+0s,2299161j)>=>1, #<Date: 2011-08-01 ((2455775j,0s,0n),+0s,2299161j)>=>8, #<Date: 2011-09-01 ((2455806j,0s,0n),+0s,2299161j)>=>58, #<Date: 2011-10-01 ((2455836j,0s,0n),+0s,2299161j)>=>48, #<Date: 2011-11-01 ((2455867j,0s,0n),+0s,2299161j)>=>46, #<Date: 2011-12-01 ((2455897j,0s,0n),+0s,2299161j)>=>47, #<Date: 2012-01-01 ((2455928j,0s,0n),+0s,2299161j)>=>70, #<Date: 2012-02-01 ((2455959j,0s,0n),+0s,2299161j)>=>95, #<Date: 2012-03-01 ((2455988j,0s,0n),+0s,2299161j)>=>68, #<Date: 2012-04-01 ((2456019j,0s,0n),+0s,2299161j)>=>50, #<Date: 2012-05-01 ((2456049j,0s,0n),+0s,2299161j)>=>77, #<Date: 2012-06-01 ((2456080j,0s,0n),+0s,2299161j)>=>129, #<Date: 2012-07-01 ((2456110j,0s,0n),+0s,2299161j)>=>84, #<Date: 2012-08-01 ((2456141j,0s,0n),+0s,2299161j)>=>70, #<Date: 2012-09-01 ((2456172j,0s,0n),+0s,2299161j)>=>102, #<Date: 2012-10-01 ((2456202j,0s,0n),+0s,2299161j)>=>53, #<Date: 2012-11-01 ((2456233j,0s,0n),+0s,2299161j)>=>75, #<Date: 2012-12-01 ((2456263j,0s,0n),+0s,2299161j)>=>58, #<Date: 2013-01-01 ((2456294j,0s,0n),+0s,2299161j)>=>242, #<Date: 2013-02-01 ((2456325j,0s,0n),+0s,2299161j)>=>266, #<Date: 2013-03-01 ((2456353j,0s,0n),+0s,2299161j)>=>253, #<Date: 2013-04-01 ((2456384j,0s,0n),+0s,2299161j)>=>449, #<Date: 2013-05-01 ((2456414j,0s,0n),+0s,2299161j)>=>609, #<Date: 2013-06-01 ((2456445j,0s,0n),+0s,2299161j)>=>561, #<Date: 2013-07-01 ((2456475j,0s,0n),+0s,2299161j)>=>595, #<Date: 2013-08-01 ((2456506j,0s,0n),+0s,2299161j)>=>736, #<Date: 2013-09-01 ((2456537j,0s,0n),+0s,2299161j)>=>67}

Is this expected?

Does MySQL timezone support need to be installed if it uses system timezone?

The MySQL timezone page linked in the README says:

The current values of the global and client-specific time zones can be retrieved like this:

mysql> SELECT @@global.time_zone, @@session.time_zone;
timezone values can be given in several formats, none of which are case sensitive:

The value 'SYSTEM' indicates that the time zone should be the same as the system time zone.

The value can be given as a string indicating an offset from UTC, such as '+10:00' or '-6:00'.

The value can be given as a named time zone, such as 'Europe/Helsinki', 'US/Eastern', or 'MET'. Named time zones can be used only if the time zone information tables in the mysql database have been created and populated.

When I run the query described I get SYSTEM for both the global and session timezones.

Do I still need to install the timezone stuff in my MySQL?

Version 1.0.0

If you're following Semantic Versioning then this gem is most likely already version 1.0.0:

  1. It's been released to the public
  2. People are using it in a production setting.

It's a good idea to start your released gem at 1.0.0 because it allows people to use optimistic versioning without issue. Worried about breaking changes you might have? Don't worry, semantic versioning has that covered by having you bump the major version!

group_by_anything keeps sending me nil results

I keep receiving nil data.

[11] pry(main)> Car.group_by_day(:created_at).count
   (539.1ms)  SELECT COUNT(*) AS count_all,   CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'Etc/UTC'), '%Y-%m-%d 00:00:00'), 'Etc/UTC', '+00:00') AS day FROM `cars` GROUP BY CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'Etc/UTC'), '%Y-%m-%d 00:00:00'), 'Etc/UTC', '+00:00')
=> {nil=>1395958}
[12] pry(main)>

I have:

[12] pry(main)> Car.count
   (606.4ms)  SELECT COUNT(*) FROM `cars`
=> 1395958 

An example of car entry:

[13] pry(main)> Car.first
  Car Load (0.8ms)  SELECT `cars`.* FROM `cars` LIMIT 1
=> #<Car id: 1, name: "2010 Ford F-150 SVT Raptor***WOW, THIS IS BIG AND B...", price: 39800, price_original: nil, currency: nil, dealer: "DISCOVERY FOR", dealer_url: "http://autocatch.com/dealer/discovery-ford/index.ht...", phone: "(888) 529-0229", year: 2010, make: "Ford", model: "Model F150", mileage: 67676, body_style: "Truck", transmission: "Automatic", ext_color: "N/A", int_color: "N/A", doors: 4, drive_train: "", engine: "5.40", cylinders: 8, fuel_type: "Type ", status: "", address: "", city: nil, state: nil, country: "Canada", source_url: "", source_name: "Autocatch", deactivated: nil, created_at: "2013-04-29 15:40:59", updated_at: "2013-04-29 15:40:59", lat: nil, lon: nil>

I'm using MySQL, ruby 2.0.0dev and rails 3.2.13.

Why it keeps showing me nil=>1395958 ???

get around mysql_tzinfo_to_sql

Hi,

I don't have permission to change mysql table in RDS instance. Is there any way to get around mysql_tzinfo_to_sql? or even get around time zone is acceptable. I get stuck here for few days.

Thanks

Group By Returns Results for Entire Table

In my setup, I have groups and groups have many posts. I'm trying to group the posts in each group, but instead I'm getting grouped results for the entire posts table. here's my code:

@group.posts.unscoped.group_by_week(:created_at).count

Is it possible to get grouped sums of a method result that is not a table column?

I'm aware that this might not be an issue that is related to this gem, but a more general question regarding Rails. Nevertheless I thought this might still be a good place to ask.

Let's assume I have a query like this:

LineItem.group_by_month(:created_at).sum("quantity")

That works as expected. However we have a special case of LineItems that require additional calculations to get a total_quantity, like

def total_quantity
    value1 + value2 + value3
end

I'd like to use a query like

LineItem.group_by_month(:created_at).sum(&:total_quantity)

Unfortunately this returns a PG:GroupingError that tells me the query expects that "column "line_items.id" must appear in the GROUP BY clause or be used in an aggregate function", which is probably necessary for a valid SQL query, but the problem remains:

Is there an elegant way to aggregate the total_quantity, or in general the result of a method, in a groupdate query?

Connection adapter not supported: SQLite

I installed groupdate (and chartkick) today, following the ActiveAdmin example here, but changing the model to an application-specific one that wanted graphed: http://www.patrickedelman.com/simple-charting-for-activeadmin/

That seemed to work fine. The page showed up, and it was filled with a very pretty line graph.

When I tried moving the code into the ActiveAdmin dashboard page (dashboard.rb), I got "Connection adapter not supported: SQLite" errors every time I tried to render it. And now I get those errors on the original example page as well!

I'm not sure if this is a groupdate problem, or some kind of configuration state problem on my end. But I'd appreciate any advice!

PG::Error: ERROR: column "<table>.created_at" must appear in the GROUP BY clause or be used in an aggregate function

In Rails 3.1.10, on Postgres 9.1.2, if I run this:

Drop.group_by_day(:created_at).count

I get this error:

ActiveRecord::StatementInvalid: PG::Error: ERROR:  column "drops.created_at" must appear in the GROUP BY clause or be used in an aggregate function

Not sure what could be causing this, but currently trying to update rails to see if that could be related.

EDIT:
I saw that someone else was having this issue but they weren't including an operation, though I am.
I've since upgraded to Rails 3.2.13 and am having the same issue.

Timezone change in the data OR Missing date seems to break the output

I have a some data in a database (Postgres 9.3) and when I group it using group_by_day and there is a date missing the remaining values output are all zero.
i.e. there were no orders on 2013/10/22

Order.group_by_day(:created_at).count

SQL Output

SELECT COUNT(*) AS count_all, (DATE_TRUNC('day', ("created_at"::timestamptz - INTERVAL '0 hour') AT TIME ZONE 'America/Sao_Paulo') + INTERVAL '0 hour') AT TIME ZONE 'America/Sao_Paulo' AS day 
FROM "orders"  
WHERE ("created_at" IS NOT NULL) 
GROUP BY (DATE_TRUNC('day', ("created_at"::timestamptz - INTERVAL '0 hour') AT TIME ZONE 'America/Sao_Paulo') + INTERVAL '0 hour') AT TIME ZONE 'America/Sao_Paulo'

I'm using groupdate v2.2.1

Timezone info (2013) (may be relevant):

"America/Sao_Paulo" Daylight Savings Time 
Start DST:  Sunday, 20 October 2013
1 hour forward

Sample Data (copied from the raw SQL results):

4,"2013-10-05 00:00:00-03"
4,"2013-10-06 00:00:00-03"
1,"2013-10-07 00:00:00-03"
4,"2013-10-09 00:00:00-03"
8,"2013-10-10 00:00:00-03"
14,"2013-10-11 00:00:00-03"
4,"2013-10-12 00:00:00-03"
3,"2013-10-13 00:00:00-03"
4,"2013-10-14 00:00:00-03"
6,"2013-10-15 00:00:00-03"
2,"2013-10-16 00:00:00-03"
1,"2013-10-17 00:00:00-03"
6,"2013-10-18 00:00:00-03"
1,"2013-10-19 00:00:00-03"
1,"2013-10-20 01:00:00-02"
3,"2013-10-21 00:00:00-02"
1,"2013-10-23 00:00:00-02"
1,"2013-10-29 00:00:00-02"
3,"2013-10-30 00:00:00-02"
2,"2013-11-06 00:00:00-02"
2,"2013-11-12 00:00:00-02"
1,"2013-11-15 00:00:00-02"
2,"2013-11-23 00:00:00-02"

Output (Ruby console) Check from 21 Oct 2013 onwards

 Fri, 04 Oct 2013 00:00:00 BRT -03:00=>4,
 Sat, 05 Oct 2013 00:00:00 BRT -03:00=>4,
 Sun, 06 Oct 2013 00:00:00 BRT -03:00=>1,
 Mon, 07 Oct 2013 00:00:00 BRT -03:00=>0,
 Tue, 08 Oct 2013 00:00:00 BRT -03:00=>4,
 Wed, 09 Oct 2013 00:00:00 BRT -03:00=>8,
 Thu, 10 Oct 2013 00:00:00 BRT -03:00=>14,
 Fri, 11 Oct 2013 00:00:00 BRT -03:00=>4,
 Sat, 12 Oct 2013 00:00:00 BRT -03:00=>3,
 Sun, 13 Oct 2013 00:00:00 BRT -03:00=>4,
 Mon, 14 Oct 2013 00:00:00 BRT -03:00=>6,
 Tue, 15 Oct 2013 00:00:00 BRT -03:00=>2,
 Wed, 16 Oct 2013 00:00:00 BRT -03:00=>1,
 Thu, 17 Oct 2013 00:00:00 BRT -03:00=>6,
 Fri, 18 Oct 2013 00:00:00 BRT -03:00=>1,
 Sat, 19 Oct 2013 00:00:00 BRT -03:00=>1,
 Sun, 20 Oct 2013 01:00:00 BRST -02:00=>3,
 Mon, 21 Oct 2013 01:00:00 BRST -02:00=>0,
 Tue, 22 Oct 2013 01:00:00 BRST -02:00=>0,
 Wed, 23 Oct 2013 01:00:00 BRST -02:00=>0,
 Thu, 24 Oct 2013 01:00:00 BRST -02:00=>0,
 Fri, 25 Oct 2013 01:00:00 BRST -02:00=>0,
 Sat, 26 Oct 2013 01:00:00 BRST -02:00=>0,
 Sun, 27 Oct 2013 01:00:00 BRST -02:00=>0,
 Mon, 28 Oct 2013 01:00:00 BRST -02:00=>0,
 Tue, 29 Oct 2013 01:00:00 BRST -02:00=>0,
 Wed, 30 Oct 2013 01:00:00 BRST -02:00=>0,
 Thu, 31 Oct 2013 01:00:00 BRST -02:00=>0,
 Fri, 01 Nov 2013 01:00:00 BRST -02:00=>0,
 Sat, 02 Nov 2013 01:00:00 BRST -02:00=>0,
 Sun, 03 Nov 2013 01:00:00 BRST -02:00=>0,
 Mon, 04 Nov 2013 01:00:00 BRST -02:00=>0,
 Tue, 05 Nov 2013 01:00:00 BRST -02:00=>0,
 Wed, 06 Nov 2013 01:00:00 BRST -02:00=>0,
 Thu, 07 Nov 2013 01:00:00 BRST -02:00=>0,
 Fri, 08 Nov 2013 01:00:00 BRST -02:00=>0,
 Sat, 09 Nov 2013 01:00:00 BRST -02:00=>0,
 Sun, 10 Nov 2013 01:00:00 BRST -02:00=>0,
 Mon, 11 Nov 2013 01:00:00 BRST -02:00=>0,
 Tue, 12 Nov 2013 01:00:00 BRST -02:00=>0,
 Wed, 13 Nov 2013 01:00:00 BRST -02:00=>0,
 Thu, 14 Nov 2013 01:00:00 BRST -02:00=>0,
 Fri, 15 Nov 2013 01:00:00 BRST -02:00=>0,
 Sat, 16 Nov 2013 01:00:00 BRST -02:00=>0,
 Sun, 17 Nov 2013 01:00:00 BRST -02:00=>0,
 Mon, 18 Nov 2013 01:00:00 BRST -02:00=>0,
 Tue, 19 Nov 2013 01:00:00 BRST -02:00=>0,
 Wed, 20 Nov 2013 01:00:00 BRST -02:00=>0,
 Thu, 21 Nov 2013 01:00:00 BRST -02:00=>0,
 Fri, 22 Nov 2013 01:00:00 BRST -02:00=>0,

Is this gem framework agnostic?

I'm currently using sinatra+activerecord for an app and I tried to integrate this gem but it makes my app crash, here is the log:

[2013-06-11T01:35:00.666758 #4092] DEBUG -- :    (258.9ms)  SELECT COUNT(*) AS count_all, CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'Etc/UTC'), '%Y-%m-01 00:00:00'), 'Etc/UTC', '+00:00') AS month FROM `projects` GROUP BY CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'Etc/UTC'), '%Y-%m-01 00:00:00'), 'Etc/UTC', '+00:00')
[2013-06-11T01:35:00.667166 #4092] DEBUG -- : Errno::EBADF: Bad file descriptor: SELECT COUNT(*) AS count_all, CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'Etc/UTC'), '%Y-%m-01 00:00:00'), 'Etc/UTC', '+00:00') AS month FROM `projects`  GROUP BY CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'Etc/UTC'), '%Y-%m-01 00:00:00'), 'Etc/UTC', '+00:00')
ActiveRecord::StatementInvalid - Errno::EBADF: Bad file descriptor: SELECT COUNT(*) AS count_all, CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'Etc/UTC'), '%Y-%m-01 00:00:00'), 'Etc/UTC', '+00:00') AS month FROM `projects`  GROUP BY CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'Etc/UTC'), '%Y-%m-01 00:00:00'), 'Etc/UTC', '+00:00'):
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:215:in `exec_query'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:224:in `select'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation/calculations.rb:297:in `execute_grouped_calculation'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation/calculations.rb:214:in `perform_calculation'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation/calculations.rb:159:in `calculate'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation/calculations.rb:58:in `count'
  app.rb:16:in `block in <main>'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1541:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1541:in `block in compile!'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:950:in `[]'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:950:in `block (3 levels) in route!'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:966:in `route_eval'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:950:in `block (2 levels) in route!'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:987:in `block in process_route'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:985:in `catch'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:985:in `process_route'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:948:in `block in route!'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:947:in `each'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:947:in `route!'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1059:in `block in dispatch!'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in `block in invoke'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in `catch'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in `invoke'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1056:in `dispatch!'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:882:in `block in call!'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in `block in invoke'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in `catch'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in `invoke'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:882:in `call!'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:870:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-protection-1.5.0/lib/rack/protection/xss_header.rb:18:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-protection-1.5.0/lib/rack/protection/path_traversal.rb:16:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-protection-1.5.0/lib/rack/protection/json_csrf.rb:18:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-protection-1.5.0/lib/rack/protection/base.rb:49:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-protection-1.5.0/lib/rack/protection/base.rb:49:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-protection-1.5.0/lib/rack/protection/frame_options.rb:31:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-1.5.2/lib/rack/logger.rb:15:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:212:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:205:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/showexceptions.rb:21:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:175:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1949:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1449:in `block in call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1726:in `synchronize'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/sinatra-1.4.3/lib/sinatra/base.rb:1449:in `call'
  /Users/diego/.rvm/gems/ruby-1.9.3-p429/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
  /Users/diego/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/diego/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/diego/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

Is this gem framework agnostic?

Cheers,
Diego

Timezone functionality not working correctly

I'm not sure if it's a problem with the time zones or an error on the gem, but I'm getting bad results.
I'll explain it better: you have a record with the date: 2014-03-07 13:05:00. You are using the Europe/Madrid time zone. When you ask for a group by month, the query that generates its:

DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(`central_date`, INTERVAL 0 HOUR), '+00:00', 'Europe/Madrid'), '%Y-%m-01 00:00:00'), 'Europe/Madrid', '+00:00'), INTERVAL 0 HOUR)

The problem comes on the outermost convert_tz call, since date_format(..., '%Y-%m-01 00:00:00') will convert the date to: 2014-03-01 00:00:00, then that outermost convert_tz will generate the date 2014-02-28 23:00:00, which is obviously wrong...
I'm not sure why that outermost call is there, it seems like an error... or I'm missing something.

The same happens if you group by other time intervals...

Multiple column grouping

I was happy to see that groupdate supports the addition of extra columns to group by, e.g.

@reviews.joins(:product).group_by_month(:date).group("products.name").count

Which returns:

{ [ Mon, 01 Dec 2008 00:00:00 UTC +00:00, "Apples"]=>6,
 [Thu, 01 Jan 2009 00:00:00 UTC +00:00, "Apples"]=>5,
 [Mon, 01 Dec 2008 00:00:00 UTC +00:00, "Oranges"]=>2,
 [Thu, 01 Jan 2009 00:00:00 UTC +00:00, "Oranges"]=>9
}

However, additional options will conflict to this...format will be applied to the group-array rather than just the time field:

@reviews.joins(:product).group_by_month(:date, {format: '%s'}
                                                                 ).group("products.name").count

undefined method `strftime' for [Mon, 01 Dec 2008 00:00:00 UTC +00:00, "Apples"]:Array

The fix for this seems straightforward...I was thinking that extra groupings could be specified explicitly as a parameter to the options argument, so that the formatter function could be applied separately to the datetime column. But that might break the chainability of calls if the groupdate object is passed around the application...but maybe it's reasonable to expect that that kind of adhoc modification is an edge-case? I can submit a patch, but I'm guessing you may have had a reason to keep things as they are.

Select multiple aggregate results?

Hi,

is it possible to get results like this:

{ Mon, 01 Dec 2008 00:00:00 UTC +00:00=>[6,1],
 Thu, 01 Jan 2009 00:00:00 UTC +00:00=>[5,22],
 Mon, 01 Dec 2008 00:00:00 UTC +00:00=>[2,8],
 Thu, 01 Jan 2009 00:00:00 UTC +00:00=>[9,12]
}

Ignore the date grouping, it could be by day or month or anything else.

I want to select the count and the sum of a colum in just one query (see array result values).

How can i accomplish this?

Best regards,
Thedarkside

Postgres queries fail if an ORDER BY is specified

Example:

MyModel.order(:id).group_by_year(:created_at).count will work in MySQL, but not Postgres. However, MyModel.group_by_year(:created_at).count will work in either.

Granted, the order clause is meaningless here, but I have some models that have an order clause as part of their default scope, so these models will fail by default.

Solution:

I've found it works if I append .reorder('') to my queries. For example, MyModel.order(:created_at).group_by_year(:created_at).reorder('').count goes through just fine. It may be worth appending the reorder statement by default.

group_by_hour returns extra nil values

When doing 'group_by_hour', it creates extra keys not based on the data and returns back keys for every hour of the day. Is this intentional, or is there a way around this behavior?

For example:

grouped_data = data.group_by_hour{|d| d[:time]}
=> {Sat, 13 Sep 2014 22:00:00 UTC +00:00=>[{:time=>Sat, 13 Sep 2014 22:48:07 UTC +00:00, :stat=>4, :p=>1}],
 Sat, 13 Sep 2014 23:00:00 UTC +00:00=>[{:time=>Sat, 13 Sep 2014 23:07:50 UTC +00:00, :stat=>4, :p=>1}],
 Sun, 14 Sep 2014 00:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 01:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 02:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 03:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 04:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 05:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 06:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 07:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 08:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 09:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 10:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 11:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 12:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 13:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 14:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 15:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 16:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 17:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 18:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 19:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 20:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 21:00:00 UTC +00:00=>[],
 Sun, 14 Sep 2014 22:00:00 UTC +00:00=>
  [{:time=>Sun, 14 Sep 2014 22:55:49 UTC +00:00, :stat=>8, :p=>1},
   {:time=>Sun, 14 Sep 2014 22:55:22 UTC +00:00, :stat=>8, :p=>1}]}

Undefined method error

Hey ankane,

I get "undefined method `group_by_day' for #Class:0x007fff32a98c40:0x007fff32d38ce8" error everytime I use groupdate (similar to this issue: #35). Any idea?

I am using mysql and rails 3.2 but I am using sequel instead of active record - might this be an issue?

Thanks!

Groupdate.day_start should be a Time object OR provide support for time_offset

For example:

2.hours
30.minutes

etc

or alternatively provide support for a Groupdate.time_offset allowing for shifting of time values.

The use case is where a user wishes to group accumulated data that falls on whole minutes. At whole hours (for example) this data would currently shift into the next hour (or day or week, or month), but it is data representing the aggregation of the previous x minutes.

If grouping by date, for example, with 30 minute reporting data, the 00:00 value is for the previous day.

groupdate rails 4 deprecation warnings when used with will_paginate gem

(this is a bug only when using BOTH gems, so I am placing the bug in both places)

Tracking down warnings on Rails 4

When trying to update my existing Rails 4 app, I was coming up with some weird warnings littered all over my tests, and with some Googling, some other people seemed to have it too.

rails/rails#10507

/Users/maxwell/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/core.rb:103: warning: already initialized constant #<Module:0x007fbb60919038>::AttrNames
/Users/maxwell/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/core.rb:103: warning: previous definition of AttrNames was here

After doing some real yak shaving, I realized that it was having the combo of both the will_paginate and groupdate gems in my Gemfile.

Here is the repo showing the problem: https://github.com/maxwell/rails4_will_paginate_groupdate_warnings

To see the errors for yourself.

bundle
rake db:migrate

bundle && rails r "Post.first"

Then, you will see the warning

if you comment out either of the gems and re-run

bundle && rails r "Post.first"

no warnings!

Not sure of exactly WHY, but at least now I know WHO :)

how to multiline

hi

i have a table where i have to group by data and another id,, eg:
model tasks:

  • name
  • task_id
  • project_id
  • created_at

not sure how to multiline for each task_id and per day

any help appreciated

thx

Scoping time_range

So this new time_range scoping (with all the dates in the series)

User.group_by_day(:created_at, range: 2.weeks.ago.midnight..Time.now).count

works really nice.

Wonder if there is a possibility to make ranges somehow work with scopes? I mean something like this instead;

User.for_given_range(2.weeks.ago.midnight..Time.now).group_by_day(:created_at).count

?
Would be lot easier to use this from controller (and has_scope gem)

Add support for MongoDB?

MongoDB only supports two time zones (UTC and local time), but it's possible to group by hour in the database and aggregate the results in Ruby.

This works for:

  • count
  • sum
  • minimum
  • maximum

But not for:

  • average
  • time zones with a partial hour offset

Let me know if this is useful.

DateTime range returns all zeros

time_range = DateTime.parse("2013-06-01")..DateTime.parse("2013-06-07")
User.group_by_day(:created_at, nil, time_range).count

returns

{
    Sat, 01 Jun 2013 00:00:00 +0000 => 0,
    Sun, 02 Jun 2013 00:00:00 +0000 => 0,
    Mon, 03 Jun 2013 00:00:00 +0000 => 0,
    Tue, 04 Jun 2013 00:00:00 +0000 => 0,
    Wed, 05 Jun 2013 00:00:00 +0000 => 0,
    Thu, 06 Jun 2013 00:00:00 +0000 => 0,
    Fri, 07 Jun 2013 00:00:00 +0000 => 0
}

Is it possible to auto-deduce the range for a series?

I'd like my group_by_month('created_at').count to return as a series – but instead of specifying a fixed range like 11.months.ago..Time.zone.now I want the series to include all months from the first to the last in the data.

Groupdate 2.0

  • day_start option
  • change preferred method call User.group_by_day(:created_at, time_zone: time_zone, range: range).count
  • return series by default User.group_by_day(:created_at, series: false).count

Passing in library options don't work

Hi, I tired <%= line_chart data, library: {backgroundColor: "#eee"} %>
but instead I wanted to hide the legend of the GoogleChart with {legend: "none"} which is part of the google customization options. However the legends still appear.

Issues with SQLite

I've recently discovered Groupdate & Chartkick and find it to be super useful as someone who is coding completely in RoR with minimal JS. When using the group_by_* commands, I am getting the following error: "Connection adapter not supported: SQLite".

  1. I looked at the closed issues and there was a thread relating to SQLite but I wasn't sure if that was resolved.
  2. As someone new to programming, I'm a bit confused by setting time zone support. I saw the line "mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql" but not sure where to run this. Does this need to be done in the rails console for each environment (dev & production)? Is this supposed to be placed within an actual file? The MySQL resource link did not clarify anything for me.

I appreciate any assistance since I find your tools to be very helpful for my projects.

All MySQL queries returning nil groups

I thought I had this working the other day, but maybe not.

Anyway, I've tried groupdate on two different projects using the mysql2 gem, on both Mac and Linux, and both are returning the same results: all queries are returning nil as the sole group.

These same commands all work swimmingly in Postgres (and SQLite with mieko's pull request).

Examples:

>> Document.unscoped.group_by_month(:created_at).count
   (0.3ms)  SELECT COUNT(*) AS count_all, CONVERT_TZ(DATE_FORMAT(CO>> Document.unscoped.group_by_month(:created_at).count
   (0.3ms)  SELECT COUNT(*) AS count_all, CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'America/Chicago'), '%Y-%m-01 00:00:00'), 'America/Chicago', '+00:00') AS month FROM `assets` WHERE `assets`.`type` IN ('Document') GROUP BY CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'America/Chicago'), '%Y-%m-01 00:00:00'), 'America/Chicago', '+00:00')
=> {nil=>3}NVERT_TZ(`created_at`, '+00:00', 'America/Chicago'), '%Y-%m-01 00:00:00'), 'America/Chicago', '+00:00') AS month FROM `assets` WHERE `assets`.`type` IN ('Document') GROUP BY CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`created_at`, '+00:00', 'America/Chicago'), '%Y-%m-01 00:00:00'), 'America/Chicago', '+00:00')
=> {nil=>3}
>> Task.group_by_year('tasks.updated_at').count
   (0.8ms)  SELECT COUNT(*) AS count_all, CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`tasks`.`updated_at`, '+00:00', 'Etc/UTC'), '%Y-01-01 00:00:00'), 'Etc/UTC', '+00:00') AS year FROM `tasks` GROUP BY CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(`tasks`.`updated_at`, '+00:00', 'Etc/UTC'), '%Y-01-01 00:00:00'), 'Etc/UTC', '+00:00')
=> {nil=>23}

Any idea what I can do to get these working?

SemVer not respected from 2.1.1 to 2.2.0

Hi,

I'm using groupdate in a project

I just updated from 2.1.1 to 2.2.0 and my specs don't pass anymore

I was using this signature before:

my_model.group_by_day(:date, nil, range)

Now I get this error:

ArgumentError: wrong number of arguments (3 for 0..1)

Please let me know if you need more information

Result mapping is always given zero results

Hi
i am using ruby 1.9.3 and rails 2.3.14 and mysql 5.5,

when i am running the following in rails console
User.group_by_day(:created_at).count
SELECT COUNT(*) AS count_all, DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(created_at, INTERVAL 0 HOUR), '+00:00', 'America/Chicago'), '%Y-%m-%d 00:00:00'), 'America/Chicago', '+00:00'), INTERVAL 0 HOUR) AS day FROM users WHERE (created_at IS NOT NULL) GROUP BY DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(created_at, INTERVAL 0 HOUR), '+00:00', 'America/Chicago'), '%Y-%m-%d 00:00:00'), 'America/Chicago', '+00:00'), INTERVAL 0 HOUR)
=> {Mon, 16 Jun 2014 00:00:00 CDT -05:00=>0, Tue, 17 Jun 2014 00:00:00 CDT -05:00=>0, Wed, 18 Jun 2014 00:00:00 CDT -05:00=>0, Thu, 19 Jun 2014 00:00:00 CDT -05:00=>0, Fri, 20 Jun 2014 00:00:00 CDT -05:00=>0, Sat, 21 Jun 2014 00:00:00 CDT -05:00=>0, Sun, 22 Jun 2014 00:00:00 CDT -05:00=>0, Mon, 23 Jun 2014 00:00:00 CDT -05:00=>0, Tue, 24 Jun 2014 00:00:00 CDT -05:00=>0, Wed, 25 Jun 2014 00:00:00 CDT -05:00=>0, Thu, 26 Jun 2014 00:00:00 CDT -05:00=>0, Fri, 27 Jun 2014 00:00:00 CDT -05:00=>0, Sat, 28 Jun 2014 00:00:00 CDT -05:00=>0, Sun, 29 Jun 2014 00:00:00 CDT -05:00=>0, Mon, 30 Jun 2014 00:00:00 CDT -05:00=>0, Tue, 01 Jul 2014 00:00:00 CDT -05:00=>0, Wed, 02 Jul 2014 00:00:00 CDT -05:00=>0, Thu, 03 Jul 2014 00:00:00 CDT -05:00=>0, Fri, 04 Jul 2014 00:00:00 CDT -05:00=>0, Sat, 05 Jul 2014 00:00:00 CDT -05:00=>0, Sun, 06 Jul 2014 00:00:00 CDT -05:00=>0, Mon, 07 Jul 2014 00:00:00 CDT -05:00=>0, Tue, 08 Jul 2014 00:00:00 CDT -05:00=>0, Wed, 09 Jul 2014 00:00:00 CDT -05:00=>0, Thu, 10 Jul 2014 00:00:00 CDT -05:00=>0, Fri, 11 Jul 2014 00:00:00 CDT -05:00=>0, Sat, 12 Jul 2014 00:00:00 CDT -05:00=>0, Sun, 13 Jul 2014 00:00:00 CDT -05:00=>0, Mon, 14 Jul 2014 00:00:00 CDT -05:00=>0}

the results are zero.
but when i run same command in mysql console

SELECT COUNT(*) AS count_all, DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(created_at, INTERVAL 0 HOUR), '+00:00', 'America/Chicago'), '%Y-%m-%d 00:00:00'), 'America/Chicago', '+00:00'), INTERVAL 0 HOUR) AS day FROM users WHERE (created_at IS NOT NULL) GROUP BY DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(created_at, INTERVAL 0 HOUR), '+00:00', 'America/Chicago'), '%Y-%m-%d 00:00:00'), 'America/Chicago', '+00:00'), INTERVAL 0 HOUR)
-> ;
+-----------+---------------------+
| count_all | day |
+-----------+---------------------+
| 1 | 2014-06-17 05:00:00 |
| 7 | 2014-06-18 05:00:00 |
| 2 | 2014-06-19 05:00:00 |
| 2 | 2014-06-23 05:00:00 |
| 1 | 2014-06-24 05:00:00 |
| 1 | 2014-06-30 05:00:00 |
| 2 | 2014-07-07 05:00:00 |
| 1 | 2014-07-10 05:00:00 |
| 3 | 2014-07-11 05:00:00 |
| 1 | 2014-07-14 05:00:00 |
| 1 | 2014-07-15 05:00:00 |
+-----------+---------------------+
11 rows in set (0.00 sec)

Group => 0

I have "discussions" and would like to show statistics on discussion creation so I set out to get some charts working (Chartkick + groupdate). Going by the very first example on the readme, Discussion.group_by_day(:created_at).count should work I assume. Here's the results: http://pastie.org/9001756. The correct dates are shown, but the count is always 0 when it should be either 1 or 0 in this case. The issue is consistent with other tables as well.

  • Running Rails 4.0.2 and Ruby 1.9.3
  • Using MySQL Database and time zones are working

Could be a bug, or maybe there's something simple that I'm missing. Thanks for any help in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.