Class: Rollup
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Rollup
- Defined in:
- lib/rollup.rb,
lib/rollup/model.rb,
lib/rollup/utils.rb,
lib/rollup/version.rb,
lib/rollup/aggregator.rb
Defined Under Namespace
Modules: Model, Utils Classes: Aggregator
Constant Summary collapse
- VERSION =
not used in gemspec to avoid superclass mismatch be sure to update there as well
"0.4.0"
Class Attribute Summary collapse
-
.time_zone ⇒ Object
do not memoize so Time.zone can change.
-
.week_start ⇒ Object
Returns the value of attribute week_start.
Class Method Summary collapse
- .list ⇒ Object
- .multi_series(name, interval: "day") ⇒ Object
-
.rename(old_name, new_name) ⇒ Object
TODO maybe use in_batches.
- .series(name, interval: "day", dimensions: {}) ⇒ Object
- .where_dimensions(dimensions) ⇒ Object
Instance Method Summary collapse
-
#inspect ⇒ Object
feels cleaner than overriding _read_attribute.
- #time ⇒ Object
Class Attribute Details
.time_zone ⇒ Object
do not memoize so Time.zone can change
14 15 16 |
# File 'lib/rollup.rb', line 14 def time_zone (defined?(@time_zone) && @time_zone) || Time.zone || "Etc/UTC" end |
.week_start ⇒ Object
Returns the value of attribute week_start.
7 8 9 |
# File 'lib/rollup.rb', line 7 def week_start @week_start end |
Class Method Details
.list ⇒ Object
63 64 65 66 67 |
# File 'lib/rollup.rb', line 63 def list select(:name, :interval).distinct.order(:name, :interval).map do |r| {name: r.name, interval: r.interval} end end |
.multi_series(name, interval: "day") ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rollup.rb', line 31 def multi_series(name, interval: "day") Utils.check_dimensions relation = where(name: name, interval: interval) # use select_all to reduce allocations sql = relation.order(:time).select(Utils.time_sql(interval), :value, :dimensions).to_sql result = connection.select_all(sql).rows result.group_by { |r| JSON.parse(r[2]) }.map do |dimensions, rollups| {dimensions: dimensions, data: Utils.make_series(rollups, interval)} end end |
.rename(old_name, new_name) ⇒ Object
TODO maybe use in_batches
70 71 72 |
# File 'lib/rollup.rb', line 70 def rename(old_name, new_name) where(name: old_name).update_all(name: new_name) end |
.series(name, interval: "day", dimensions: {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rollup.rb', line 18 def series(name, interval: "day", dimensions: {}) Utils.check_dimensions if dimensions.any? relation = where(name: name, interval: interval) relation = relation.where(dimensions: dimensions) if Utils.dimensions_supported? # use select_all due to incorrect casting with pluck sql = relation.order(:time).select(Utils.time_sql(interval), :value).to_sql result = connection.select_all(sql).rows Utils.make_series(result, interval) end |
.where_dimensions(dimensions) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rollup.rb', line 45 def where_dimensions(dimensions) Utils.check_dimensions relation = self dimensions.each do |k, v| k = k.to_s relation = if v.nil? relation.where("dimensions ->> ? IS NULL", k) elsif v.is_a?(Array) relation.where("dimensions ->> ? IN (?)", k, v.map { |vi| vi.as_json.to_s }) else relation.where("dimensions ->> ? = ?", k, v.as_json.to_s) end end relation end |
Instance Method Details
#inspect ⇒ Object
feels cleaner than overriding _read_attribute
76 77 78 79 80 81 82 |
# File 'lib/rollup.rb', line 76 def inspect if Utils.date_interval?(interval) super.sub(/time: "[^"]+"/, "time: \"#{time.to_formatted_s(:db)}\"") else super end end |
#time ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rollup.rb', line 84 def time if Utils.date_interval?(interval) && !time_before_type_cast.nil? if time_before_type_cast.is_a?(Time) time_before_type_cast.utc.to_date else Date.parse(time_before_type_cast.to_s) end else super end end |