Class: ForemanResourceQuota::ResourceQuota
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- ForemanResourceQuota::ResourceQuota
show all
- Extended by:
- FriendlyId
- Includes:
- Authorizable, Exceptions, ResourceQuotaHelper, Parameterizable::ByIdName
- Defined in:
- app/models/foreman_resource_quota/resource_quota.rb
Constant Summary
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
#find_largest_unit, #natural_resource_name_by_type, #resource_value_to_string, #units_by_type, #utilization_from_resource_origins
Class Method Details
.permission_name ⇒ Object
29
30
31
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 29
def self.permission_name
'resource_quotas'
end
|
Instance Method Details
#active_resources ⇒ Object
144
145
146
147
148
149
150
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 144
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
129
130
131
132
133
134
135
136
137
138
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 129
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_hash ⇒ Object
103
104
105
106
107
108
109
110
111
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 103
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
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 58
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_hosts ⇒ Object
33
34
35
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 33
def number_of_hosts
hosts_resources.size
end
|
#number_of_missing_hosts ⇒ Object
45
46
47
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 45
def number_of_missing_hosts
missing_hosts.size
end
|
#number_of_usergroups ⇒ Object
41
42
43
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 41
def number_of_usergroups
usergroups.size
end
|
#number_of_users ⇒ Object
37
38
39
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 37
def number_of_users
users.size
end
|
#to_label ⇒ Object
140
141
142
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 140
def to_label
name
end
|
#update_hosts_resources(hosts_resources_hash) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 113
def update_hosts_resources(hosts_resources_hash)
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|
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'app/models/foreman_resource_quota/resource_quota.rb', line 81
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
|