Skip to content

Commit

Permalink
Merge pull request #17277 from lpichler/get_allocatted_values_also_fo…
Browse files Browse the repository at this point in the history
…r_metrics

Get allocated values of sub metrics for cloud volumes in chargeback without rollups
(cherry picked from commit 4c02603)

https://bugzilla.redhat.com/show_bug.cgi?id=1578124
  • Loading branch information
gtanzillo authored and simaishi committed May 14, 2018
1 parent 7ca3a30 commit 53a3e9e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/models/chargeable_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def measure_metering(consumption, options, sub_metric = nil)
def measure(consumption, options, sub_metric = nil)
return consumption.consumed_hours_in_interval if metering?
return 1.0 if fixed?
return 0 if options.method_for_allocated_metrics != :current_value && consumption.none?(metric)
return 0 if options.method_for_allocated_metrics != :current_value && consumption.none?(metric, sub_metric)
return consumption.send(options.method_for_allocated_metrics, metric, sub_metric) if allocated?
return consumption.avg(metric) if used?
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/chargeback/consumption_with_rollups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def current_value(metric, _sub_metric) # used for containers allocated metrics
end
end

def none?(metric)
values(metric).empty?
def none?(metric, sub_metric)
values(metric, sub_metric).empty?
end

def chargeback_fields_present
Expand Down
28 changes: 16 additions & 12 deletions app/models/chargeback/consumption_without_rollups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def parents_determining_rate
MiqEnterprise.my_enterprise].compact
end

def none?(metric)
current_value(metric).nil?
def none?(metric, sub_metric = nil)
current_value(metric, sub_metric).nil?
end

def chargeback_fields_present
Expand All @@ -45,18 +45,22 @@ def metering_used_fields_present
0 # we don't count used hours in metering report
end

def current_value(metric, _sub_metric = nil)
def current_value(metric, sub_metric = nil)
# Return the last seen allocation for charging purposes.
@value ||= {}
@value[metric] ||= case metric
when 'derived_vm_numvcpus' # Allocated CPU count
resource.hardware.try(:cpu_total_cores)
when 'derived_memory_available'
resource.hardware.try(:memory_mb)
when 'derived_vm_allocated_disk_storage'
resource.allocated_disk_storage
end
@value[metric]
metric_key = "#{metric}#{sub_metric}"
@value[metric_key] ||= case metric
when 'derived_vm_numvcpus' # Allocated CPU count
resource.hardware.try(:cpu_total_cores)
when 'derived_memory_available'
resource.hardware.try(:memory_mb)
when 'derived_vm_allocated_disk_storage'
if sub_metric.present?
resource.cloud_volumes.where(:volume_type => sub_metric).sum(:size) || 0
else
resource.allocated_disk_storage
end
end
end
alias avg current_value
alias max current_value
Expand Down
43 changes: 43 additions & 0 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,49 @@
fields = described_class.attribute_names
expect(fields).not_to include(cloud_volume_hdd_field)
end

context 'without including metrics' do
let(:ssd_volume_type) { 'ssd' }
let(:ssd_size_1) { 1_234 }
let!(:cloud_volume_1) { FactoryGirl.create(:cloud_volume_openstack, :volume_type => ssd_volume_type, :size => ssd_size_1) }

let(:ssd_disk_1) { FactoryGirl.create(:disk, :size => ssd_size_1, :backing => cloud_volume_1) }

let(:ssd_size_2) { 4_234 }
let!(:cloud_volume_2) { FactoryGirl.create(:cloud_volume_openstack, :volume_type => ssd_volume_type, :size => ssd_size_2) }

let(:ssd_disk_2) { FactoryGirl.create(:disk, :size => ssd_size_2, :backing => cloud_volume_2) }

let(:hardware) { FactoryGirl.create(:hardware, :disks => [ssd_disk_1, ssd_disk_2]) }

let(:resource) { FactoryGirl.create(:vm_vmware_cloud, :hardware => hardware, :created_on => month_beginning) }

let(:storage_chargeback_rate) { FactoryGirl.create(:chargeback_rate, :detail_params => detail_params, :rate_type => "Storage") }

let(:parent_classification) { FactoryGirl.create(:classification) }
let(:classification) { FactoryGirl.create(:classification, :parent_id => parent_classification.id) }

let(:rate_assignment_options) { {:cb_rate => storage_chargeback_rate, :object => MiqEnterprise.first } }
let(:options) { base_options.merge(:interval => 'daily', :tag => nil, :entity_id => resource.id, :include_metrics => false) }

before do
# create rate detail for cloud volume
allocated_storage_rate_detail = storage_chargeback_rate.chargeback_rate_details.detect { |x| x.chargeable_field.metric == 'derived_vm_allocated_disk_storage' }
new_rate_detail = allocated_storage_rate_detail.dup
new_rate_detail.sub_metric = ssd_volume_type
new_rate_detail.chargeback_tiers = allocated_storage_rate_detail.chargeback_tiers.map(&:dup)
new_rate_detail.save
storage_chargeback_rate.chargeback_rate_details << new_rate_detail
storage_chargeback_rate.save

ChargebackRate.set_assignments(:storage, [rate_assignment_options])
end

it 'reports sub metric and costs' do
skip('this case needs to be fixed in new chargeback') if Settings.new_chargeback
expect(subject.storage_allocated_ssd_metric).to eq(ssd_size_1 + ssd_size_2)
end
end
end

subject { ChargebackVm.build_results_for_report_ChargebackVm(options).first.first }
Expand Down

0 comments on commit 53a3e9e

Please sign in to comment.