diff --git a/app/controllers/arf_reports_controller.rb b/app/controllers/arf_reports_controller.rb index 32c4c57f..ef6a9c73 100644 --- a/app/controllers/arf_reports_controller.rb +++ b/app/controllers/arf_reports_controller.rb @@ -13,8 +13,7 @@ def index # Avoid using includes() with nested associations and "order by" together. Otherwise, # includes() will use join tables instead and Rails somehow create many objects and # high memory consumption. - @arf_reports_pg = resource_base.search_for(params[:search], :order => params[:order]) - .paginate(:page => params[:page], :per_page => params[:per_page]) + @arf_reports_pg = resource_base_search_and_page.load arf_report_ids = @arf_reports_pg.pluck(:id) @arf_reports = resource_base.includes(:policy, :openscap_proxy, :host => %i[policies last_report_object host_statuses]) .where(id: arf_report_ids) diff --git a/app/models/foreman_openscap/arf_report.rb b/app/models/foreman_openscap/arf_report.rb index 164586ca..cc33e7c9 100644 --- a/app/models/foreman_openscap/arf_report.rb +++ b/app/models/foreman_openscap/arf_report.rb @@ -53,11 +53,14 @@ class ArfReport < ::Report scope :failed, lambda { where("(#{report_status_column} >> #{bit_mask 'failed'}) > 0") } scope :not_failed, lambda { where("(#{report_status_column} >> #{bit_mask 'failed'}) = 0") } + scope :select_compliance_failed, lambda { select("reports.*, (#{report_status_column} >> #{bit_mask 'failed'}) as compliance_failed") } scope :othered, lambda { where("(#{report_status_column} >> #{bit_mask 'othered'}) > 0").merge(not_failed) } scope :not_othered, lambda { where("(#{report_status_column} >> #{bit_mask 'othered'}) = 0") } + scope :select_compliance_othered, lambda { select("reports.*, (#{report_status_column} >> #{bit_mask 'othered'}) as compliance_othered") } scope :passed, lambda { where("(#{report_status_column} >> #{bit_mask 'passed'}) > 0").merge(not_failed).merge(not_othered) } + scope :select_compliance_passed, lambda { select("reports.*, (#{report_status_column} >> #{bit_mask 'passed'}) as compliance_passed") } scope :by_rule_result, lambda { |rule_name, rule_result| joins(:sources).where(:sources => { :value => rule_name }, :logs => { :result => rule_result }) } diff --git a/test/functional/api/v2/compliance/arf_reports_controller_test.rb b/test/functional/api/v2/compliance/arf_reports_controller_test.rb index 11ac66b0..ee064d9f 100644 --- a/test/functional/api/v2/compliance/arf_reports_controller_test.rb +++ b/test/functional/api/v2/compliance/arf_reports_controller_test.rb @@ -436,6 +436,30 @@ class Api::V2::Compliance::ArfReportsControllerTest < ActionController::TestCase assert_equal report.id, response['results'].first["id"].to_i end + test "should order by compliance_[failed|passed|othered]" do + reports_cleanup + policy = FactoryBot.create(:policy) + create_arf_report_for_search({ "passed" => 4, "othered" => 0, "failed" => 1 }, policy) + create_arf_report_for_search({ "passed" => 1, "othered" => 0, "failed" => 0 }, policy) + create_arf_report_for_search({ "passed" => 15, "othered" => 9, "failed" => 0 }, policy) + create_arf_report_for_search({ "passed" => 2, "othered" => 3, "failed" => 7 }, policy) + + get :index, :params => { :order => "compliance_failed DESC" }, :session => set_session_user + assert_response :success + response = ActiveSupport::JSON.decode(@response.body) + assert_equal 7, response['results'].first['failed'] + + get :index, :params => { :order => "compliance_passed DESC" }, :session => set_session_user + assert_response :success + response = ActiveSupport::JSON.decode(@response.body) + assert_equal 15, response['results'].first['passed'] + + get :index, :params => { :order => "compliance_othered DESC" }, :session => set_session_user + assert_response :success + response = ActiveSupport::JSON.decode(@response.body) + assert_equal 9, response['results'].first['othered'] + end + private def reports_cleanup diff --git a/test/functional/arf_reports_controller_test.rb b/test/functional/arf_reports_controller_test.rb index 915ea79a..53f70ece 100644 --- a/test/functional/arf_reports_controller_test.rb +++ b/test/functional/arf_reports_controller_test.rb @@ -37,4 +37,13 @@ class ArfReportsControllerTest < ActionController::TestCase get :download_html, :params => { :id => arf_report.id }, :session => set_session_user assert_equal report_html, @response.body end + + test "should order arf reports by compliance_[failed|passed|othered]" do + get :index, params: { order: "compliance_failed DESC" }, :session => set_session_user + assert_response :success + get :index, params: { order: "compliance_passed DESC" }, :session => set_session_user + assert_response :success + get :index, params: { order: "compliance_othered DESC" }, :session => set_session_user + assert_response :success + end end