Skip to content

Commit

Permalink
Merge pull request #17874 from lpichler/add_log_messages_to_chargeback
Browse files Browse the repository at this point in the history
Add log messages to Chargeback
  • Loading branch information
gtanzillo committed Aug 27, 2018
2 parents a8b2145 + 3919965 commit de1bce0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
15 changes: 13 additions & 2 deletions 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 Down Expand Up @@ -167,13 +172,19 @@ def calculate_fixed_compute_metric(consumption)
def calculate_costs(consumption, rates)
calculate_fixed_compute_metric(consumption)
self.class.try(:refresh_dynamic_metric_columns)

_log.debug("Consumption Type: #{consumption.class}")
rates.each do |rate|
_log.debug("Calculation with rate: #{rate.id} #{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.chargeback_tiers.each do |tier|
_log.debug("Start: #{tier.start} Finish: #{tier.finish} Fixed Rate: #{tier.fixed_rate} Variable Rate: #{tier.variable_rate}")
end
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
3 changes: 3 additions & 0 deletions app/models/chargeback/consumption_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ def self.for_report(cb_class, options, region)
extra_resources = cb_class.try(:extra_resources_without_rollups, region) || []
timerange.step_value(interval_duration).each_cons(2) do |query_start_time, query_end_time|
extra_resources.each do |resource|
_log.info("Using ConsumptionWithoutRollups for resource #{resource.id} #{resource.class} for time range #{[query_start_time, query_end_time].inspect}")
consumption = ConsumptionWithoutRollups.new(resource, query_start_time, query_end_time)
_log.info("Consumed Hours for ConsumptionWithoutRollups: #{consumption.consumed_hours_in_interval}")
yield(consumption) unless consumption.consumed_hours_in_interval.zero?
end

Expand All @@ -24,6 +26,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
18 changes: 17 additions & 1 deletion app/models/mixins/assignment_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,28 +182,44 @@ def tag_class(klass)
# @option options :parents
# @option options :tag_list
def get_assigned_for_target(target, options = {})
_log.debug("Input for get_assigned_for_target id: #{target.id} class: #{target.class}") if target
if options[:parents]
parents = options[:parents]
_log.debug("Parents are passed from parameter")
else
_log.debug("Parents are not passed from parameter")
parents = self::ASSIGNMENT_PARENT_ASSOCIATIONS.flat_map do |rel|
(rel == :my_enterprise ? MiqEnterprise.my_enterprise : target.try(rel)) || []
end
parents << target
end

parents.each { |parent| _log.debug("parent id: #{parent.id} class: #{parent.class}") } if parents.kind_of?(Array)

tlist = parents.collect { |p| "#{p.class.base_model.name.underscore}/id/#{p.id}" } # Assigned directly to parents
tlist += options[:tag_list] if options[:tag_list] # Assigned to target (passed in)
if options[:tag_list] # Assigned to target (passed in)
tlist += options[:tag_list]
_log.debug("Using tag list: #{options[:tag_list].join(', ')}")
end

_log.debug("Directly assigned to parents: #{tlist.join(', ')}")

individually_assigned_resources = tlist.flat_map { |t| assignments_cached[t] }.uniq

_log.debug("Individually assigned resources: #{individually_assigned_resources.map { |x| "id:#{x.id} class:#{x.class}" }.join(', ')}")

# look for alert_set running off of tags (not individual tags)
# TODO: we may need to change taggings-related code to use base_model too
tlist = Tagging.where("tags.name like '/managed/%'")
.where(:taggable => parents)
.references(:tag).includes(:tag).map do |t|
"#{tag_class(t.taggable_type)}/tag#{t.tag.name}"
end

_log.debug("Tags assigned to parents: #{tlist.join(', ')}")
tagged_resources = tlist.flat_map { |t| assignments_cached[t] }.uniq

_log.debug("Tagged resources: #{individually_assigned_resources.map { |x| "id:#{x.id} class:#{x.class}" }.join(', ')}")
(individually_assigned_resources + tagged_resources).uniq
end

Expand Down

0 comments on commit de1bce0

Please sign in to comment.