Coder Social home page Coder Social logo

groupdate's Introduction

Groupdate

The simplest way to group by:

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

🎉 Time zones supported!! the best part

🍰 Get the entire series - the other best part

Works with Rails 3.0+

Supports PostgreSQL and MySQL

Build Status

💘 Goes hand in hand with Chartkick

Get Started

Group by day

User.group_by_day(:created_at).count
# {
#   2013-04-16 00:00:00 UTC => 50,
#   2013-04-17 00:00:00 UTC => 100,
#   2013-04-18 00:00:00 UTC => 34
# }

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

You can also group by:

  • second
  • minute
  • hour
  • week
  • month
  • year

and

  • hour_of_day
  • day_of_week (Sunday = 0, Monday = 1, etc)

Use it anywhere you can use group.

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
# {
#   2013-03-10 00:00:00 PST => 70,
#   2013-03-17 00:00:00 PDT => 54,
#   2013-03-24 00:00:00 PDT => 80
# }

Time zone objects also work.

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

Order

You can order in descending order with:

User.group_by_day(:created_at).reverse_order.count

or

User.group_by_day(:created_at).order("day desc").count

Pretty Keys

To get keys in a different format, use:

User.group_by_hour_of_day(:created_at, format: "%l %P").count.keys.first # 12 am

Takes a String, which is passed to strftime, or a Proc

Installation

Add this line to your application’s Gemfile:

gem 'groupdate'

For MySQL

Time zone support must be installed on the server.

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

For JRuby

Use the master version of your JDBC adapter. You will get incorrect results for versions before this commit.

# postgresql
gem "activerecord-jdbcpostgresql-adapter", :github => "jruby/activerecord-jdbc-adapter"

# mysql
gem "activerecord-jdbcmysql-adapter", :github => "jruby/activerecord-jdbc-adapter"

Upgrading to 2.0

Groupdate 2.0 brings a number a great 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:

groupdate's People

Contributors

ankane avatar caulfield avatar tompesman avatar

Watchers

James Cloos avatar Kirill Petrenko avatar

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.