From f5df793288a8250880588869cf2eda63b3c314d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Thu, 20 Oct 2016 14:43:30 +0200 Subject: [PATCH] Refactor: move and rename method: report_time_range The chargeback's timerange depends only on Chargeback report options. --- app/models/chargeback.rb | 28 +------------------------ app/models/chargeback/report_options.rb | 24 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/app/models/chargeback.rb b/app/models/chargeback.rb index 5a5de71d2cb..74aecefcfc8 100644 --- a/app/models/chargeback.rb +++ b/app/models/chargeback.rb @@ -33,7 +33,7 @@ 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, options.interval) + timerange = options.report_time_range data = {} interval_duration = interval_to_duration(options.interval) @@ -223,32 +223,6 @@ def self.get_time_range(perf, interval) end end - # @option options :interval_size [Fixednum] Used with :end_interval_offset to generate time range - # @option options :end_interval_offset - def self.get_report_time_range(options, interval) - raise _("Option 'interval_size' is required") if options[:interval_size].nil? - - end_interval_offset = options[:end_interval_offset] || 0 - start_interval_offset = (end_interval_offset + options[:interval_size] - 1) - - ts = Time.now.in_time_zone(options.tz) - case interval - when "daily" - start_time = (ts - start_interval_offset.days).beginning_of_day.utc - end_time = (ts - end_interval_offset.days).end_of_day.utc - when "weekly" - start_time = (ts - start_interval_offset.weeks).beginning_of_week.utc - end_time = (ts - end_interval_offset.weeks).end_of_week.utc - when "monthly" - start_time = (ts - start_interval_offset.months).beginning_of_month.utc - end_time = (ts - end_interval_offset.months).end_of_month.utc - else - raise _("interval '%{interval}' is not supported") % {:interval => interval} - end - - start_time..end_time - end - def self.report_cb_model(model) model.gsub(/^Chargeback/, "") end diff --git a/app/models/chargeback/report_options.rb b/app/models/chargeback/report_options.rb index 4755031acf5..9ea580365ad 100644 --- a/app/models/chargeback/report_options.rb +++ b/app/models/chargeback/report_options.rb @@ -22,11 +22,35 @@ def self.new_from_h(hash) def initialize(*) super self.interval ||= 'daily' + self.end_interval_offset ||= 0 end def tz # TODO: Support time profiles via options[:ext_options][:time_profile] @tz ||= Metric::Helper.get_time_zone(ext_options) end + + def report_time_range + raise _("Option 'interval_size' is required") if interval_size.nil? + + start_interval_offset = (end_interval_offset + interval_size - 1) + + ts = Time.now.in_time_zone(tz) + case interval + when 'daily' + start_time = (ts - start_interval_offset.days).beginning_of_day.utc + end_time = (ts - end_interval_offset.days).end_of_day.utc + when 'weekly' + start_time = (ts - start_interval_offset.weeks).beginning_of_week.utc + end_time = (ts - end_interval_offset.weeks).end_of_week.utc + when 'monthly' + start_time = (ts - start_interval_offset.months).beginning_of_month.utc + end_time = (ts - end_interval_offset.months).end_of_month.utc + else + raise _("interval '%{interval}' is not supported") % {:interval => interval} + end + + start_time..end_time + end end end