From d023b6edb1c1ae49946d864e3dbea0689ed668ef Mon Sep 17 00:00:00 2001 From: lpichler Date: Wed, 14 Feb 2018 21:06:34 +0100 Subject: [PATCH] Calculation for grouping rollups when max method is used 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. --- app/models/chargeback/consumption_with_rollups.rb | 7 +++++++ app/models/chargeback/consumption_without_rollups.rb | 1 + app/models/chargeback/report_options.rb | 1 + 3 files changed, 9 insertions(+) diff --git a/app/models/chargeback/consumption_with_rollups.rb b/app/models/chargeback/consumption_with_rollups.rb index 66e7b7fa1c0..71d61993657 100644 --- a/app/models/chargeback/consumption_with_rollups.rb +++ b/app/models/chargeback/consumption_with_rollups.rb @@ -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 diff --git a/app/models/chargeback/consumption_without_rollups.rb b/app/models/chargeback/consumption_without_rollups.rb index 786f13fcebc..9b8882060ea 100644 --- a/app/models/chargeback/consumption_without_rollups.rb +++ b/app/models/chargeback/consumption_without_rollups.rb @@ -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 diff --git a/app/models/chargeback/report_options.rb b/app/models/chargeback/report_options.rb index 57bdf954f7d..35146dc0fb9 100644 --- a/app/models/chargeback/report_options.rb +++ b/app/models/chargeback/report_options.rb @@ -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