Skip to content

Commit

Permalink
cash cloud volumes in ChargebackVm
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Jun 14, 2018
1 parent 887cc81 commit ce3bb5d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions app/models/chargeback_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_') }
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/lib/miq_expression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,21 @@

# 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)
expect(report_fields).to include(volume_2_type_field_cost)

# 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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ce3bb5d

Please sign in to comment.