diff options
author | Rémy Coutable <remy@rymai.me> | 2017-03-07 19:34:43 +0100 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-04-14 15:20:55 +0200 |
commit | cfe19b795e076b73df75ee57839640667283651c (patch) | |
tree | e5de8f36de866a24a9e1068063a115640062e801 /lib | |
parent | 91ac0e038ab51dd2f30f2bb7c91837fa588ca250 (diff) | |
download | gitlab-ce-cfe19b795e076b73df75ee57839640667283651c.tar.gz |
Add a new Gitlab::UserActivities class to track user activities
This new class uses a Redis Hash instead of a Sorted Set.
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/user_activities.rb | 34 | ||||
-rw-r--r-- | lib/gitlab/user_activities/activity.rb | 16 | ||||
-rw-r--r-- | lib/gitlab/user_activities/activity_set.rb | 67 |
3 files changed, 34 insertions, 83 deletions
diff --git a/lib/gitlab/user_activities.rb b/lib/gitlab/user_activities.rb new file mode 100644 index 00000000000..eb36ab9fded --- /dev/null +++ b/lib/gitlab/user_activities.rb @@ -0,0 +1,34 @@ +module Gitlab + class UserActivities + include Enumerable + + KEY = 'users:activities'.freeze + BATCH_SIZE = 500 + + def self.record(key, time = Time.now) + Gitlab::Redis.with do |redis| + redis.hset(KEY, key, time.to_i) + end + end + + def delete(*keys) + Gitlab::Redis.with do |redis| + redis.hdel(KEY, keys) + end + end + + def each + cursor = 0 + loop do + cursor, pairs = + Gitlab::Redis.with do |redis| + redis.hscan(KEY, cursor, count: BATCH_SIZE) + end + + Hash[pairs].each { |pair| yield pair } + + break if cursor == '0' + end + end + end +end diff --git a/lib/gitlab/user_activities/activity.rb b/lib/gitlab/user_activities/activity.rb deleted file mode 100644 index ec052870ee3..00000000000 --- a/lib/gitlab/user_activities/activity.rb +++ /dev/null @@ -1,16 +0,0 @@ -module Gitlab - module UserActivities - class Activity - attr_reader :username - - def initialize(username, time) - @username = username - @time = time - end - - def last_activity_at - @last_activity_at ||= Time.at(@time).to_s(:db) - end - end - end -end diff --git a/lib/gitlab/user_activities/activity_set.rb b/lib/gitlab/user_activities/activity_set.rb deleted file mode 100644 index 6b8e540e99b..00000000000 --- a/lib/gitlab/user_activities/activity_set.rb +++ /dev/null @@ -1,67 +0,0 @@ -module Gitlab - module UserActivities - class ActivitySet - delegate :total_count, - :total_pages, - :current_page, - :limit_value, - :first_page?, - :prev_page, - :last_page?, - :next_page, to: :pagination_delegate - - KEY = 'user/activities' - - def self.record(user) - Gitlab::Redis.with do |redis| - redis.zadd(KEY, Time.now.to_i, user.username) - end - end - - def initialize(from: nil, page: nil, per_page: nil) - @from = sanitize_date(from) - @to = Time.now.to_i - @page = page - @per_page = per_page - end - - def activities - @activities ||= raw_activities.map { |activity| Activity.new(*activity) } - end - - private - - def sanitize_date(date) - Time.strptime(date, "%Y-%m-%d").to_i - rescue TypeError, ArgumentError - default_from - end - - def pagination_delegate - @pagination_delegate ||= Gitlab::PaginationDelegate.new(page: @page, - per_page: @per_page, - count: count) - end - - def raw_activities - Gitlab::Redis.with do |redis| - redis.zrangebyscore(KEY, @from, @to, with_scores: true, limit: limit) - end - end - - def count - Gitlab::Redis.with do |redis| - redis.zcount(KEY, @from, @to) - end - end - - def limit - [pagination_delegate.offset, pagination_delegate.limit_value] - end - - def default_from - 6.months.ago.to_i - end - end - end -end |