Skip to content

Commit

Permalink
Do not charge for hours that are yet to come
Browse files Browse the repository at this point in the history
For variable_rate, we charge only that much that has been consumed.

Perhaps I'll end the service tmrw, I should not be charged in advanced.

https://bugzilla.redhat.com/show_bug.cgi?id=1402072
  • Loading branch information
isimluk committed Jan 5, 2017
1 parent 73c99b9 commit c94a8fb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions app/models/chargeback/consumption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ def initialize(start_time, end_time)
@start_time, @end_time = start_time, end_time
end

def hours_in_interval
@hours_in_interval ||= (@end_time - @start_time).round / 1.hour
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
end

def hours_in_month
Expand All @@ -16,6 +20,10 @@ def hours_in_month

private

def hours_in_interval
@hours_in_interval ||= (@end_time - @start_time).round / 1.hour
end

def monthly?
# A heuristic. Is the interval lenght about 30 days?
(hours_in_interval * 1.hour - 1.month).abs < 3.days
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 / hours_in_interval
metric_sum / past_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.hours_in_interval]
[metric_value, hourly_cost(metric_value, consumption) * consumption.past_hours_in_interval]
end

def first_tier?(tier,tiers)
Expand Down

0 comments on commit c94a8fb

Please sign in to comment.