Skip to content

Commit

Permalink
Do not charge for hours before resource creation
Browse files Browse the repository at this point in the history
  • Loading branch information
isimluk committed Jan 10, 2017
1 parent e2d0561 commit 1317470
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
15 changes: 9 additions & 6 deletions app/models/chargeback/consumption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/chargeback/consumption_with_rollups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/models/chargeback_rate_detail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1317470

Please sign in to comment.