Sha256: dadd9269d189b8fb8ac0b97a27f0975c3337aa8cc76b26333861bb6787e2fcf1

Contents?: true

Size: 1.55 KB

Versions: 12

Compression:

Stored size: 1.55 KB

Contents

module InventorySync
  module Async
    class HostResult
      attr_reader :uuid_by_fqdn

      def initialize(result)
        @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']] }]
      end

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

      def touched
        @touched ||= []
      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 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

12 entries across 12 versions & 1 rubygems

Version Path
foreman_rh_cloud-3.0.28 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-5.0.28 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-4.0.27 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-3.0.26 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-4.0.26 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-4.0.25.1 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-3.0.25 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-4.0.25 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-3.0.24.1 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-4.0.24.1 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-3.0.24 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-4.0.24 lib/inventory_sync/async/host_result.rb