diff --git a/app/models/chargeback.rb b/app/models/chargeback.rb index 7a4b449e9ba..e20d60f895b 100644 --- a/app/models/chargeback.rb +++ b/app/models/chargeback.rb @@ -7,6 +7,7 @@ class Chargeback < ActsAsArModel :chargeback_rates => :string, :entity => :binary, :tag_name => :string, + :label_name => :string, :fixed_compute_metric => :integer, ) @@ -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 @@ -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 : _('') + elsif @options[:groupby_label].present? + label_value = self.class.groupby_label_value(consumption, options[:groupby_label]) + self.label_name = label_value.present? ? label_value : _('') else init_extra_fields(consumption) end @@ -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 diff --git a/app/models/chargeback/report_options.rb b/app/models/chargeback/report_options.rb index c667538ccc2..fc916b7f802 100644 --- a/app/models/chargeback/report_options.rb +++ b/app/models/chargeback/report_options.rb @@ -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 diff --git a/app/models/chargeback_container_image.rb b/app/models/chargeback_container_image.rb index ea8a7e50402..86f409f0e28 100644 --- a/app/models/chargeback_container_image.rb +++ b/app/models/chargeback_container_image.rb @@ -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) diff --git a/app/models/mixins/custom_attribute_mixin.rb b/app/models/mixins/custom_attribute_mixin.rb index f57a2b3decd..ae2f412b5f7 100644 --- a/app/models/mixins/custom_attribute_mixin.rb +++ b/app/models/mixins/custom_attribute_mixin.rb @@ -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 diff --git a/lib/miq_expression.rb b/lib/miq_expression.rb index 762a8eb8a7d..520c063a5e1 100644 --- a/lib/miq_expression.rb +++ b/lib/miq_expression.rb @@ -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]