about summary refs log tree commit diff
path: root/app/lib/activity_tracker.rb
blob: 6d3401b37bb36f44770b23523d59a02bd51c4c9a (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true

class ActivityTracker
  include Redisable

  EXPIRE_AFTER = 6.months.seconds

  def initialize(prefix, type)
    @prefix = prefix
    @type   = type
  end

  def add(value = 1, at_time = Time.now.utc)
    key = key_at(at_time)

    case @type
    when :basic
      redis.incrby(key, value)
    when :unique
      redis.pfadd(key, value)
    end

    redis.expire(key, EXPIRE_AFTER)
  end

  def get(start_at, end_at = Time.now.utc)
    (start_at.to_date...end_at.to_date).map do |date|
      key = key_at(date.to_time(:utc))

      value = begin
        case @type
        when :basic
          redis.get(key).to_i
        when :unique
          redis.pfcount(key)
        end
      end

      [date, value]
    end
  end

  def sum(start_at, end_at = Time.now.utc)
    keys = (start_at.to_date...end_at.to_date).flat_map { |date| [key_at(date.to_time(:utc)), legacy_key_at(date)] }.uniq

    case @type
    when :basic
      redis.mget(*keys).map(&:to_i).sum
    when :unique
      redis.pfcount(*keys)
    end
  end

  class << self
    def increment(prefix)
      new(prefix, :basic).add
    end

    def record(prefix, value)
      new(prefix, :unique).add(value)
    end
  end

  private

  def key_at(at_time)
    "#{@prefix}:#{at_time.beginning_of_day.to_i}"
  end

  def legacy_key_at(at_time)
    "#{@prefix}:#{at_time.to_date.cweek}"
  end
end