groupdate 5.2.1 → 6.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/LICENSE.txt +1 -1
- data/README.md +13 -18
- data/lib/groupdate/adapters/base_adapter.rb +51 -0
- data/lib/groupdate/adapters/mysql_adapter.rb +63 -0
- data/lib/groupdate/adapters/postgresql_adapter.rb +46 -0
- data/lib/groupdate/adapters/sqlite_adapter.rb +51 -0
- data/lib/groupdate/magic.rb +57 -3
- data/lib/groupdate/series_builder.rb +5 -52
- data/lib/groupdate/version.rb +1 -1
- data/lib/groupdate.rb +22 -3
- metadata +15 -110
- 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: f0169fac01ba6b7b7a051ae37d2cc3d5b89b137b7bb3d609df7a633b534ac89b
|
4
|
+
data.tar.gz: ccdf270ed3726d0c0a52e9fafe0fb3b2281f6b31ac0a9a731eaedfbda8f42067
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa3150d5c5edd803e225045d745faf191fef78f483fec13aac05f4fb7dc0ee04a1b7a9340f1f8cf35a8d1c79ed09a6e6e6c4432e094d54faf5c7af48385c5e68
|
7
|
+
data.tar.gz: 97137d089ec745cdbcf08d1c7999f3efefab9c8cb3dd39e4e033f28184b8e4d4955cb4f7777cd7e8f8d28a3dda8b3dce2826242322de1949c2f72ad11f24a32d
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,24 @@
|
|
1
|
+
## 6.0.0 (2022-01-15)
|
2
|
+
|
3
|
+
- Changed SQL to return dates instead of times for day, week, month, quarter, and year
|
4
|
+
- Added `n` option for Redshift
|
5
|
+
- Removed `dates` option
|
6
|
+
- Raise `ActiveRecord::UnknownAttributeReference` for non-attribute arguments
|
7
|
+
- Raise `ArgumentError` for ranges with string bounds
|
8
|
+
- Dropped support for Ruby < 2.6 and Rails < 5.2
|
9
|
+
|
10
|
+
## 5.2.4 (2021-12-15)
|
11
|
+
|
12
|
+
- Simplified queries for Active Record 7 and MySQL
|
13
|
+
|
14
|
+
## 5.2.3 (2021-12-06)
|
15
|
+
|
16
|
+
- Fixed error and warnings with Active Record 7
|
17
|
+
|
18
|
+
## 5.2.2 (2021-02-08)
|
19
|
+
|
20
|
+
- Added support for `nil..nil` ranges in `range` option
|
21
|
+
|
1
22
|
## 5.2.1 (2020-09-09)
|
2
23
|
|
3
24
|
- Improved error message for invalid ranges
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -15,14 +15,14 @@ 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
|
|
22
22
|
Add this line to your application’s Gemfile:
|
23
23
|
|
24
24
|
```ruby
|
25
|
-
gem
|
25
|
+
gem "groupdate"
|
26
26
|
```
|
27
27
|
|
28
28
|
For MySQL and SQLite, also follow [these instructions](#additional-instructions).
|
@@ -210,17 +210,12 @@ If grouping on date columns which don’t need time zone conversion, use:
|
|
210
210
|
User.group_by_week(:created_on, time_zone: false).count
|
211
211
|
```
|
212
212
|
|
213
|
-
###
|
213
|
+
### Default Scopes
|
214
214
|
|
215
|
-
If
|
215
|
+
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:
|
216
216
|
|
217
217
|
```ruby
|
218
|
-
|
219
|
-
|
220
|
-
# check against permitted columns
|
221
|
-
raise "Unpermitted column" unless ["column_a", "column_b"].include?(column)
|
222
|
-
|
223
|
-
User.group_by_day(column).count
|
218
|
+
User.unscope(:order).group_by_day(:count).count
|
224
219
|
```
|
225
220
|
|
226
221
|
## Arrays and Hashes
|
@@ -244,7 +239,7 @@ users.group_by_day(series: true) { |u| u.created_at }
|
|
244
239
|
Count
|
245
240
|
|
246
241
|
```ruby
|
247
|
-
users.group_by_day { |u| u.created_at }.
|
242
|
+
users.group_by_day { |u| u.created_at }.to_h { |k, v| [k, v.count] }
|
248
243
|
```
|
249
244
|
|
250
245
|
## Additional Instructions
|
@@ -257,8 +252,6 @@ users.group_by_day { |u| u.created_at }.map { |k, v| [k, v.count] }.to_h
|
|
257
252
|
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
258
253
|
```
|
259
254
|
|
260
|
-
or copy and paste [these statements](https://gist.githubusercontent.com/ankane/1d6b0022173186accbf0/raw/time_zone_support.sql) into a SQL console.
|
261
|
-
|
262
255
|
You can confirm it worked with:
|
263
256
|
|
264
257
|
```sql
|
@@ -283,13 +276,15 @@ Groupdate.time_zone = false
|
|
283
276
|
|
284
277
|
## Upgrading
|
285
278
|
|
286
|
-
###
|
279
|
+
### 6.0
|
287
280
|
|
288
|
-
Groupdate
|
281
|
+
Groupdate 6.0 protects against unsafe input by default. For non-attribute arguments, use:
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
User.group_by_day(Arel.sql(known_safe_value)).count
|
285
|
+
```
|
289
286
|
|
290
|
-
|
291
|
-
- The `day_start` option is now consistent between Active Record and enumerable
|
292
|
-
- Deprecated positional arguments for time zone and range have been removed
|
287
|
+
Also, the `dates` option has been removed.
|
293
288
|
|
294
289
|
## History
|
295
290
|
|
@@ -0,0 +1,51 @@
|
|
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
|
+
@relation = relation
|
8
|
+
@column = column
|
9
|
+
@period = period
|
10
|
+
@time_zone = time_zone
|
11
|
+
@time_range = time_range
|
12
|
+
@week_start = week_start
|
13
|
+
@day_start = day_start
|
14
|
+
@n_seconds = n_seconds
|
15
|
+
|
16
|
+
if ActiveRecord::VERSION::MAJOR >= 7
|
17
|
+
if ActiveRecord.default_timezone == :local
|
18
|
+
raise Groupdate::Error, "ActiveRecord.default_timezone must be :utc to use Groupdate"
|
19
|
+
end
|
20
|
+
else
|
21
|
+
if relation.default_timezone == :local
|
22
|
+
raise Groupdate::Error, "ActiveRecord::Base.default_timezone must be :utc to use Groupdate"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def generate
|
28
|
+
@relation.group(group_clause).where(*where_clause)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def where_clause
|
34
|
+
if @time_range.is_a?(Range)
|
35
|
+
if @time_range.end
|
36
|
+
op = @time_range.exclude_end? ? "<" : "<="
|
37
|
+
if @time_range.begin
|
38
|
+
["#{column} >= ? AND #{column} #{op} ?", @time_range.begin, @time_range.end]
|
39
|
+
else
|
40
|
+
["#{column} #{op} ?", @time_range.end]
|
41
|
+
end
|
42
|
+
else
|
43
|
+
["#{column} >= ?", @time_range.begin]
|
44
|
+
end
|
45
|
+
else
|
46
|
+
["#{column} IS NOT NULL"]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,63 @@
|
|
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
|
+
["CAST(DATE_FORMAT(#{day_start_column} - INTERVAL ((? + DAYOFWEEK(#{day_start_column})) % 7) DAY, '%Y-%m-%d') AS DATE)", time_zone, day_start, 12 - week_start, time_zone, day_start]
|
24
|
+
when :quarter
|
25
|
+
["CAST(CONCAT(YEAR(#{day_start_column}), '-', LPAD(1 + 3 * (QUARTER(#{day_start_column}) - 1), 2, '00'), '-01') AS DATE)", time_zone, day_start, time_zone, day_start]
|
26
|
+
when :day, :month, :year
|
27
|
+
format =
|
28
|
+
case period
|
29
|
+
when :day
|
30
|
+
"%Y-%m-%d"
|
31
|
+
when :month
|
32
|
+
"%Y-%m-01"
|
33
|
+
else # year
|
34
|
+
"%Y-01-01"
|
35
|
+
end
|
36
|
+
|
37
|
+
["CAST(DATE_FORMAT(#{day_start_column}, ?) AS DATE)", time_zone, day_start, format]
|
38
|
+
when :custom
|
39
|
+
["FROM_UNIXTIME((UNIX_TIMESTAMP(#{column}) DIV ?) * ?)", n_seconds, n_seconds]
|
40
|
+
else
|
41
|
+
format =
|
42
|
+
case period
|
43
|
+
when :second
|
44
|
+
"%Y-%m-%d %H:%i:%S"
|
45
|
+
when :minute
|
46
|
+
"%Y-%m-%d %H:%i:00"
|
47
|
+
else # hour
|
48
|
+
"%Y-%m-%d %H:00:00"
|
49
|
+
end
|
50
|
+
|
51
|
+
["CONVERT_TZ(DATE_FORMAT(#{day_start_column}, ?) + INTERVAL ? second, ?, '+00:00')", time_zone, day_start, format, day_start, time_zone]
|
52
|
+
end
|
53
|
+
|
54
|
+
clean_group_clause(@relation.send(:sanitize_sql_array, query))
|
55
|
+
end
|
56
|
+
|
57
|
+
def clean_group_clause(clause)
|
58
|
+
# zero quoted in Active Record 7+
|
59
|
+
clause.gsub(/ (\-|\+) INTERVAL 0 second/, "").gsub(/ (\-|\+) INTERVAL '0' second/, "")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,46 @@
|
|
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 ?)::date", time_zone, day_start_interval, 13 - week_start, time_zone, day_start_interval, day_start_interval]
|
25
|
+
when :custom
|
26
|
+
if @relation.connection.adapter_name == "Redshift"
|
27
|
+
["TIMESTAMP 'epoch' + (FLOOR(EXTRACT(EPOCH FROM #{column}::timestamp) / ?) * ?) * INTERVAL '1 second'", n_seconds, n_seconds]
|
28
|
+
else
|
29
|
+
["TO_TIMESTAMP(FLOOR(EXTRACT(EPOCH FROM #{column}::timestamptz) / ?) * ?)", n_seconds, n_seconds]
|
30
|
+
end
|
31
|
+
when :day, :month, :quarter, :year
|
32
|
+
["DATE_TRUNC(?, #{day_start_column})::date", period, time_zone, day_start_interval]
|
33
|
+
else
|
34
|
+
# day start is always 0 for seconds, minute, hour
|
35
|
+
["DATE_TRUNC(?, #{day_start_column}) AT TIME ZONE ?", period, time_zone, day_start_interval, time_zone]
|
36
|
+
end
|
37
|
+
|
38
|
+
clean_group_clause(@relation.send(:sanitize_sql_array, query))
|
39
|
+
end
|
40
|
+
|
41
|
+
def clean_group_clause(clause)
|
42
|
+
clause.gsub(/ (\-|\+) INTERVAL '0 second'/, "")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
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', #{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"
|
36
|
+
when :month
|
37
|
+
"%Y-%m-01"
|
38
|
+
when :quarter
|
39
|
+
raise Groupdate::Error, "Quarter not supported for SQLite"
|
40
|
+
else # year
|
41
|
+
"%Y-01-01"
|
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
@@ -22,7 +22,7 @@ module Groupdate
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def validate_keywords
|
25
|
-
known_keywords = [:time_zone, :
|
25
|
+
known_keywords = [:time_zone, :series, :format, :locale, :range, :reverse]
|
26
26
|
|
27
27
|
if %i[week day_of_week].include?(period)
|
28
28
|
known_keywords << :week_start
|
@@ -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
|
@@ -132,6 +145,15 @@ module Groupdate
|
|
132
145
|
lambda { |k| k.to_i }
|
133
146
|
when :day_of_week
|
134
147
|
lambda { |k| (k.to_i - 1 - week_start) % 7 }
|
148
|
+
when :day, :week, :month, :quarter, :year
|
149
|
+
if day_start != 0
|
150
|
+
day_start_hour = day_start / 3600
|
151
|
+
day_start_min = (day_start % 3600) / 60
|
152
|
+
day_start_sec = (day_start % 3600) % 60
|
153
|
+
lambda { |k| k.in_time_zone(time_zone).change(hour: day_start_hour, min: day_start_min, sec: day_start_sec) }
|
154
|
+
else
|
155
|
+
lambda { |k| k.in_time_zone(time_zone) }
|
156
|
+
end
|
135
157
|
else
|
136
158
|
utc = ActiveSupport::TimeZone["UTC"]
|
137
159
|
lambda { |k| (k.is_a?(String) || !k.respond_to?(:to_time) ? utc.parse(k.to_s) : k.to_time).in_time_zone(time_zone) }
|
@@ -176,11 +198,19 @@ module Groupdate
|
|
176
198
|
def self.generate_relation(relation, field:, **options)
|
177
199
|
magic = Groupdate::Magic::Relation.new(**options)
|
178
200
|
|
201
|
+
adapter_name = relation.connection.adapter_name
|
202
|
+
adapter = Groupdate.adapters[adapter_name]
|
203
|
+
raise Groupdate::Error, "Connection adapter not supported: #{adapter_name}" unless adapter
|
204
|
+
|
205
|
+
# very important
|
206
|
+
column = validate_column(field)
|
207
|
+
column = resolve_column(relation, column)
|
208
|
+
|
179
209
|
# generate ActiveRecord relation
|
180
210
|
relation =
|
181
|
-
|
211
|
+
adapter.new(
|
182
212
|
relation,
|
183
|
-
column:
|
213
|
+
column: column,
|
184
214
|
period: magic.period,
|
185
215
|
time_zone: magic.time_zone,
|
186
216
|
time_range: magic.time_range,
|
@@ -196,6 +226,30 @@ module Groupdate
|
|
196
226
|
relation
|
197
227
|
end
|
198
228
|
|
229
|
+
class << self
|
230
|
+
# basic version of Active Record disallow_raw_sql!
|
231
|
+
# symbol = column (safe), Arel node = SQL (safe), other = untrusted
|
232
|
+
# matches table.column and column
|
233
|
+
def validate_column(column)
|
234
|
+
unless column.is_a?(Symbol) || column.is_a?(Arel::Nodes::SqlLiteral)
|
235
|
+
column = column.to_s
|
236
|
+
unless /\A\w+(\.\w+)?\z/i.match(column)
|
237
|
+
raise ActiveRecord::UnknownAttributeReference, "Query method called with non-attribute argument(s): #{column.inspect}. Use Arel.sql() for known-safe values."
|
238
|
+
end
|
239
|
+
end
|
240
|
+
column
|
241
|
+
end
|
242
|
+
|
243
|
+
# resolves eagerly
|
244
|
+
# need to convert both where_clause (easy)
|
245
|
+
# and group_clause (not easy) if want to avoid this
|
246
|
+
def resolve_column(relation, column)
|
247
|
+
node = relation.send(:relation).send(:arel_columns, [column]).first
|
248
|
+
node = Arel::Nodes::SqlLiteral.new(node) if node.is_a?(String)
|
249
|
+
relation.connection.visitor.accept(node, Arel::Collectors::SQLString.new).value
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
199
253
|
# allow any options to keep flexible for future
|
200
254
|
def self.process_result(relation, result, **options)
|
201
255
|
relation.groupdate_values.reverse.each do |gv|
|
@@ -2,8 +2,6 @@ module Groupdate
|
|
2
2
|
class SeriesBuilder
|
3
3
|
attr_reader :period, :time_zone, :day_start, :week_start, :n_seconds, :options
|
4
4
|
|
5
|
-
CHECK_PERIODS = [:day, :week, :month, :quarter, :year]
|
6
|
-
|
7
5
|
def initialize(period:, time_zone:, day_start:, week_start:, n_seconds:, **options)
|
8
6
|
@period = period
|
9
7
|
@time_zone = time_zone
|
@@ -23,41 +21,12 @@ module Groupdate
|
|
23
21
|
verified_data[k] = data.delete(k)
|
24
22
|
end
|
25
23
|
|
26
|
-
# this is a fun one
|
27
|
-
# PostgreSQL and Ruby both return the 2nd hour when converting/parsing a backward DST change
|
28
|
-
# Other databases and Active Support return the 1st hour (as expected)
|
29
|
-
# Active Support good: ActiveSupport::TimeZone["America/Los_Angeles"].parse("2013-11-03 01:00:00")
|
30
|
-
# MySQL good: SELECT CONVERT_TZ('2013-11-03 01:00:00', 'America/Los_Angeles', 'Etc/UTC');
|
31
|
-
# Ruby not good: Time.parse("2013-11-03 01:00:00")
|
32
|
-
# PostgreSQL not good: SELECT '2013-11-03 01:00:00'::timestamp AT TIME ZONE 'America/Los_Angeles';
|
33
|
-
# we need to account for this here
|
34
|
-
if series_default && CHECK_PERIODS.include?(period)
|
35
|
-
data.each do |k, v|
|
36
|
-
key = multiple_groups ? k[group_index] : k
|
37
|
-
# TODO only do this for PostgreSQL
|
38
|
-
# this may mask some inconsistent time zone errors
|
39
|
-
# but not sure there's a better approach
|
40
|
-
if key.hour == (key - 1.hour).hour && series.include?(key - 1.hour)
|
41
|
-
key -= 1.hour
|
42
|
-
if multiple_groups
|
43
|
-
k[group_index] = key
|
44
|
-
else
|
45
|
-
k = key
|
46
|
-
end
|
47
|
-
verified_data[k] = v
|
48
|
-
elsif key != round_time(key)
|
49
|
-
# only need to show what database returned since it will cast in Ruby time zone
|
50
|
-
raise Groupdate::Error, "Database and Ruby have inconsistent time zone info. Database returned #{key}"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
24
|
unless entire_series?(series_default)
|
56
25
|
series = series.select { |k| verified_data[k] }
|
57
26
|
end
|
58
27
|
|
59
28
|
value = 0
|
60
|
-
result =
|
29
|
+
result = series.to_h do |k|
|
61
30
|
value = verified_data[k] || (@options[:carry_forward] && value) || default_value
|
62
31
|
key =
|
63
32
|
if multiple_groups
|
@@ -67,7 +36,7 @@ module Groupdate
|
|
67
36
|
end
|
68
37
|
|
69
38
|
[key, value]
|
70
|
-
end
|
39
|
+
end
|
71
40
|
|
72
41
|
result
|
73
42
|
end
|
@@ -81,7 +50,7 @@ module Groupdate
|
|
81
50
|
|
82
51
|
if day_start != 0
|
83
52
|
# apply day_start to a time object that's not affected by DST
|
84
|
-
time =
|
53
|
+
time = time.change(zone: utc)
|
85
54
|
time -= day_start.seconds
|
86
55
|
end
|
87
56
|
|
@@ -121,23 +90,12 @@ module Groupdate
|
|
121
90
|
|
122
91
|
if day_start != 0 && time.is_a?(Time)
|
123
92
|
time += day_start.seconds
|
124
|
-
time =
|
93
|
+
time = time.change(zone: time_zone)
|
125
94
|
end
|
126
95
|
|
127
96
|
time
|
128
97
|
end
|
129
98
|
|
130
|
-
def change_zone
|
131
|
-
@change_zone ||= begin
|
132
|
-
if ActiveSupport::VERSION::STRING >= "5.2"
|
133
|
-
->(time, zone) { time.change(zone: zone) }
|
134
|
-
else
|
135
|
-
# TODO make more efficient
|
136
|
-
->(time, zone) { zone.parse(time.strftime("%Y-%m-%d %H:%M:%S")) }
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
99
|
def time_range
|
142
100
|
@time_range ||= begin
|
143
101
|
time_range = options[:range]
|
@@ -148,10 +106,6 @@ module Groupdate
|
|
148
106
|
case v
|
149
107
|
when nil, Date, Time
|
150
108
|
# 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
109
|
else
|
156
110
|
raise ArgumentError, "Range bounds should be Date or Time, not #{v.class.name}"
|
157
111
|
end
|
@@ -289,7 +243,6 @@ module Groupdate
|
|
289
243
|
def key_format
|
290
244
|
@key_format ||= begin
|
291
245
|
locale = options[:locale] || I18n.locale
|
292
|
-
use_dates = options.key?(:dates) ? options[:dates] : Groupdate.dates
|
293
246
|
|
294
247
|
if options[:format]
|
295
248
|
if options[:format].respond_to?(:call)
|
@@ -312,7 +265,7 @@ module Groupdate
|
|
312
265
|
I18n.localize(key, format: options[:format], locale: locale)
|
313
266
|
end
|
314
267
|
end
|
315
|
-
elsif [:day, :week, :month, :quarter, :year].include?(period)
|
268
|
+
elsif [:day, :week, :month, :quarter, :year].include?(period)
|
316
269
|
lambda { |k| k.to_date }
|
317
270
|
else
|
318
271
|
lambda { |k| k }
|
data/lib/groupdate/version.rb
CHANGED
data/lib/groupdate.rb
CHANGED
@@ -1,23 +1,28 @@
|
|
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/sqlite_adapter"
|
16
|
+
|
11
17
|
module Groupdate
|
12
18
|
class Error < RuntimeError; end
|
13
19
|
|
14
20
|
PERIODS = [:second, :minute, :hour, :day, :week, :month, :quarter, :year, :day_of_week, :hour_of_day, :minute_of_hour, :day_of_month, :day_of_year, :month_of_year]
|
15
21
|
METHODS = PERIODS.map { |v| :"group_by_#{v}" } + [:group_by_period]
|
16
22
|
|
17
|
-
mattr_accessor :week_start, :day_start, :time_zone
|
23
|
+
mattr_accessor :week_start, :day_start, :time_zone
|
18
24
|
self.week_start = :sunday
|
19
25
|
self.day_start = 0
|
20
|
-
self.dates = true
|
21
26
|
|
22
27
|
# api for gems like ActiveMedian
|
23
28
|
def self.process_result(relation, result, **options)
|
@@ -26,8 +31,22 @@ module Groupdate
|
|
26
31
|
end
|
27
32
|
result
|
28
33
|
end
|
34
|
+
|
35
|
+
def self.adapters
|
36
|
+
@adapters ||= {}
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.register_adapter(name, adapter)
|
40
|
+
Array(name).each do |n|
|
41
|
+
adapters[n] = adapter
|
42
|
+
end
|
43
|
+
end
|
29
44
|
end
|
30
45
|
|
46
|
+
Groupdate.register_adapter ["Mysql2", "Mysql2Spatial", "Mysql2Rgeo"], Groupdate::Adapters::MySQLAdapter
|
47
|
+
Groupdate.register_adapter ["PostgreSQL", "PostGIS", "Redshift"], Groupdate::Adapters::PostgreSQLAdapter
|
48
|
+
Groupdate.register_adapter "SQLite", Groupdate::Adapters::SQLiteAdapter
|
49
|
+
|
31
50
|
require "groupdate/enumerable"
|
32
51
|
|
33
52
|
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:
|
4
|
+
version: 6.0.0
|
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: 2022-01-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,114 +16,16 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5'
|
19
|
+
version: '5.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
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
|
26
|
+
version: '5.2'
|
27
|
+
description:
|
28
|
+
email: andrew@ankane.org
|
127
29
|
executables: []
|
128
30
|
extensions: []
|
129
31
|
extra_rdoc_files: []
|
@@ -134,18 +36,21 @@ 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/sqlite_adapter.rb
|
137
43
|
- lib/groupdate/enumerable.rb
|
138
44
|
- lib/groupdate/magic.rb
|
139
45
|
- lib/groupdate/query_methods.rb
|
140
46
|
- lib/groupdate/relation.rb
|
141
|
-
- lib/groupdate/relation_builder.rb
|
142
47
|
- lib/groupdate/series_builder.rb
|
143
48
|
- lib/groupdate/version.rb
|
144
49
|
homepage: https://github.com/ankane/groupdate
|
145
50
|
licenses:
|
146
51
|
- MIT
|
147
52
|
metadata: {}
|
148
|
-
post_install_message:
|
53
|
+
post_install_message:
|
149
54
|
rdoc_options: []
|
150
55
|
require_paths:
|
151
56
|
- lib
|
@@ -153,15 +58,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
153
58
|
requirements:
|
154
59
|
- - ">="
|
155
60
|
- !ruby/object:Gem::Version
|
156
|
-
version: '2.
|
61
|
+
version: '2.6'
|
157
62
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
158
63
|
requirements:
|
159
64
|
- - ">="
|
160
65
|
- !ruby/object:Gem::Version
|
161
66
|
version: '0'
|
162
67
|
requirements: []
|
163
|
-
rubygems_version: 3.
|
164
|
-
signing_key:
|
68
|
+
rubygems_version: 3.3.3
|
69
|
+
signing_key:
|
165
70
|
specification_version: 4
|
166
71
|
summary: The simplest way to group temporal data
|
167
72
|
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.begin, @time_range.end]
|
198
|
-
else
|
199
|
-
["#{column} #{op} ?", @time_range.end]
|
200
|
-
end
|
201
|
-
else
|
202
|
-
["#{column} >= ?", @time_range.begin]
|
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
|