Skip to content
Snippets Groups Projects

Create partitioned CI component usage table

Merged Leaminn Ma requested to merge create-catalog-resource-component-usages-table into master
2 unresolved threads
23 files
+ 396
3
Compare changes
  • Side-by-side
  • Inline
Files
23
  • 0fc56a67
    Add table to track CI component usage · 0fc56a67
    Leaminn Ma authored
    Adds a new partitioned table p_catalog_resource_component_usages.
    This table will track when a CI component is included in a project's
    pipeline. Only unique records of component-project-used_date are
    inserted.
    
    This data will be used to display component usage popularity
    aggregated by component or catalog_resource per 30 day rolling
    window.
    
    This table creates a new partition every 30 days. For now, the
    table will drop partitions with data older than 1 year. This
    duration will likely be reduced in a follow up issue.
    
    Changelog: added
# frozen_string_literal: true
module Ci
module Catalog
module Resources
module Components
# This model is used to track when a project includes a component in a pipeline.
class Usage < ::ApplicationRecord
include PartitionedTable
include IgnorableColumns
PARTITION_DURATION = 30.days
self.table_name = 'p_catalog_resource_component_usages'
self.primary_key = :id
self.sequence_name = :p_catalog_resource_component_usages_id_seq
ignore_column :partition_id, remove_never: true
belongs_to :component, class_name: 'Ci::Catalog::Resources::Component', inverse_of: :usages
belongs_to :catalog_resource, class_name: 'Ci::Catalog::Resource', inverse_of: :component_usages
belongs_to :used_by_project, class_name: 'Project', inverse_of: :ci_component_usages
validates :component, :catalog_resource, :used_by_project, presence: true
validates :used_date, uniqueness: { scope: [:component_id, :used_by_project_id] }
scope :for_partition, ->(partition) { where(partition_id: partition) }
partitioned_by :partition_id, strategy: :sliding_list,
next_partition_if: ->(active_partition) do
oldest_record_in_partition = Ci::Catalog::Resources::Components::Usage
.select(:id, :used_date)
.for_partition(active_partition.value)
.order(:id)
.limit(1)
.take
oldest_record_in_partition.present? &&
oldest_record_in_partition.used_date <= (Date.today - PARTITION_DURATION)
end,
detach_partition_if: ->(partition) do
newest_record_in_partition = Ci::Catalog::Resources::Components::Usage
.select(:id, :used_date)
.for_partition(partition.value)
.order(id: :desc)
.limit(1)
.take
# TO DO: Duration to be shortened in https://gitlab.com/gitlab-org/gitlab/-/issues/443681
newest_record_in_partition.present? &&
newest_record_in_partition.used_date <= (Date.today - 365.days)
end
before_validation :set_used_date, unless: :used_date?
private
def set_used_date
self.used_date = Date.today
end
end
end
end
end
end
Loading