统计在线用户

ecloud_sso
anke1460 5 years ago
parent 05b0a14323
commit 92e88f08d1

@ -35,6 +35,7 @@ module LoginHelper
UserAction.create(action_id: user&.id, action_type: 'Login', user_id: user&.id, ip: request.remote_ip) UserAction.create(action_id: user&.id, action_type: 'Login', user_id: user&.id, ip: request.remote_ip)
user.update_column(:last_login_on, Time.now) user.update_column(:last_login_on, Time.now)
# 注册完成后有一天的试用申请(先去掉) # 注册完成后有一天的试用申请(先去掉)
# UserDayCertification.create(user_id: user.id, status: 1) # UserDayCertification.create(user_id: user.id, status: 1)
end end
@ -44,6 +45,8 @@ module LoginHelper
if autologin = cookies.delete(autologin_cookie_name) if autologin = cookies.delete(autologin_cookie_name)
User.current.delete_autologin_token(autologin) User.current.delete_autologin_token(autologin)
end end
UserOnline.logout(User.current.id)
User.current.delete_session_token(session[:tk]) User.current.delete_session_token(session[:tk])
self.logged_user = nil self.logged_user = nil
end end
@ -52,6 +55,7 @@ module LoginHelper
default_yun_session = "#{laboratory.try(:identifier).split('.').first}_user_id" default_yun_session = "#{laboratory.try(:identifier).split('.').first}_user_id"
# end # end
session[:"#{default_yun_session}"] = nil session[:"#{default_yun_session}"] = nil
session[:request_user_id] = nil
end end
# Sets the logged in user # Sets the logged in user
@ -78,6 +82,8 @@ module LoginHelper
# # end # # end
# session[:user_id] = user.id # session[:user_id] = user.id
UserOnline.login(user.id)
session[:request_user_id] = user.id
session[:"#{default_yun_session}"] = user.id session[:"#{default_yun_session}"] = user.id
session[:ctime] = Time.now.utc.to_i session[:ctime] = Time.now.utc.to_i
session[:atime] = Time.now.utc.to_i session[:atime] = Time.now.utc.to_i

@ -4,6 +4,7 @@ class MainController < ApplicationController
skip_before_action :setup_laboratory skip_before_action :setup_laboratory
def first_stamp def first_stamp
UserOnline.login(session[:request_user_id]) if session[:request_user_id]
render :json => { status: 0, message: Time.now.to_i } render :json => { status: 0, message: Time.now.to_i }
end end

@ -0,0 +1,41 @@
module UserOnline
class << self
def login(user_id)
set_bit(user_id, 1)
end
def logout(user_id)
set_bit(user_id, 0)
end
def set_bit(user_id, flag)
if !Rails.cache.data.exists(cache_key)
Rails.cache.data.setbit(cache_key, user_id, flag)
Rails.cache.data.expire(cache_key, 20 * 60 + 10)
else
Rails.cache.data.setbit(cache_key, user_id, flag)
end
end
def count
if Rails.cache.is_a?(ActiveSupport::Cache::RedisStore)
Rails.cache.data.bitcount(cache_key)
else
0
end
end
def cache_key
if Rails.cache.is_a?(ActiveSupport::Cache::RedisStore)
# 10分钟为一段记录用户在线, 统计范围为20分钟内的线用户
# TODO 更精确时长
begin_hour = Time.now.beginning_of_hour
minutes_piece = (Time.now - begin_hour) / 600
time = begin_hour.since((minutes_piece.to_i - 1) * 600).strftime("%H-%M")
"online_user_#{time}"
else
raise '请配置config.cache_store = redis_store'
end
end
end
end

@ -72,7 +72,7 @@
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<h5 class="card-title text-uppercase text-muted mb-0">当前在线用户数</h5> <h5 class="card-title text-uppercase text-muted mb-0">当前在线用户数</h5>
<span class="h2 font-weight-bold mb-0"><%= @online_users %></span> <span class="h2 font-weight-bold mb-0"><%= UserOnline.count %></span>
</div> </div>
<div class="col-auto"> <div class="col-auto">
<div class="icon icon-shape rounded-circle shadow"> <div class="icon icon-shape rounded-circle shadow">

Loading…
Cancel
Save