From 6c696247aea0498ef3e81886fa576e2943148534 Mon Sep 17 00:00:00 2001 From: lpichler Date: Fri, 17 Aug 2018 11:52:36 +0200 Subject: [PATCH] Add log message to Chargeback --- app/models/chargeback.rb | 25 +++++++++++++++++++- app/models/chargeback/consumption_history.rb | 1 + app/models/chargeback_rate_detail.rb | 3 +++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/models/chargeback.rb b/app/models/chargeback.rb index 45a74a210408..93b7dea45905 100644 --- a/app/models/chargeback.rb +++ b/app/models/chargeback.rb @@ -32,12 +32,17 @@ def self.build_results_for_report_chargeback(options) data = {} rates = RatesCache.new(options) + _log.debug("With report options: #{options.inspect}") MiqRegion.all.each do |region| + _log.debug("For region #{region.region}") + ConsumptionHistory.for_report(self, options, region.region) do |consumption| rates_to_apply = rates.get(consumption) key = report_row_key(consumption) + _log.debug("Report row key #{key}") + data[key] ||= new(options, consumption, region.region) chargeback_rates = data[key]["chargeback_rates"].split(', ') + rates_to_apply.collect(&:description) @@ -57,6 +62,21 @@ def self.build_results_for_report_chargeback(options) [data.values] end + def self.log_rates(rates) + rates.each do |rate| + _log.debug("Rate: #{rate.description}") + rate.chargeback_rate_details.each do |rate_detail| + chargeable_field = rate_detail.chargeable_field + _log.debug("Description #{chargeable_field.description}") + _log.debug("Metric: #{chargeable_field.metric} Group: #{chargeable_field.group} Source: #{chargeable_field.source}") + + rate_detail.chargeback_tiers.each do |tier| + _log.debug("Start: #{tier.start} Finish: #{tier.finish} Fixed Rate: #{tier.fixed_rate} Variable Rate: #{tier.variable_rate}") + end + end + end + end + def self.report_row_key(consumption) ts_key = @options.start_of_report_step(consumption.timestamp) if @options[:groupby_tag].present? @@ -156,11 +176,14 @@ def calculate_costs(consumption, rates) self.class.try(:refresh_dynamic_metric_columns) rates.each do |rate| + _log.debug("Calculation with rate: #{rate.description}(#{rate.rate_type})") rate.rate_details_relevant_to(relevant_fields, self.class.attribute_names).each do |r| + _log.debug("Metric: #{r.chargeable_field.metric} Group: #{r.chargeable_field.group} Source: #{r.chargeable_field.source}") r.charge(consumption, @options).each do |field, value| next if @options.skip_field_accumulation?(field, self[field]) - + _log.debug("Calculation with field: #{field} and with value: #{value}") (self[field] = self[field].kind_of?(Numeric) ? (self[field] || 0) + value : value) + _log.debug("Accumulated value: #{self[field]}") end end end diff --git a/app/models/chargeback/consumption_history.rb b/app/models/chargeback/consumption_history.rb index e55fa2486484..7d0bfdb170ad 100644 --- a/app/models/chargeback/consumption_history.rb +++ b/app/models/chargeback/consumption_history.rb @@ -24,6 +24,7 @@ def self.for_report(cb_class, options, region) # values are grouped by resource_id and timestamp (query_start_time...query_end_time) records.each_value do |rollup_record_ids| metric_rollup_records = MetricRollup.where(:id => rollup_record_ids).order(:resource_type, :resource_id, :timestamp).pluck(*ChargeableField.cols_on_metric_rollup) + _log.debug("Count of Metric Rollups for consumption: #{metric_rollup_records.count}") consumption = ConsumptionWithRollups.new(metric_rollup_records, query_start_time, query_end_time) yield(consumption) unless consumption.consumed_hours_in_interval.zero? end diff --git a/app/models/chargeback_rate_detail.rb b/app/models/chargeback_rate_detail.rb index c1727198a24a..7faa72c0a0e8 100644 --- a/app/models/chargeback_rate_detail.rb +++ b/app/models/chargeback_rate_detail.rb @@ -241,6 +241,9 @@ def gratis? def metric_and_cost_by(consumption, options) metric_value = chargeable_field.measure(consumption, options, sub_metric) hourly_cost = hourly_cost(metric_value, consumption) + + _log.debug("Consumption interval: #{consumption.consumption_start} - #{consumption.consumption_end}") + _log.debug("Consumed hours: #{consumption.consumed_hours_in_interval}") cost = chargeable_field.metering? ? hourly_cost : hourly_cost * consumption.consumed_hours_in_interval [metric_value, cost] end