Skip to content

Commit

Permalink
Refactor: Extract methods to tell what the RateDetail affects
Browse files Browse the repository at this point in the history
This will be needed once we will have ChargebackRateDetails with dynamic
rate/metric names.
  • Loading branch information
isimluk committed Nov 18, 2016
1 parent 6c172d9 commit 9492963
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
15 changes: 5 additions & 10 deletions app/models/chargeback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,19 @@ def calculate_costs(metric_rollup_records, rates, hours_in_interval)
metric_value, cost = r.metric_and_cost_by(metric_rollup_records, hours_in_interval)
end

accumulate_metrics_and_costs_per_rate(r.rate_name, r.group, metric_value, cost)
accumulate_metrics_and_costs_per_rate(r, metric_value, cost)
end
end
end

def accumulate_metrics_and_costs_per_rate(rate_name, rate_group, metric, cost)
cost_key = "#{rate_name}_cost" # metric cost value (e.g. Storage [Used|Allocated|Fixed] Cost)
metric_key = "#{rate_name}_metric" # metric value (e.g. Storage [Used|Allocated|Fixed])
cost_group_key = "#{rate_group}_cost" # for total of metric's costs (e.g. Storage Total Cost)
metric_group_key = "#{rate_group}_metric" # for total of metrics (e.g. Storage Total)

def accumulate_metrics_and_costs_per_rate(rate, metric, cost)
col_hash = {}

defined_column_for_report = (self.class.report_col_options.keys & [metric_key, cost_key]).present?
defined_column_for_report = (self.class.report_col_options.keys & [rate.metric_keys[0], rate.cost_keys[0]]).present?

if defined_column_for_report
[metric_key, metric_group_key].each { |col| col_hash[col] = metric }
[cost_key, cost_group_key, 'total_cost'].each { |col| col_hash[col] = cost }
rate.metric_keys.each { |col| col_hash[col] = metric }
rate.cost_keys.each { |col| col_hash[col] = cost }
end

col_hash.each do |k, val|
Expand Down
11 changes: 11 additions & 0 deletions app/models/chargeback_rate_detail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ def contiguous_tiers?
!error
end

def metric_keys
["#{rate_name}_metric", # metric value (e.g. Storage [Used|Allocated|Fixed])
"#{group}_metric"] # total of metric's group (e.g. Storage Total)
end

def cost_keys
["#{rate_name}_cost", # cost associated with metric (e.g. Storage [Used|Allocated|Fixed] Cost)
"#{group}_cost", # cost associated with metric's group (e.g. Storage Total Cost)
'total_cost']
end

def metric_and_cost_by(metric_rollup_records, hours_in_interval)
@hours_in_interval = hours_in_interval
metric_value = metric_value_by(metric_rollup_records)
Expand Down

0 comments on commit 9492963

Please sign in to comment.