2022-04-23 23:32:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-04-26 19:49:26 +00:00
|
|
|
# Implementa rollups recursivos
|
2022-04-23 23:32:20 +00:00
|
|
|
module RecursiveRollup
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
private
|
|
|
|
|
|
|
|
# Genera un rollup recursivo en base al período anterior y aplica una
|
|
|
|
# operación.
|
|
|
|
#
|
|
|
|
# @param :name [String]
|
|
|
|
# @param :interval_previous [String]
|
|
|
|
# @param :interval [String]
|
|
|
|
# @param :operation [Symbol]
|
|
|
|
# @param :dimensions [Hash]
|
2022-04-30 12:56:19 +00:00
|
|
|
# @param :beginning [Time]
|
|
|
|
# @return [Rollup]
|
|
|
|
def recursive_rollup(name:, interval_previous:, interval:, dimensions:, beginning:, operation: :sum)
|
|
|
|
Rollup.where(name: name, interval: interval_previous, dimensions: dimensions)
|
|
|
|
.where('time >= ?', beginning.try(:"beginning_of_#{interval}"))
|
|
|
|
.group(*dimensions_to_jsonb_query(dimensions))
|
|
|
|
.rollup(name, interval: interval, update: true) do |rollup|
|
2022-04-23 23:32:20 +00:00
|
|
|
rollup.try(operation, :value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-30 17:39:31 +00:00
|
|
|
# Reducir las estadísticas calculadas aplicando un rollup sobre el
|
|
|
|
# intervalo más amplio.
|
|
|
|
#
|
|
|
|
# @param :name [String]
|
|
|
|
# @param :beginning [Time]
|
|
|
|
# @param :operation [Symbol]
|
|
|
|
# @param :dimensions [Hash]
|
|
|
|
# @return [nil]
|
|
|
|
def reduce_rollup(name, beginning, operation, **dimensions)
|
|
|
|
Stat::INTERVALS.reduce do |previous, current|
|
|
|
|
recursive_rollup(name: name,
|
|
|
|
interval_previous: previous,
|
|
|
|
interval: current,
|
|
|
|
dimensions: dimensions,
|
|
|
|
beginning: beginning,
|
|
|
|
operation: operation)
|
|
|
|
|
|
|
|
# Devolver el intervalo actual
|
|
|
|
current
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2022-04-23 23:32:20 +00:00
|
|
|
# @param :dimensions [Hash]
|
|
|
|
# @return [Array]
|
|
|
|
def dimensions_to_jsonb_query(dimensions)
|
|
|
|
dimensions.keys.map do |key|
|
|
|
|
"dimensions->'#{key}'"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|