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

Filter relevant fields also according to chargeback class in Chargeback #17414

Merged
merged 1 commit into from May 21, 2018

Conversation

lpichler
Copy link
Contributor

@lpichler lpichler commented May 14, 2018

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1580543

This bug has been discovered after this fix and unfortunately chargeback report is broken with mentioned PR.

Error msg (during generation report for VMs)

Generating report... Costs2
/Users/liborpichler/manageiq/manageiq/.bundle/ruby/2.4.0/gems/activemodel-5.0.7/lib/active_model/attribute_methods.rb:433:in `method_missing': undefined method `derived_vm_numvcpu_cores' for #<MetricRollup:0x00007fbc0ef7d588> (NoMethodError)
Did you mean?  derived_vm_numvcpus
               derived_vm_numvcpus=
               derived_vm_numvcpus?
               derived_vm_numvcpus_change
               derived_vm_numvcpus_was
	from /Users/liborpichler/manageiq/manageiq/app/models/chargeback/consumption_with_rollups.rb:91:in `collect'
	from /Users/liborpichler/manageiq/manageiq/app/models/chargeback/consumption_with_rollups.rb:91:in `values'
	from /Users/liborpichler/manageiq/manageiq/app/models/chargeback/consumption_with_rollups.rb:66:in `none?'

Note: derived_vm_numvcpu_cores is related only to chargeback for chargeback for containers.

Why?

Chargeback rates contain chargeback rate details (ChargebackRateDetail) for all types of chargeback (VMs, ContainerProjects, ContainerImages).

In calculation we are cycling thru all rate details independently on chargeback type and then irrelevant fields should be skipped for certain chargeback type. It was basically accidentally skipped before this recent fix and now after the fix, relevant field is ( for examplecpu core allocated ) not skipped for certain chargeback type(example: for Chargeback for VM - even field is related only for chargeback for containers) because of eventual participation on calculation of total_costcolumn done in fix.

Fix
Now column represented by chargeback detail rate has to be present as attribute in related chargeback class.

cc @gtanzillo

@miq-bot assign @jrafanie

@miq-bot add_label gaprindashvili/yes

@lpichler
Copy link
Contributor Author

@miq-bot add_label chargeback

@miq-bot
Copy link
Member

miq-bot commented May 14, 2018

Checked commit lpichler@213289c with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
6 files checked, 0 offenses detected
Everything looks fine. 🍰

@@ -152,7 +152,7 @@ def calculate_costs(consumption, rates)
self.class.try(:refresh_dynamic_metric_columns)

rates.each do |rate|
rate.rate_details_relevant_to(relevant_fields).each do |r|
rate.rate_details_relevant_to(relevant_fields, self.class.attribute_names).each do |r|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like this should be a method... I believe you're doing a set intersection of relevant_fields and self.class.attribute_names) in rate_details_relevant_to below, right?

def calculate_costs(consumption, rates)
  self.fixed_compute_metric = consumption.chargeback_fields_present if consumption.chargeback_fields_present
  self.class.try(:refresh_dynamic_metric_columns)
	 
  rates.each do |rate|
    rate.rate_details_relevant_to(relevant_fields & self.class.attribute_names).each do |r|

Either that or move the set intersection into the method below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately , it is not same.

Originally,
method rate_details_relevant_to is going thru all rate chargeback details and it is basically asking each rate detail chargeback - Does your cost calculation affect a selected report fields(report_cols) ?

And it will meet also rate detail chargeback with derived_vm_numvcpu_cores.
But if you have only total_cost on report - this total value are affecting all columns - derived_vm_numvcpu_cores included.

But we need to refuse derived_vm_numvcpu_cores for chargeback vm.

Hopefully, you understand better.

And sure we need to do some refactoring here: for example similar condition is also in ChargebackRateDetail#charge:97 method, I will do it in folluw up PR

def rate_details_relevant_to(report_cols)
# we can memoize, as we get the same report_cols thrrough the life of the object
@relevant ||= chargeback_rate_details.select { |r| r.affects_report_fields(report_cols) }
def rate_details_relevant_to(report_cols, allowed_cols)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't want to change this method signature if we don't have to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we don't want but it is necessary here. But we can improve with some unification because similar condition is also in ChargebackRateDetail#charge:97 method,as I mentioned above.

@@ -12,7 +12,7 @@ class ChargebackRateDetail < ApplicationRecord

delegate :rate_type, :to => :chargeback_rate, :allow_nil => true

delegate :metric_key, :cost_keys, :rate_key, :to => :chargeable_field
delegate :metric_column_key, :metric_key, :cost_keys, :rate_key, :to => :chargeable_field
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, we still want to delegate metric_key, don't we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we need it in ChargebackRateDetail#charge method

# fixed_compute_metric is used in report and calculations
# TODO: remove and unify with metric_key
def metric_column_key
fixed? ? metric_key.gsub(/\_1|\_2/, '') : metric_key
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this is doing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is hack,

we have chargeback_rate_details and each detail is representing metric column na cost column.

For example:

�CPU Allocated Cost has metric column: cpu_allocated_metric and cost column cpu_allocated_cost

so when we are checking if cpu_allocated_cost is allowed to report, we are asking here https://github.com/ManageIQ/manageiq/pull/17414/files/213289cd034ea8054bbb9a30c80d229cd776c049#diff-7c998acd3d11c70ed830e216754f4b6bR42
if string cpu_allocated_cost (from selected fields) is related to metric_key which is based from ChargeableField#group and Chargeback#source.

But we have to add exception for fixed rate detail because there are two columns
fixed_compute_1_cost and fixed_compute_1_cost but related metric field is only one: fixed_compute_metric and then method ChargeableField#metric_key is not enough (it would produce fixed_compute_1_metric ) but we need to base check in fixed_compute_metric (some for storage)

so input of this method is fixed_compute_1_metric and it should remove _1 (etc.)

@jrafanie jrafanie added this to the Sprint 86 Ending May 21, 2018 milestone May 21, 2018
@jrafanie
Copy link
Member

@lpichler I'll merge this for now to fix the issue. Can you do your suggested improvements in a followup PR? Thanks!

@jrafanie jrafanie added the bug label May 21, 2018
@jrafanie jrafanie merged commit 6f61221 into ManageIQ:master May 21, 2018
@lpichler lpichler deleted the fix_chargeback_column_filtering branch May 21, 2018 19:00
@lpichler
Copy link
Contributor Author

sure @jrafanie, thanks!

@simaishi
Copy link
Contributor

Backported to Gaprindashvili via #17639

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants