ankane / rollup

Rollup time-series data in Rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rollup ranges util

hqm42 opened this issue · comments

When incrementally filling rollups I encountered an edge case where you can not detect if an interval needs to be recalculated or not. Let's say I want to lazily calculate an account balance. Whenever I need the current balance I .rollup() my transfers with the sum of the amount column. When a transfer is added and no .rollup is needed until the end of the day, the amount of the transfer will end up in the rollup. I could add timestamp columns to the rollups table to check if it has been updated outside the interval. Unfortunately this this is impossible as I am unable to programmatically get the end of the interval.

It would be nice to have a Rollup::Utils method to get the ranges of the last and current interval:

Rollup::Utils.get_interval_range(:day, last: 2)
=> [
  Fri, 12 May 2023 00:00:00.000000000 CEST +02:00..Fri, 12 May 2023 23:59:59.999999999 CEST +02:00,
  Thu,11 May 2023 00:00:00.000000000 CEST +02:00..Thu, 11 May 2023 23:59:59.999999999 CEST +02:00
]

Whit these it would b trivial to implement a CompleteUnfinishedRollupsJob:

def perform(name, interval)
  _range_current, range_last_finished = Rollup::Utils.get_interval_range(:day, last: 2)
  if Rollup.where(name: name, created_at: range_last_finished, updated_at: range_last_finished).exists?
    Transfer.where(...).rollup(name, interval: interval, last: 1, current: false) { ... }
  end
end

This will only leave a short stale rollup time window between ending an interval and performing the CompleteUnfinishedRollupsJob.

Hi @hqm42, thanks for the suggestion, but don't think it's common enough to add. An alternative is to use Active Support methods directly.