Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract getting key and fields to method in chargeback #11645

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 28 additions & 19 deletions app/models/chargeback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,14 @@ def self.build_results_for_report_chargeback(options)
_log.info("Found #{recs.length} records for time range #{[query_start_time, query_end_time].inspect}")

unless recs.empty?
ts_key = get_group_key_ts(recs.first, interval, tz)

recs.each do |perf|
next if perf.resource.nil?
key, extra_fields = if @options[:groupby_tag].present?
get_tag_keys_and_fields(perf, ts_key)
else
get_keys_and_extra_fields(perf, ts_key)
end

if data[key].nil?
start_ts, end_ts, display_range = get_time_range(perf, interval, tz)
data[key] = {
"start_date" => start_ts,
"end_date" => end_ts,
"display_range" => display_range,
"interval_name" => interval,
"chargeback_rates" => ''
}.merge(extra_fields)
end
key, extra_fields = key_and_fields(perf, interval, tz)
data[key] ||= extra_fields

rates_to_apply = cb.get_rates(perf)
data[key]["chargeback_rates"] = (data[key]["chargeback_rates"].split(', ') + rates_to_apply.collect(&:description)).uniq.join(', ')
chargeback_rates = data[key]["chargeback_rates"].split(', ') + rates_to_apply.collect(&:description)
data[key]["chargeback_rates"] = chargeback_rates.uniq.join(', ')
calculate_costs(perf, data[key], rates_to_apply)
end
end
Expand All @@ -74,6 +59,30 @@ def self.build_results_for_report_chargeback(options)
[data.map { |r| new(r.last) }]
end

def self.key_and_fields(metric_rollup_record, interval, tz)
ts_key = get_group_key_ts(metric_rollup_record, interval, tz)

key, extra_fields = if @options[:groupby_tag].present?
get_tag_keys_and_fields(metric_rollup_record, ts_key)
else
get_keys_and_extra_fields(metric_rollup_record, ts_key)
end

[key, date_fields(metric_rollup_record, interval, tz).merge(extra_fields)]
end

def self.date_fields(metric_rollup_record, interval, tz)
start_ts, end_ts, display_range = get_time_range(metric_rollup_record, interval, tz)

{
'start_date' => start_ts,
'end_date' => end_ts,
'display_range' => display_range,
'interval_name' => interval,
'chargeback_rates' => ''
}
end

def self.get_tag_keys_and_fields(perf, ts_key)
tag = perf.tag_names.split("|").select { |x| x.starts_with?(@options[:groupby_tag]) }.first # 'department/*'
tag = tag.split('/').second unless tag.blank? # 'department/finance' -> 'finance'
Expand Down