Skip to content

Commit

Permalink
Account for only those rates that affect the report
Browse files Browse the repository at this point in the history
  • Loading branch information
isimluk committed Jan 3, 2017
1 parent 5ad3d0f commit 64cbb70
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/models/chargeback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def calculate_costs(consumption, rates)
self.fixed_compute_metric = consumption.chargeback_fields_present if consumption.chargeback_fields_present

rates.each do |rate|
rate.chargeback_rate_details.each do |r|
rate.rate_details_relevant_to(relevant_fields).each do |r|
r.charge(relevant_fields, consumption).each do |field, value|
next unless self.class.attribute_names.include?(field)
self[field] = (self[field] || 0) + value
Expand Down Expand Up @@ -135,6 +135,6 @@ def self.load_custom_attribute(custom_attribute)
private

def relevant_fields
@relevant_fields ||= self.class.report_col_options.keys.to_set
@relevant_fields ||= (@options.report_cols || self.class.report_col_options.keys).to_set
end
end # class Chargeback
5 changes: 5 additions & 0 deletions app/models/chargeback_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class ChargebackRate < ApplicationRecord

VALID_CB_RATE_TYPES = ["Compute", "Storage"]

def rate_details_relevant_to(report_cols)
# we can memoize, as we get the same report_cols thrrough the life of the object
@relevant ||= chargeback_rate_details.select { |r| r.affects_report_fields(report_cols) }
end

def self.validate_rate_type(type)
unless VALID_CB_RATE_TYPES.include?(type.to_s.capitalize)
raise "Chargeback rate type '#{type}' is not supported"
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 @@ -140,7 +140,7 @@ def adjustment_measure(hr, pu_destiny)
end

def affects_report_fields(report_cols)
(metric_keys & report_cols).present? || ((cost_keys & report_cols).present? && !gratis?)
(metric_keys.to_set & report_cols).present? || ((cost_keys.to_set & report_cols).present? && !gratis?)
end

def rate_name
Expand Down

0 comments on commit 64cbb70

Please sign in to comment.