-
-
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
17 changed files
with
574 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,33 @@ | ||
class Download < DownloadRecord | ||
belongs_to :rubygem | ||
belongs_to :version | ||
|
||
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,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 |
107 changes: 107 additions & 0 deletions
107
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,107 @@ | ||
class CreateDownloadsContinuousAggregates < ActiveRecord::Migration[7.0] | ||
disable_ddl_transaction! | ||
|
||
def execute(...) | ||
puts(...) | ||
super | ||
end | ||
|
||
def create_continuous_aggregate(name, ...) | ||
execute "DROP MATERIALIZED VIEW IF EXISTS #{connection.quote_table_name(name)} CASCADE;" | ||
super | ||
rescue ActiveRecord::StatementInvalid => e | ||
raise unless e.cause.is_a?(PG::DatetimeFieldOverflow) | ||
say "WARNING: #{e}" | ||
nil | ||
end | ||
|
||
def continuous_aggregate( | ||
duration:, | ||
from: Download.table_name, | ||
start_offset:, | ||
end_offset:, | ||
schedule_window:, | ||
retention: start_offset | ||
) | ||
name = "downloads_" + duration.inspect.parameterize(separator: '_') | ||
start_offset ||= 20.years | ||
|
||
create_continuous_aggregate( | ||
name, | ||
Download. | ||
time_bucket(duration.iso8601, select_alias: :occurred_at). | ||
group(:rubygem_id, :version_id). | ||
select(:rubygem_id, :version_id). | ||
sum(:downloads, :downloads). | ||
reorder(nil). | ||
from(from). | ||
to_sql | ||
) | ||
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, select_alias: :occurred_at). | ||
group(:rubygem_id). | ||
select(:rubygem_id). | ||
sum(:downloads, :downloads). | ||
from(name). | ||
to_sql | ||
) | ||
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, select_alias: :occurred_at). | ||
sum(:downloads, :downloads). | ||
from(all_versions_name). | ||
to_sql | ||
) | ||
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 | ||
ActiveRecord::Base.logger = Logger.new(STDERR) | ||
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.