Skip to content

Commit

Permalink
Merge pull request #942 from parthaa/empty-params
Browse files Browse the repository at this point in the history
Hits uploaded doesn't fail if system has no hits
  • Loading branch information
parthaa authored Jan 22, 2025
2 parents 21aae16 + 9a34804 commit 4cec882
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/services/foreman_rh_cloud/hits_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,25 @@ def update_hits
facet.hits.delete_all
hits = @payload[:hits]
# rubocop:disable Rails/SkipsModelValidations
facet.hits.insert_all(hits)
facet.hits.insert_all(hits) if hits.present?
# rubocop:enable Rails/SkipsModelValidations
InsightsFacet.reset_counters(facet.id, :hits_count)
end

def update_rules_and_resolutions
return if @payload[:rules].blank?
# rubocop:disable Rails/SkipsModelValidations
::InsightsRule.upsert_all(@payload[:rules], unique_by: :rule_id)
rules = @payload[:rules].map { |rule| rule[:rule_id] }

return if @payload[:resolutions].blank?
::InsightsResolution.where(rule_id: rules).delete_all
::InsightsResolution.insert_all(@payload[:resolutions])
# rubocop:enable Rails/SkipsModelValidations
end

def update_details
return if @payload[:details].blank?
fact_name = FactName.where(name: "insights::hit_details", short_name: 'insights_details').first_or_create
fact_value = @host.fact_values.where(fact_name: fact_name).first_or_create
fact_value.update(value: @payload[:details])
Expand Down
21 changes: 21 additions & 0 deletions test/unit/services/foreman_rh_cloud/hits_uploader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,25 @@ class HitsUploaderTest < ActiveSupport::TestCase
refute_nil fact_value
assert_equal PAYLOAD[:details], fact_value.value
end

test "empty payload" do
payload = {
resolutions: [],
rules: [],
hits: [],
details: "",
}

@host = FactoryBot.create(:host)
@uuid = SecureRandom.uuid
uploader = ForemanRhCloud::HitsUploader.new(host: @host, payload: payload, uuid: @uuid)
uploader.upload!
@host.reload
assert_equal @uuid, @host.insights_facet.uuid
assert_empty @host.insights_facet.hits
assert_equal 0, @host.insights_facet.hits_count
fact_name = FactName.where(name: "insights::hit_details").first
fact_value = @host.fact_values.where(fact_name: fact_name).first
assert_nil fact_value
end
end

0 comments on commit 4cec882

Please sign in to comment.