rollups 0.1.4 → 0.2.0

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: eb7a00e9ed34a6924e0d6ea7c27fbe96275817274abcc5e0aed4c03edf38f9b2
4
- data.tar.gz: cb52591ce2a2f40c173bb925659a7b31812522dd2c81c0906ad2aff80b03f000
3
+ metadata.gz: 5bc765589c1bb3ac755335e36a1a283ef4aeac8ac54a6bc102d3358c57768628
4
+ data.tar.gz: 0eab24b5cd948823ab190674618fec7ce0935b91d740384854e0474b931d9318
5
5
  SHA512:
6
- metadata.gz: a7a044a8e1b85c51de0043e48381037adb080bded3dbd620fc1dec3715c8cf735a94778470672b269a66e31c764889dbc2d58f02dee7cb542eb739496d36d4c2
7
- data.tar.gz: b66ea1e6f81de6bce70d947c816aee005363cdea4e61a1a98cdf4b83714f8b8c814bcba29806436589d6e5ee820f8704267944359341aa20afd84ceda215fb65
6
+ metadata.gz: 8a64dd7d050f06d49a5d8261b09b0437faf7a579e086b154903daf86de86ad799a1b97c98d44149b0e0fb20e4531a9d435b787adccefff7b89afb91f4cc44af3
7
+ data.tar.gz: 60ac29b15fb518bfaad4026ca02d55a926a829f1229f758993a07cace6117030247211750fb1bc33deb9547e55e406ab81e1a06f2ecc3f9208d626d62744aa3c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.2.0 (2022-04-05)
2
+
3
+ - Added `range` option
4
+ - Dropped support for Active Record < 5.2
5
+
1
6
  ## 0.1.4 (2021-12-15)
2
7
 
3
8
  - Fixed warning with Active Record 7
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2020-2021 Andrew Kane
3
+ Copyright (c) 2020-2022 Andrew Kane
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -11,13 +11,13 @@ Works great with [Ahoy](https://github.com/ankane/ahoy) and [Searchjoy](https://
11
11
  Add this line to your application’s Gemfile:
12
12
 
13
13
  ```ruby
14
- gem 'rollups'
14
+ gem "rollups"
15
15
  ```
16
16
 
17
17
  For Rails < 6, also add:
18
18
 
19
19
  ```ruby
20
- gem 'activerecord-import'
20
+ gem "activerecord-import"
21
21
  ```
22
22
 
23
23
  And run:
@@ -173,6 +173,12 @@ To recalculate the last few intervals, use:
173
173
  User.rollup("New users", last: 3)
174
174
  ```
175
175
 
176
+ To recalculate a time range, use:
177
+
178
+ ```ruby
179
+ User.rollup("New users", range: 1.week.ago.all_week)
180
+ ```
181
+
176
182
  To only store data for completed intervals, use:
177
183
 
178
184
  ```ruby
@@ -4,13 +4,15 @@ class Rollup
4
4
  @klass = klass # or relation
5
5
  end
6
6
 
7
- def rollup(name, column: nil, interval: "day", dimension_names: nil, time_zone: nil, current: true, last: nil, clear: false, &block)
7
+ def rollup(name, column: nil, interval: "day", dimension_names: nil, time_zone: nil, current: nil, last: nil, clear: false, range: nil, &block)
8
8
  raise "Name can't be blank" if name.blank?
9
9
 
10
10
  column ||= @klass.rollup_column || :created_at
11
- validate_column(column)
11
+ # Groupdate 6+ validates, but keep this for now for additional safety
12
+ # no need to quote/resolve column here, as Groupdate handles it
13
+ column = validate_column(column)
12
14
 
13
- relation = perform_group(name, column: column, interval: interval, time_zone: time_zone, current: current, last: last, clear: clear)
15
+ relation = perform_group(name, column: column, interval: interval, time_zone: time_zone, current: current, last: last, clear: clear, range: range)
14
16
  result = perform_calculation(relation, &block)
15
17
 
16
18
  dimension_names = set_dimension_names(dimension_names, relation)
@@ -23,18 +25,24 @@ class Rollup
23
25
 
24
26
  # basic version of Active Record disallow_raw_sql!
25
27
  # symbol = column (safe), Arel node = SQL (safe), other = untrusted
26
- # no need to quote/resolve column here, as Groupdate handles it
27
- # TODO remove this method when gem depends on Groupdate 6+
28
+ # matches table.column and column
28
29
  def validate_column(column)
29
- # matches table.column and column
30
- unless column.is_a?(Symbol) || column.is_a?(Arel::Nodes::SqlLiteral) || /\A\w+(\.\w+)?\z/i.match(column.to_s)
31
- raise "Non-attribute argument: #{column}. Use Arel.sql() for known-safe values"
30
+ unless column.is_a?(Symbol) || column.is_a?(Arel::Nodes::SqlLiteral)
31
+ column = column.to_s
32
+ unless /\A\w+(\.\w+)?\z/i.match(column)
33
+ raise ActiveRecord::UnknownAttributeReference, "Query method called with non-attribute argument(s): #{column.inspect}. Use Arel.sql() for known-safe values."
34
+ end
32
35
  end
36
+ column
33
37
  end
34
38
 
35
- def perform_group(name, column:, interval:, time_zone:, current:, last:, clear:)
39
+ def perform_group(name, column:, interval:, time_zone:, current:, last:, clear:, range:)
40
+ raise ArgumentError, "Cannot use last and range together" if last && range
36
41
  raise ArgumentError, "Cannot use last and clear together" if last && clear
42
+ raise ArgumentError, "Cannot use range and clear together" if range && clear
43
+ raise ArgumentError, "Cannot use range and current together" if range && !current.nil?
37
44
 
45
+ current = true if current.nil?
38
46
  time_zone ||= Rollup.time_zone
39
47
 
40
48
  gd_options = {
@@ -48,6 +56,10 @@ class Rollup
48
56
 
49
57
  if last
50
58
  gd_options[:last] = last
59
+ elsif range
60
+ gd_options[:range] = range
61
+ gd_options[:expand_range] = true
62
+ gd_options.delete(:current)
51
63
  elsif !clear
52
64
  # if no rollups, compute all intervals
53
65
  # if rollups, recompute last interval
@@ -1,5 +1,5 @@
1
1
  class Rollup
2
2
  # not used in gemspec to avoid superclass mismatch
3
3
  # be sure to update there as well
4
- VERSION = "0.1.4"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rollups
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-16 00:00:00.000000000 Z
11
+ date: 2022-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.1'
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.1'
26
+ version: '5.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: groupdate
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '5.2'
33
+ version: '6.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '5.2'
40
+ version: '6.1'
41
41
  description:
42
42
  email: andrew@ankane.org
43
43
  executables: []
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  requirements: []
78
- rubygems_version: 3.2.32
78
+ rubygems_version: 3.3.7
79
79
  signing_key:
80
80
  specification_version: 4
81
81
  summary: Rollup time-series data in Rails