Skip to content

Commit

Permalink
Add getter for rollup records in consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Feb 15, 2021
1 parent 17cd92b commit dba8cd1
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions app/models/chargeback/consumption_with_rollups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class ConsumptionWithRollups < Consumption

def initialize(metric_rollup_records, start_time, end_time)
super(start_time, end_time)
@rollup_array = metric_rollup_records
@rollup_records = metric_rollup_records
end

def rollup_records
@rollup_records
end

def hash_features_affecting_rate
Expand All @@ -21,7 +25,7 @@ def hash_features_affecting_rate
end

def tag_names
@tag_names ||= @rollup_array.inject([]) do |memo, rollup|
@tag_names ||= rollup_records.inject([]) do |memo, rollup|
memo |= all_tag_names(rollup)
memo
end
Expand Down Expand Up @@ -67,7 +71,7 @@ def tag_list_with_prefix_for(rollup)
end

def tag_list_with_prefix
@tag_list_with_prefix ||= @rollup_array.map { |rollup| tag_list_with_prefix_for(rollup) }.flatten.uniq
@tag_list_with_prefix ||= rollup_records.map { |rollup| tag_list_with_prefix_for(rollup) }.flatten.uniq
end

def sum(metric, sub_metric = nil)
Expand All @@ -82,7 +86,7 @@ def max(metric, sub_metric = nil)
def sum_of_maxes_from_grouped_values(metric, sub_metric = nil)
return max(metric, sub_metric) if sub_metric
@grouped_values ||= {}
grouped_rollups = @rollup_array.group_by { |x| x[ChargeableField.col_index(:resource_id)] }
grouped_rollups = rollup_records.group_by { |x| x[ChargeableField.col_index(:resource_id)] }

@grouped_values[metric] ||= grouped_rollups.map do |_, rollups|
rollups.map { |x| rollup_field(x, metric) }.compact.max
Expand All @@ -108,7 +112,7 @@ def none?(metric, sub_metric)
end

def chargeback_fields_present
@chargeback_fields_present ||= @rollup_array.count { |rollup| chargeback_fields_present?(rollup) }
@chargeback_fields_present ||= rollup_records.count { |rollup| chargeback_fields_present?(rollup) }
end

def chargeback_fields_present?(rollup_record)
Expand All @@ -126,12 +130,12 @@ def metering_used_fields_present?(rollup_record)
end

def metering_used_fields_present
@metering_used_fields_present ||= @rollup_array.count { |rollup| metering_used_fields_present?(rollup) }
@metering_used_fields_present ||= rollup_records.count { |rollup| metering_used_fields_present?(rollup) }
end

def metering_allocated_for(metric)
@metering_allocated_metric ||= {}
@metering_allocated_metric[metric] ||= @rollup_array.count do |rollup|
@metering_allocated_metric[metric] ||= rollup_records.count do |rollup|
rollup_record = rollup_field(rollup, metric)
rollup_record.present? && rollup_record.nonzero?
end
Expand Down Expand Up @@ -161,7 +165,7 @@ def sub_metric_rollups(sub_metric)
def values(metric, sub_metric = nil)
@values ||= {}
@values["#{metric}#{sub_metric}"] ||= begin
sub_metric ? sub_metric_rollups(sub_metric) : @rollup_array.collect { |x| rollup_field(x, metric) }.compact
sub_metric ? sub_metric_rollups(sub_metric) : rollup_records.collect { |x| rollup_field(x, metric) }.compact
end
end

Expand All @@ -183,7 +187,7 @@ def v_derived_cpu_total_cores_used_for(rollup)
end

def first_metric_rollup_record
first_rollup_id = @rollup_array.first[ChargeableField.col_index(:id)]
first_rollup_id = rollup_records.first[ChargeableField.col_index(:id)]
@fmrr ||= MetricRollup.find(first_rollup_id) if first_rollup_id
end
end
Expand Down

0 comments on commit dba8cd1

Please sign in to comment.