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

Better formatting for rate columns in chargeback reports #20228

Merged
merged 1 commit into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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