Skip to content

Commit

Permalink
Calculate Metering Used Hours only from used hours
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Dec 18, 2017
1 parent 9faa390 commit bfd22ce
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/models/chargeback/consumption_with_rollups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def chargeback_fields_present
@chargeback_fields_present ||= @rollups.count(&:chargeback_fields_present?)
end

def metering_used_fields_present
@metering_used_fields_present ||= @rollups.count(&:metering_used_fields_present?)
end

private

def born_at
Expand Down
4 changes: 4 additions & 0 deletions app/models/chargeback/consumption_without_rollups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def chargeback_fields_present
1 # Yes, charge this interval as fixed_compute_*_*
end

def metering_used_fields_present
0 # we don't count used hours in metering report
end

def current_value(metric, _sub_metric = nil)
# Return the last seen allocation for charging purposes.
@value ||= {}
Expand Down
2 changes: 1 addition & 1 deletion app/models/metering.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Metering
def calculate_costs(consumption, _)
self.fixed_compute_metric = consumption.chargeback_fields_present if consumption.chargeback_fields_present
self.metering_used_metric = fixed_compute_metric
self.metering_used_metric = consumption.metering_used_fields_present if consumption.metering_used_fields_present
self.existence_hours_metric = consumption.consumed_hours_in_interval

relevant_fields.each do |field|
Expand Down
6 changes: 6 additions & 0 deletions app/models/metric_rollup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class MetricRollup < ApplicationRecord
net_usage_rate_average derived_vm_used_disk_storage
derived_vm_allocated_disk_storage).freeze

METERING_USED_METRIC_FIELDS = %w(cpu_usagemhz_rate_average derived_memory_used net_usage_rate_average).freeze

CAPTURE_INTERVAL_NAMES = %w(hourly daily).freeze

#
Expand Down Expand Up @@ -70,4 +72,8 @@ def chargeback_fields_present?

@chargeback_fields_present = CHARGEBACK_METRIC_FIELDS.any? { |field| send(field).present? && send(field).nonzero? }
end

def metering_used_fields_present?
@metering_used_fields_present ||= METERING_USED_METRIC_FIELDS.any? { |field| send(field).present? && send(field).nonzero? }
end
end
20 changes: 20 additions & 0 deletions spec/models/metering_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,25 @@
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)
end

context 'count of used hours is different than count of metric rollups' do
let(:used_metric_attributtes_with_zeros) do
# create hash with metrics as keys with zeros in values
# e.g. {"cpu_usagemhz_rate_average"=>0, "derived_memory_used"=>0, ...
Hash[MetricRollup::METERING_USED_METRIC_FIELDS.inject([]) { |result_array, metric| result_array << [metric, 0] }]
end

let(:count_of_metric_rollup_with_zero_used_metric) { 20 }

before do
vm.metric_rollups.limit(20).each { |record| record.update(used_metric_attributtes_with_zeros) }
end

it 'calculates metering used hours only from used metrics' do
expect(subject.metering_used_metric).to eq(vm.metric_rollups.count - count_of_metric_rollup_with_zero_used_metric)
expect(subject.metering_used_metric).to eq(40)
expect(subject.metering_used_metric).not_to eq(subject.fixed_compute_metric)
end
end
end
end

0 comments on commit bfd22ce

Please sign in to comment.