Class: ForemanInventoryUpload::Async::CreateMissingInsightsFacets

Inherits:
Actions::EntryAction
  • Object
show all
Defined in:
lib/foreman_inventory_upload/async/create_missing_insights_facets.rb

Instance Method Summary collapse

Instance Method Details

#plan(organization_id) ⇒ Object



4
5
6
# File 'lib/foreman_inventory_upload/async/create_missing_insights_facets.rb', line 4

def plan(organization_id)
  plan_self(organization_id: organization_id)
end

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/foreman_inventory_upload/async/create_missing_insights_facets.rb', line 8

def run
  organization = ::Organization.find(input[:organization_id])
  hosts_without_facets = ::ForemanInventoryUpload::Generators::Queries.for_org(organization, hosts_query: 'null? insights_uuid')
  facet_count = hosts_without_facets.count
  hosts_without_facets.each do |batch|
    facets = batch.pluck(:id, 'katello_subscription_facets.uuid').map do |host_id, uuid|
      {
        host_id: host_id,
        uuid: uuid,
      }
    end
    # We don't need to validate the facets here as we create the necessary fields.
    # rubocop:disable Rails/SkipsModelValidations
    InsightsFacet.upsert_all(facets, unique_by: :host_id) unless facets.empty?
    # rubocop:enable Rails/SkipsModelValidations
  end
  output[:result] = facet_count.zero? ? _("There were no missing Insights facets") : format(_("Missing Insights facets created: %s"), facet_count)
  Rails.logger.info output[:result]
end