From 64cbb707174ed5b207208c5a7dce85f80f0082ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Wed, 30 Nov 2016 10:27:16 +0100 Subject: [PATCH] Account for only those rates that affect the report --- app/models/chargeback.rb | 4 ++-- app/models/chargeback_rate.rb | 5 +++++ app/models/chargeback_rate_detail.rb | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/models/chargeback.rb b/app/models/chargeback.rb index 96deaff70c5..9062efc08e4 100644 --- a/app/models/chargeback.rb +++ b/app/models/chargeback.rb @@ -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 @@ -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 diff --git a/app/models/chargeback_rate.rb b/app/models/chargeback_rate.rb index bf5b1e60cbc..689f249da29 100644 --- a/app/models/chargeback_rate.rb +++ b/app/models/chargeback_rate.rb @@ -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" diff --git a/app/models/chargeback_rate_detail.rb b/app/models/chargeback_rate_detail.rb index f09600a8fe2..917e92a30b0 100644 --- a/app/models/chargeback_rate_detail.rb +++ b/app/models/chargeback_rate_detail.rb @@ -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