Skip to content

Commit

Permalink
Fixes #38039 - Fix ordering by compliance_[failed|passed|othered]
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren committed Dec 2, 2024
1 parent 507d29a commit a05eb50
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
3 changes: 1 addition & 2 deletions app/controllers/arf_reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions app/models/foreman_openscap/arf_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) }

Expand Down
24 changes: 24 additions & 0 deletions test/functional/api/v2/compliance/arf_reports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/functional/arf_reports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a05eb50

Please sign in to comment.