Module: ForemanResourceQuota::ResourceQuotaHelper
- Included in:
- HostManagedExtensions, ResourceQuota
- Defined in:
- app/helpers/foreman_resource_quota/resource_quota_helper.rb
Constant Summary collapse
- FACTOR_B_TO_KB =
1024
- FACTOR_B_TO_MB =
1024 * 1024
- FACTOR_B_TO_GB =
1024 * FACTOR_B_TO_MB
Instance Method Summary collapse
- #find_largest_unit(resource_value, units) ⇒ Object
- #natural_resource_name_by_type(resource_type) ⇒ Object
- #resource_value_to_string(resource_value, resource_type) ⇒ Object
- #units_by_type(resource_type) ⇒ Object
-
#utilization_from_resource_origins(resources, hosts, custom_resource_origins: nil) ⇒ Object
Use different resource origins to determine host resource utilization.
Instance Method Details
#find_largest_unit(resource_value, units) ⇒ Object
37 38 39 40 41 42 |
# File 'app/helpers/foreman_resource_quota/resource_quota_helper.rb', line 37 def find_largest_unit(resource_value, units) units.reverse_each do |unit| return unit.values_at(:symbol, :factor) if resource_value >= unit[:factor] end units[0].values_at(:symbol, :factor) end |
#natural_resource_name_by_type(resource_type) ⇒ Object
9 10 11 12 13 14 |
# File 'app/helpers/foreman_resource_quota/resource_quota_helper.rb', line 9 def natural_resource_name_by_type(resource_type) type_names = { cpu_cores: 'CPU cores', memory_mb: 'Memory', disk_gb: 'Disk space' } key = resource_type.is_a?(String) ? resource_type.to_sym : resource_type raise "No natural name for unknown resource type '#{resource_type}'" unless type_names.key?(key) type_names[key] end |
#resource_value_to_string(resource_value, resource_type) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'app/helpers/foreman_resource_quota/resource_quota_helper.rb', line 44 def resource_value_to_string(resource_value, resource_type) (symbol, factor) = find_largest_unit(resource_value, units_by_type(resource_type)) unit_applied_value = (resource_value / factor).round(1) format_text = if (unit_applied_value % 1).zero? '%.0f %s' else '%.1f %s' end format(format_text, unit_applied_value, symbol) end |
#units_by_type(resource_type) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/helpers/foreman_resource_quota/resource_quota_helper.rb', line 16 def units_by_type(resource_type) type_units = { cpu_cores: [ { symbol: 'cores', factor: 1 }, ], memory_mb: [ { symbol: 'MB', factor: 1 }, { symbol: 'GB', factor: FACTOR_B_TO_KB }, { symbol: 'TB', factor: FACTOR_B_TO_MB }, ], disk_gb: [ { symbol: 'GB', factor: 1 }, { symbol: 'TB', factor: FACTOR_B_TO_KB }, { symbol: 'PB', factor: FACTOR_B_TO_MB }, ], } key = resource_type.to_sym raise "No units for unknown resource type '#{resource_type}'" unless type_units.key?(key) type_units[key] end |
#utilization_from_resource_origins(resources, hosts, custom_resource_origins: nil) ⇒ Object
Use different resource origins to determine host resource utilization.
- iterates all given hosts and tries do determine their resources utilization
Returns:
[ <hosts_resources>, <missing_hosts_resources> ]
for example:
[
{ "host_a": { cpu_cores: 20, memory_mb: 8196 }, "host_b": { cpu_cores: 15, memory_mb: nil } },
{ "host_c": [ :memory_mb ] },
]
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/helpers/foreman_resource_quota/resource_quota_helper.rb', line 64 def utilization_from_resource_origins(resources, hosts, custom_resource_origins: nil) hosts_resources = create_hosts_resources_hash(hosts, resources) missing_hosts_resources = create_missing_hosts_resources_hash(hosts, resources) hosts_hash = hosts.index_by(&:name) resource_classes = custom_resource_origins || default_resource_origin_classes resource_classes.each do |origin_class| origin_class.new.collect_resources!( hosts_resources, missing_hosts_resources, hosts_hash ) end [hosts_resources, missing_hosts_resources] end |