From 573a4410ed2d8727ab07c51b402c74166caaf966 Mon Sep 17 00:00:00 2001 From: Ian Ballou Date: Mon, 23 Dec 2024 15:46:50 +0000 Subject: [PATCH] Refs #38072 - add sorting support to bootc_image api --- .../api/v2/host_bootc_images_controller.rb | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/app/controllers/katello/api/v2/host_bootc_images_controller.rb b/app/controllers/katello/api/v2/host_bootc_images_controller.rb index a271fe90dd7..8685c588cc9 100644 --- a/app/controllers/katello/api/v2/host_bootc_images_controller.rb +++ b/app/controllers/katello/api/v2/host_bootc_images_controller.rb @@ -8,15 +8,21 @@ class Api::V2::HostBootcImagesController < Api::V2::ApiController end api :GET, "/hosts/bootc_images", N_("List booted bootc container images for hosts") - param :page, :number, :desc => N_("Page number, starting at 1") - param :per_page, :number, :desc => N_("Number of results per page to return") + param_group :search, Api::V2::ApiController def bootc_images - bootc_image_map = bootc_host_image_map(params[:search]) - - page = params[:page].present? ? params[:page].to_i : 1 + params[:sort_by] ||= 'bootc_booted_image' + params[:sort_order] ||= 'asc' + if params[:order] + (params[:sort_by], params[:sort_order]) = params[:order].split(' ') + else + params[:order] = "#{params[:sort_by]} #{params[:sort_order]}" + end per_page = params[:per_page].present? ? params[:per_page].to_i : Setting[:entries_per_page] + page = params[:page].present? ? params[:page].to_i : 1 + + bootc_image_map = bootc_host_image_map paged_images = bootc_image_map.to_a.paginate(page: page, per_page: per_page) - results = paged_images.collect { |image| { image_name: image[0], digests: image[1] } } + results = paged_images.collect { |image| { bootc_booted_image: image[0], digests: image[1] } } render json: { total: bootc_image_map.size, page: page, per_page: per_page, subtotal: bootc_image_map.size, results: results} end @@ -32,12 +38,11 @@ def resource_class ::Host::Managed end - def bootc_host_image_map(host_search) - # TODO: can this be optimized such that it doesn't require two queries? - content_facets = ::Katello::Host::ContentFacet.where(host_id: ::Host::Managed.joins(:content_facet).search_for(host_search).pluck(:id)) + def bootc_host_image_map + content_facets = ::Katello::Host::ContentFacet.where(host_id: ::Host::Managed.joins(:content_facet).search_for(*search_options).pluck(:id)) aggregate_bootc_data = content_facets.where.not(bootc_booted_image: nil, bootc_booted_digest: nil). select(:bootc_booted_image, :bootc_booted_digest, 'COUNT(hosts.id) as host_count'). - joins(:host).group(:bootc_booted_image, :bootc_booted_digest).order(:bootc_booted_image) + joins(:host).group(:bootc_booted_image, :bootc_booted_digest).order(params[:order]) bootc_image_map = Hash.new { |h, k| h[k] = [] } aggregate_bootc_data.each do |host_image| bootc_image_map[host_image.bootc_booted_image] << { bootc_booted_digest: host_image.bootc_booted_digest, host_count: host_image.host_count.to_i }