Skip to content

Commit

Permalink
Fixes #20167 - speed up errata api
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsherrill committed Jul 11, 2017
1 parent 728016d commit 485dd17
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
5 changes: 3 additions & 2 deletions app/controllers/katello/api/v2/errata_controller.rb
Expand Up @@ -14,6 +14,7 @@ class Api::V2::ErrataController < Api::V2::ApiController
param :errata_restrict_installable, :bool, :desc => N_("show only errata with one or more installable hosts")
param_group :search, Api::V2::ApiController
def index
params[:errata_restrict_applicable] = false if ::Foreman::Cast.to_bool(params[:errata_restrict_installable])
super
end

Expand All @@ -39,11 +40,11 @@ def custom_index_relation(collection)
hosts = ::Host::Managed.authorized("view_hosts")
hosts = hosts.where(:organization_id => params[:organization_id]) if params[:organization_id]
if ::Foreman::Cast.to_bool(params[:errata_restrict_applicable])
collection = collection.applicable_to_hosts(hosts)
collection = collection.where(:id => Erratum.applicable_to_hosts(hosts))
end

if ::Foreman::Cast.to_bool(params[:errata_restrict_installable])
collection = collection.installable_for_hosts(hosts)
collection = collection.where(:id => Erratum.ids_installable_for_hosts(hosts))
end
collection
end
Expand Down
20 changes: 15 additions & 5 deletions app/models/katello/erratum.rb
Expand Up @@ -52,7 +52,7 @@ def self.applicable_to_hosts(hosts)
# which is calculated elsewhere.

self.joins(:content_facets).
where("#{Katello::Host::ContentFacet.table_name}.host_id" => hosts).uniq
where("#{Katello::Host::ContentFacet.table_name}.host_id" => hosts)
end

def self.applicable_to_hosts_dashboard(hosts)
Expand Down Expand Up @@ -89,21 +89,31 @@ def hosts_available(org_id = nil)
end

def self.installable_for_hosts(hosts = nil)
self.where(:id => ids_installable_for_hosts(hosts))
end

def self.ids_installable_for_hosts(hosts = nil)
hosts = ::Host.where(:id => hosts) if hosts && hosts.is_a?(Array)

# Main goal of this query
# 1) Get me the applicable errata for these set of hosts
# 2) Now further prune this list. Only include errata from repos that have been "enabled" on those hosts.
# In other words, prune the list to only include the errate in the "bound" repositories signified by
# the inner join between ContentFacetRepository and RepositoryErratum
query = self.
query = self.joins(:content_facet_errata).
joins("INNER JOIN #{Katello::ContentFacetRepository.table_name} on \
#{Katello::ContentFacetRepository.table_name}.content_facet_id = #{Katello::ContentFacetErratum.table_name}.content_facet_id").
joins("INNER JOIN #{Katello::RepositoryErratum.table_name} AS host_repo_errata ON \
host_repo_errata.erratum_id = #{Katello::Erratum.table_name}.id AND \
#{Katello::ContentFacetRepository.table_name}.repository_id = host_repo_errata.repository_id")
query = query.joins(:content_facet_errata) unless hosts

query = query.joins(:content_facets).where("#{Katello::Host::ContentFacet.table_name}.host_id" => hosts.map(&:id)) if hosts
query.uniq
if hosts
query = query.where("#{Katello::ContentFacetRepository.table_name}.content_facet_id" => hosts.joins(:content_facet))
else
query = query.joins(:content_facet_errata)
end

query
end

def update_from_json(json)
Expand Down
2 changes: 1 addition & 1 deletion app/models/katello/host/content_facet.rb
Expand Up @@ -125,7 +125,7 @@ def self.in_content_view_version_environments(version_environments)
end

def self.with_non_installable_errata(errata)
subquery = Katello::Erratum.select("#{Katello::Erratum.table_name}.id").installable_for_hosts
subquery = Katello::Erratum.select("#{Katello::Erratum.table_name}.id").ids_installable_for_hosts
.where("#{Katello::ContentFacetRepository.table_name}.content_facet_id = #{Katello::Host::ContentFacet.table_name}.id").to_sql
self.joins(:applicable_errata).where("#{Katello::Erratum.table_name}.id" => errata).where("#{Katello::Erratum.table_name}.id NOT IN (#{subquery})").uniq
end
Expand Down

0 comments on commit 485dd17

Please sign in to comment.