diff --git a/app/models/chargeback/consumption_with_rollups.rb b/app/models/chargeback/consumption_with_rollups.rb index 66e7b7fa1c0a..71d619936570 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 786f13fcebce..9b8882060ea0 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 57bdf954f7da..35146dc0fb9e 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