Skip to content

Commit

Permalink
Decouple Chargeback from ManageIQ/manageiq-consumption since it's not…
Browse files Browse the repository at this point in the history
… being used
  • Loading branch information
gtanzillo committed Nov 22, 2019
1 parent 645d631 commit df7eb32
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 53 deletions.
5 changes: 1 addition & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ gem "manageiq-password", "~>0.3", :require => false
gem "manageiq-postgres_ha_admin", "~>3.1", :require => false
gem "memoist", "~>0.15.0", :require => false
gem "mime-types", "~>3.0", :path => File.expand_path("mime-types-redirector", __dir__)
gem "money-rails", "~> 1.9", :require => false
gem "more_core_extensions", "~>3.7"
gem "nakayoshi_fork", "~>0.0.3" # provides a more CoW friendly fork (GC a few times before fork)
gem "net-ldap", "~>0.16.1", :require => false
Expand Down Expand Up @@ -208,10 +209,6 @@ group :smartstate, :manageiq_default do
gem "manageiq-smartstate", "~>0.3.1", :require => false
end

group :consumption, :manageiq_default do
manageiq_plugin "manageiq-consumption"
end

group :ui_dependencies do # Added to Bundler.require in config/application.rb
manageiq_plugin "manageiq-decorators"
manageiq_plugin "manageiq-ui-classic"
Expand Down
50 changes: 1 addition & 49 deletions app/models/chargeback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@ def self.build_results_for_report_chargeback(options)
chargeback_rates = data[key]["chargeback_rates"].split(', ') + rates_to_apply.collect(&:description)
data[key]["chargeback_rates"] = chargeback_rates.uniq.join(', ')

# we are getting hash with metrics and costs for metrics defined for chargeback
if Settings[:new_chargeback]
data[key].new_chargeback_calculate_costs(consumption, rates_to_apply)
else
data[key].calculate_costs(consumption, rates_to_apply)
end
data[key].calculate_costs(consumption, rates_to_apply)
end
end

Expand Down Expand Up @@ -136,49 +131,6 @@ def showback_category
end
end

def new_chargeback_calculate_costs(consumption, rates)
self.fixed_compute_metric = consumption.chargeback_fields_present if consumption.chargeback_fields_present

rates.each do |rate|
plan = ManageIQ::Showback::PricePlan.find_or_create_by(:description => rate.description,
:name => rate.description,
:resource => MiqEnterprise.first)

data = {}
rate.rate_details_relevant_to(relevant_fields, self.class.attribute_names).each do |r|
r.populate_showback_rate(plan, r, showback_category)
measure = r.chargeable_field.showback_measure
dimension, _, _ = r.chargeable_field.showback_dimension
value = r.chargeable_field.measure(consumption, @options)
data[measure] ||= {}
data[measure][dimension] = [value, r.showback_unit(ChargeableField::UNITS[r.chargeable_field.metric])]
end

# TODO: duration_of_report_step is 30.days for price plans but for consumption history,
# it's used for date ranges and needs to be 1.month with rails 5.1
duration = @options.interval == "monthly" ? 30.days : @options.duration_of_report_step
results = plan.calculate_list_of_costs_input(resource_type: showback_category,
data: data,
start_time: consumption.instance_variable_get("@start_time"),
end_time: consumption.instance_variable_get("@end_time"),
cycle_duration: duration)

results.each do |cost_value, sb_rate|
r = ChargebackRateDetail.find(sb_rate.concept)
metric = r.chargeable_field.metric
metric_index = ChargeableField::VIRTUAL_COL_USES.invert[metric] || metric
metric_value = data[r.chargeable_field.group][metric_index]
metric_field = [r.chargeable_field.group, r.chargeable_field.source, "metric"].join("_")
cost_field = [r.chargeable_field.group, r.chargeable_field.source, "cost"].join("_")
_, total_metric_field, total_field = r.chargeable_field.cost_keys
self[total_field] = (self[total_field].to_f || 0) + cost_value.to_f
self[total_metric_field] = (self[total_metric_field].to_f || 0) + cost_value.to_f
self[cost_field] = cost_value.to_f
self[metric_field] = metric_value.first.to_f
end
end
end

def calculate_fixed_compute_metric(consumption)
return unless consumption.chargeback_fields_present

Expand Down
111 changes: 111 additions & 0 deletions config/initializers/money.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
require 'money-rails'

# encoding : utf-8
Money.locale_backend = :i18n

MoneyRails.configure do |config|

# To set the default currency
#

# Set default bank object
#
# Example:
# config.default_bank = EuCentralBank.new

# Add exchange rates to current money bank object.
# (The conversion rate refers to one direction only)
#
# Example:
# config.add_rate "USD", "CAD", 1.24515
# config.add_rate "CAD", "USD", 0.803115

# To handle the inclusion of validations for monetized fields
# The default value is true
#
# config.include_validations = true

# Default ActiveRecord migration configuration values for columns:
#
config.amount_column = { prefix: '', # column name prefix
postfix: '_subunits', # column name postfix
column_name: nil, # full column name (overrides prefix, postfix and accessor name)
type: :bigint, # column type
present: true, # column will be created
null: false, # other options will be treated as column options
default: 0
}

config.currency_column = { prefix: '',
postfix: '_currency',
column_name: nil,
type: :string,
present: true,
null: false,
default: 'US8'
}

config.register_currency = {
:priority => 1,
:iso_code => :us8,
:name => "US Dollar with subunit of 8 digits",
:symbol => "$",
:symbol_first => true,
:subunit => "Subcent",
:subunit_to_unit => 100_000_000,
:thousands_separator => ",",
:decimal_mark => "."
}

config.add_rate "USD", "US8", 1
config.add_rate "US8", "USD", 1

config.default_currency = :us8

# Register a custom currency
#
# Example:
# config.register_currency = {
# :priority => 1,
# :iso_code => "EU4",
# :name => "Euro with subunit of 4 digits",
# :symbol => "€",
# :symbol_first => true,
# :subunit => "Subcent",
# :subunit_to_unit => 10000,
# :thousands_separator => ".",
# :decimal_mark => ","
# }

# Specify a rounding mode
# Any one of:
#
# BigDecimal::ROUND_UP,
# BigDecimal::ROUND_DOWN,
# BigDecimal::ROUND_HALF_UP,
# BigDecimal::ROUND_HALF_DOWN,
# BigDecimal::ROUND_HALF_EVEN,
# BigDecimal::ROUND_CEILING,
# BigDecimal::ROUND_FLOOR
#
# set to BigDecimal::ROUND_HALF_EVEN by default
#
# config.rounding_mode = BigDecimal::ROUND_HALF_UP

# Set default money format globally.
# Default value is nil meaning "ignore this option".
# Example:
#
# config.default_format = {
# :no_cents_if_whole => nil,
# :symbol => nil,
# :sign_before_symbol => nil
# }

# Set default raise_error_on_money_parsing option
# It will be raise error if assigned different currency
# The default value is false
#
# Example:
# config.raise_error_on_money_parsing = false
end

0 comments on commit df7eb32

Please sign in to comment.