Class: ActiveRedisStats::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_redis_stats/base.rb

Direct Known Subclasses

Count::Base, Rank::Base

Class Method Summary collapse

Class Method Details

.all_key(offset: 0) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



84
85
86
# File 'lib/active_redis_stats/base.rb', line 84

def self.all_key(offset: 0)
  interval_key(:inf)
end

.all_keys(offset: 0) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument



89
90
91
92
93
94
95
96
# File 'lib/active_redis_stats/base.rb', line 89

def self.all_keys(offset: 0)
  max = 9 + offset
  min = 0 + offset

  max.downto(min).collect do |num|
    interval_key(:all, time: num.years.ago(TIME))
  end
end

.day_key(offset: 0) ⇒ Object



46
47
48
# File 'lib/active_redis_stats/base.rb', line 46

def self.day_key(offset: 0)
  interval_key(:month, time: offset.days.ago(TIME))
end

.day_keys(offset: 0) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/active_redis_stats/base.rb', line 50

def self.day_keys(offset: 0)
  boy = offset.days.ago(TIME).beginning_of_day

  0.upto(23).collect do |num|
    interval_key(:day, time: num.hours.from_now(boy))
  end
end

.expiration(key, seconds) ⇒ Object



98
99
100
101
102
103
# File 'lib/active_redis_stats/base.rb', line 98

def self.expiration(key, seconds)
  return if seconds.nil?

  ActiveRedisDB::Key
    .expire(primary_key(key), seconds)
end

.hour_key(offset: 0) ⇒ Object



32
33
34
# File 'lib/active_redis_stats/base.rb', line 32

def self.hour_key(offset: 0)
  interval_key(:day, time: offset.minutes.ago(TIME))
end

.hour_keys(offset: 0) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/active_redis_stats/base.rb', line 36

def self.hour_keys(offset: 0)
  adj = (30 * offset)
  max = 29 + adj
  min = 0 + adj

  max.downto(min).collect do |num|
    interval_key(:hour, time: num.minutes.ago(TIME))
  end
end

.interval_key(format, time: TIME) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/active_redis_stats/base.rb', line 24

def self.interval_key(format, time: TIME)
  format = format.to_sym
  return "#{format}:#{format}" if format == :inf

  interval = time.format(FORMATS[format])
  "#{format}:#{interval}"
end

.month_key(offset: 0) ⇒ Object



58
59
60
# File 'lib/active_redis_stats/base.rb', line 58

def self.month_key(offset: 0)
  interval_key(:year, time: offset.months.ago(TIME))
end

.month_keys(offset: 0) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/active_redis_stats/base.rb', line 62

def self.month_keys(offset: 0)
  boy = offset.months.ago(TIME).beginning_of_month
  max = boy.end_of_month.day - 1

  0.upto(max).collect do |num|
    interval_key(:month, time: num.days.from_now(boy))
  end
end

.year_key(offset: 0) ⇒ Object



71
72
73
# File 'lib/active_redis_stats/base.rb', line 71

def self.year_key(offset: 0)
  interval_key(:all, time: offset.years.ago(TIME))
end

.year_keys(offset: 0) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/active_redis_stats/base.rb', line 75

def self.year_keys(offset: 0)
  boy = offset.years.ago(TIME).beginning_of_year

  0.upto(11).collect do |num|
    interval_key(:year, time: num.months.from_now(boy))
  end
end