diff --git a/app/models/chargeback/consumption.rb b/app/models/chargeback/consumption.rb index 5198726e76c..9ea8bc205f2 100644 --- a/app/models/chargeback/consumption.rb +++ b/app/models/chargeback/consumption.rb @@ -4,12 +4,15 @@ def initialize(start_time, end_time) @start_time, @end_time = start_time, end_time end - def past_hours_in_interval - # We cannot charge for future hours (i.e. weekly report on Monday, should charge just monday) - @past_hours_in_interval ||= begin - past = (Time.current - @start_time).round / 1.hour - [past, hours_in_interval].min - end + def consumed_hours_in_interval + # Why we need this? + # 1) We cannot charge for hours until the resources existed (vm provisioned in the middle of month) + # 2) We cannot charge for future hours (i.e. weekly report on Monday, should charge just monday) + @consumed_hours_in_interval ||= begin + consuption_start = [@start_time, resource.try(:created_on)].compact.max + consumption_end = [Time.current, @end_time].min + (consumption_end - consuption_start).round / 1.hour + end end def hours_in_month diff --git a/app/models/chargeback/consumption_with_rollups.rb b/app/models/chargeback/consumption_with_rollups.rb index 04d84ee7d70..4cc75d07c11 100644 --- a/app/models/chargeback/consumption_with_rollups.rb +++ b/app/models/chargeback/consumption_with_rollups.rb @@ -19,7 +19,7 @@ def max(metric) def avg(metric) metric_sum = values(metric).sum - metric_sum / past_hours_in_interval + metric_sum / consumed_hours_in_interval end def none?(metric) diff --git a/app/models/chargeback_rate_detail.rb b/app/models/chargeback_rate_detail.rb index 6368608af8e..afbe04d2e40 100644 --- a/app/models/chargeback_rate_detail.rb +++ b/app/models/chargeback_rate_detail.rb @@ -233,7 +233,7 @@ def cost_keys def metric_and_cost_by(consumption) metric_value = metric_value_by(consumption) - [metric_value, hourly_cost(metric_value, consumption) * consumption.past_hours_in_interval] + [metric_value, hourly_cost(metric_value, consumption) * consumption.consumed_hours_in_interval] end def first_tier?(tier,tiers)