Skip to content

Commit

Permalink
Add possibily to group by date only in chargeback
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Aug 22, 2018
1 parent 11229c9 commit 44841fa
Show file tree
Hide file tree
Showing 3 changed files with 82 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 @@ -68,6 +68,8 @@ def self.report_row_key(consumption)
elsif @options.group_by_tenant?
tenant = @options.tenant_for(consumption)
"#{tenant ? tenant.id : 'none'}_#{ts_key}"
elsif @options.group_by_date_only?
ts_key
else
default_key(consumption, ts_key)
end
Expand Down Expand Up @@ -151,8 +153,19 @@ def new_chargeback_calculate_costs(consumption, rates)
end
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)
self.class.try(:refresh_dynamic_metric_columns)

rates.each do |rate|
Expand Down Expand Up @@ -185,7 +198,9 @@ def self.report_label_field
def self.set_chargeback_report_options(rpt, group_by, header_for_tag, groupby_label, 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
static_cols = group_by == "label" ? [report_label_field] : static_cols
static_cols = group_by == "tenant" ? ['tenant_name'] : static_cols
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 @@ -19,6 +19,7 @@ class Chargeback
:include_metrics, # enable charging allocated resources with C & U
:method_for_allocated_metrics,
:group_by_tenant?,
:group_by_date_only?,
:cumulative_rate_calculation,
) do
def self.new_from_h(hash)
Expand Down Expand Up @@ -145,6 +146,10 @@ def group_by_tenant?
self[:groupby] == 'tenant'
end

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

private

def tag_hash
Expand Down
60 changes: 60 additions & 0 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,66 @@ def result_row_for_vm(vm)
expect(subject_row_for_tenant(tenant_1).cpu_allocated_cost).to eq(all_vms_cpu_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,
:tenant_id => tenant_5.id,
: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,
:tenant_id => tenant_5.id,
: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 }

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
end

Expand Down

0 comments on commit 44841fa

Please sign in to comment.