-
-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
582 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class DownloadResource < Avo::BaseResource | ||
self.title = :id | ||
self.includes = [] | ||
# self.search_query = -> do | ||
# scope.ransack(id_eq: params[:q], m: "or").result(distinct: false) | ||
# end | ||
|
||
field :id, as: :id | ||
# Fields generated from the model | ||
field :rubygem_id, as: :number | ||
field :version_id, as: :number | ||
field :downloads, as: :number | ||
field :key, as: :text | ||
field :bucket, as: :text | ||
field :occurred_at, as: :date_time | ||
# add fields here | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# This controller has been generated to enable Rails' resource routes. | ||
# More information on https://docs.avohq.io/2.0/controllers.html | ||
class Avo::DownloadsController < Avo::ResourcesController | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Download < DownloadRecord | ||
belongs_to :rubygem, validate: false, optional: true | ||
belongs_to :version, validate: false, optional: true | ||
|
||
validates :occurred_at, presence: true | ||
validates :downloads, presence: true | ||
|
||
def self.suffix = nil | ||
def self.time_period = nil | ||
|
||
def self.class_name_for(suffix:, time_period:) | ||
raise ArgumentError if suffix && !time_period | ||
"#{time_period.iso8601}#{"_#{suffix}" if suffix}".classify | ||
end | ||
|
||
[15.minutes, 1.day, 1.month, 1.year].each do |time_period| | ||
(%w[all_versions all_gems] << nil).each do |suffix| | ||
table_name = "#{Download.table_name}_#{time_period.inspect.parameterize(separator: '_')}#{"_#{suffix}" if suffix}" | ||
::Download.class_eval(<<~RUBY, __FILE__, __LINE__ + 1) # rubocop:disable Style/DocumentDynamicEvalDefinition | ||
class #{class_name_for(suffix:, time_period:)} < Download | ||
attribute :downloads, :integer | ||
def readonly? | ||
true | ||
end | ||
def self.table_name = #{table_name.dump} | ||
def self.suffix = #{suffix&.dump || :nil} | ||
def self.time_period = #{time_period.inspect.parameterize(separator: '_').dump} | ||
end | ||
RUBY | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class DownloadRecord < ApplicationRecord | ||
include Timescaledb::Rails::Model | ||
|
||
self.abstract_class = true | ||
|
||
connects_to database: { writing: :downloads, reading: :downloads } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Timescaledb::Rails::Railtie.configure do | ||
config.record_base = "::DownloadRecord" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CreateDownloads < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :downloads, id: false do |t| | ||
t.integer :rubygem_id, null: false | ||
t.integer :version_id, null: false | ||
t.integer :downloads, null: false | ||
t.integer :log_ticket_id, null: true | ||
t.timestamptz :occurred_at, null: false | ||
end | ||
|
||
add_index :downloads, [:rubygem_id, :version_id, :occurred_at, :log_ticket_id], unique: true, name: 'idx_downloads_by_version_log_ticket' | ||
create_hypertable :downloads, :occurred_at, chunk_time_interval: '7 days', partitioning_column: 'rubygem_id' | ||
end | ||
end |
101 changes: 101 additions & 0 deletions
101
db/downloads_migrate/20230308224729_create_downloads_continuous_aggregates.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
class CreateDownloadsContinuousAggregates < ActiveRecord::Migration[7.0] | ||
disable_ddl_transaction! | ||
|
||
def quote_table_name(...) | ||
connection.quote_table_name(...) | ||
end | ||
|
||
def continuous_aggregate( | ||
duration:, | ||
from: Download.table_name, | ||
start_offset:, | ||
end_offset:, | ||
schedule_window:, | ||
retention: start_offset | ||
) | ||
name = "downloads_" + duration.inspect.parameterize(separator: '_') | ||
|
||
create_continuous_aggregate( | ||
name, | ||
Download. | ||
time_bucket(duration.iso8601, quote_table_name("#{from}.occurred_at"), select_alias: 'occurred_at'). | ||
group(:rubygem_id, :version_id). | ||
select(:rubygem_id, :version_id). | ||
sum(quote_table_name("#{from}.downloads"), :downloads). | ||
reorder(nil). | ||
from(from). | ||
to_sql, | ||
force: true | ||
) | ||
add_continuous_aggregate_policy(name, start_offset&.iso8601, end_offset&.iso8601, schedule_window) | ||
add_hypertable_retention_policy(name, retention.iso8601) if retention | ||
|
||
all_versions_name = name + "_all_versions" | ||
create_continuous_aggregate( | ||
all_versions_name, | ||
Download. | ||
time_bucket(duration.iso8601, quote_table_name("#{name}.occurred_at"), select_alias: 'occurred_at'). | ||
group(:rubygem_id). | ||
select(:rubygem_id). | ||
sum(quote_table_name("#{name}.downloads"), :downloads). | ||
from(name). | ||
to_sql, | ||
force: true | ||
) | ||
add_continuous_aggregate_policy(all_versions_name, start_offset&.iso8601, end_offset&.iso8601, schedule_window) | ||
add_hypertable_retention_policy(all_versions_name, retention.iso8601) if retention | ||
|
||
all_gems_name = name + "_all_gems" | ||
create_continuous_aggregate( | ||
all_gems_name, | ||
Download. | ||
time_bucket(duration.iso8601, quote_table_name("#{all_versions_name}.occurred_at"), select_alias: 'occurred_at'). | ||
sum(quote_table_name("#{all_versions_name}.downloads"), :downloads). | ||
from(all_versions_name). | ||
to_sql, | ||
force: true | ||
) | ||
add_continuous_aggregate_policy(all_gems_name, start_offset&.iso8601, end_offset&.iso8601, schedule_window) | ||
add_hypertable_retention_policy(all_gems_name, retention.iso8601) if retention | ||
|
||
name | ||
end | ||
|
||
def change | ||
# https://github.com/timescale/timescaledb/issues/5474 | ||
Download.create!(version_id: 0, rubygem_id: 0, occurred_at: Time.at(0), downloads: 0) | ||
|
||
from = continuous_aggregate( | ||
duration: 15.minutes, | ||
start_offset: 1.week, | ||
end_offset: 1.hour, | ||
schedule_window: 1.hour | ||
) | ||
|
||
from = continuous_aggregate( | ||
duration: 1.day, | ||
start_offset: 2.years, | ||
end_offset: 1.day, | ||
schedule_window: 12.hours, | ||
from: | ||
) | ||
|
||
from = continuous_aggregate( | ||
duration: 1.month, | ||
start_offset: nil, | ||
end_offset: 1.month, | ||
schedule_window: 1.day, | ||
retention: nil, | ||
from: | ||
) | ||
|
||
from = continuous_aggregate( | ||
duration: 1.year, | ||
start_offset: nil, | ||
end_offset: 1.year, | ||
schedule_window: 1.month, | ||
retention: nil, | ||
from: | ||
) | ||
end | ||
end |
Oops, something went wrong.