groupdate 3.0.0 → 3.0.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/.travis.yml +3 -2
- data/CHANGELOG.md +5 -0
- data/Gemfile +1 -1
- data/README.md +15 -1
- data/Rakefile +5 -1
- data/lib/groupdate/magic.rb +26 -1
- data/lib/groupdate/version.rb +1 -1
- data/test/gemfiles/{activerecord50.gemfile → activerecord42.gemfile} +1 -1
- data/test/gemfiles/redshift.gemfile +7 -0
- data/test/redshift_test.rb +18 -0
- data/test/test_helper.rb +25 -2
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd126e51d0c9b683eae721f9c2bf6a133409039b
|
4
|
+
data.tar.gz: 0f5f4487b87fbe82f22eea5edebe9558cc883593
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b9c7878256b705354bb3ccef1c87946fa94815f34144cf1332775ec448f2db35bcd485399dc91bddef7089d56d92e94ed9be18acf4332fe415074b927a1d32e
|
7
|
+
data.tar.gz: 8653c47e5f08f1001974aad183fbd7c182673a0da9436d8e1d79b288cac728621a472d3c9abc92ee5b5303ffc0d4a5d2090e96a69df3b33786096e92d845d35f
|
data/.travis.yml
CHANGED
@@ -8,7 +8,7 @@ gemfile:
|
|
8
8
|
- test/gemfiles/activerecord32.gemfile
|
9
9
|
- test/gemfiles/activerecord40.gemfile
|
10
10
|
- test/gemfiles/activerecord41.gemfile
|
11
|
-
- test/gemfiles/
|
11
|
+
- test/gemfiles/activerecord42.gemfile
|
12
12
|
sudo: false
|
13
13
|
script: bundle exec rake test
|
14
14
|
before_install:
|
@@ -24,4 +24,5 @@ matrix:
|
|
24
24
|
allow_failures:
|
25
25
|
- rvm: jruby
|
26
26
|
gemfile: Gemfile
|
27
|
-
-
|
27
|
+
- rvm: jruby
|
28
|
+
gemfile: test/gemfiles/activerecord42.gemfile
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -13,7 +13,7 @@ The simplest way to group by:
|
|
13
13
|
|
14
14
|
Works with Rails 3.1+
|
15
15
|
|
16
|
-
Supports PostgreSQL and
|
16
|
+
Supports PostgreSQL, MySQL, and Redshift, plus arrays and hashes
|
17
17
|
|
18
18
|
[](https://travis-ci.org/ankane/groupdate)
|
19
19
|
|
@@ -164,6 +164,20 @@ User.group_by_hour_of_day(:created_at, format: "%-l %P").count
|
|
164
164
|
|
165
165
|
Takes a `String`, which is passed to [strftime](http://strfti.me/), 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.
|
166
166
|
|
167
|
+
### Series
|
168
|
+
|
169
|
+
The entire series is returned by default. To exclude points without data, use:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
User.group_by_day(:created_at, series: false).count
|
173
|
+
```
|
174
|
+
|
175
|
+
Or change the default value with:
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
User.group_by_day(:created_at, default_value: "missing").count
|
179
|
+
```
|
180
|
+
|
167
181
|
### Dynamic Grouping
|
168
182
|
|
169
183
|
```ruby
|
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ require "rake/testtask"
|
|
4
4
|
task default: :test
|
5
5
|
Rake::TestTask.new do |t|
|
6
6
|
t.libs << "test"
|
7
|
-
t.
|
7
|
+
t.test_files = FileList["test/**/*_test.rb"].exclude(/redshift/)
|
8
8
|
end
|
9
9
|
|
10
10
|
namespace :test do
|
@@ -16,4 +16,8 @@ namespace :test do
|
|
16
16
|
t.libs << "test"
|
17
17
|
t.pattern = "test/mysql_test.rb"
|
18
18
|
end
|
19
|
+
Rake::TestTask.new(:redshift) do |t|
|
20
|
+
t.libs << "test"
|
21
|
+
t.pattern = "test/redshift_test.rb"
|
22
|
+
end
|
19
23
|
end
|
data/lib/groupdate/magic.rb
CHANGED
@@ -77,6 +77,25 @@ module Groupdate
|
|
77
77
|
else
|
78
78
|
["(DATE_TRUNC('#{field}', (#{column}::timestamptz - INTERVAL '#{day_start} second') AT TIME ZONE ?) + INTERVAL '#{day_start} second') AT TIME ZONE ?", time_zone, time_zone]
|
79
79
|
end
|
80
|
+
when "Redshift"
|
81
|
+
case field
|
82
|
+
when :day_of_week # Sunday = 0, Monday = 1, etc.
|
83
|
+
["EXTRACT(DOW from CONVERT_TIMEZONE(?, #{column}::timestamp) - INTERVAL '#{day_start} second')::integer", time_zone]
|
84
|
+
when :hour_of_day
|
85
|
+
["EXTRACT(HOUR from CONVERT_TIMEZONE(?, #{column}::timestamp) - INTERVAL '#{day_start} second')::integer", time_zone]
|
86
|
+
when :day_of_month
|
87
|
+
["EXTRACT(DAY from CONVERT_TIMEZONE(?, #{column}::timestamp) - INTERVAL '#{day_start} second')::integer", time_zone]
|
88
|
+
when :month_of_year
|
89
|
+
["EXTRACT(MONTH from CONVERT_TIMEZONE(?, #{column}::timestamp) - INTERVAL '#{day_start} second')::integer", time_zone]
|
90
|
+
when :week # start on Sunday, not Redshift default Monday
|
91
|
+
# Redshift does not return timezone information; it
|
92
|
+
# always says it is in UTC time, so we must convert
|
93
|
+
# back to UTC to play properly with the rest of Groupdate.
|
94
|
+
#
|
95
|
+
["CONVERT_TIMEZONE(?, 'Etc/UTC', DATE_TRUNC(?, CONVERT_TIMEZONE(?, #{column}) - INTERVAL '#{week_start} day' - INTERVAL '#{day_start} second'))::timestamp + INTERVAL '#{week_start} day' + INTERVAL '#{day_start} second'", time_zone, field, time_zone]
|
96
|
+
else
|
97
|
+
["CONVERT_TIMEZONE(?, 'Etc/UTC', DATE_TRUNC(?, CONVERT_TIMEZONE(?, #{column}) - INTERVAL '#{day_start} second'))::timestamp + INTERVAL '#{day_start} second'", time_zone, field, time_zone]
|
98
|
+
end
|
80
99
|
else
|
81
100
|
raise "Connection adapter not supported: #{adapter_name}"
|
82
101
|
end
|
@@ -205,8 +224,14 @@ module Groupdate
|
|
205
224
|
step = 1.send(field)
|
206
225
|
end
|
207
226
|
|
208
|
-
|
227
|
+
last_step = series.last
|
228
|
+
while (next_step = round_time(last_step + step)) && time_range.cover?(next_step)
|
229
|
+
if next_step == last_step
|
230
|
+
last_step += step
|
231
|
+
next
|
232
|
+
end
|
209
233
|
series << next_step
|
234
|
+
last_step = next_step
|
210
235
|
end
|
211
236
|
|
212
237
|
series
|
data/lib/groupdate/version.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class TestRedshift < Minitest::Test
|
4
|
+
include TestGroupdate
|
5
|
+
include TestDatabase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
super
|
9
|
+
@@setup ||= begin
|
10
|
+
abort("REDSHIFT_URL environment variable must be set in order to run tests") unless ENV["REDSHIFT_URL"].present?
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(ENV["REDSHIFT_URL"])
|
13
|
+
|
14
|
+
create_redshift_tables
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -49,6 +49,22 @@ def create_tables
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
+
def create_redshift_tables
|
53
|
+
ActiveRecord::Migration.verbose = false
|
54
|
+
|
55
|
+
if ActiveRecord::Migration.table_exists?(:users)
|
56
|
+
ActiveRecord::Migration.drop_table(:users, force: :cascade)
|
57
|
+
end
|
58
|
+
|
59
|
+
if ActiveRecord::Migration.table_exists?(:posts)
|
60
|
+
ActiveRecord::Migration.drop_table(:posts, force: :cascade)
|
61
|
+
end
|
62
|
+
|
63
|
+
ActiveRecord::Migration.execute "CREATE TABLE users (id INT IDENTITY(1,1) PRIMARY KEY, name VARCHAR(255), score INT, created_at DATETIME, created_on DATE);"
|
64
|
+
|
65
|
+
ActiveRecord::Migration.execute "CREATE TABLE posts (id INT IDENTITY(1,1) PRIMARY KEY, user_id INT REFERENCES users, created_at DATETIME);"
|
66
|
+
end
|
67
|
+
|
52
68
|
module TestDatabase
|
53
69
|
def test_zeros_previous_scope
|
54
70
|
create_user "2013-05-01"
|
@@ -332,12 +348,19 @@ module TestDatabase
|
|
332
348
|
created_on: created_at ? Date.parse(created_at) : nil
|
333
349
|
)
|
334
350
|
|
335
|
-
# hack for
|
336
|
-
|
351
|
+
# hack for Redshift adapter, which doesn't return id on creation...
|
352
|
+
user = User.last if user.id.nil?
|
353
|
+
|
354
|
+
# hack for MySQL & Redshift adapters
|
355
|
+
user.update_attributes(created_at: nil, created_on: nil) if created_at.nil? && is_redshift?
|
337
356
|
|
338
357
|
user
|
339
358
|
end
|
340
359
|
|
360
|
+
def is_redshift?
|
361
|
+
ActiveRecord::Base.connection.adapter_name == "Redshift"
|
362
|
+
end
|
363
|
+
|
341
364
|
def teardown
|
342
365
|
User.delete_all
|
343
366
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: groupdate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -136,9 +136,11 @@ files:
|
|
136
136
|
- test/gemfiles/activerecord32.gemfile
|
137
137
|
- test/gemfiles/activerecord40.gemfile
|
138
138
|
- test/gemfiles/activerecord41.gemfile
|
139
|
-
- test/gemfiles/
|
139
|
+
- test/gemfiles/activerecord42.gemfile
|
140
|
+
- test/gemfiles/redshift.gemfile
|
140
141
|
- test/mysql_test.rb
|
141
142
|
- test/postgresql_test.rb
|
143
|
+
- test/redshift_test.rb
|
142
144
|
- test/test_helper.rb
|
143
145
|
homepage: https://github.com/ankane/groupdate
|
144
146
|
licenses:
|
@@ -170,8 +172,10 @@ test_files:
|
|
170
172
|
- test/gemfiles/activerecord32.gemfile
|
171
173
|
- test/gemfiles/activerecord40.gemfile
|
172
174
|
- test/gemfiles/activerecord41.gemfile
|
173
|
-
- test/gemfiles/
|
175
|
+
- test/gemfiles/activerecord42.gemfile
|
176
|
+
- test/gemfiles/redshift.gemfile
|
174
177
|
- test/mysql_test.rb
|
175
178
|
- test/postgresql_test.rb
|
179
|
+
- test/redshift_test.rb
|
176
180
|
- test/test_helper.rb
|
177
181
|
has_rdoc:
|