groupdate 5.2.0 → 5.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/LICENSE.txt +1 -1
- data/README.md +9 -3
- data/lib/groupdate/adapters/base_adapter.rb +76 -0
- data/lib/groupdate/adapters/mysql_adapter.rb +57 -0
- data/lib/groupdate/adapters/postgresql_adapter.rb +44 -0
- data/lib/groupdate/adapters/redshift_adapter.rb +39 -0
- data/lib/groupdate/adapters/sqlite_adapter.rb +51 -0
- data/lib/groupdate/magic.rb +18 -1
- data/lib/groupdate/series_builder.rb +27 -8
- data/lib/groupdate/version.rb +1 -1
- data/lib/groupdate.rb +23 -1
- metadata +13 -107
- data/lib/groupdate/relation_builder.rb +0 -227
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b6c83f192d439e35025586a076762413d80daab15b85e81561404bdf078acad
|
4
|
+
data.tar.gz: 382dd612bbd1db8e131345981576b7a6c61c525d8a991fb8a3fc60678ce8d6ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d33022921907669f28eed522daec36e84552aabd41a411ff556923c0029cc72a003d30cd9cbeecba27e3d795fc279f71ca05a01cbc8c5cf6a0bd7256a478f72
|
7
|
+
data.tar.gz: 87c778a1972590cc6609a3c5a096fef6dde88c70ab663b5ebac820de89ec356359f802ea1d8ba38280378e877caabb1974e1d6a81c689eef92e6d7accf2f356a
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
## 5.2.4 (2021-12-15)
|
2
|
+
|
3
|
+
- Simplified queries for Active Record 7 and MySQL
|
4
|
+
|
5
|
+
## 5.2.3 (2021-12-06)
|
6
|
+
|
7
|
+
- Fixed error and warnings with Active Record 7
|
8
|
+
|
9
|
+
## 5.2.2 (2021-02-08)
|
10
|
+
|
11
|
+
- Added support for `nil..nil` ranges in `range` option
|
12
|
+
|
13
|
+
## 5.2.1 (2020-09-09)
|
14
|
+
|
15
|
+
- Improved error message for invalid ranges
|
16
|
+
- Fixed bug with date string ranges
|
17
|
+
|
1
18
|
## 5.2.0 (2020-09-07)
|
2
19
|
|
3
20
|
- Added warning for non-attribute argument
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -15,7 +15,7 @@ Supports PostgreSQL, MySQL, and Redshift, plus arrays and hashes (and limited su
|
|
15
15
|
|
16
16
|
:cupid: Goes hand in hand with [Chartkick](https://www.chartkick.com)
|
17
17
|
|
18
|
-
[![Build Status](https://
|
18
|
+
[![Build Status](https://github.com/ankane/groupdate/workflows/build/badge.svg?branch=master)](https://github.com/ankane/groupdate/actions)
|
19
19
|
|
20
20
|
## Installation
|
21
21
|
|
@@ -223,6 +223,14 @@ raise "Unpermitted column" unless ["column_a", "column_b"].include?(column)
|
|
223
223
|
User.group_by_day(column).count
|
224
224
|
```
|
225
225
|
|
226
|
+
### Default Scopes
|
227
|
+
|
228
|
+
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:
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
User.unscope(:order).group_by_day(:count).count
|
232
|
+
```
|
233
|
+
|
226
234
|
## Arrays and Hashes
|
227
235
|
|
228
236
|
```ruby
|
@@ -257,8 +265,6 @@ users.group_by_day { |u| u.created_at }.map { |k, v| [k, v.count] }.to_h
|
|
257
265
|
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
258
266
|
```
|
259
267
|
|
260
|
-
or copy and paste [these statements](https://gist.githubusercontent.com/ankane/1d6b0022173186accbf0/raw/time_zone_support.sql) into a SQL console.
|
261
|
-
|
262
268
|
You can confirm it worked with:
|
263
269
|
|
264
270
|
```sql
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Groupdate
|
2
|
+
module Adapters
|
3
|
+
class BaseAdapter
|
4
|
+
attr_reader :period, :column, :day_start, :week_start, :n_seconds
|
5
|
+
|
6
|
+
def initialize(relation, column:, period:, time_zone:, time_range:, week_start:, day_start:, n_seconds:)
|
7
|
+
# very important
|
8
|
+
column = validate_column(column)
|
9
|
+
|
10
|
+
@relation = relation
|
11
|
+
@column = resolve_column(relation, column)
|
12
|
+
@period = period
|
13
|
+
@time_zone = time_zone
|
14
|
+
@time_range = time_range
|
15
|
+
@week_start = week_start
|
16
|
+
@day_start = day_start
|
17
|
+
@n_seconds = n_seconds
|
18
|
+
|
19
|
+
if ActiveRecord::VERSION::MAJOR >= 7
|
20
|
+
if ActiveRecord.default_timezone == :local
|
21
|
+
raise Groupdate::Error, "ActiveRecord.default_timezone must be :utc to use Groupdate"
|
22
|
+
end
|
23
|
+
else
|
24
|
+
if relation.default_timezone == :local
|
25
|
+
raise Groupdate::Error, "ActiveRecord::Base.default_timezone must be :utc to use Groupdate"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def generate
|
31
|
+
@relation.group(group_clause).where(*where_clause)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def where_clause
|
37
|
+
if @time_range.is_a?(Range)
|
38
|
+
if @time_range.end
|
39
|
+
op = @time_range.exclude_end? ? "<" : "<="
|
40
|
+
if @time_range.begin
|
41
|
+
["#{column} >= ? AND #{column} #{op} ?", @time_range.begin, @time_range.end]
|
42
|
+
else
|
43
|
+
["#{column} #{op} ?", @time_range.end]
|
44
|
+
end
|
45
|
+
else
|
46
|
+
["#{column} >= ?", @time_range.begin]
|
47
|
+
end
|
48
|
+
else
|
49
|
+
["#{column} IS NOT NULL"]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# basic version of Active Record disallow_raw_sql!
|
54
|
+
# symbol = column (safe), Arel node = SQL (safe), other = untrusted
|
55
|
+
# matches table.column and column
|
56
|
+
def validate_column(column)
|
57
|
+
unless column.is_a?(Symbol) || column.is_a?(Arel::Nodes::SqlLiteral)
|
58
|
+
column = column.to_s
|
59
|
+
unless /\A\w+(\.\w+)?\z/i.match(column)
|
60
|
+
warn "[groupdate] Non-attribute argument: #{column}. Use Arel.sql() for known-safe values. This will raise an error in Groupdate 6"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
column
|
64
|
+
end
|
65
|
+
|
66
|
+
# resolves eagerly
|
67
|
+
# need to convert both where_clause (easy)
|
68
|
+
# and group_clause (not easy) if want to avoid this
|
69
|
+
def resolve_column(relation, column)
|
70
|
+
node = relation.send(:relation).send(:arel_columns, [column]).first
|
71
|
+
node = Arel::Nodes::SqlLiteral.new(node) if node.is_a?(String)
|
72
|
+
relation.connection.visitor.accept(node, Arel::Collectors::SQLString.new).value
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Groupdate
|
2
|
+
module Adapters
|
3
|
+
class MySQLAdapter < BaseAdapter
|
4
|
+
def group_clause
|
5
|
+
time_zone = @time_zone.tzinfo.name
|
6
|
+
day_start_column = "CONVERT_TZ(#{column}, '+00:00', ?) - INTERVAL ? second"
|
7
|
+
|
8
|
+
query =
|
9
|
+
case period
|
10
|
+
when :minute_of_hour
|
11
|
+
["MINUTE(#{day_start_column})", time_zone, day_start]
|
12
|
+
when :hour_of_day
|
13
|
+
["HOUR(#{day_start_column})", time_zone, day_start]
|
14
|
+
when :day_of_week
|
15
|
+
["DAYOFWEEK(#{day_start_column}) - 1", time_zone, day_start]
|
16
|
+
when :day_of_month
|
17
|
+
["DAYOFMONTH(#{day_start_column})", time_zone, day_start]
|
18
|
+
when :day_of_year
|
19
|
+
["DAYOFYEAR(#{day_start_column})", time_zone, day_start]
|
20
|
+
when :month_of_year
|
21
|
+
["MONTH(#{day_start_column})", time_zone, day_start]
|
22
|
+
when :week
|
23
|
+
["CONVERT_TZ(DATE_FORMAT(#{day_start_column} - INTERVAL ((? + DAYOFWEEK(#{day_start_column})) % 7) DAY, '%Y-%m-%d 00:00:00') + INTERVAL ? second, ?, '+00:00')", time_zone, day_start, 12 - week_start, time_zone, day_start, day_start, time_zone]
|
24
|
+
when :quarter
|
25
|
+
["CONVERT_TZ(DATE_FORMAT(DATE(CONCAT(YEAR(#{day_start_column}), '-', LPAD(1 + 3 * (QUARTER(#{day_start_column}) - 1), 2, '00'), '-01')), '%Y-%m-%d %H:%i:%S') + INTERVAL ? second, ?, '+00:00')", time_zone, day_start, time_zone, day_start, day_start, time_zone]
|
26
|
+
when :custom
|
27
|
+
["FROM_UNIXTIME((UNIX_TIMESTAMP(#{column}) DIV ?) * ?)", n_seconds, n_seconds]
|
28
|
+
else
|
29
|
+
format =
|
30
|
+
case period
|
31
|
+
when :second
|
32
|
+
"%Y-%m-%d %H:%i:%S"
|
33
|
+
when :minute
|
34
|
+
"%Y-%m-%d %H:%i:00"
|
35
|
+
when :hour
|
36
|
+
"%Y-%m-%d %H:00:00"
|
37
|
+
when :day
|
38
|
+
"%Y-%m-%d 00:00:00"
|
39
|
+
when :month
|
40
|
+
"%Y-%m-01 00:00:00"
|
41
|
+
else # year
|
42
|
+
"%Y-01-01 00:00:00"
|
43
|
+
end
|
44
|
+
|
45
|
+
["CONVERT_TZ(DATE_FORMAT(#{day_start_column}, ?) + INTERVAL ? second, ?, '+00:00')", time_zone, day_start, format, day_start, time_zone]
|
46
|
+
end
|
47
|
+
|
48
|
+
clean_group_clause(@relation.send(:sanitize_sql_array, query))
|
49
|
+
end
|
50
|
+
|
51
|
+
def clean_group_clause(clause)
|
52
|
+
# zero quoted in Active Record 7+
|
53
|
+
clause.gsub(/ (\-|\+) INTERVAL 0 second/, "").gsub(/ (\-|\+) INTERVAL '0' second/, "")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Groupdate
|
2
|
+
module Adapters
|
3
|
+
class PostgreSQLAdapter < BaseAdapter
|
4
|
+
def group_clause
|
5
|
+
time_zone = @time_zone.tzinfo.name
|
6
|
+
day_start_column = "#{column}::timestamptz AT TIME ZONE ? - INTERVAL ?"
|
7
|
+
day_start_interval = "#{day_start} second"
|
8
|
+
|
9
|
+
query =
|
10
|
+
case period
|
11
|
+
when :minute_of_hour
|
12
|
+
["EXTRACT(MINUTE FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
13
|
+
when :hour_of_day
|
14
|
+
["EXTRACT(HOUR FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
15
|
+
when :day_of_week
|
16
|
+
["EXTRACT(DOW FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
17
|
+
when :day_of_month
|
18
|
+
["EXTRACT(DAY FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
19
|
+
when :day_of_year
|
20
|
+
["EXTRACT(DOY FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
21
|
+
when :month_of_year
|
22
|
+
["EXTRACT(MONTH FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
23
|
+
when :week
|
24
|
+
["(DATE_TRUNC('day', #{day_start_column} - INTERVAL '1 day' * ((? + EXTRACT(DOW FROM #{day_start_column})::integer) % 7)) + INTERVAL ?) AT TIME ZONE ?", time_zone, day_start_interval, 13 - week_start, time_zone, day_start_interval, day_start_interval, time_zone]
|
25
|
+
when :custom
|
26
|
+
["TO_TIMESTAMP(FLOOR(EXTRACT(EPOCH FROM #{column}::timestamptz) / ?) * ?)", n_seconds, n_seconds]
|
27
|
+
else
|
28
|
+
if day_start == 0
|
29
|
+
# prettier
|
30
|
+
["DATE_TRUNC(?, #{day_start_column}) AT TIME ZONE ?", period, time_zone, day_start_interval, time_zone]
|
31
|
+
else
|
32
|
+
["(DATE_TRUNC(?, #{day_start_column}) + INTERVAL ?) AT TIME ZONE ?", period, time_zone, day_start_interval, day_start_interval, time_zone]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
clean_group_clause(@relation.send(:sanitize_sql_array, query))
|
37
|
+
end
|
38
|
+
|
39
|
+
def clean_group_clause(clause)
|
40
|
+
clause.gsub(/ (\-|\+) INTERVAL '0 second'/, "")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Groupdate
|
2
|
+
module Adapters
|
3
|
+
class RedshiftAdapter < BaseAdapter
|
4
|
+
def group_clause
|
5
|
+
time_zone = @time_zone.tzinfo.name
|
6
|
+
day_start_column = "CONVERT_TIMEZONE(?, #{column}::timestamp) - INTERVAL ?"
|
7
|
+
day_start_interval = "#{day_start} second"
|
8
|
+
|
9
|
+
query =
|
10
|
+
case period
|
11
|
+
when :minute_of_hour
|
12
|
+
["EXTRACT(MINUTE from #{day_start_column})::integer", time_zone, day_start_interval]
|
13
|
+
when :hour_of_day
|
14
|
+
["EXTRACT(HOUR from #{day_start_column})::integer", time_zone, day_start_interval]
|
15
|
+
when :day_of_week
|
16
|
+
["EXTRACT(DOW from #{day_start_column})::integer", time_zone, day_start_interval]
|
17
|
+
when :day_of_month
|
18
|
+
["EXTRACT(DAY from #{day_start_column})::integer", time_zone, day_start_interval]
|
19
|
+
when :day_of_year
|
20
|
+
["EXTRACT(DOY from #{day_start_column})::integer", time_zone, day_start_interval]
|
21
|
+
when :month_of_year
|
22
|
+
["EXTRACT(MONTH from #{day_start_column})::integer", time_zone, day_start_interval]
|
23
|
+
when :week # start on Sunday, not Redshift default Monday
|
24
|
+
# Redshift does not return timezone information; it
|
25
|
+
# always says it is in UTC time, so we must convert
|
26
|
+
# back to UTC to play properly with the rest of Groupdate.
|
27
|
+
week_start_interval = "#{week_start} day"
|
28
|
+
["CONVERT_TIMEZONE(?, 'Etc/UTC', DATE_TRUNC('week', #{day_start_column} - INTERVAL ?) + INTERVAL ? + INTERVAL ?)::timestamp", time_zone, time_zone, day_start_interval, week_start_interval, week_start_interval, day_start_interval]
|
29
|
+
when :custom
|
30
|
+
raise Groupdate::Error, "Not implemented yet"
|
31
|
+
else
|
32
|
+
["CONVERT_TIMEZONE(?, 'Etc/UTC', DATE_TRUNC(?, #{day_start_column}) + INTERVAL ?)::timestamp", time_zone, period, time_zone, day_start_interval, day_start_interval]
|
33
|
+
end
|
34
|
+
|
35
|
+
@relation.send(:sanitize_sql_array, query)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Groupdate
|
2
|
+
module Adapters
|
3
|
+
class SQLiteAdapter < BaseAdapter
|
4
|
+
def group_clause
|
5
|
+
raise Groupdate::Error, "Time zones not supported for SQLite" unless @time_zone.utc_offset.zero?
|
6
|
+
raise Groupdate::Error, "day_start not supported for SQLite" unless day_start.zero?
|
7
|
+
|
8
|
+
query =
|
9
|
+
if period == :week
|
10
|
+
["strftime('%Y-%m-%d 00:00:00 UTC', #{column}, '-6 days', ?)", "weekday #{(week_start + 1) % 7}"]
|
11
|
+
elsif period == :custom
|
12
|
+
["datetime((strftime('%s', #{column}) / ?) * ?, 'unixepoch')", n_seconds, n_seconds]
|
13
|
+
else
|
14
|
+
format =
|
15
|
+
case period
|
16
|
+
when :minute_of_hour
|
17
|
+
"%M"
|
18
|
+
when :hour_of_day
|
19
|
+
"%H"
|
20
|
+
when :day_of_week
|
21
|
+
"%w"
|
22
|
+
when :day_of_month
|
23
|
+
"%d"
|
24
|
+
when :day_of_year
|
25
|
+
"%j"
|
26
|
+
when :month_of_year
|
27
|
+
"%m"
|
28
|
+
when :second
|
29
|
+
"%Y-%m-%d %H:%M:%S UTC"
|
30
|
+
when :minute
|
31
|
+
"%Y-%m-%d %H:%M:00 UTC"
|
32
|
+
when :hour
|
33
|
+
"%Y-%m-%d %H:00:00 UTC"
|
34
|
+
when :day
|
35
|
+
"%Y-%m-%d 00:00:00 UTC"
|
36
|
+
when :month
|
37
|
+
"%Y-%m-01 00:00:00 UTC"
|
38
|
+
when :quarter
|
39
|
+
raise Groupdate::Error, "Quarter not supported for SQLite"
|
40
|
+
else # year
|
41
|
+
"%Y-01-01 00:00:00 UTC"
|
42
|
+
end
|
43
|
+
|
44
|
+
["strftime(?, #{column})", format]
|
45
|
+
end
|
46
|
+
|
47
|
+
@relation.send(:sanitize_sql_array, query)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/groupdate/magic.rb
CHANGED
@@ -69,12 +69,25 @@ module Groupdate
|
|
69
69
|
@day_start ||= ((options[:day_start] || Groupdate.day_start).to_f * 3600).round
|
70
70
|
end
|
71
71
|
|
72
|
+
def range
|
73
|
+
@range ||= begin
|
74
|
+
time_range = options[:range]
|
75
|
+
|
76
|
+
if time_range.is_a?(Range) && time_range.begin.nil? && time_range.end.nil?
|
77
|
+
nil
|
78
|
+
else
|
79
|
+
time_range
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
72
84
|
def series_builder
|
73
85
|
@series_builder ||=
|
74
86
|
SeriesBuilder.new(
|
75
87
|
**options,
|
76
88
|
period: period,
|
77
89
|
time_zone: time_zone,
|
90
|
+
range: range,
|
78
91
|
day_start: day_start,
|
79
92
|
week_start: week_start,
|
80
93
|
n_seconds: n_seconds
|
@@ -176,9 +189,13 @@ module Groupdate
|
|
176
189
|
def self.generate_relation(relation, field:, **options)
|
177
190
|
magic = Groupdate::Magic::Relation.new(**options)
|
178
191
|
|
192
|
+
adapter_name = relation.connection.adapter_name
|
193
|
+
adapter = Groupdate.adapters[adapter_name]
|
194
|
+
raise Groupdate::Error, "Connection adapter not supported: #{adapter_name}" unless adapter
|
195
|
+
|
179
196
|
# generate ActiveRecord relation
|
180
197
|
relation =
|
181
|
-
|
198
|
+
adapter.new(
|
182
199
|
relation,
|
183
200
|
column: field,
|
184
201
|
period: magic.period,
|
@@ -141,16 +141,35 @@ module Groupdate
|
|
141
141
|
def time_range
|
142
142
|
@time_range ||= begin
|
143
143
|
time_range = options[:range]
|
144
|
-
|
145
|
-
if time_range.is_a?(Range)
|
146
|
-
|
147
|
-
|
144
|
+
|
145
|
+
if time_range.is_a?(Range)
|
146
|
+
# check types
|
147
|
+
[time_range.begin, time_range.end].each do |v|
|
148
|
+
case v
|
149
|
+
when nil, Date, Time
|
150
|
+
# good
|
151
|
+
when String
|
152
|
+
# TODO raise error in Groupdate 6
|
153
|
+
warn "[groupdate] Range bounds should be Date or Time, not #{v.class.name}. This will raise an error in Groupdate 6"
|
154
|
+
break
|
155
|
+
else
|
156
|
+
raise ArgumentError, "Range bounds should be Date or Time, not #{v.class.name}"
|
157
|
+
end
|
148
158
|
end
|
149
|
-
|
150
|
-
|
151
|
-
|
159
|
+
|
160
|
+
start = time_range.begin
|
161
|
+
start = start.in_time_zone(time_zone) if start
|
162
|
+
|
163
|
+
exclude_end = time_range.exclude_end?
|
164
|
+
|
165
|
+
finish = time_range.end
|
166
|
+
finish = finish.in_time_zone(time_zone) if finish
|
167
|
+
if time_range.end.is_a?(Date) && !exclude_end
|
168
|
+
finish += 1.day
|
169
|
+
exclude_end = true
|
152
170
|
end
|
153
|
-
|
171
|
+
|
172
|
+
time_range = Range.new(start, finish, exclude_end)
|
154
173
|
elsif !time_range && options[:last]
|
155
174
|
if period == :quarter
|
156
175
|
step = 3.months
|
data/lib/groupdate/version.rb
CHANGED
data/lib/groupdate.rb
CHANGED
@@ -1,13 +1,20 @@
|
|
1
1
|
# dependencies
|
2
|
+
require "active_support"
|
2
3
|
require "active_support/core_ext/module/attribute_accessors"
|
3
4
|
require "active_support/time"
|
4
5
|
|
5
6
|
# modules
|
6
7
|
require "groupdate/magic"
|
7
|
-
require "groupdate/relation_builder"
|
8
8
|
require "groupdate/series_builder"
|
9
9
|
require "groupdate/version"
|
10
10
|
|
11
|
+
# adapters
|
12
|
+
require "groupdate/adapters/base_adapter"
|
13
|
+
require "groupdate/adapters/mysql_adapter"
|
14
|
+
require "groupdate/adapters/postgresql_adapter"
|
15
|
+
require "groupdate/adapters/redshift_adapter"
|
16
|
+
require "groupdate/adapters/sqlite_adapter"
|
17
|
+
|
11
18
|
module Groupdate
|
12
19
|
class Error < RuntimeError; end
|
13
20
|
|
@@ -26,8 +33,23 @@ module Groupdate
|
|
26
33
|
end
|
27
34
|
result
|
28
35
|
end
|
36
|
+
|
37
|
+
def self.adapters
|
38
|
+
@adapters ||= {}
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.register_adapter(name, adapter)
|
42
|
+
Array(name).each do |n|
|
43
|
+
adapters[n] = adapter
|
44
|
+
end
|
45
|
+
end
|
29
46
|
end
|
30
47
|
|
48
|
+
Groupdate.register_adapter ["Mysql2", "Mysql2Spatial", "Mysql2Rgeo"], Groupdate::Adapters::MySQLAdapter
|
49
|
+
Groupdate.register_adapter ["PostgreSQL", "PostGIS"], Groupdate::Adapters::PostgreSQLAdapter
|
50
|
+
Groupdate.register_adapter "Redshift", Groupdate::Adapters::RedshiftAdapter
|
51
|
+
Groupdate.register_adapter "SQLite", Groupdate::Adapters::SQLiteAdapter
|
52
|
+
|
31
53
|
require "groupdate/enumerable"
|
32
54
|
|
33
55
|
ActiveSupport.on_load(:active_record) do
|
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: 5.2.
|
4
|
+
version: 5.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,106 +24,8 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '5'
|
27
|
-
|
28
|
-
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rake
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: minitest
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: activerecord
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: pg
|
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
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: mysql2
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: sqlite3
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
114
|
-
requirements:
|
115
|
-
- - ">="
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
requirements:
|
122
|
-
- - ">="
|
123
|
-
- !ruby/object:Gem::Version
|
124
|
-
version: '0'
|
125
|
-
description:
|
126
|
-
email: andrew@chartkick.com
|
27
|
+
description:
|
28
|
+
email: andrew@ankane.org
|
127
29
|
executables: []
|
128
30
|
extensions: []
|
129
31
|
extra_rdoc_files: []
|
@@ -134,18 +36,22 @@ files:
|
|
134
36
|
- README.md
|
135
37
|
- lib/groupdate.rb
|
136
38
|
- lib/groupdate/active_record.rb
|
39
|
+
- lib/groupdate/adapters/base_adapter.rb
|
40
|
+
- lib/groupdate/adapters/mysql_adapter.rb
|
41
|
+
- lib/groupdate/adapters/postgresql_adapter.rb
|
42
|
+
- lib/groupdate/adapters/redshift_adapter.rb
|
43
|
+
- lib/groupdate/adapters/sqlite_adapter.rb
|
137
44
|
- lib/groupdate/enumerable.rb
|
138
45
|
- lib/groupdate/magic.rb
|
139
46
|
- lib/groupdate/query_methods.rb
|
140
47
|
- lib/groupdate/relation.rb
|
141
|
-
- lib/groupdate/relation_builder.rb
|
142
48
|
- lib/groupdate/series_builder.rb
|
143
49
|
- lib/groupdate/version.rb
|
144
50
|
homepage: https://github.com/ankane/groupdate
|
145
51
|
licenses:
|
146
52
|
- MIT
|
147
53
|
metadata: {}
|
148
|
-
post_install_message:
|
54
|
+
post_install_message:
|
149
55
|
rdoc_options: []
|
150
56
|
require_paths:
|
151
57
|
- lib
|
@@ -160,8 +66,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
66
|
- !ruby/object:Gem::Version
|
161
67
|
version: '0'
|
162
68
|
requirements: []
|
163
|
-
rubygems_version: 3.
|
164
|
-
signing_key:
|
69
|
+
rubygems_version: 3.2.32
|
70
|
+
signing_key:
|
165
71
|
specification_version: 4
|
166
72
|
summary: The simplest way to group temporal data
|
167
73
|
test_files: []
|
@@ -1,227 +0,0 @@
|
|
1
|
-
module Groupdate
|
2
|
-
class RelationBuilder
|
3
|
-
attr_reader :period, :column, :day_start, :week_start, :n_seconds
|
4
|
-
|
5
|
-
def initialize(relation, column:, period:, time_zone:, time_range:, week_start:, day_start:, n_seconds:)
|
6
|
-
# very important
|
7
|
-
validate_column(column)
|
8
|
-
|
9
|
-
@relation = relation
|
10
|
-
@column = resolve_column(relation, column)
|
11
|
-
@period = period
|
12
|
-
@time_zone = time_zone
|
13
|
-
@time_range = time_range
|
14
|
-
@week_start = week_start
|
15
|
-
@day_start = day_start
|
16
|
-
@n_seconds = n_seconds
|
17
|
-
|
18
|
-
if relation.default_timezone == :local
|
19
|
-
raise Groupdate::Error, "ActiveRecord::Base.default_timezone must be :utc to use Groupdate"
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def generate
|
24
|
-
@relation.group(group_clause).where(*where_clause)
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def group_clause
|
30
|
-
time_zone = @time_zone.tzinfo.name
|
31
|
-
adapter_name = @relation.connection.adapter_name
|
32
|
-
query =
|
33
|
-
case adapter_name
|
34
|
-
when "Mysql2", "Mysql2Spatial", "Mysql2Rgeo"
|
35
|
-
day_start_column = "CONVERT_TZ(#{column}, '+00:00', ?) - INTERVAL ? second"
|
36
|
-
|
37
|
-
case period
|
38
|
-
when :minute_of_hour
|
39
|
-
["MINUTE(#{day_start_column})", time_zone, day_start]
|
40
|
-
when :hour_of_day
|
41
|
-
["HOUR(#{day_start_column})", time_zone, day_start]
|
42
|
-
when :day_of_week
|
43
|
-
["DAYOFWEEK(#{day_start_column}) - 1", time_zone, day_start]
|
44
|
-
when :day_of_month
|
45
|
-
["DAYOFMONTH(#{day_start_column})", time_zone, day_start]
|
46
|
-
when :day_of_year
|
47
|
-
["DAYOFYEAR(#{day_start_column})", time_zone, day_start]
|
48
|
-
when :month_of_year
|
49
|
-
["MONTH(#{day_start_column})", time_zone, day_start]
|
50
|
-
when :week
|
51
|
-
["CONVERT_TZ(DATE_FORMAT(#{day_start_column} - INTERVAL ((? + DAYOFWEEK(#{day_start_column})) % 7) DAY, '%Y-%m-%d 00:00:00') + INTERVAL ? second, ?, '+00:00')", time_zone, day_start, 12 - week_start, time_zone, day_start, day_start, time_zone]
|
52
|
-
when :quarter
|
53
|
-
["CONVERT_TZ(DATE_FORMAT(DATE(CONCAT(YEAR(#{day_start_column}), '-', LPAD(1 + 3 * (QUARTER(#{day_start_column}) - 1), 2, '00'), '-01')), '%Y-%m-%d %H:%i:%S') + INTERVAL ? second, ?, '+00:00')", time_zone, day_start, time_zone, day_start, day_start, time_zone]
|
54
|
-
when :custom
|
55
|
-
["FROM_UNIXTIME((UNIX_TIMESTAMP(#{column}) DIV ?) * ?)", n_seconds, n_seconds]
|
56
|
-
else
|
57
|
-
format =
|
58
|
-
case period
|
59
|
-
when :second
|
60
|
-
"%Y-%m-%d %H:%i:%S"
|
61
|
-
when :minute
|
62
|
-
"%Y-%m-%d %H:%i:00"
|
63
|
-
when :hour
|
64
|
-
"%Y-%m-%d %H:00:00"
|
65
|
-
when :day
|
66
|
-
"%Y-%m-%d 00:00:00"
|
67
|
-
when :month
|
68
|
-
"%Y-%m-01 00:00:00"
|
69
|
-
else # year
|
70
|
-
"%Y-01-01 00:00:00"
|
71
|
-
end
|
72
|
-
|
73
|
-
["CONVERT_TZ(DATE_FORMAT(#{day_start_column}, ?) + INTERVAL ? second, ?, '+00:00')", time_zone, day_start, format, day_start, time_zone]
|
74
|
-
end
|
75
|
-
when "PostgreSQL", "PostGIS"
|
76
|
-
day_start_column = "#{column}::timestamptz AT TIME ZONE ? - INTERVAL ?"
|
77
|
-
day_start_interval = "#{day_start} second"
|
78
|
-
|
79
|
-
case period
|
80
|
-
when :minute_of_hour
|
81
|
-
["EXTRACT(MINUTE FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
82
|
-
when :hour_of_day
|
83
|
-
["EXTRACT(HOUR FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
84
|
-
when :day_of_week
|
85
|
-
["EXTRACT(DOW FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
86
|
-
when :day_of_month
|
87
|
-
["EXTRACT(DAY FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
88
|
-
when :day_of_year
|
89
|
-
["EXTRACT(DOY FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
90
|
-
when :month_of_year
|
91
|
-
["EXTRACT(MONTH FROM #{day_start_column})::integer", time_zone, day_start_interval]
|
92
|
-
when :week
|
93
|
-
["(DATE_TRUNC('day', #{day_start_column} - INTERVAL '1 day' * ((? + EXTRACT(DOW FROM #{day_start_column})::integer) % 7)) + INTERVAL ?) AT TIME ZONE ?", time_zone, day_start_interval, 13 - week_start, time_zone, day_start_interval, day_start_interval, time_zone]
|
94
|
-
when :custom
|
95
|
-
["TO_TIMESTAMP(FLOOR(EXTRACT(EPOCH FROM #{column}::timestamptz) / ?) * ?)", n_seconds, n_seconds]
|
96
|
-
else
|
97
|
-
if day_start == 0
|
98
|
-
# prettier
|
99
|
-
["DATE_TRUNC(?, #{day_start_column}) AT TIME ZONE ?", period, time_zone, day_start_interval, time_zone]
|
100
|
-
else
|
101
|
-
["(DATE_TRUNC(?, #{day_start_column}) + INTERVAL ?) AT TIME ZONE ?", period, time_zone, day_start_interval, day_start_interval, time_zone]
|
102
|
-
end
|
103
|
-
end
|
104
|
-
when "SQLite"
|
105
|
-
raise Groupdate::Error, "Time zones not supported for SQLite" unless @time_zone.utc_offset.zero?
|
106
|
-
raise Groupdate::Error, "day_start not supported for SQLite" unless day_start.zero?
|
107
|
-
|
108
|
-
if period == :week
|
109
|
-
["strftime('%Y-%m-%d 00:00:00 UTC', #{column}, '-6 days', ?)", "weekday #{(week_start + 1) % 7}"]
|
110
|
-
elsif period == :custom
|
111
|
-
["datetime((strftime('%s', #{column}) / ?) * ?, 'unixepoch')", n_seconds, n_seconds]
|
112
|
-
else
|
113
|
-
format =
|
114
|
-
case period
|
115
|
-
when :minute_of_hour
|
116
|
-
"%M"
|
117
|
-
when :hour_of_day
|
118
|
-
"%H"
|
119
|
-
when :day_of_week
|
120
|
-
"%w"
|
121
|
-
when :day_of_month
|
122
|
-
"%d"
|
123
|
-
when :day_of_year
|
124
|
-
"%j"
|
125
|
-
when :month_of_year
|
126
|
-
"%m"
|
127
|
-
when :second
|
128
|
-
"%Y-%m-%d %H:%M:%S UTC"
|
129
|
-
when :minute
|
130
|
-
"%Y-%m-%d %H:%M:00 UTC"
|
131
|
-
when :hour
|
132
|
-
"%Y-%m-%d %H:00:00 UTC"
|
133
|
-
when :day
|
134
|
-
"%Y-%m-%d 00:00:00 UTC"
|
135
|
-
when :month
|
136
|
-
"%Y-%m-01 00:00:00 UTC"
|
137
|
-
when :quarter
|
138
|
-
raise Groupdate::Error, "Quarter not supported for SQLite"
|
139
|
-
else # year
|
140
|
-
"%Y-01-01 00:00:00 UTC"
|
141
|
-
end
|
142
|
-
|
143
|
-
["strftime(?, #{column})", format]
|
144
|
-
end
|
145
|
-
when "Redshift"
|
146
|
-
day_start_column = "CONVERT_TIMEZONE(?, #{column}::timestamp) - INTERVAL ?"
|
147
|
-
day_start_interval = "#{day_start} second"
|
148
|
-
|
149
|
-
case period
|
150
|
-
when :minute_of_hour
|
151
|
-
["EXTRACT(MINUTE from #{day_start_column})::integer", time_zone, day_start_interval]
|
152
|
-
when :hour_of_day
|
153
|
-
["EXTRACT(HOUR from #{day_start_column})::integer", time_zone, day_start_interval]
|
154
|
-
when :day_of_week
|
155
|
-
["EXTRACT(DOW from #{day_start_column})::integer", time_zone, day_start_interval]
|
156
|
-
when :day_of_month
|
157
|
-
["EXTRACT(DAY from #{day_start_column})::integer", time_zone, day_start_interval]
|
158
|
-
when :day_of_year
|
159
|
-
["EXTRACT(DOY from #{day_start_column})::integer", time_zone, day_start_interval]
|
160
|
-
when :month_of_year
|
161
|
-
["EXTRACT(MONTH from #{day_start_column})::integer", time_zone, day_start_interval]
|
162
|
-
when :week # start on Sunday, not Redshift default Monday
|
163
|
-
# Redshift does not return timezone information; it
|
164
|
-
# always says it is in UTC time, so we must convert
|
165
|
-
# back to UTC to play properly with the rest of Groupdate.
|
166
|
-
week_start_interval = "#{week_start} day"
|
167
|
-
["CONVERT_TIMEZONE(?, 'Etc/UTC', DATE_TRUNC('week', #{day_start_column} - INTERVAL ?) + INTERVAL ? + INTERVAL ?)::timestamp", time_zone, time_zone, day_start_interval, week_start_interval, week_start_interval, day_start_interval]
|
168
|
-
when :custom
|
169
|
-
raise Groupdate::Error, "Not implemented yet"
|
170
|
-
else
|
171
|
-
["CONVERT_TIMEZONE(?, 'Etc/UTC', DATE_TRUNC(?, #{day_start_column}) + INTERVAL ?)::timestamp", time_zone, period, time_zone, day_start_interval, day_start_interval]
|
172
|
-
end
|
173
|
-
else
|
174
|
-
raise Groupdate::Error, "Connection adapter not supported: #{adapter_name}"
|
175
|
-
end
|
176
|
-
|
177
|
-
clause = @relation.send(:sanitize_sql_array, query)
|
178
|
-
|
179
|
-
# cleaner queries in logs
|
180
|
-
clause = clean_group_clause_postgresql(clause)
|
181
|
-
clean_group_clause_mysql(clause)
|
182
|
-
end
|
183
|
-
|
184
|
-
def clean_group_clause_postgresql(clause)
|
185
|
-
clause.gsub(/ (\-|\+) INTERVAL '0 second'/, "")
|
186
|
-
end
|
187
|
-
|
188
|
-
def clean_group_clause_mysql(clause)
|
189
|
-
clause.gsub(/ (\-|\+) INTERVAL 0 second/, "")
|
190
|
-
end
|
191
|
-
|
192
|
-
def where_clause
|
193
|
-
if @time_range.is_a?(Range)
|
194
|
-
if @time_range.end
|
195
|
-
op = @time_range.exclude_end? ? "<" : "<="
|
196
|
-
if @time_range.begin
|
197
|
-
["#{column} >= ? AND #{column} #{op} ?", @time_range.first, @time_range.last]
|
198
|
-
else
|
199
|
-
["#{column} #{op} ?", @time_range.last]
|
200
|
-
end
|
201
|
-
else
|
202
|
-
["#{column} >= ?", @time_range.first]
|
203
|
-
end
|
204
|
-
else
|
205
|
-
["#{column} IS NOT NULL"]
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
# basic version of Active Record disallow_raw_sql!
|
210
|
-
# symbol = column (safe), Arel node = SQL (safe), other = untrusted
|
211
|
-
def validate_column(column)
|
212
|
-
# matches table.column and column
|
213
|
-
unless column.is_a?(Symbol) || column.is_a?(Arel::Nodes::SqlLiteral) || /\A\w+(\.\w+)?\z/i.match(column.to_s)
|
214
|
-
warn "[groupdate] Non-attribute argument: #{column}. Use Arel.sql() for known-safe values. This will raise an error in Groupdate 6"
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
# resolves eagerly
|
219
|
-
# need to convert both where_clause (easy)
|
220
|
-
# and group_clause (not easy) if want to avoid this
|
221
|
-
def resolve_column(relation, column)
|
222
|
-
node = relation.send(:relation).send(:arel_columns, [column]).first
|
223
|
-
node = Arel::Nodes::SqlLiteral.new(node) if node.is_a?(String)
|
224
|
-
relation.connection.visitor.accept(node, Arel::Collectors::SQLString.new).value
|
225
|
-
end
|
226
|
-
end
|
227
|
-
end
|