Skip to content

Commit

Permalink
Remove download references from specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatas committed Sep 17, 2024
1 parent a31f296 commit 06d97e0
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 115 deletions.
5 changes: 2 additions & 3 deletions lib/timescaledb/continuous_aggregates_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def create_continuous_aggregates(with_data: false)
@aggregates.each do |aggregate_name, config|
@timeframes.each do |timeframe|
klass = const_get("#{aggregate_name}_per_#{timeframe}".classify)

connection.execute <<~SQL
CREATE MATERIALIZED VIEW IF NOT EXISTS #{klass.table_name}
WITH (timescaledb.continuous) AS
Expand Down Expand Up @@ -132,12 +131,12 @@ class << self
if previous_timeframe
prev_klass = base_model.const_get("#{aggregate_name}_per_#{previous_timeframe}".classify)
select_clause = base_model.apply_rollup_rules("#{config[:select]}")
self.base_query = "SELECT time_bucket(#{interval}, #{time_column}) as #{time_column}, #{select_clause} FROM #{prev_klass.table_name} GROUP BY #{[1, *config[:group_by]].join(', ')}"
self.base_query = "SELECT time_bucket(#{interval}, #{time_column}) as #{time_column}, #{select_clause} FROM \"#{prev_klass.table_name}\" GROUP BY #{[1, *config[:group_by]].join(', ')}"
else
scope = base_model.public_send(config[:scope_name])
config[:select] = scope.select_values.join(', ')
config[:group_by] = scope.group_values
self.base_query = scope.rollup(interval).to_sql
self.base_query = "SELECT time_bucket(#{interval}, #{time_column}) as #{time_column}, #{config[:select]} FROM \"#{scope.table_name}\" GROUP BY #{[1, *config[:group_by]].join(', ')}"
end

def self.refresh!(start_time = nil, end_time = nil)
Expand Down
23 changes: 23 additions & 0 deletions spec/support/active_record/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,28 @@ class HypertableSkipAllScopes < ActiveRecord::Base
acts_as_hypertable time_column: :timestamp, skip_association_scopes: true, skip_default_scopes: true
end

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

acts_as_hypertable time_column: 'ts'

scope :total, -> { select("count(*) as total") }
scope :by_identifier, -> { select("identifier, count(*) as total").group(:identifier) }
scope :by_version, -> { select("identifier, version, count(*) as total").group(:identifier, :version) }

continuous_aggregates(
time_column: 'ts',
timeframes: [:minute, :hour, :day, :month],
scopes: [:total, :by_identifier, :by_version],
refresh_policy: {
minute: { start_offset: "10 minutes", end_offset: "1 minute", schedule_interval: "1 minute" },
hour: { start_offset: "4 hour", end_offset: "1 hour", schedule_interval: "1 hour" },
day: { start_offset: "3 day", end_offset: "1 day", schedule_interval: "1 hour" },
month: { start_offset: "3 month", end_offset: "1 hour", schedule_interval: "1 hour" }
}
)
end

class NonHypertable < ActiveRecord::Base
end
Loading

0 comments on commit 06d97e0

Please sign in to comment.