Skip to content

Commit

Permalink
Fixes #36976 - Too many audit records slow down CV loading (Katello#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
sjha4 authored Mar 6, 2024
1 parent 7fe287f commit 30c6977
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
64 changes: 23 additions & 41 deletions app/models/katello/content_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -781,45 +781,6 @@ def related_composite_cvs
content_views
end

def audited_cv_repositories_since_last_publish
audited_repositories = self.audits.filter do |a|
!a.audited_changes["repository_ids"].nil?
end
if latest_version_object
audited_repositories.filter! { |a| a.created_at > latest_version_object.created_at }
end
audited_repositories
end

def audited_cv_repository_changed
audited_repositories_changes = Audit.where(auditable_id: repositories, auditable_type: "Katello::Repository").filter do |a|
a.audited_changes["publication_href"].present? || a.audited_changes["version_href"].present?
end
if latest_version_object
audited_repositories_changes = audited_repositories_changes.filter { |a| a.created_at > latest_version_object.created_at }
end
audited_repositories_changes
end

def audited_cv_filters_changed
audited_filters = Audit.where(auditable_type: "Katello::ContentViewFilter",
associated_id: id,
associated_type: "Katello::ContentView")
if latest_version_object
audited_filters = audited_filters.filter { |a| a.created_at > latest_version_object.created_at }
end
audited_filters
end

def audited_cv_filter_rules_changed
audited_filter_rules = Audit.where(associated_id: id,
associated_type: "Katello::ContentView").where("auditable_type LIKE '%FilterRule%'")
if latest_version_object
audited_filter_rules = audited_filter_rules.filter { |a| a.created_at > latest_version_object.created_at }
end
audited_filter_rules
end

def composite_cv_components_changed?
return true unless latest_version_object
published_component_version_ids = latest_version_object.components.pluck(:id) || []
Expand Down Expand Up @@ -885,8 +846,29 @@ def needs_publish?
end

def audited_changes_present?
audited_cv_repositories_since_last_publish.present? || audited_cv_repository_changed.present? ||
audited_cv_filters_changed.present? || audited_cv_filter_rules_changed.present?
latest_version_created_at = latest_version_object.created_at
cv_repository_ids = repositories.pluck(:id)

audited_changes_like = ->(param) {
Arel.sql("#{Audit.table_name}.audited_changes ilike '%#{param}%'")
}

table = Audit.arel_table
repository_condition = table[:auditable_id].eq(id).and(audited_changes_like.call("repository_ids"))

cv_repository_condition = table[:auditable_id].in(cv_repository_ids)
.and(table[:auditable_type].eq('Katello::Repository'))
.and(Arel.sql("(#{audited_changes_like.call("publication_href")} OR #{audited_changes_like.call("version_href")})"))

content_view_filter_condition = table[:auditable_type].eq('Katello::ContentViewFilter').and(table[:associated_id].eq(id))

filter_rule_condition = table[:associated_id].eq(id).and(table[:auditable_type].matches('%FilterRule%'))

base_query = table[:created_at].gt(latest_version_created_at)

final_query = base_query.and(repository_condition.or(cv_repository_condition).or(content_view_filter_condition).or(filter_rule_condition))

Audit.where(final_query).exists?
end

def dependency_solving_changed?
Expand Down
1 change: 0 additions & 1 deletion app/views/katello/api/v2/content_views/base.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ attributes :import_only
attributes :generated_for
attributes :related_cv_count
attributes :related_composite_cvs
attributes :needs_publish? => :needs_publish
attributes :filtered? => :filtered

node :next_version do |content_view|
Expand Down
1 change: 1 addition & 0 deletions app/views/katello/api/v2/content_views/show.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ object @resource
extends "katello/api/v2/content_views/base"

attributes :content_host_count
attributes :needs_publish? => :needs_publish

node :errors do
unless @resource.valid?
Expand Down

0 comments on commit 30c6977

Please sign in to comment.