class Rollup

Constants

VERSION

not used in gemspec to avoid superclass mismatch be sure to update there as well

Attributes

time_zone[W]
week_start[RW]

Public Class Methods

list() click to toggle source
# 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") click to toggle source
# 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) click to toggle source

TODO maybe use in_batches

# 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: {}) click to toggle source
# 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
time_zone() click to toggle source

do not memoize so Time.zone can change

# File lib/rollup.rb, line 14
def time_zone
  (defined?(@time_zone) && @time_zone) || Time.zone || "Etc/UTC"
end
where_dimensions(dimensions) click to toggle source
# 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

Public Instance Methods

inspect() click to toggle source

feels cleaner than overriding _read_attribute

Calls superclass method
# File lib/rollup.rb, line 76
def inspect
  if Utils.date_interval?(interval)
    super.sub(/time: "[^"]+"/, "time: \"#{time.to_s(:db)}\"")
  else
    super
  end
end
time() click to toggle source
Calls superclass method
# 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