groupdate

The simplest way to group temporal data

  • 所有者: ankane/groupdate
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Groupdate

The simplest way to group by:

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

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

:cake: Get the entire series - the other best part

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

:cupid: 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

  • 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, 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 = :mon # first three letters of day

or

User.group_by_week(:created_at, week_start: :mon).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 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.

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

User Input

If passing user input as the column, be sure to sanitize it first like you must with group.

column = params[:column]

# check against permitted columns
raise "Unpermitted column" unless ["column_a", "column_b"].include?(column)

User.group_by_day(column).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 }.map {, k, v, [k, v.count] }.to_h

Additional Instructions

For MySQL

Time zone support must be installed on the server.

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

or copy and paste these statements into a SQL console.

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 or week_start options
  • 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

Upgrading

4.0

Groupdate 4.0 brings a number of improvements. Here are a few to be aware of:

  • group_by methods return an ActiveRecord::Relation instead of a Groupdate::Series
  • Invalid options now throw an ArgumentError
  • week_start now affects day_of_week
  • Custom calculation methods are supported by default

3.0

Groupdate 3.0 brings a number of improvements. Here are a few to be aware of:

  • Date objects are now returned for day, week, month, quarter, and year by default. Use dates: false for the previous behavior, or change this globally with Groupdate.dates = false.
  • Array and hash methods no longer return the entire series by default. Use series: true for the previous behavior.
  • The series: false option now returns the correct type and order, and plays nicely with other options.

2.0

Groupdate 2.0 brings a number of improvements. Here are two things to be aware of:

  • the entire series is returned by default
  • ActiveSupport::TimeWithZone keys are now returned for every database adapter - adapters previously returned Time or String keys

History

View the changelog

Groupdate follows Semantic Versioning

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.

主要指标

概览
名称与所有者ankane/groupdate
主编程语言Ruby
编程语言Ruby (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2013-04-18 05:39:03
推送于2025-06-04 00:43:12
最后一次提交2025-06-03 17:43:10
发布数64
最新版本名称v6.7.0 (发布于 2025-06-02 17:04:15)
第一版名称v0.0.1 (发布于 2013-04-17 22:40:13)
用户参与
星数3.8k
关注者数45
派生数237
提交数838
已启用问题?
问题数226
打开的问题数2
拉请求数29
打开的拉请求数0
关闭的拉请求数34
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?