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
27
28
29
30
31
32
33
# File 'lib/foreman_inventory_upload/async/create_missing_insights_facets.rb', line 8

def run
  organization = ::Organization.find(input[:organization_id])
  # Find hosts with subscription facets but without insights facets
  # Note: We can't use scoped_search 'null? insights_uuid' because the null? operator
  # doesn't work with ext_methods - it would check hosts.id IS NULL instead of the facet
  hosts_without_facets = ::ForemanInventoryUpload::Generators::Queries.for_org(organization, use_batches: false)
                                                                      .left_outer_joins(:insights)
                                                                      .where(insights_facets: { id: nil })

  facet_count = 0
  hosts_without_facets.in_batches(of: ForemanInventoryUpload.slice_size) 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
    facet_count += facets.size
  end
  output[:result] = facet_count.zero? ? _("There were no missing Insights facets") : format(_("Missing Insights facets created: %s"), facet_count)
  Rails.logger.debug output[:result]
end