timescaledb icon indicating copy to clipboard operation
timescaledb copied to clipboard

Refresh Policy automatic

Open jonatas opened this issue 11 months ago • 0 comments

It's boring to keep repeating the same minimal refresh policy.


class Event < ActiveRecord::Base
  extend Timescaledb::ActsAsHypertable
  include Timescaledb::ContinuousAggregatesHelper

  acts_as_hypertable time_column: "time",
    segment_by: "identifier"

  scope :count_clicks, -> { select("count(*)").where(identifier: "click") }
  scope :count_views, -> { select("count(*)").where(identifier: "views") }

  continuous_aggregates scopes: [:count_clicks, :count_views],
    timeframes: [:minute, :hour, :day],
    refresh_policy: {
      minute: {
        start_offset: '3 minute',
        end_offset: '1 minute',
        schedule_interval: '1 minute'
      },
      hour: {
        start_offset: '3 hours',
        end_offset: '1 hour',
        schedule_interval: '1 minute'
      },
      day: {
        start_offset: '3 day',
        end_offset: '1 day',
        schedule_interval: '1 minute'
      }
    }
end

This part is pretty repetitive and should be simple as:

refresh_policy: :minimal

And then, we can allow users to implement policy profiles.

Timescaledb::RefreshPolicy = lambda do |name, timeframe|
  case name
  when :minimal
     {
        start_offset: '3 #{timeframe}',
        end_offset: '1 #{timeframe}',
        schedule_interval: '1 #{timeframe}'
     }
  else
    fail "missing definition for #{name} policy"
  end
end

jonatas avatar Jan 08 '25 14:01 jonatas