Skip to content

Commit

Permalink
Improve rate formatting in ChargebackRateDetail.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Jun 1, 2020
1 parent cd61222 commit 3897ce5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
17 changes: 14 additions & 3 deletions app/models/chargeback_rate_detail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,23 @@ def sub_metric_human
sub_metric.present? ? sub_metric.capitalize : 'All'
end

def format_rate(rate, suffix = "")
MiqReport.new.format_currency_with_delimiter(rate, :unit => detail_currency.symbol) + " / " + PER_TIME_TYPES[per_time] + suffix
end

def format_unit(rate_unit)
unit = showback_unit(rate_unit)
unit.presence || Dictionary.gettext(per_unit, :notfound => :titleize)
end

def rate_values(consumption, options)
fixed_rate, variable_rate = find_rate(chargeable_field.measure(consumption, options, sub_metric))
hourly_fixed_rate = hourly(fixed_rate, consumption)
hourly_variable_rate = hourly(variable_rate, consumption)

"#{hourly_fixed_rate}/#{hourly_variable_rate}"
rates = []
rates.push(format_rate(fixed_rate)) if fixed_rate > 0.0
rates.push(format_rate(variable_rate, " / #{format_unit(per_unit)}")) if variable_rate > 0.0

rates.join(" + ")
end

def charge(consumption, options)
Expand Down
25 changes: 13 additions & 12 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

let(:hourly_variable_tier_rate) { {:variable_rate => hourly_rate.to_s} }
let(:count_hourly_variable_tier_rate) { {:variable_rate => count_hourly_rate.to_s} }
let(:fixed_compute_tier_rate) { {:fixed_rate => hourly_rate.to_s} }

let(:detail_params) do
{
Expand All @@ -35,7 +36,7 @@
:chargeback_rate_detail_net_io_used => {:tiers => [hourly_variable_tier_rate]},
:chargeback_rate_detail_storage_used => {:tiers => [count_hourly_variable_tier_rate]},
:chargeback_rate_detail_storage_allocated => {:tiers => [count_hourly_variable_tier_rate]},
:chargeback_rate_detail_fixed_compute_cost => {:tiers => [hourly_variable_tier_rate]}
:chargeback_rate_detail_fixed_compute_cost => {:tiers => [fixed_compute_tier_rate]}
}
end

Expand Down Expand Up @@ -183,8 +184,8 @@ def pluck_rollup(metric_rollup_records)
end

it 'shows rates' do
expect(subject.storage_allocated_sdd_rate).to eq("0.0/1.0")
expect(subject.storage_allocated_hdd_rate).to eq("0.0/1.0")
expect(subject.storage_allocated_sdd_rate).to eq("€1.00 / Hourly / GiB")
expect(subject.storage_allocated_hdd_rate).to eq("€1.00 / Hourly / GiB")
end

it "doesn't return removed cloud volume types fields" do
Expand Down Expand Up @@ -740,15 +741,15 @@ def result_row_by(chargeback_result, date)
let(:hourly_variable_tier_rate) { {:fixed_rate => 100, :variable_rate => hourly_rate.to_s} }

it 'shows rates' do
expect(subject.cpu_allocated_rate).to eq("0.0/1.0")
expect(subject.cpu_used_rate).to eq("100.0/0.01")
expect(subject.disk_io_used_rate).to eq("100.0/0.01")
expect(subject.fixed_compute_1_rate).to eq("100.0/0.01")
expect(subject.memory_allocated_rate).to eq("100.0/0.01")
expect(subject.memory_used_rate).to eq("100.0/0.01")
expect(subject.net_io_used_rate).to eq("100.0/0.01")
expect(subject.storage_allocated_rate).to eq("0.0/1.0")
expect(subject.storage_used_rate).to eq("0.0/1.0")
expect(subject.cpu_allocated_rate).to eq("€1.00 / Hourly / Cpu")
expect(subject.cpu_used_rate).to eq("100.00 / Hourly + €0.01 / Hourly / MHz")
expect(subject.disk_io_used_rate).to eq("100.00 / Hourly + €0.01 / Hourly / Mbps")
expect(subject.fixed_compute_1_rate).to eq("€0.01 / Hourly")
expect(subject.memory_allocated_rate).to eq("100.00 / Hourly + €0.01 / Hourly / MiB")
expect(subject.memory_used_rate).to eq("100.00 / Hourly + €0.01 / Hourly / MiB")
expect(subject.net_io_used_rate).to eq("100.00 / Hourly + €0.01 / Hourly / Mbps")
expect(subject.storage_allocated_rate).to eq("€1.00 / Hourly / GiB")
expect(subject.storage_used_rate).to eq("€1.00 / Hourly / GiB")
end
end

Expand Down

0 comments on commit 3897ce5

Please sign in to comment.