Sha256: 28ddb8907b0c8829a5a1c61871230e790d6dc4223dbf6e569ddc5441dddc8580

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

Stored size: 1.06 KB

Contents

module InventorySync
  module Async
    class HostResult
      def initialize(result)
        @total = result['total']
        @count = result['count']
        @page = result['page']
        @per_page = result['per_page']
        @fqdns = result["results"].map { |host| host['fqdn'] }
      end

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

      def touched
        @touched ||= []
      end

      def host_id(fqdn)
        hosts[fqdn]
      end

      def hosts
        @hosts ||= Hash[
          Host.where(name: @fqdns).pluck(:name, :id)
        ]
      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

10 entries across 10 versions & 1 rubygems

Version Path
foreman_rh_cloud-1.0.18 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-1.0.17 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-1.0.16 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-1.0.15 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-1.0.14 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-1.0.13.1 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-1.0.13 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-1.0.12 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-1.0.11 lib/inventory_sync/async/host_result.rb
foreman_rh_cloud-1.0.10 lib/inventory_sync/async/host_result.rb