-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Foreman RH Cloud API Bindings to upload hits
Co-authored-by: Evgeni Golov <[email protected]> Co-authored-by: Jeremy Lenz <[email protected]>
- Loading branch information
1 parent
dc80828
commit ff0ba10
Showing
4 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module ForemanRhCloud | ||
class UploadHits | ||
def initialize(host:, uuid: nil, payload:) | ||
@host = host | ||
@uuid = uuid | ||
@payload = payload | ||
end | ||
|
||
def upload! | ||
ActiveRecord::Base.transaction do | ||
update_facets | ||
update_hits | ||
update_rules_and_resolutions | ||
update_details | ||
end | ||
end | ||
|
||
private | ||
|
||
def update_facets | ||
facet = InsightsFacet.find_or_create_by(host_id: @host.id) | ||
facet.update!(uuid: @uuid) if @uuid.present? | ||
@host.reload | ||
end | ||
|
||
def update_hits | ||
facet = @host.insights | ||
facet.hits.delete_all | ||
hits = @payload[:hits] | ||
# rubocop:disable Rails/SkipsModelValidations | ||
facet.hits.insert_all(hits) | ||
# rubocop:enable Rails/SkipsModelValidations | ||
InsightsFacet.reset_counters(facet.id, :hits_count) | ||
end | ||
|
||
def update_rules_and_resolutions | ||
# rubocop:disable Rails/SkipsModelValidations | ||
::InsightsRule.upsert_all(@payload[:rules], unique_by: :rule_id) | ||
rules = @payload[:rules].map { |rule| rule[:rule_id] } | ||
::InsightsResolution.where(rule_id: rules).delete_all | ||
::InsightsResolution.insert_all(@payload[:resolutions]) | ||
# rubocop:enable Rails/SkipsModelValidations | ||
end | ||
|
||
def update_details | ||
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]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
db/migrate/20241217190624_add_unique_index_to_rule_id_and_host_id_in_insights_hits.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddUniqueIndexToRuleIdAndHostIdInInsightsHits < ActiveRecord::Migration[7.0] | ||
def change | ||
add_index :insights_hits, [:rule_id, :host_id], unique: true, name: 'index_insight_hits_on_rule_id_and_host_id' | ||
end | ||
end |