Skip to content

Commit

Permalink
Add log message to Chargeback
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Aug 17, 2018
1 parent 5c20422 commit 6c69624
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
25 changes: 24 additions & 1 deletion app/models/chargeback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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?
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/chargeback/consumption_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/models/chargeback_rate_detail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6c69624

Please sign in to comment.