From 0bfe78eacc55fbf95b5d187182447312c35e1b97 Mon Sep 17 00:00:00 2001 From: lpichler Date: Wed, 7 Mar 2018 10:36:09 +0100 Subject: [PATCH] Report datetime columns for begining and end of resource --- app/models/chargeback/consumption.rb | 10 ++++++++-- app/models/metering.rb | 2 ++ app/models/metering_container_image.rb | 6 ++++-- app/models/metering_container_project.rb | 6 ++++-- app/models/metering_vm.rb | 6 ++++-- lib/miq_expression.rb | 1 + spec/models/metering_container_image_spec.rb | 2 ++ spec/models/metering_container_project_spec.rb | 2 ++ spec/models/metering_vm_spec.rb | 17 +++++++++++++++++ 9 files changed, 44 insertions(+), 8 deletions(-) diff --git a/app/models/chargeback/consumption.rb b/app/models/chargeback/consumption.rb index 65537d82b15..d26d55abb41 100644 --- a/app/models/chargeback/consumption.rb +++ b/app/models/chargeback/consumption.rb @@ -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 @@ -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 diff --git a/app/models/metering.rb b/app/models/metering.rb index eaacd6869a2..d6999612614 100644 --- a/app/models/metering.rb +++ b/app/models/metering.rb @@ -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) diff --git a/app/models/metering_container_image.rb b/app/models/metering_container_image.rb index ec53e322f5f..58d195a055b 100644 --- a/app/models/metering_container_image.rb +++ b/app/models/metering_container_image.rb @@ -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 diff --git a/app/models/metering_container_project.rb b/app/models/metering_container_project.rb index d7f4edc9cba..d1a52f2f65a 100644 --- a/app/models/metering_container_project.rb +++ b/app/models/metering_container_project.rb @@ -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 diff --git a/app/models/metering_vm.rb b/app/models/metering_vm.rb index 92b66071571..695eb16041c 100644 --- a/app/models/metering_vm.rb +++ b/app/models/metering_vm.rb @@ -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 diff --git a/lib/miq_expression.rb b/lib/miq_expression.rb index 91f92761fa9..e0b9957e6f6 100644 --- a/lib/miq_expression.rb +++ b/lib/miq_expression.rb @@ -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) diff --git a/spec/models/metering_container_image_spec.rb b/spec/models/metering_container_image_spec.rb index 0b64bb58a27..25cbfb3628e 100644 --- a/spec/models/metering_container_image_spec.rb +++ b/spec/models/metering_container_image_spec.rb @@ -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 diff --git a/spec/models/metering_container_project_spec.rb b/spec/models/metering_container_project_spec.rb index 77abaf6fd81..4d278c483d6 100644 --- a/spec/models/metering_container_project_spec.rb +++ b/spec/models/metering_container_project_spec.rb @@ -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 diff --git a/spec/models/metering_vm_spec.rb b/spec/models/metering_vm_spec.rb index 59ac50ee0bf..61cbf97b808 100644 --- a/spec/models/metering_vm_spec.rb +++ b/spec/models/metering_vm_spec.rb @@ -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 @@ -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