groupdate 5.1.0 → 5.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 163a969c3eb174890b78b4512c6a6631e02722a43f1fbcc30926f3e48cdba461
4
- data.tar.gz: 256ee01a6e991c7513390e8cdf6d27b3e4458018e0980d63e9506bccfc6923d8
3
+ metadata.gz: 733ede5e2375b5b97d951b5ef313d8cd09843250f5c64bb6f611945b951f9156
4
+ data.tar.gz: e753145a244b5f7dc3ac58127edc301ea656eca3cadd702112b1fe54f798c6d5
5
5
  SHA512:
6
- metadata.gz: 37093986846259dc59a17187af24097a2ed9d881de6a48ff9a394883e5a936a569fd758daf820cc9444e69212d29548ab21a816f7fac0cba1b7e502ac2d99198
7
- data.tar.gz: d1639375454ed0d1d33600226287a484ae7001c3759446b2c196dd24f1e003f3fce7f0c8f2253d90819bc280260bedfed2fa6f622f24ad46c590c05739177df2
6
+ metadata.gz: e182d88f6d17a55465590c1096c942ca62d79107ce244509cb9350dd6587ac3ec5ff99d65a06c45c31416232d34c20764094ef895b3f28be3657959ef0791ef1
7
+ data.tar.gz: '095c5500b970a6654b7f21dcdbaec7ee3a80146ce15de4c5e124ac2f9a6afb31dd2e155ec8f90db629658de3ff1ebc416fc8620c4e8dd2464ff2d3b175548e09'
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## 5.2.3 (2021-12-06)
2
+
3
+ - Fixed error and warnings with Active Record 7
4
+
5
+ ## 5.2.2 (2021-02-08)
6
+
7
+ - Added support for `nil..nil` ranges in `range` option
8
+
9
+ ## 5.2.1 (2020-09-09)
10
+
11
+ - Improved error message for invalid ranges
12
+ - Fixed bug with date string ranges
13
+
14
+ ## 5.2.0 (2020-09-07)
15
+
16
+ - Added warning for non-attribute argument
17
+ - Added support for beginless and endless ranges in `range` option
18
+
1
19
  ## 5.1.0 (2020-07-30)
2
20
 
3
21
  - Added `n` option to minute and second for custom durations
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013-2020 Andrew Kane
1
+ Copyright (c) 2013-2021 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
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://travis-ci.org/ankane/groupdate.svg?branch=master)](https://travis-ci.org/ankane/groupdate)
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
 
@@ -60,7 +60,7 @@ and
60
60
  - day_of_year
61
61
  - month_of_year
62
62
 
63
- Use it anywhere you can use `group`. Works with `count`, `sum`, `minimum`, `maximum`, and `average`. For `median`, check out [ActiveMedian](https://github.com/ankane/active_median).
63
+ Use it anywhere you can use `group`. Works with `count`, `sum`, `minimum`, `maximum`, and `average`. For `median` and `percentile`, check out [ActiveMedian](https://github.com/ankane/active_median).
64
64
 
65
65
  ### Time Zones
66
66
 
@@ -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,56 @@
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
+ clause.gsub(/ (\-|\+) INTERVAL 0 second/, "")
53
+ end
54
+ end
55
+ end
56
+ 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
@@ -47,7 +47,6 @@ module Groupdate
47
47
  # TODO better messages
48
48
  raise ArgumentError, "Unrecognized time zone" unless time_zone
49
49
  raise ArgumentError, "Unrecognized :week_start option" unless week_start
50
- raise ArgumentError, "Cannot use endless range for :range option" if options[:range].is_a?(Range) && !options[:range].end
51
50
  raise ArgumentError, ":day_start must be between 0 and 24" if (day_start / 3600) < 0 || (day_start / 3600) >= 24
52
51
  end
53
52
 
@@ -70,12 +69,25 @@ module Groupdate
70
69
  @day_start ||= ((options[:day_start] || Groupdate.day_start).to_f * 3600).round
71
70
  end
72
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
+
73
84
  def series_builder
74
85
  @series_builder ||=
75
86
  SeriesBuilder.new(
76
87
  **options,
77
88
  period: period,
78
89
  time_zone: time_zone,
90
+ range: range,
79
91
  day_start: day_start,
80
92
  week_start: week_start,
81
93
  n_seconds: n_seconds
@@ -177,9 +189,13 @@ module Groupdate
177
189
  def self.generate_relation(relation, field:, **options)
178
190
  magic = Groupdate::Magic::Relation.new(**options)
179
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
+
180
196
  # generate ActiveRecord relation
181
197
  relation =
182
- RelationBuilder.new(
198
+ adapter.new(
183
199
  relation,
184
200
  column: field,
185
201
  period: magic.period,
@@ -11,7 +11,6 @@ module Groupdate
11
11
  @day_start = day_start
12
12
  @n_seconds = n_seconds
13
13
  @options = options
14
- @round_time = {}
15
14
  @week_start_key = Groupdate::Magic::DAYS[@week_start] if @week_start
16
15
  end
17
16
 
@@ -142,11 +141,35 @@ module Groupdate
142
141
  def time_range
143
142
  @time_range ||= begin
144
143
  time_range = options[:range]
145
- if time_range.is_a?(Range) && time_range.first.is_a?(Date)
146
- # convert range of dates to range of times
147
- last = time_range.last.in_time_zone(time_zone)
148
- last += 1.day unless time_range.exclude_end?
149
- time_range = Range.new(time_range.first.in_time_zone(time_zone), last, true)
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
158
+ end
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
170
+ end
171
+
172
+ time_range = Range.new(start, finish, exclude_end)
150
173
  elsif !time_range && options[:last]
151
174
  if period == :quarter
152
175
  step = 3.months
@@ -200,7 +223,7 @@ module Groupdate
200
223
  else
201
224
  time_range = self.time_range
202
225
  time_range =
203
- if time_range.is_a?(Range)
226
+ if time_range.is_a?(Range) && time_range.begin && time_range.end
204
227
  time_range
205
228
  else
206
229
  # use first and last values
@@ -211,11 +234,23 @@ module Groupdate
211
234
  data.keys.sort
212
235
  end
213
236
 
214
- tr = sorted_keys.first..sorted_keys.last
215
- if options[:current] == false && sorted_keys.any? && round_time(now) >= tr.last
216
- tr = tr.first...round_time(now)
237
+ if time_range.is_a?(Range)
238
+ if sorted_keys.any?
239
+ if time_range.begin
240
+ time_range.begin..sorted_keys.last
241
+ else
242
+ Range.new(sorted_keys.first, time_range.end, time_range.exclude_end?)
243
+ end
244
+ else
245
+ nil..nil
246
+ end
247
+ else
248
+ tr = sorted_keys.first..sorted_keys.last
249
+ if options[:current] == false && sorted_keys.any? && round_time(now) >= tr.last
250
+ tr = tr.first...round_time(now)
251
+ end
252
+ tr
217
253
  end
218
- tr
219
254
  end
220
255
 
221
256
  if time_range.begin
@@ -1,3 +1,3 @@
1
1
  module Groupdate
2
- VERSION = "5.1.0"
2
+ VERSION = "5.2.3"
3
3
  end
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.1.0
4
+ version: 5.2.3
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: 2020-07-31 00:00:00.000000000 Z
11
+ date: 2021-12-07 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
- - !ruby/object:Gem::Dependency
28
- name: bundler
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.1.2
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,207 +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
- @relation = relation
7
- @column = resolve_column(relation, column)
8
- @period = period
9
- @time_zone = time_zone
10
- @time_range = time_range
11
- @week_start = week_start
12
- @day_start = day_start
13
- @n_seconds = n_seconds
14
-
15
- if relation.default_timezone == :local
16
- raise Groupdate::Error, "ActiveRecord::Base.default_timezone must be :utc to use Groupdate"
17
- end
18
- end
19
-
20
- def generate
21
- @relation.group(group_clause).where(*where_clause)
22
- end
23
-
24
- private
25
-
26
- def group_clause
27
- time_zone = @time_zone.tzinfo.name
28
- adapter_name = @relation.connection.adapter_name
29
- query =
30
- case adapter_name
31
- when "Mysql2", "Mysql2Spatial", "Mysql2Rgeo"
32
- day_start_column = "CONVERT_TZ(#{column}, '+00:00', ?) - INTERVAL ? second"
33
-
34
- case period
35
- when :minute_of_hour
36
- ["MINUTE(#{day_start_column})", time_zone, day_start]
37
- when :hour_of_day
38
- ["HOUR(#{day_start_column})", time_zone, day_start]
39
- when :day_of_week
40
- ["DAYOFWEEK(#{day_start_column}) - 1", time_zone, day_start]
41
- when :day_of_month
42
- ["DAYOFMONTH(#{day_start_column})", time_zone, day_start]
43
- when :day_of_year
44
- ["DAYOFYEAR(#{day_start_column})", time_zone, day_start]
45
- when :month_of_year
46
- ["MONTH(#{day_start_column})", time_zone, day_start]
47
- when :week
48
- ["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]
49
- when :quarter
50
- ["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]
51
- when :custom
52
- ["FROM_UNIXTIME((UNIX_TIMESTAMP(#{column}) DIV ?) * ?)", n_seconds, n_seconds]
53
- else
54
- format =
55
- case period
56
- when :second
57
- "%Y-%m-%d %H:%i:%S"
58
- when :minute
59
- "%Y-%m-%d %H:%i:00"
60
- when :hour
61
- "%Y-%m-%d %H:00:00"
62
- when :day
63
- "%Y-%m-%d 00:00:00"
64
- when :month
65
- "%Y-%m-01 00:00:00"
66
- else # year
67
- "%Y-01-01 00:00:00"
68
- end
69
-
70
- ["CONVERT_TZ(DATE_FORMAT(#{day_start_column}, ?) + INTERVAL ? second, ?, '+00:00')", time_zone, day_start, format, day_start, time_zone]
71
- end
72
- when "PostgreSQL", "PostGIS"
73
- day_start_column = "#{column}::timestamptz AT TIME ZONE ? - INTERVAL ?"
74
- day_start_interval = "#{day_start} second"
75
-
76
- case period
77
- when :minute_of_hour
78
- ["EXTRACT(MINUTE FROM #{day_start_column})::integer", time_zone, day_start_interval]
79
- when :hour_of_day
80
- ["EXTRACT(HOUR FROM #{day_start_column})::integer", time_zone, day_start_interval]
81
- when :day_of_week
82
- ["EXTRACT(DOW FROM #{day_start_column})::integer", time_zone, day_start_interval]
83
- when :day_of_month
84
- ["EXTRACT(DAY FROM #{day_start_column})::integer", time_zone, day_start_interval]
85
- when :day_of_year
86
- ["EXTRACT(DOY FROM #{day_start_column})::integer", time_zone, day_start_interval]
87
- when :month_of_year
88
- ["EXTRACT(MONTH FROM #{day_start_column})::integer", time_zone, day_start_interval]
89
- when :week
90
- ["(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]
91
- when :custom
92
- ["TO_TIMESTAMP(FLOOR(EXTRACT(EPOCH FROM #{column}::timestamptz) / ?) * ?)", n_seconds, n_seconds]
93
- else
94
- if day_start == 0
95
- # prettier
96
- ["DATE_TRUNC(?, #{day_start_column}) AT TIME ZONE ?", period, time_zone, day_start_interval, time_zone]
97
- else
98
- ["(DATE_TRUNC(?, #{day_start_column}) + INTERVAL ?) AT TIME ZONE ?", period, time_zone, day_start_interval, day_start_interval, time_zone]
99
- end
100
- end
101
- when "SQLite"
102
- raise Groupdate::Error, "Time zones not supported for SQLite" unless @time_zone.utc_offset.zero?
103
- raise Groupdate::Error, "day_start not supported for SQLite" unless day_start.zero?
104
-
105
- if period == :week
106
- ["strftime('%Y-%m-%d 00:00:00 UTC', #{column}, '-6 days', ?)", "weekday #{(week_start + 1) % 7}"]
107
- elsif period == :custom
108
- ["datetime((strftime('%s', #{column}) / ?) * ?, 'unixepoch')", n_seconds, n_seconds]
109
- else
110
- format =
111
- case period
112
- when :minute_of_hour
113
- "%M"
114
- when :hour_of_day
115
- "%H"
116
- when :day_of_week
117
- "%w"
118
- when :day_of_month
119
- "%d"
120
- when :day_of_year
121
- "%j"
122
- when :month_of_year
123
- "%m"
124
- when :second
125
- "%Y-%m-%d %H:%M:%S UTC"
126
- when :minute
127
- "%Y-%m-%d %H:%M:00 UTC"
128
- when :hour
129
- "%Y-%m-%d %H:00:00 UTC"
130
- when :day
131
- "%Y-%m-%d 00:00:00 UTC"
132
- when :month
133
- "%Y-%m-01 00:00:00 UTC"
134
- when :quarter
135
- raise Groupdate::Error, "Quarter not supported for SQLite"
136
- else # year
137
- "%Y-01-01 00:00:00 UTC"
138
- end
139
-
140
- ["strftime(?, #{column})", format]
141
- end
142
- when "Redshift"
143
- day_start_column = "CONVERT_TIMEZONE(?, #{column}::timestamp) - INTERVAL ?"
144
- day_start_interval = "#{day_start} second"
145
-
146
- case period
147
- when :minute_of_hour
148
- ["EXTRACT(MINUTE from #{day_start_column})::integer", time_zone, day_start_interval]
149
- when :hour_of_day
150
- ["EXTRACT(HOUR from #{day_start_column})::integer", time_zone, day_start_interval]
151
- when :day_of_week
152
- ["EXTRACT(DOW from #{day_start_column})::integer", time_zone, day_start_interval]
153
- when :day_of_month
154
- ["EXTRACT(DAY from #{day_start_column})::integer", time_zone, day_start_interval]
155
- when :day_of_year
156
- ["EXTRACT(DOY from #{day_start_column})::integer", time_zone, day_start_interval]
157
- when :month_of_year
158
- ["EXTRACT(MONTH from #{day_start_column})::integer", time_zone, day_start_interval]
159
- when :week # start on Sunday, not Redshift default Monday
160
- # Redshift does not return timezone information; it
161
- # always says it is in UTC time, so we must convert
162
- # back to UTC to play properly with the rest of Groupdate.
163
- week_start_interval = "#{week_start} day"
164
- ["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]
165
- when :custom
166
- raise Groupdate::Error, "Not implemented yet"
167
- else
168
- ["CONVERT_TIMEZONE(?, 'Etc/UTC', DATE_TRUNC(?, #{day_start_column}) + INTERVAL ?)::timestamp", time_zone, period, time_zone, day_start_interval, day_start_interval]
169
- end
170
- else
171
- raise Groupdate::Error, "Connection adapter not supported: #{adapter_name}"
172
- end
173
-
174
- clause = @relation.send(:sanitize_sql_array, query)
175
-
176
- # cleaner queries in logs
177
- clause = clean_group_clause_postgresql(clause)
178
- clean_group_clause_mysql(clause)
179
- end
180
-
181
- def clean_group_clause_postgresql(clause)
182
- clause.gsub(/ (\-|\+) INTERVAL '0 second'/, "")
183
- end
184
-
185
- def clean_group_clause_mysql(clause)
186
- clause.gsub(/ (\-|\+) INTERVAL 0 second/, "")
187
- end
188
-
189
- def where_clause
190
- if @time_range.is_a?(Range)
191
- op = @time_range.exclude_end? ? "<" : "<="
192
- ["#{column} >= ? AND #{column} #{op} ?", @time_range.first, @time_range.last]
193
- else
194
- ["#{column} IS NOT NULL"]
195
- end
196
- end
197
-
198
- # resolves eagerly
199
- # need to convert both where_clause (easy)
200
- # and group_clause (not easy) if want to avoid this
201
- def resolve_column(relation, column)
202
- node = relation.send(:relation).send(:arel_columns, [column]).first
203
- node = Arel::Nodes::SqlLiteral.new(node) if node.is_a?(String)
204
- relation.connection.visitor.accept(node, Arel::Collectors::SQLString.new).value
205
- end
206
- end
207
- end