Module: ThreeScale::Backend::TimeHacks

Defined in:
lib/3scale/backend/extensions/time.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ONE_MINUTE =
60
ONE_HOUR =
60 * ONE_MINUTE
ONE_DAY =
24 * ONE_HOUR

Instance Method Summary collapse

Instance Method Details

#beginning_of_bucket(seconds_in_bucket) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/3scale/backend/extensions/time.rb', line 8

def beginning_of_bucket(seconds_in_bucket)
  if seconds_in_bucket > 30 || seconds_in_bucket < 1 || !seconds_in_bucket.is_a?(Integer)
    raise Exception, "seconds_in_bucket cannot be larger than 30 seconds or smaller than 1"
  end
  norm_sec = (sec/seconds_in_bucket)*seconds_in_bucket
  self.class.utc(year, month, day, hour, min, norm_sec)
end

#to_compact_sObject

This function is equivalent to: strftime(‘%Y%m%d%H%M%S’).sub(/00,6$/, ”).

When profiling, we found that this method was one of the ones which consumed more CPU time so we decided to optimize it.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/3scale/backend/extensions/time.rb', line 52

def to_compact_s
  s = year * 10000 +  month * 100 + day
  if sec != 0
    s = s * 100000 + hour * 1000 + min * 10
    MODS[sec] == 0 ? s + DIVS[sec] : s * 10 + sec
  elsif min != 0
    s = s * 1000 + hour * 10
    MODS[min] == 0 ? s + DIVS[min] : s * 10 + min
  elsif hour != 0
    s = s * 10
    MODS[hour] == 0 ? s + DIVS[hour] : s * 10 + hour
  else
    s
  end.to_s
end

#to_not_compact_sObject



68
69
70
71
# File 'lib/3scale/backend/extensions/time.rb', line 68

def to_not_compact_s
  (year * 10000000000 + month * 100000000 + day * 1000000 +
   hour * 10000 + min * 100 + sec).to_s
end