diff --git a/app/models/chargeback/consumption_history.rb b/app/models/chargeback/consumption_history.rb index edc526ef735..e370dee2cda 100644 --- a/app/models/chargeback/consumption_history.rb +++ b/app/models/chargeback/consumption_history.rb @@ -33,22 +33,13 @@ def self.for_report(cb_class, options) end def self.base_rollup_scope - base_rollup = MetricRollup.includes( - :resource => [:hardware, :tenant, :tags, :vim_performance_states, :custom_attributes, - {:container_image => :custom_attributes}], - :parent_host => :tags, - :parent_ems_cluster => :tags, - :parent_storage => :tags, - :parent_ems => :tags) - .select(*Metric::BASE_COLS).order('resource_id, timestamp') - - base_rollup.select(*ChargeableField.chargeable_cols_on_metric_rollup).with_resource + MetricRollup.select(*(Metric::BASE_COLS + ChargeableField.chargeable_cols_on_metric_rollup)) end private_class_method :base_rollup_scope def self.uniq_timestamp_record_map(report_scope, group_by_tenant = false) - main_select = MetricRollup.select(:id, :resource_id).arel.ast.to_sql + main_select = MetricRollup.select(:id, :resource_id).with_resource.arel.ast.to_sql .gsub("SELECT", "DISTINCT ON (resource_type, resource_id, timestamp)") .gsub(/ FROM.*$/, '') diff --git a/spec/models/chargeback_vm_spec.rb b/spec/models/chargeback_vm_spec.rb index 6cc790f8c93..5a94ad3165f 100644 --- a/spec/models/chargeback_vm_spec.rb +++ b/spec/models/chargeback_vm_spec.rb @@ -818,7 +818,7 @@ def result_row_for_vm(vm) let(:report_options) { Chargeback::ReportOptions.new } let(:timestamp_key) { 'Fri, 13 May 2016 10:40:00 UTC +00:00' } let(:beginning_of_day) { timestamp_key.in_time_zone.beginning_of_day } - let(:metric_rollup) { FactoryGirl.build(:metric_rollup_vm_hr, :timestamp => timestamp_key, :resource => @vm1) } + let(:metric_rollup) { FactoryGirl.create(:metric_rollup_vm_hr, :timestamp => timestamp_key, :resource => @vm1) } let(:consumption) { Chargeback::ConsumptionWithRollups.new([metric_rollup], nil, nil) } subject { described_class.report_row_key(consumption) } before do @@ -844,7 +844,7 @@ def result_row_for_vm(vm) context 'with parent ems' do let(:metric_rollup) do - FactoryGirl.build(:metric_rollup_vm_hr, :tag_names => 'environment/prod', + FactoryGirl.create(:metric_rollup_vm_hr, :tag_names => 'environment/prod', :parent_host_id => @host1.id, :parent_ems_cluster_id => @ems_cluster.id, :parent_ems_id => ems.id, :parent_storage_id => @storage.id, :resource => @vm1, :resource_name => @vm1.name) @@ -857,7 +857,7 @@ def result_row_for_vm(vm) context 'when parent ems is missing' do let(:metric_rollup) do - FactoryGirl.build(:metric_rollup_vm_hr, :tag_names => 'environment/prod', + FactoryGirl.create(:metric_rollup_vm_hr, :tag_names => 'environment/prod', :parent_host_id => @host1.id, :parent_ems_cluster_id => @ems_cluster.id, :parent_storage_id => @storage.id, :resource => @vm1, :resource_name => @vm1.name) @@ -887,7 +887,6 @@ def result_row_for_vm(vm) :parent_ems_id => ems.id, :parent_storage_id => @storage.id, :resource => @vm) end - let(:consumption) { Chargeback::ConsumptionWithRollups.new([metric_rollup], nil, nil) } before do @storage.tag_with([classification_1.tag.name, classification_2.tag.name], :ns => '*') @@ -899,9 +898,11 @@ def result_row_for_vm(vm) skip('this feature needs to be added to new chargeback') if Settings.new_chargeback [rate_assignment_options_1, rate_assignment_options_2].each do |rate_assignment| - metric_rollup.tag_names = rate_assignment[:tag].first.tag.send(:name_path) + metric_rollup.update_attributes!(:tag_names => rate_assignment[:tag].first.tag.send(:name_path)) @vm.tag_with(["/managed/#{metric_rollup.tag_names}"], :ns => '*') @vm.reload + + consumption = Chargeback::ConsumptionWithRollups.new([metric_rollup], nil, nil) uniq_rates = Chargeback::RatesCache.new.get(consumption) consumption.instance_variable_set(:@tag_names, nil) consumption.instance_variable_set(:@hash_features_affecting_rate, nil) @@ -935,10 +936,10 @@ def result_row_for_vm(vm) let(:metering_used_hours) { 24 } let(:hardware) do - FactoryGirl.build(:hardware, + FactoryGirl.create(:hardware, :cpu_total_cores => cores, :memory_mb => mem_mb, - :disks => [FactoryGirl.build(:disk, :size => disk_b)]) + :disks => [FactoryGirl.create(:disk, :size => disk_b)]) end let(:fixed_cost) { hourly_rate * 24 }