Class: ForemanResourceQuota::ResourceQuota

Inherits:
ApplicationRecord
  • Object
show all
Extended by:
FriendlyId
Includes:
Authorizable, Exceptions, ResourceQuotaHelper, Parameterizable::ByIdName
Defined in:
app/models/foreman_resource_quota/resource_quota.rb

Constant Summary

Constants included from ResourceQuotaHelper

ForemanResourceQuota::ResourceQuotaHelper::FACTOR_B_TO_GB, ForemanResourceQuota::ResourceQuotaHelper::FACTOR_B_TO_KB, ForemanResourceQuota::ResourceQuotaHelper::FACTOR_B_TO_MB

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResourceQuotaHelper

#find_largest_unit, #natural_resource_name_by_type, #resource_value_to_string, #units_by_type, #utilization_from_resource_origins

Class Method Details

.permission_nameObject



38
39
40
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 38

def self.permission_name
  'resource_quotas'
end

.unassignedObject



34
35
36
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 34

def self.unassigned
  find_by(unassigned: true)
end

Instance Method Details

#active_resourcesObject



157
158
159
160
161
162
163
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 157

def active_resources
  resources = []
  %i[cpu_cores memory_mb disk_gb].each do |res|
    resources << res unless self[res].nil?
  end
  resources
end

#determine_utilization(additional_hosts = []) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 142

def determine_utilization(additional_hosts = [])
  quota_hosts = (hosts | (additional_hosts))
  all_host_resources, missing_hosts_resources = call_utilization_helper(quota_hosts)
  update_hosts_resources(all_host_resources)

  Rails.logger.warn create_hosts_resources_warning(missing_hosts_resources) unless missing_hosts.empty?
rescue StandardError => e
  Rails.logger.error("An error occured while determining resources for quota '#{name}': #{e}")
  raise e
end

#hosts_resources_as_hashObject



116
117
118
119
120
121
122
123
124
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 116

def hosts_resources_as_hash
  resources_hash = hosts.map(&:name).index_with { {} }
  hosts_resources.each do |host_resources_item|
    active_resources do |resource_name|
      resources_hash[host_resources_item.host.name][resource_name] = host_resources_item.send(resource_name)
    end
  end
  resources_hash
end

#missing_hosts(exclude: []) ⇒ Object

Returns a Hash with host name as key and a list of missing resources as value

{ <host name>: [<list of missing resources>] }
for example:
{
  "host_a": [ :cpu_cores, :disk_gb ],
  "host_b": [ :memory_mb ],
}

Parameters:

- exclude: an Array of host names to exclude from the utilization


71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 71

def missing_hosts(exclude: [])
  missing_hosts = {}
  active_resources.each do |single_resource|
    hosts_resources.where(single_resource => nil).includes([:host]).find_each do |host_resources_item|
      host_name = host_resources_item.host.name
      next if exclude.include?(host_name)
      missing_hosts[host_name] ||= []
      missing_hosts[host_name] << single_resource
    end
  end
  missing_hosts
end

#number_of_hostsObject



46
47
48
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 46

def number_of_hosts
  hosts_resources.size
end

#number_of_missing_hostsObject



58
59
60
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 58

def number_of_missing_hosts
  missing_hosts.size
end

#number_of_usergroupsObject



54
55
56
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 54

def number_of_usergroups
  usergroups.size
end

#number_of_usersObject



50
51
52
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 50

def number_of_users
  users.size
end

#to_labelObject



153
154
155
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 153

def to_label
  name
end

#unassigned?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 42

def unassigned?
  unassigned
end

#update_hosts_resources(hosts_resources_hash) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 126

def update_hosts_resources(hosts_resources_hash)
  # Only update hosts that are associated with this quota
  update_hosts = hosts.where(name: hosts_resources_hash.keys)
  update_hosts_ids = update_hosts.pluck(:name, :id).to_h
  hosts_resources_hash.each do |host_name, resources|
    # Update the host_resources without loading the whole host object
    host_resources_item = hosts_resources.find_by(host_id: update_hosts_ids[host_name])
    if host_resources_item
      host_resources_item.resources = resources
      host_resources_item.save
    else
      Rails.logger.warn "HostResources not found for host_name: #{host_name}"
    end
  end
end

#utilization(exclude: []) ⇒ Object

Returns a Hash with the quota resources and their utilization as key-value pair It returns always all resources, even if they are not used (nil in that case). For example:

{
  cpu_cores: 10,
  memory_mb: nil,
  disk_gb: 20,
}

Parameters:

- exclude: an Array of host names to exclude from the utilization


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 94

def utilization(exclude: [])
  current_utilization = {
    cpu_cores: nil,
    memory_mb: nil,
    disk_gb: nil,
  }

  active_resources.each do |resource|
    current_utilization[resource] = 0
  end

  hosts_resources.each do |host_resources_item|
    next if exclude.include?(host_resources_item.host.name)

    active_resources.each do |resource|
      current_utilization[resource] += host_resources_item.send(resource).to_i
    end
  end

  current_utilization
end