Skip to content

Commit

Permalink
Merge pull request #17909 from lpichler/add_date_only_option_to_charg…
Browse files Browse the repository at this point in the history
…eback_fine

[FINE] Add possibily to group by date only in chargeback
  • Loading branch information
simaishi committed Aug 27, 2018
2 parents 9cfc0ea + 13ed59f commit 72b1cef
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
19 changes: 17 additions & 2 deletions app/models/chargeback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def self.report_row_key(consumption)
classification = @options.classification_for(consumption)
classification_id = classification.present? ? classification.id : 'none'
"#{classification_id}_#{ts_key}"
elsif @options.group_by_date_only?
ts_key
else
default_key(consumption, ts_key)
end
Expand All @@ -63,8 +65,19 @@ def initialize(options, consumption)
self.entity ||= consumption.resource
end

def calculate_fixed_compute_metric(consumption)
return unless consumption.chargeback_fields_present

if @options.group_by_date_only?
self.fixed_compute_metric ||= 0
self.fixed_compute_metric += consumption.chargeback_fields_present
else
self.fixed_compute_metric = consumption.chargeback_fields_present
end
end

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

rates.each do |rate|
rate.rate_details_relevant_to(relevant_fields).each do |r|
Expand All @@ -91,7 +104,9 @@ def self.report_tag_field
def self.set_chargeback_report_options(rpt, group_by, header_for_tag, tz)
rpt.cols = %w(start_date display_range)

static_cols = group_by == "project" ? report_static_cols - ["image_name"] : report_static_cols
static_cols = report_static_cols
static_cols -= ["image_name"] if group_by == "project"
static_cols -= ["vm_name"] if group_by == "date-only"
static_cols = group_by == "tag" ? [report_tag_field] : static_cols
rpt.cols += static_cols
rpt.col_order = static_cols + ["display_range"]
Expand Down
5 changes: 5 additions & 0 deletions app/models/chargeback/report_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Chargeback
:userid,
:ext_options,
:include_metrics, # enable charging allocated resources with C & U
:group_by_date_only?
) do
def self.new_from_h(hash)
new(*hash.values_at(*members))
Expand Down Expand Up @@ -107,6 +108,10 @@ def classification_for(consumption)
tag_hash[tag]
end

def group_by_date_only?
self[:groupby] == 'date-only'
end

private

def tag_hash
Expand Down
68 changes: 68 additions & 0 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,73 @@
expect(subject.storage_cost).to eq(subject.storage_allocated_cost + subject.storage_used_cost)
end
end

context 'test against group by date-only report' do
let(:options_group_date_only) do
{
:interval => "daily",
:interval_size => 7,
:end_interval_offset => 0,
:owner => admin.userid,
:method_for_allocated_metrics => :max,
:include_metrics => true,
:groupby => "date-only"
}
end

let(:options_group_date) do
{
:interval => "daily",
:interval_size => 7,
:end_interval_offset => 0,
:owner => admin.userid,
:method_for_allocated_metrics => :max,
:include_metrics => true,
:groupby => "date"
}
end

let(:result_group_by_date_only) { ChargebackVm.build_results_for_report_ChargebackVm(options_group_date_only).first }
let(:result_group_by_date) { ChargebackVm.build_results_for_report_ChargebackVm(options_group_date).first }

let(:vm_1_1) { FactoryGirl.create(:vm_vmware, :created_on => month_beginning, :miq_group => nil) }
let(:vm_2_1) { FactoryGirl.create(:vm_vmware, :created_on => month_beginning, :miq_group => nil) }

before do
add_metric_rollups_for([vm_1_1, vm_2_1], month_beginning...month_end, 8.hours, metric_rollup_params.merge!(:derived_vm_numvcpus => 1, :cpu_usagemhz_rate_average => 50))
end

def result_row_by(chargeback_result, date)
chargeback_result.select { |x| x.display_range == date }
end

it 'is grouping values per date' do
skip('this feature needs to be added to new chargeback') if Settings.new_chargeback

((month_end - 5.days)..month_end).step_value(1.day) do |display_range|
display_range = display_range.strftime('%m/%d/%Y')
rs1 = result_row_by(result_group_by_date_only, display_range)
rs2 = result_row_by(result_group_by_date, display_range)

%w(cpu_allocated_metric
cpu_allocated_cost
cpu_used_metric
cpu_used_cost
disk_io_used_metric
disk_io_used_cost
fixed_compute_metric
fixed_compute_1_cost
memory_allocated_metric
memory_allocated_cost
net_io_used_metric
net_io_used_cost
storage_allocated_metric
storage_allocated_cost
storage_used_metric
storage_used_cost).each { |field| expect(rs2.map { |x| x.send(field) }.sum).to eq(rs1.map { |x| x.send(field) }.sum) }
end
end
end
end

context "Report a chargeback of a tenant" do
Expand All @@ -248,6 +315,7 @@

context "Monthly" do
let(:options) { base_options.merge(:interval => 'monthly') }

before do
add_metric_rollups_for(@vm1, month_beginning...month_end, 12.hours, metric_rollup_params)
end
Expand Down

0 comments on commit 72b1cef

Please sign in to comment.