Skip to content

Commit

Permalink
Merge pull request #17552 from lpichler/get_rid_off_big_query
Browse files Browse the repository at this point in the history
Get rid off query "All in One" in Chargeback
  • Loading branch information
gtanzillo authored Jun 14, 2018
2 parents 887cc81 + e5b6698 commit 019d2ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
13 changes: 2 additions & 11 deletions app/models/chargeback/consumption_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,13 @@ def self.for_report(cb_class, options, region)
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.*$/, '')

Expand Down
17 changes: 10 additions & 7 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,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
Expand All @@ -879,7 +879,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)
Expand All @@ -892,7 +892,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)
Expand Down Expand Up @@ -922,7 +922,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 => '*')
Expand All @@ -934,9 +933,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)
Expand Down Expand Up @@ -1039,6 +1040,8 @@ def find_result_by_vm_name_and_region(chargeback_result, vm_name, region)
subject! { ChargebackVm.build_results_for_report_ChargebackVm(options_tenant).first }

it "report from all regions and only for tenant_1" do
skip('this feature needs to be added to new chargeback rating') if Settings.new_chargeback

# report only VMs from tenant 1
vm_ids = subject.map(&:vm_id)
vm_ids_from_tenant = [tenant_1, tenant_1_region_1].map { |t| t.subtree.map(&:vms).map(&:ids) }.flatten
Expand Down Expand Up @@ -1080,10 +1083,10 @@ def find_result_by_vm_name_and_region(chargeback_result, vm_name, region)
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 }
Expand Down

0 comments on commit 019d2ad

Please sign in to comment.