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



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

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

.all_keys(offset: 0) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument



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

def 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



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

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

.day_keys(offset: 0) ⇒ Object



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

def 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



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

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

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

.hour_key(offset: 0) ⇒ Object



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

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

.hour_keys(offset: 0) ⇒ Object



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

def 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



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

def 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



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

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

.month_keys(offset: 0) ⇒ Object



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

def 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



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

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

.year_keys(offset: 0) ⇒ Object



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

def 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