Sha256: d669629e9988859fcabf9afbbaa5ba83e078b4bbe1dd94831bac3d19eba51430

Contents?: true

Size: 1.68 KB

Versions: 23

Compression:

Stored size: 1.68 KB

Contents

module InventorySync
  module Async
    class HostResult
      attr_reader :uuid_by_fqdn, :organization

      def initialize(result, organization)
        @organization = organization
        @total = result['total']
        @count = result['count']
        @page = result['page']
        @per_page = result['per_page']
        @sub_ids = result["results"].map { |host| host['subscription_manager_id'] }
        @uuid_by_sub_id = Hash[result["results"].map { |host| [host['subscription_manager_id'], host['id']] }]
        @uuid_by_fqdn = Hash[result["results"].map { |host| [host['fqdn']&.downcase, host['id']] }]
        @results = result["results"]
      end

      def status_hashes
        @sub_ids.map do |sub_id|
          host_id = host_id(sub_id)
          if host_id
            {
              host_id: host_id,
              status: InventorySync::InventoryStatus::SYNC,
              reported_at: DateTime.current,
              type: InventorySync::InventoryStatus.name,
            }
          end
        end.compact
      end

      def host_id(sub_id)
        hosts[sub_id]
      end

      def hosts
        @hosts ||= Hash[
          Katello::Host::SubscriptionFacet.where(uuid: @sub_ids).pluck(:uuid, :host_id)
        ]
      end

      def host_uuids
        @host_uuids ||= Hash[@sub_ids.map { |sub_id| [host_id(sub_id), @uuid_by_sub_id[sub_id]] }].except(nil)
      end

      def missing_hosts
        @results.select { |host| hosts[host['subscription_manager_id']].nil? }
      end

      def percentage
        ratio = @per_page * @page * 1.0 / @total * 100
        ratio > 100 ? 100 : ratio.truncate(2)
      end

      def last?
        @total <= @per_page * @page
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
foreman_rh_cloud-11.1.0 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-10.0.3 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-11.0.3 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-9.0.59 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-11.0.2 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-11.0.1 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-11.0.0 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-9.0.58 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-10.0.2 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-9.0.57 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-10.0.1 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-9.0.56 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-9.0.55 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-9.0.54 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-9.0.53 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-9.0.52 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-8.0.52 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-9.0.51 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-8.0.51 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-8.0.50 lib/inventory_sync/async/host_result.rb