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

Group by docker label in chargeback for container images #16097

Merged
merged 1 commit into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions app/models/chargeback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Chargeback < ActsAsArModel
:chargeback_rates => :string,
:entity => :binary,
:tag_name => :string,
:label_name => :string,
:fixed_compute_metric => :integer,
)

Expand Down Expand Up @@ -39,6 +40,8 @@ def self.report_row_key(consumption)
classification = @options.classification_for(consumption)
classification_id = classification.present? ? classification.id : 'none'
"#{classification_id}_#{ts_key}"
elsif @options[:groupby_label].present?
"#{groupby_label_value(consumption, @options[:groupby_label])}_#{ts_key}"
else
default_key(consumption, ts_key)
end
Expand All @@ -48,12 +51,19 @@ def self.default_key(consumption, ts_key)
"#{consumption.resource_id}_#{ts_key}"
end

def self.groupby_label_value(consumption, groupby_label)
nil
end

def initialize(options, consumption)
@options = options
super()
if @options[:groupby_tag].present?
classification = @options.classification_for(consumption)
self.tag_name = classification.present? ? classification.description : _('<Empty>')
elsif @options[:groupby_label].present?
label_value = self.class.groupby_label_value(consumption, options[:groupby_label])
self.label_name = label_value.present? ? label_value : _('<Empty>')
else
init_extra_fields(consumption)
end
Expand Down Expand Up @@ -88,17 +98,28 @@ def self.report_tag_field
"tag_name"
end

def self.set_chargeback_report_options(rpt, group_by, header_for_tag, tz)
def self.report_label_field
"label_name"
end

def self.set_chargeback_report_options(rpt, group_by, header_for_tag, groupby_label, tz)
rpt.cols = %w(start_date display_range)

static_cols = group_by == "project" ? report_static_cols - ["image_name"] : report_static_cols
static_cols = group_by == "tag" ? [report_tag_field] : static_cols
static_cols = group_by == "label" ? [report_label_field] : static_cols
rpt.cols += static_cols
rpt.col_order = static_cols + ["display_range"]
rpt.sortby = static_cols + ["start_date"]

rpt.col_order.each do |c|
header_column = (c == report_tag_field && header_for_tag) ? header_for_tag : c
header_column = if (c == report_tag_field && header_for_tag)
header_for_tag
elsif (c == report_label_field && groupby_label)
groupby_label
else
c
end
rpt.headers.push(Dictionary.gettext(header_column, :type => :column, :notfound => :titleize))
rpt.col_formats.push(nil) # No formatting needed on the static cols
end
Expand Down
1 change: 1 addition & 0 deletions app/models/chargeback/report_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Chargeback
:service_id,
:groupby,
:groupby_tag,
:groupby_label,
:userid,
:ext_options,
:include_metrics, # enable charging allocated resources with C & U
Expand Down
14 changes: 14 additions & 0 deletions app/models/chargeback_container_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,25 @@ def self.build_results_for_report_ChargebackContainerImage(options)

@unknown_project ||= OpenStruct.new(:id => 0, :name => _('Unknown Project'), :ems_ref => _('Unknown'))
@unknown_image ||= OpenStruct.new(:id => 0, :full_name => _('Unknown Image'))

load_custom_attribute_groupby(options[:groupby_label]) if options[:groupby_label].present?
build_results_for_report_chargeback(options)
ensure
@data_index = @containers = nil
end

def self.load_custom_attribute_groupby(groupby_label)
report_cb_model(self.name).safe_constantize.add_custom_attribute(groupby_label_method(groupby_label))
end

def self.groupby_label_method(groupby_label)
CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX + groupby_label + CustomAttributeMixin::SECTION_SEPARATOR + 'docker_labels'
end

def self.groupby_label_value(consumption, groupby_label)
ChargebackContainerImage.image(consumption).try(groupby_label_method(groupby_label))
end

def self.default_key(metric_rollup_record, ts_key)
project = self.project(metric_rollup_record)
image = self.image(metric_rollup_record)
Expand Down
5 changes: 5 additions & 0 deletions app/models/mixins/custom_attribute_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def self.to_human(column)
_("%{section}: %{custom_key}") % { :custom_key => col_name, :section => section.try(:titleize) || DEFAULT_SECTION_NAME}
end

def self.column_name(custom_key)
return if custom_key.nil?
CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX + custom_key
end

def self.select_virtual_custom_attributes(cols)
cols.nil? ? [] : cols.select { |x| x.start_with?(CUSTOM_ATTRIBUTES_PREFIX) }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ def self._custom_details_for(model, options)
custom_attributes_details = []

klass.custom_keys.each do |custom_key|
custom_detail_column = [model, CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX + custom_key].join("-")
custom_detail_column = [model, CustomAttributeMixin.column_name(custom_key)].join("-")
custom_detail_name = CustomAttributeMixin.to_human(custom_key)

if options[:include_model]
Expand Down