Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh Policy automatic #89

Open
jonatas opened this issue Jan 8, 2025 · 0 comments
Open

Refresh Policy automatic #89

jonatas opened this issue Jan 8, 2025 · 0 comments

Comments

@jonatas
Copy link
Collaborator

jonatas commented Jan 8, 2025

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant