Skip to content

Commit

Permalink
Refactor: move and rename method: report_time_range
Browse files Browse the repository at this point in the history
The chargeback's timerange depends only on Chargeback report options.
  • Loading branch information
isimluk committed Oct 24, 2016
1 parent e0ecace commit f5df793
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
28 changes: 1 addition & 27 deletions app/models/chargeback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions app/models/chargeback/report_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f5df793

Please sign in to comment.