active_users_measure.rb 848 B

123456789101112131415161718192021222324252627282930313233
  1. # frozen_string_literal: true
  2. class Admin::Metrics::Measure::ActiveUsersMeasure < Admin::Metrics::Measure::BaseMeasure
  3. def key
  4. 'active_users'
  5. end
  6. protected
  7. def perform_total_query
  8. activity_tracker.sum(time_period.first, time_period.last)
  9. end
  10. def perform_previous_total_query
  11. activity_tracker.sum(previous_time_period.first, previous_time_period.last)
  12. end
  13. def perform_data_query
  14. activity_tracker.get(time_period.first, time_period.last).map { |date, value| { date: date.to_time(:utc).iso8601, value: value.to_s } }
  15. end
  16. def activity_tracker
  17. @activity_tracker ||= ActivityTracker.new('activity:logins', :unique)
  18. end
  19. def time_period
  20. (@start_at.to_date..@end_at.to_date)
  21. end
  22. def previous_time_period
  23. ((@start_at.to_date - length_of_period)..(@end_at.to_date - length_of_period))
  24. end
  25. end