Class: InventorySync::Async::HostResult

Inherits:
Object
  • Object
show all
Defined in:
lib/inventory_sync/async/host_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, organization) ⇒ HostResult

Returns a new instance of HostResult.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/inventory_sync/async/host_result.rb', line 6

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

Instance Attribute Details

#organizationObject (readonly)

Returns the value of attribute organization.



4
5
6
# File 'lib/inventory_sync/async/host_result.rb', line 4

def organization
  @organization
end

#uuid_by_fqdnObject (readonly)

Returns the value of attribute uuid_by_fqdn.



4
5
6
# File 'lib/inventory_sync/async/host_result.rb', line 4

def uuid_by_fqdn
  @uuid_by_fqdn
end

Instance Method Details

#host_id(sub_id) ⇒ Object



32
33
34
# File 'lib/inventory_sync/async/host_result.rb', line 32

def host_id(sub_id)
  hosts[sub_id]
end

#host_uuidsObject



42
43
44
# File 'lib/inventory_sync/async/host_result.rb', line 42

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

#hostsObject



36
37
38
39
40
# File 'lib/inventory_sync/async/host_result.rb', line 36

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

#last?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/inventory_sync/async/host_result.rb', line 55

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

#missing_hostsObject



46
47
48
# File 'lib/inventory_sync/async/host_result.rb', line 46

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

#percentageObject



50
51
52
53
# File 'lib/inventory_sync/async/host_result.rb', line 50

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

#status_hashesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/inventory_sync/async/host_result.rb', line 18

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