Skip to content

Commit

Permalink
Merge pull request #18569 from lpichler/add_report_interval_date_and_…
Browse files Browse the repository at this point in the history
…report_generation

Add report interval date and report generation to Chargeback reports
  • Loading branch information
gtanzillo authored Mar 26, 2019
2 parents 68b8aae + 27eac97 commit f025418
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
25 changes: 16 additions & 9 deletions app/models/chargeback.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
class Chargeback < ActsAsArModel
set_columns_hash( # Fields common to any chargeback type
:start_date => :datetime,
:end_date => :datetime,
:interval_name => :string,
:display_range => :string,
:chargeback_rates => :string,
:entity => :binary,
:tag_name => :string,
:label_name => :string,
:fixed_compute_metric => :integer,
:start_date => :datetime,
:end_date => :datetime,
:interval_name => :string,
:display_range => :string,
:report_interval_range => :string,
:report_generation_date => :datetime,
:chargeback_rates => :string,
:entity => :binary,
:tag_name => :string,
:label_name => :string,
:fixed_compute_metric => :integer,
)

ALLOWED_FIELD_SUFFIXES = %w[
_rate
_cost
-owner_name
_metric
-report_interval_range
-report_generation_date
-provider_name
-provider_uid
-project_uid
Expand Down Expand Up @@ -186,6 +190,9 @@ def calculate_fixed_compute_metric(consumption)
def calculate_costs(consumption, rates)
calculate_fixed_compute_metric(consumption)
self.class.try(:refresh_dynamic_metric_columns)
self.report_interval_range = "#{consumption.report_interval_start.strftime('%m/%d/%Y')} - #{consumption.report_interval_end.strftime('%m/%d/%Y')}"
self.report_generation_date = Time.current

_log.debug("Consumption Type: #{consumption.class}")
rates.each do |rate|
_log.debug("Calculation with rate: #{rate.id} #{rate.description}(#{rate.rate_type})")
Expand Down
8 changes: 8 additions & 0 deletions app/models/chargeback/consumption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def consumption_end
[Time.current, @end_time, resource_end_of_life].compact.min
end

def report_interval_start
@start_time
end

def report_interval_end
[Time.current, @end_time].min
end

private

def hours_in_interval
Expand Down
33 changes: 33 additions & 0 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,39 @@ def pluck_rollup(metric_rollup_records)
expect(subject.cpu_allocated_cost).to eq(1200) # ?
end
end

context "current month and previous month" do
let(:options) { base_options.merge(:interval => 'monthly') }
let(:finish_time) { Time.current }
let(:finish_time_formatted) { finish_time.strftime('%m/%d/%Y') }
let(:report_start) { month_end + 2.days }
subject { ChargebackVm.build_results_for_report_ChargebackVm(options).first }

let(:first_month_beginning) { month_beginning }
let(:first_month_beginning_formatted) { first_month_beginning.strftime('%m/%d/%Y') }
let(:second_month_beginning) { month_beginning + 1.month }
let(:second_month_beginning_formatted) { second_month_beginning.strftime('%m/%d/%Y') }

before do
Timecop.travel(report_start)
@vm1.update_attributes(:retires_on => finish_time)
add_metric_rollups_for(@vm1, month_beginning...finish_time, 8.hours, metric_rollup_params)
end

it "reports report interval range and report generation date" do
skip('this feature needs to be added to new chargeback') if Settings.new_chargeback

# reporting of first month
report_range = "#{first_month_beginning_formatted} - #{second_month_beginning_formatted}"
expect(subject.first.report_interval_range).to eq(report_range)
expect(subject.first.report_generation_date.strftime('%m/%d/%Y')).to eq(finish_time_formatted)

# reporting of second month
report_range = "#{second_month_beginning_formatted} - #{(second_month_beginning + 2.days).strftime('%m/%d/%Y')}"
expect(subject.second.report_interval_range).to eq(report_range)
expect(subject.second.report_generation_date.strftime('%m/%d/%Y')).to eq(finish_time_formatted)
end
end
end

context 'monthly report, group by tenants' do
Expand Down
5 changes: 3 additions & 2 deletions spec/models/metering_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
end

let(:allowed_attributes) do
%w(start_date
%w[start_date
end_date
interval_name
display_range
Expand Down Expand Up @@ -222,7 +222,8 @@
tenant_name
beginning_of_resource_existence_in_report_interval
end_of_resource_existence_in_report_interval
)
report_interval_range
report_generation_date]
end

it 'lists proper attributes' do
Expand Down

0 comments on commit f025418

Please sign in to comment.