groupdate 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/README.md +58 -19
- data/groupdate.gemspec +0 -1
- data/lib/groupdate.rb +5 -5
- data/lib/groupdate/version.rb +1 -1
- data/test/groupdate_test.rb +22 -18
- metadata +1 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db67453333111a42475d9ca697eb81181c6f291d
|
4
|
+
data.tar.gz: bf1a4d74a65edc1aa710ac417800fe5d5d2c43bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc7436598003f15a4219dc3fae9964b99bba54cb610b2a553b84b5365e5434f2ccca3c3c0ed2fb344a00e64c03e61a334700a32d837c687460e24b334dabab1f
|
7
|
+
data.tar.gz: dd4de87e97a685898f3d61f465e768cf6886df28497b5b9b1f7015b9c1ca9ab48870e3acc1dc8bc59279824f14ba252cd4db9f85bcae0cc2b0722a37487cab2b
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -18,23 +18,23 @@ PostgreSQL and MySQL only at the moment - support for other datastores coming so
|
|
18
18
|
```ruby
|
19
19
|
User.group_by_day(:created_at).count
|
20
20
|
# {
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
21
|
+
# 2013-04-16 00:00:00 UTC => 50,
|
22
|
+
# 2013-04-17 00:00:00 UTC => 100,
|
23
|
+
# 2013-04-18 00:00:00 UTC => 34
|
24
24
|
# }
|
25
25
|
|
26
26
|
Task.group_by_month(:updated_at).count
|
27
27
|
# {
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
28
|
+
# 2013-02-01 00:00:00 UTC => 84,
|
29
|
+
# 2013-03-01 00:00:00 UTC => 23,
|
30
|
+
# 2013-04-01 00:00:00 UTC => 44
|
31
31
|
# }
|
32
32
|
|
33
33
|
Goal.group_by_year(:accomplished_at).count
|
34
34
|
# {
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
35
|
+
# 2011-01-01 00:00:00 UTC => 7,
|
36
|
+
# 2012-01-01 00:00:00 UTC => 11,
|
37
|
+
# 2013-01-01 00:00:00 UTC => 3
|
38
38
|
# }
|
39
39
|
```
|
40
40
|
|
@@ -43,9 +43,9 @@ The default time zone is `Time.zone`. Pass a time zone as the second argument.
|
|
43
43
|
```ruby
|
44
44
|
User.group_by_week(:created_at, "Pacific Time (US & Canada)").count
|
45
45
|
# {
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
46
|
+
# 2013-03-03 08:00:00 UTC => 80,
|
47
|
+
# 2013-03-10 08:00:00 UTC => 70,
|
48
|
+
# 2013-03-17 07:00:00 UTC => 54
|
49
49
|
# }
|
50
50
|
|
51
51
|
# equivalently
|
@@ -61,19 +61,19 @@ You can also group by the day of the week or hour of the day.
|
|
61
61
|
# day of the week
|
62
62
|
User.group_by_day_of_week(:created_at).count
|
63
63
|
# {
|
64
|
-
#
|
65
|
-
#
|
64
|
+
# 0 => 54, # Sunday
|
65
|
+
# 1 => 2, # Monday
|
66
66
|
# ...
|
67
|
-
#
|
67
|
+
# 6 => 3 # Saturday
|
68
68
|
# }
|
69
69
|
|
70
70
|
# hour of the day
|
71
71
|
User.group_by_hour_of_day(:created_at, "Pacific Time (US & Canada)").count
|
72
72
|
# {
|
73
|
-
#
|
74
|
-
#
|
73
|
+
# 0 => 34,
|
74
|
+
# 1 => 61,
|
75
75
|
# ...
|
76
|
-
#
|
76
|
+
# 23 => 12
|
77
77
|
# }
|
78
78
|
```
|
79
79
|
|
@@ -89,7 +89,38 @@ Go nuts!
|
|
89
89
|
Request.where(page: "/home").group_by_minute(:started_at).maximum(:request_time)
|
90
90
|
```
|
91
91
|
|
92
|
-
|
92
|
+
### Note
|
93
|
+
|
94
|
+
activerecord <= 4.0.0.beta1 and the pg gem returns String objects instead of Time objects.
|
95
|
+
[This is fixed on activerecord master](https://github.com/rails/rails/commit/2cc09441c2de57b024b11ba666ba1e72c2b20cfe)
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
User.group_by_day(:created_at).count
|
99
|
+
|
100
|
+
# mysql2
|
101
|
+
# pg and activerecord master
|
102
|
+
{2013-04-22 00:00:00 UTC => 1} # Time object
|
103
|
+
|
104
|
+
# pg and activerecord <= 4.0.0.beta1
|
105
|
+
{"2013-04-22 00:00:00+00" => 1} # String
|
106
|
+
```
|
107
|
+
|
108
|
+
Another data type inconsistency
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
User.group_by_day_of_week(:created_at).count
|
112
|
+
|
113
|
+
# mysql2
|
114
|
+
{0 => 1, 4 => 1} # Integer
|
115
|
+
|
116
|
+
# pg and activerecord <= 4.0.0.beta1
|
117
|
+
{"0" => 1, "4" => 1} # String
|
118
|
+
|
119
|
+
# pg and activerecord master
|
120
|
+
{0.0 => 1, 4.0 => 1} # Float
|
121
|
+
```
|
122
|
+
|
123
|
+
These are *not* a result of groupdate (and unfortunately cannot be fixed by groupdate)
|
93
124
|
|
94
125
|
## Installation
|
95
126
|
|
@@ -99,6 +130,14 @@ Add this line to your application's Gemfile:
|
|
99
130
|
gem 'groupdate'
|
100
131
|
```
|
101
132
|
|
133
|
+
### MySQL only
|
134
|
+
|
135
|
+
[Time zone support](http://dev.mysql.com/doc/refman/5.6/en/time-zone-support.html) must be installed on the server.
|
136
|
+
|
137
|
+
```
|
138
|
+
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
139
|
+
```
|
140
|
+
|
102
141
|
## Complete list
|
103
142
|
|
104
143
|
group_by_?
|
data/groupdate.gemspec
CHANGED
data/lib/groupdate.rb
CHANGED
@@ -39,15 +39,15 @@ module Groupdate
|
|
39
39
|
end
|
40
40
|
query =
|
41
41
|
case connection.adapter_name
|
42
|
-
when "
|
42
|
+
when "Mysql2"
|
43
43
|
case field
|
44
44
|
when "day_of_week" # Sunday = 0, Monday = 1, etc
|
45
45
|
# use CONCAT for consistent return type (String)
|
46
|
-
["
|
46
|
+
["DAYOFWEEK(CONVERT_TZ(#{column}, '+00:00', ?)) - 1", time_zone]
|
47
47
|
when "hour_of_day"
|
48
|
-
["
|
48
|
+
["EXTRACT(HOUR from CONVERT_TZ(#{column}, '+00:00', ?))", time_zone]
|
49
49
|
when "week"
|
50
|
-
["
|
50
|
+
["CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(#{column}, INTERVAL (DAYOFWEEK(CONVERT_TZ(#{column}, '+00:00', ?)) - 1) DAY), '+00:00', ?), '%Y-%m-%d 00:00:00'), ?, '+00:00')", time_zone, time_zone, time_zone]
|
51
51
|
else
|
52
52
|
format =
|
53
53
|
case field
|
@@ -65,7 +65,7 @@ module Groupdate
|
|
65
65
|
"%Y-01-01 00:00:00"
|
66
66
|
end
|
67
67
|
|
68
|
-
["
|
68
|
+
["CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(#{column}, '+00:00', ?), '#{format}'), ?, '+00:00')", time_zone, time_zone]
|
69
69
|
end
|
70
70
|
when "PostgreSQL"
|
71
71
|
case field
|
data/lib/groupdate/version.rb
CHANGED
data/test/groupdate_test.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require "minitest/spec"
|
2
1
|
require "minitest/autorun"
|
2
|
+
require "minitest/pride"
|
3
3
|
require "active_record"
|
4
4
|
require "groupdate"
|
5
5
|
require "logger"
|
@@ -15,7 +15,7 @@ class User < ActiveRecord::Base
|
|
15
15
|
end
|
16
16
|
|
17
17
|
describe Groupdate do
|
18
|
-
%w(postgresql
|
18
|
+
%w(postgresql mysql2).each do |adapter|
|
19
19
|
describe adapter do
|
20
20
|
before do
|
21
21
|
ActiveRecord::Base.establish_connection adapter: adapter, database: "groupdate"
|
@@ -37,84 +37,88 @@ describe Groupdate do
|
|
37
37
|
].each{|u| User.create!(u) }
|
38
38
|
|
39
39
|
assert_equal(
|
40
|
-
{"2013-04-01 00:00:00+00" => 1, "2013-04-02 00:00:00+00" => 1},
|
40
|
+
{"2013-04-01 00:00:00+00".to_time => 1, "2013-04-02 00:00:00+00".to_time => 1},
|
41
41
|
User.where("score > 1").group_by_day(:created_at).count
|
42
42
|
)
|
43
43
|
end
|
44
44
|
|
45
45
|
it "group_by_second" do
|
46
46
|
create_user "2013-04-01 00:00:01 UTC"
|
47
|
-
assert_equal({"2013-04-01 00:00:01+00" => 1}, User.group_by_second(:created_at).count)
|
47
|
+
assert_equal({"2013-04-01 00:00:01+00".to_time => 1}, User.group_by_second(:created_at).count)
|
48
48
|
end
|
49
49
|
|
50
50
|
it "group_by_minute" do
|
51
51
|
create_user "2013-04-01 00:01:01 UTC"
|
52
|
-
assert_equal({"2013-04-01 00:01:00+00" => 1}, User.group_by_minute(:created_at).count)
|
52
|
+
assert_equal({"2013-04-01 00:01:00+00".to_time => 1}, User.group_by_minute(:created_at).count)
|
53
53
|
end
|
54
54
|
|
55
55
|
it "group_by_hour" do
|
56
56
|
create_user "2013-04-01 01:01:01 UTC"
|
57
|
-
assert_equal({"2013-04-01 01:00:00+00" => 1}, User.group_by_hour(:created_at).count)
|
57
|
+
assert_equal({"2013-04-01 01:00:00+00".to_time => 1}, User.group_by_hour(:created_at).count)
|
58
58
|
end
|
59
59
|
|
60
60
|
it "group_by_day" do
|
61
61
|
create_user "2013-04-01 01:01:01 UTC"
|
62
|
-
assert_equal({"2013-04-01 00:00:00+00" => 1}, User.group_by_day(:created_at).count)
|
62
|
+
assert_equal({"2013-04-01 00:00:00+00".to_time => 1}, User.group_by_day(:created_at).count)
|
63
63
|
end
|
64
64
|
|
65
65
|
it "group_by_day with time zone" do
|
66
66
|
create_user "2013-04-01 01:01:01 UTC"
|
67
|
-
assert_equal({"2013-03-31 07:00:00+00" => 1}, User.group_by_day(:created_at, "Pacific Time (US & Canada)").count)
|
67
|
+
assert_equal({"2013-03-31 07:00:00+00".to_time => 1}, User.group_by_day(:created_at, "Pacific Time (US & Canada)").count)
|
68
68
|
end
|
69
69
|
|
70
70
|
it "group_by_week" do
|
71
71
|
create_user "2013-03-17 01:01:01 UTC"
|
72
|
-
assert_equal({"2013-03-17 00:00:00+00" => 1}, User.group_by_week(:created_at).count)
|
72
|
+
assert_equal({"2013-03-17 00:00:00+00".to_time => 1}, User.group_by_week(:created_at).count)
|
73
73
|
end
|
74
74
|
|
75
75
|
it "group_by_week with time zone" do # day of DST
|
76
76
|
create_user "2013-03-17 01:01:01 UTC"
|
77
|
-
assert_equal({"2013-03-10 08:00:00+00" => 1}, User.group_by_week(:created_at, "Pacific Time (US & Canada)").count)
|
77
|
+
assert_equal({"2013-03-10 08:00:00+00".to_time => 1}, User.group_by_week(:created_at, "Pacific Time (US & Canada)").count)
|
78
78
|
end
|
79
79
|
|
80
80
|
it "group_by_month" do
|
81
81
|
create_user "2013-04-01 01:01:01 UTC"
|
82
|
-
assert_equal({"2013-04-01 00:00:00+00" => 1}, User.group_by_month(:created_at).count)
|
82
|
+
assert_equal({"2013-04-01 00:00:00+00".to_time(:utc) => 1}, User.group_by_month(:created_at).count)
|
83
83
|
end
|
84
84
|
|
85
85
|
it "group_by_month with time zone" do
|
86
86
|
create_user "2013-04-01 01:01:01 UTC"
|
87
|
-
assert_equal({"2013-03-01 08:00:00+00" => 1}, User.group_by_month(:created_at, "Pacific Time (US & Canada)").count)
|
87
|
+
assert_equal({"2013-03-01 08:00:00+00".to_time(:utc) => 1}, User.group_by_month(:created_at, "Pacific Time (US & Canada)").count)
|
88
88
|
end
|
89
89
|
|
90
90
|
it "group_by_year" do
|
91
91
|
create_user "2013-01-01 01:01:01 UTC"
|
92
|
-
assert_equal({"2013-01-01 00:00:00+00" => 1}, User.group_by_year(:created_at).count)
|
92
|
+
assert_equal({"2013-01-01 00:00:00+00".to_time(:utc) => 1}, User.group_by_year(:created_at).count)
|
93
93
|
end
|
94
94
|
|
95
95
|
it "group_by_year with time zone" do
|
96
96
|
create_user "2013-01-01 01:01:01 UTC"
|
97
|
-
assert_equal({"2012-01-01 08:00:00+00" => 1}, User.group_by_year(:created_at, "Pacific Time (US & Canada)").count)
|
97
|
+
assert_equal({"2012-01-01 08:00:00+00".to_time(:utc) => 1}, User.group_by_year(:created_at, "Pacific Time (US & Canada)").count)
|
98
98
|
end
|
99
99
|
|
100
100
|
it "group_by_hour_of_day" do
|
101
101
|
create_user "2013-01-01 11:00:00 UTC"
|
102
|
-
|
102
|
+
expected = adapter == "mysql2" ? {11 => 1} : {11.0 => 1}
|
103
|
+
assert_equal(expected, User.group_by_hour_of_day(:created_at).count)
|
103
104
|
end
|
104
105
|
|
105
106
|
it "group_by_hour_of_day with time zone" do
|
106
107
|
create_user "2013-01-01 11:00:00 UTC"
|
107
|
-
|
108
|
+
expected = adapter == "mysql2" ? {3 => 1} : {3.0 => 1}
|
109
|
+
assert_equal(expected, User.group_by_hour_of_day(:created_at, "Pacific Time (US & Canada)").count)
|
108
110
|
end
|
109
111
|
|
110
112
|
it "group_by_day_of_week" do
|
111
113
|
create_user "2013-03-03 00:00:00 UTC"
|
112
|
-
|
114
|
+
expected = adapter == "mysql2" ? {0 => 1} : {0.0 => 1}
|
115
|
+
assert_equal(expected, User.group_by_day_of_week(:created_at).count)
|
113
116
|
end
|
114
117
|
|
115
118
|
it "group_by_day_of_week with time zone" do
|
116
119
|
create_user "2013-03-03 00:00:00 UTC"
|
117
|
-
|
120
|
+
expected = adapter == "mysql2" ? {6 => 1} : {6.0 => 1}
|
121
|
+
assert_equal(expected, User.group_by_day_of_week(:created_at, "Pacific Time (US & Canada)").count)
|
118
122
|
end
|
119
123
|
|
120
124
|
# helper methods
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: groupdate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
@@ -80,20 +80,6 @@ dependencies:
|
|
80
80
|
- - '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: mysql
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - '>='
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - '>='
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
84
|
name: mysql2
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|