about summary refs log tree commit diff
path: root/app/lib/activity_tracker.rb
blob: ae3c11b6af5c99e6f852f2ac8d284218839184ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true

class ActivityTracker
  EXPIRE_AFTER = 90.days.seconds

  class << self
    include Redisable

    def increment(prefix)
      key = [prefix, current_week].join(':')

      redis.incrby(key, 1)
      redis.expire(key, EXPIRE_AFTER)
    end

    def record(prefix, value)
      key = [prefix, current_week].join(':')

      redis.pfadd(key, value)
      redis.expire(key, EXPIRE_AFTER)
    end

    private

    def current_week
      Time.zone.today.cweek
    end
  end
end