Skip to content

Commit

Permalink
Calculation for grouping rollups when max method is used
Browse files Browse the repository at this point in the history
We have consumption blocks with set of metric rollups.
This set belongs to the tenant. When we do max on this set
we will get just max and it doesn't get max value for each VM.
It should take max value for each VM and then sum these
maxes.
Example: (VM1 and VM1 are in one tenant TI)
VM1: 2 Allocated Cores (Max)
VM2: 3 Allocated Cores (Max)
Report grouped  bytenant:
Tenant: 5 Allocated Cores (2 + 3 ) - is sum of maxes

And costs are derived from this values.

For avg: there is no such issue:
VM1: 2 Allocated Cores (Avg) (Cores 3 MetricRollups: 1, 2, 3)
VM2: 3 Allocated Cores (Avg)(Cores in 3 MetricRollups:2, 3, 4)
Report grouped  bytenant:
Tenant: 5 Allocated Cores (2.5)

(3 + 2) / 2 = 2.5
(1 + 2 + 3  + 2 + 3 + 4) = 2,5
It is same, AVG method don't have such issue.
  • Loading branch information
lpichler committed Feb 15, 2018
1 parent 76393c9 commit 71fea20
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
7 changes: 7 additions & 0 deletions app/models/chargeback/consumption_with_rollups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ def max(metric, sub_metric = nil)
values.present? ? values.max : 0
end

def sum_of_maxes_from_grouped_values(metric, sub_metric = nil)
return max(metric, sub_metric) if sub_metric
@grouped_values ||= {}
grouped_rollups = @rollups.group_by { |x| x.resource.id }
@grouped_values[metric] ||= grouped_rollups.map { |_, rollups| rollups.collect(&metric.to_sym).compact.max }.compact.sum
end

def avg(metric, sub_metric = nil)
metric_sum = values(metric, sub_metric).sum
metric_sum / consumed_hours_in_interval
Expand Down
1 change: 1 addition & 0 deletions app/models/chargeback/consumption_without_rollups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def current_value(metric, _sub_metric = nil)
end
alias avg current_value
alias max current_value
alias sum_of_maxes_from_grouped_values current_value
private :current_value
end
end
1 change: 1 addition & 0 deletions app/models/chargeback/report_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def method_for_allocated_metrics
raise "Invalid method for allocated calculations #{method}"
end

return :sum_of_maxes_from_grouped_values if method == :max && group_by_tenant?
method
end

Expand Down
12 changes: 6 additions & 6 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def subject_row_for_tenant(tenant)
expect(subject_row_for_tenant(tenant_2).cpu_used_cost).to eq(2 * 50 * hourly_usage * monthly_used_rate)

expect(subject_row_for_tenant(tenant_3).cpu_used_metric).to eq(2 * 50 * hourly_usage)
expect(subject_row_for_tenant(tenant_3).cpu_used_cost).to eq( 2 * 50 * hourly_usage * monthly_used_rate)
expect(subject_row_for_tenant(tenant_3).cpu_used_cost).to eq(2 * 50 * hourly_usage * monthly_used_rate)

expect(subject_row_for_tenant(tenant_4).cpu_used_metric).to eq(2 * 50 * hourly_usage)
expect(subject_row_for_tenant(tenant_4).cpu_used_cost).to eq(2 * 50 * hourly_usage * monthly_used_rate)
Expand Down Expand Up @@ -502,7 +502,7 @@ def subject_row_for_tenant(tenant)
expect(subject_row_for_tenant(tenant_2).cpu_used_cost).to eq(hourly_usage * 2 * 50 * monthly_used_rate)

expect(subject_row_for_tenant(tenant_3).cpu_used_metric).to eq(hourly_usage * 2 * 50)
expect(subject_row_for_tenant(tenant_3).cpu_used_cost).to eq(hourly_usage * 2 * 50* monthly_used_rate)
expect(subject_row_for_tenant(tenant_3).cpu_used_cost).to eq(hourly_usage * 2 * 50 * monthly_used_rate)

expect(subject_row_for_tenant(tenant_4).cpu_used_metric).to eq(hourly_usage * 2 * 50)
expect(subject_row_for_tenant(tenant_4).cpu_used_cost).to eq(hourly_usage * 2 * 50 * monthly_used_rate)
Expand Down Expand Up @@ -540,11 +540,11 @@ def result_row_for_vm(vm)
expect(subject_row_for_tenant(tenant_1).cpu_used_cost).to eq(all_vms_cpu_cost)

# Tenant 5 Vms
result_vm_1_5 = result_row_for_vm(vm_1_5)
result_vm_2_5 = result_row_for_vm(vm_2_5)
result_vm15 = result_row_for_vm(vm_1_5)
result_vm25 = result_row_for_vm(vm_2_5)

expect(subject_row_for_tenant(tenant_5).cpu_used_metric).to eq(result_vm_1_5.cpu_used_metric + result_vm_2_5.cpu_used_metric)
expect(subject_row_for_tenant(tenant_5).cpu_used_cost).to eq(result_vm_1_5.cpu_used_cost + result_vm_2_5.cpu_used_cost)
expect(subject_row_for_tenant(tenant_5).cpu_used_metric).to eq(result_vm15.cpu_used_metric + result_vm25.cpu_used_metric)
expect(subject_row_for_tenant(tenant_5).cpu_used_cost).to eq(result_vm15.cpu_used_cost + result_vm25.cpu_used_cost)
end

it 'calculated allocted metric and cost with using max(max is not summed up - it is taken maximum)' do
Expand Down

0 comments on commit 71fea20

Please sign in to comment.