about summary refs log tree commit diff
path: root/app/lib/admin/metrics/measure/active_users_measure.rb
blob: ac022eb9d150ae4625ef1abad2c4d72b8ce74bf6 (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
# frozen_string_literal: true

class Admin::Metrics::Measure::ActiveUsersMeasure < Admin::Metrics::Measure::BaseMeasure
  def key
    'active_users'
  end

  def total
    activity_tracker.sum(time_period.first, time_period.last)
  end

  def previous_total
    activity_tracker.sum(previous_time_period.first, previous_time_period.last)
  end

  def data
    activity_tracker.get(time_period.first, time_period.last).map { |date, value| { date: date.to_time(:utc).iso8601, value: value.to_s } }
  end

  protected

  def activity_tracker
    @activity_tracker ||= ActivityTracker.new('activity:logins', :unique)
  end

  def time_period
    (@start_at.to_date...@end_at.to_date)
  end

  def previous_time_period
    ((@start_at.to_date - length_of_period)...(@end_at.to_date - length_of_period))
  end
end