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

Report datetime columns(begining and end of resource's existence) in metering report of VMs #17100

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
10 changes: 8 additions & 2 deletions app/models/chargeback/consumption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ def consumed_hours_in_interval
# 2) We cannot charge for future hours (i.e. weekly report on Monday, should charge just monday)
# 3) We cannot charge for hours after the resource has been retired.
@consumed_hours_in_interval ||= begin
consumption_start = [@start_time, born_at].compact.max
consumption_end = [Time.current, @end_time, resource.try(:retires_on)].compact.min
consumed = (consumption_end - consumption_start).round / 1.hour
consumed > 0 ? consumed : 0
end
Expand All @@ -23,6 +21,14 @@ def hours_in_month
monthly? ? hours_in_interval : (1.month / 1.hour)
end

def consumption_start
[@start_time, born_at].compact.max
end

def consumption_end
[Time.current, @end_time, resource.try(:retires_on)].compact.min
end

private

def hours_in_interval
Expand Down
2 changes: 2 additions & 0 deletions app/models/metering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def calculate_costs(consumption, _)
self.fixed_compute_metric = consumption.chargeback_fields_present if consumption.chargeback_fields_present
self.metering_used_metric = consumption.metering_used_fields_present if consumption.metering_used_fields_present
self.existence_hours_metric = consumption.consumed_hours_in_interval
self.beginning_of_resource_existence_in_report_interval = consumption.consumption_start
self.end_of_resource_existence_in_report_interval = consumption.consumption_end

relevant_fields.each do |field|
next unless self.class.report_col_options.include?(field)
Expand Down
6 changes: 4 additions & 2 deletions app/models/metering_container_image.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class MeteringContainerImage < ChargebackContainerImage
set_columns_hash(
:metering_used_metric => :integer,
:existence_hours_metric => :integer
:metering_used_metric => :integer,
:existence_hours_metric => :integer,
:beginning_of_resource_existence_in_report_interval => :datetime,
:end_of_resource_existence_in_report_interval => :datetime
)

include Metering
Expand Down
6 changes: 4 additions & 2 deletions app/models/metering_container_project.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class MeteringContainerProject < ChargebackContainerProject
set_columns_hash(
:metering_used_metric => :integer,
:existence_hours_metric => :integer
:metering_used_metric => :integer,
:existence_hours_metric => :integer,
:beginning_of_resource_existence_in_report_interval => :datetime,
:end_of_resource_existence_in_report_interval => :datetime
)

include Metering
Expand Down
6 changes: 4 additions & 2 deletions app/models/metering_vm.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class MeteringVm < ChargebackVm
set_columns_hash(
:metering_used_metric => :integer,
:existence_hours_metric => :integer
:metering_used_metric => :integer,
:existence_hours_metric => :integer,
:beginning_of_resource_existence_in_report_interval => :datetime,
:end_of_resource_existence_in_report_interval => :datetime
)

include Metering
Expand Down
1 change: 1 addition & 0 deletions lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,7 @@ def self.reporting_available_fields(model, interval = nil)
model.constantize.try(:refresh_dynamic_metric_columns)
md = model_details(model, :include_model => false, :include_tags => true).select do |c|
allowed_suffixes = ReportController::Reports::Editor::CHARGEBACK_ALLOWED_FIELD_SUFFIXES
allowed_suffixes += ReportController::Reports::Editor::METERING_VM_ALLOWED_FIELD_SUFFIXES if model.starts_with?('Metering')
c.last.ends_with?(*allowed_suffixes)
end
td = if TAG_CLASSES.include?(cb_model)
Expand Down
2 changes: 2 additions & 0 deletions spec/models/metering_container_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
expect(subject.memory_allocated_metric).to eq(@container.limit_memory_bytes / 1.megabytes)
expect(subject.cpu_cores_allocated_metric).to eq(@container.limit_cpu_cores)
expect(subject.cpu_cores_allocated_metric).to eq(@container.limit_memory_bytes / 1.megabytes)
expect(subject.beginning_of_resource_existence_in_report_interval).to eq(month_beginning)
expect(subject.end_of_resource_existence_in_report_interval).to eq(month_beginning + 1.month)
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/models/metering_container_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
expect(subject.metering_used_metric).to eq(count_of_metric_rollup)
expect(subject.existence_hours_metric).to eq(month_beginning.end_of_month.day * 24)
expect(subject.net_io_used_metric).to eq(net_usage_rate_average * count_of_metric_rollup)
expect(subject.beginning_of_resource_existence_in_report_interval).to eq(month_beginning)
expect(subject.end_of_resource_existence_in_report_interval).to eq(month_beginning + 1.month)
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/models/metering_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@
expect(subject.net_io_used_metric).to eq(net_usage_rate_average * count_of_metric_rollup)
expect(subject.storage_allocated_metric).to eq(derived_vm_allocated_disk_storage)
expect(subject.storage_used_metric).to eq(derived_vm_used_disk_storage * count_of_metric_rollup)
expect(subject.beginning_of_resource_existence_in_report_interval).to eq(month_beginning)
expect(subject.end_of_resource_existence_in_report_interval).to eq(month_beginning + 1.month)
end

context "vm started later then beginning of report interval and it was retired earlier then end of report interval " do
let(:beginning_of_resource_existence) { month_beginning + 5.days }
let(:end_of_resource_existence) { month_beginning + 20.days }

it 'uses datetime from Vm#created_on and Vm#retires_on' do
vm.update_attributes(:created_on => beginning_of_resource_existence, :retires_on => end_of_resource_existence)
vm.metric_rollups.each { |mr| mr.update_attributes(:timestamp => beginning_of_resource_existence) }

expect(subject.beginning_of_resource_existence_in_report_interval).to eq(beginning_of_resource_existence)
expect(subject.end_of_resource_existence_in_report_interval).to eq(end_of_resource_existence)
end
end

context 'count of used hours is different than count of metric rollups' do
Expand Down Expand Up @@ -127,6 +142,8 @@
metering_used_metric
existence_hours_metric
tenant_name
beginning_of_resource_existence_in_report_interval
end_of_resource_existence_in_report_interval
)
end

Expand Down