diff --git a/app/models/chargeback_vm.rb b/app/models/chargeback_vm.rb index c883b70f8b8..cb55cb7b3dd 100644 --- a/app/models/chargeback_vm.rb +++ b/app/models/chargeback_vm.rb @@ -41,6 +41,12 @@ class ChargebackVm < Chargeback storage_allocated_cost ).freeze + cache_with_timeout(:current_volume_types) do + volume_types = CloudVolume.volume_types + volume_types.push(nil) if volume_types.present? + volume_types + end + def self.attribute_names loaded_attribute_names = super loaded_storage_allocated_attributes = loaded_attribute_names.select { |x| x.starts_with?('storage_allocated_') } @@ -57,9 +63,7 @@ def self.attribute_names # 'storage_allocated_volume_type1_cost' => {:group => [:total]}, # } def self.dynamic_columns_for(column_type) - volume_types = CloudVolume.volume_types - volume_types.push(nil) if volume_types.present? - volume_types.each_with_object({}) do |volume_type, result| + current_volume_types.each_with_object({}) do |volume_type, result| %i(metric cost rate).collect do |type| result["storage_allocated_#{volume_type || 'unclassified'}_#{type}"] = column_type end @@ -74,6 +78,9 @@ def self.refresh_dynamic_metric_columns def self.build_results_for_report_ChargebackVm(options) # Options: a hash transformable to Chargeback::ReportOptions + # Get the most up to date types from the DB + current_volume_types(true) + @report_user = User.find_by(:userid => options[:userid]) @vm_owners = @vms = nil diff --git a/spec/lib/miq_expression_spec.rb b/spec/lib/miq_expression_spec.rb index e6ff599b01c..6f6075ab433 100644 --- a/spec/lib/miq_expression_spec.rb +++ b/spec/lib/miq_expression_spec.rb @@ -50,11 +50,13 @@ # case: change name volume_2.update_attributes!(:volume_type => 'NEW_TYPE_2') + ChargebackVm.current_volume_types_clear_cache report_fields = described_class.reporting_available_fields(model).map(&:second) expect(report_fields).to include(volume_1_type_field_cost) expect(report_fields).not_to include(volume_2_type_field_cost) # old field # check existence of new name + ChargebackVm.current_volume_types_clear_cache report_fields = described_class.reporting_available_fields(model).map(&:second) volume_2_type_field_cost = "#{model}-storage_allocated_#{volume_2.volume_type}_cost" expect(report_fields).to include(volume_1_type_field_cost) @@ -62,6 +64,7 @@ # case: add volume_type volume_3 + ChargebackVm.current_volume_types_clear_cache report_fields = described_class.reporting_available_fields(model).map(&:second) expect(report_fields).to include(volume_1_type_field_cost) expect(report_fields).to include(volume_3_type_field_cost) @@ -70,6 +73,7 @@ volume_2.destroy volume_3.destroy + ChargebackVm.current_volume_types_clear_cache report_fields = described_class.reporting_available_fields(model).map(&:second) expect(report_fields).to include(volume_1_type_field_cost) expect(report_fields).not_to include(volume_2_type_field_cost) diff --git a/spec/models/chargeback_vm_spec.rb b/spec/models/chargeback_vm_spec.rb index e3f81f5ffec..e0776be0b8f 100644 --- a/spec/models/chargeback_vm_spec.rb +++ b/spec/models/chargeback_vm_spec.rb @@ -202,6 +202,7 @@ cloud_volume_hdd.destroy + described_class.current_volume_types_clear_cache described_class.refresh_dynamic_metric_columns fields = described_class.attribute_names expect(fields).not_to include(cloud_volume_hdd_field)