diff --git a/app/models/chargeback.rb b/app/models/chargeback.rb index da3b4dc2dc3..1634a935097 100644 --- a/app/models/chargeback.rb +++ b/app/models/chargeback.rb @@ -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 @@ -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| @@ -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"] diff --git a/app/models/chargeback/report_options.rb b/app/models/chargeback/report_options.rb index 0a3995a0d9b..a859c15751f 100644 --- a/app/models/chargeback/report_options.rb +++ b/app/models/chargeback/report_options.rb @@ -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)) @@ -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 diff --git a/spec/models/chargeback_vm_spec.rb b/spec/models/chargeback_vm_spec.rb index ea5ffb1fbcc..b5e872c033c 100644 --- a/spec/models/chargeback_vm_spec.rb +++ b/spec/models/chargeback_vm_spec.rb @@ -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 @@ -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