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 14, 2018
1 parent 76393c9 commit d023b6e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 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

0 comments on commit d023b6e

Please sign in to comment.