From e0ecace0b141a9ceb203fa58ce49f2cfa509f68c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Thu, 20 Oct 2016 14:33:33 +0200 Subject: [PATCH] Default interval should be property of ReportOptions --- app/models/chargeback.rb | 9 ++++----- app/models/chargeback/report_options.rb | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/models/chargeback.rb b/app/models/chargeback.rb index 2a8ef7e92d9..5a5de71d2cb 100644 --- a/app/models/chargeback.rb +++ b/app/models/chargeback.rb @@ -10,7 +10,6 @@ def self.build_results_for_report_chargeback(options) _log.info("Calculating chargeback costs...") @options = options = ReportOptions.new_from_h(options) - interval = options[:interval] || "daily" cb = new options[:ext_options] ||= {} @@ -34,10 +33,10 @@ def self.build_results_for_report_chargeback(options) rate_cols.map! { |x| VIRTUAL_COL_USES.include?(x) ? VIRTUAL_COL_USES[x] : x }.flatten! base_rollup = base_rollup.select(*rate_cols) - timerange = get_report_time_range(options, interval) + timerange = get_report_time_range(options, options.interval) data = {} - interval_duration = interval_to_duration(interval) + interval_duration = interval_to_duration(options.interval) timerange.step_value(interval_duration).each_cons(2) do |query_start_time, query_end_time| records = base_rollup.where(:timestamp => query_start_time...query_end_time, :capture_interval_name => "hourly") @@ -46,7 +45,7 @@ def self.build_results_for_report_chargeback(options) next if records.empty? _log.info("Found #{records.length} records for time range #{[query_start_time, query_end_time].inspect}") - hours_in_interval = hours_in_interval(query_start_time, query_end_time, interval) + hours_in_interval = hours_in_interval(query_start_time, query_end_time, options.interval) # we are building hash with grouped calculated values # values are grouped by resource_id and timestamp (query_start_time...query_end_time) @@ -62,7 +61,7 @@ def self.build_results_for_report_chargeback(options) # key contains resource_id and timestamp (query_start_time...query_end_time) # extra_fields there some extra field like resource name and # some of them are related to specific chargeback (ChargebackVm, ChargebackContainer,...) - key, extra_fields = key_and_fields(metric_rollup_record, interval) + key, extra_fields = key_and_fields(metric_rollup_record, options.interval) data[key] ||= extra_fields chargeback_rates = data[key]["chargeback_rates"].split(', ') + rates_to_apply.collect(&:description) diff --git a/app/models/chargeback/report_options.rb b/app/models/chargeback/report_options.rb index fa2d6c74df9..4755031acf5 100644 --- a/app/models/chargeback/report_options.rb +++ b/app/models/chargeback/report_options.rb @@ -19,6 +19,11 @@ def self.new_from_h(hash) new(*hash.values_at(*members)) end + def initialize(*) + super + self.interval ||= 'daily' + end + def tz # TODO: Support time profiles via options[:ext_options][:time_profile] @tz ||= Metric::Helper.get_time_zone(ext_options)