Skip to content

Commit

Permalink
Merge pull request ManageIQ#11794 from zeari/chargeback_kubernetes_la…
Browse files Browse the repository at this point in the history
…bels

Enable custom attributes for chargeback reports
  • Loading branch information
gtanzillo committed Oct 14, 2016
2 parents 237b81e + d4ee12c commit 9b09682
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/controllers/report_controller/reports/editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ def build_selected_fields(rpt)
f_len = fields.length
for f_idx in 1..f_len # Go thru fields in reverse
f_key = fields[f_len - f_idx].last
next if f_key.ends_with?(*CHARGEBACK_ALLOWED_FIELD_SUFFIXES) || f_key.include?('managed')
next if f_key.ends_with?(*CHARGEBACK_ALLOWED_FIELD_SUFFIXES) || f_key.include?('managed') || f_key.include?(CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX)
headers.delete(f_key)
col_formats.delete(f_key)
fields.delete_at(f_len - f_idx)
Expand Down
27 changes: 25 additions & 2 deletions app/models/chargeback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.build_results_for_report_chargeback(options)
end

base_rollup = MetricRollup.includes(
:resource => [:hardware, :tenant, :tags, :vim_performance_states],
:resource => [:hardware, :tenant, :tags, :vim_performance_states, :custom_attributes],
:parent_host => :tags,
:parent_ems_cluster => :tags,
:parent_storage => :tags,
Expand Down Expand Up @@ -84,7 +84,8 @@ def self.date_fields(metric_rollup_record, interval, tz)
'end_date' => end_ts,
'display_range' => display_range,
'interval_name' => interval,
'chargeback_rates' => ''
'chargeback_rates' => '',
'entity' => metric_rollup_record.resource
}
end

Expand Down Expand Up @@ -280,4 +281,26 @@ def self.set_chargeback_report_options(rpt, edit)
rpt.tz = edit[:new][:tz]
rpt
end

def tags
entity.try(:tags).to_a
end

def self.load_custom_attributes_for(cols)
chargeback_klass = report_cb_model(self.to_s).safe_constantize
chargeback_klass.load_custom_attributes_for(cols)
cols.each do |x|
next unless x.include?(CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX)

load_custom_attribute(x)
end
end

def self.load_custom_attribute(custom_attribute)
virtual_column(custom_attribute.to_sym, :type => :string)

define_method(custom_attribute.to_sym) do
entity.send(custom_attribute)
end
end
end # class Chargeback
3 changes: 2 additions & 1 deletion app/models/chargeback_container_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class ChargebackContainerImage < Chargeback
:memory_used_metric => :float,
:net_io_used_cost => :float,
:net_io_used_metric => :float,
:total_cost => :float
:total_cost => :float,
:entity => :binary
)

def self.build_results_for_report_ChargebackContainerImage(options)
Expand Down
7 changes: 2 additions & 5 deletions app/models/chargeback_container_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class ChargebackContainerProject < Chargeback
:memory_used_metric => :float,
:net_io_used_cost => :float,
:net_io_used_metric => :float,
:total_cost => :float
:total_cost => :float,
:entity => :binary
)

def self.build_results_for_report_ChargebackContainerProject(options)
Expand Down Expand Up @@ -99,10 +100,6 @@ def self.report_col_options
}
end

def tags
ContainerProject.includes(:tags).find_by_ems_ref(project_uid).try(:tags).to_a
end

def get_rate_parents(perf)
# Get rate from assigned containers providers only
[perf.parent_ems]
Expand Down
7 changes: 2 additions & 5 deletions app/models/chargeback_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class ChargebackVm < Chargeback
:storage_used_metric => :float,
:storage_cost => :float,
:storage_metric => :float,
:total_cost => :float
:total_cost => :float,
:entity => :binary
)

def self.build_results_for_report_ChargebackVm(options)
Expand Down Expand Up @@ -163,10 +164,6 @@ def self.report_col_options
}
end

def tags
Vm.includes(:tags).find_by_ems_ref(vm_uid).try(:tags).to_a
end

def get_rate_parents(perf)
@enterprise ||= MiqEnterprise.my_enterprise
[perf.parent_host, perf.parent_ems_cluster, perf.parent_storage, perf.parent_ems, @enterprise, perf.resource.try(:tenant)]
Expand Down
2 changes: 1 addition & 1 deletion app/models/miq_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def page_size

def load_custom_attributes
klass = db.safe_constantize
return unless klass < CustomAttributeMixin
return unless klass < CustomAttributeMixin || Chargeback.db_is_chargeback?(db)

klass.load_custom_attributes_for(cols.uniq)
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 @@ -1201,7 +1201,7 @@ def self.reporting_available_fields(model, interval = nil)
cb_model = Chargeback.report_cb_model(model)
@reporting_available_fields[model.to_s] ||=
MiqExpression.model_details(model, :include_model => false, :include_tags => true).select { |c| c.last.ends_with?(*ReportController::Reports::Editor::CHARGEBACK_ALLOWED_FIELD_SUFFIXES) } +
MiqExpression.tag_details(cb_model, model, {})
MiqExpression.tag_details(cb_model, model, {}) + _custom_details_for(cb_model, {})
else
@reporting_available_fields[model.to_s] ||= MiqExpression.model_details(model, :include_model => false, :include_tags => true)
end
Expand Down

0 comments on commit 9b09682

Please sign in to comment.