Class: FayeUserOnlineTime
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- FayeUserOnlineTime
- Defined in:
- app/models/faye_user_online_time.rb
Constant Summary collapse
- TIME2013 =
Time.parse("20130101")
- TimeFormat =
"%Y%m%d%H%M%S"
Instance Method Summary collapse
- #data ⇒ Object
- #online_time ⇒ Object
- #resave! ⇒ Object
- #spend_key ⇒ Object
- #spend_time(channel_name) ⇒ Object
- #spend_time_in_realtime(channel_name) ⇒ Object
- #start(channel_name) ⇒ Object
-
#start?(channel_name) ⇒ Boolean
只有两种状态,不管一个用户有多少个连接 1, 没开始: 设置started_at为当前值 2, 关闭所有连接: 去除started_at,更新spended 再添加一个连接: 在调用接口方实现.
- #start_key ⇒ Object
- #start_time(channel_name) ⇒ Object
- #stop(channel_name) ⇒ Object
- #uid ⇒ Object
Instance Method Details
#data ⇒ Object
50 51 52 |
# File 'app/models/faye_user_online_time.rb', line 50 def data @data ||= (JSON.parse(online_time.online_times) rescue {}) end |
#online_time ⇒ Object
57 |
# File 'app/models/faye_user_online_time.rb', line 57 def online_time; self end |
#resave! ⇒ Object
53 54 55 |
# File 'app/models/faye_user_online_time.rb', line 53 def resave! online_time.update_attributes! :online_times => data.to_json end |
#spend_key ⇒ Object
25 |
# File 'app/models/faye_user_online_time.rb', line 25 def spend_key; "#{@channel_name}_spended"; end |
#spend_time(channel_name) ⇒ Object
8 |
# File 'app/models/faye_user_online_time.rb', line 8 def spend_time channel_name; @channel_name = channel_name; data[spend_key] end |
#spend_time_in_realtime(channel_name) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/faye_user_online_time.rb', line 27 def spend_time_in_realtime channel_name _old_spend_time = self.spend_time(channel_name).to_i # 计时为0。 用户打开多个浏览器,在完成课时时其他页面没有关掉,导致faye_online_time不能关闭计时,所以无法计算spend_time _new_spend_time = 0 if _start_time = self.start_time(channel_name) # 兼容 没访问页面前 计时的start_time还没有开始 _new_spend_time = _start_time.blank? ? 0 : (Time.now - Time.parse(_start_time)) end (_old_spend_time + _new_spend_time).round(0) end |
#start(channel_name) ⇒ Object
19 20 21 22 23 |
# File 'app/models/faye_user_online_time.rb', line 19 def start channel_name @channel_name = channel_name data[start_key] = Time.now.strftime(TimeFormat) resave! end |
#start?(channel_name) ⇒ Boolean
只有两种状态,不管一个用户有多少个连接 1, 没开始: 设置started_at为当前值 2, 关闭所有连接: 去除started_at,更新spended 再添加一个连接: 在调用接口方实现
15 16 17 18 |
# File 'app/models/faye_user_online_time.rb', line 15 def start? channel_name @channel_name = channel_name !!data[start_key] end |
#start_key ⇒ Object
24 |
# File 'app/models/faye_user_online_time.rb', line 24 def start_key; "#{@channel_name}_started_at"; end |
#start_time(channel_name) ⇒ Object
9 |
# File 'app/models/faye_user_online_time.rb', line 9 def start_time channel_name; @channel_name = channel_name; data[start_key] end |
#stop(channel_name) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'app/models/faye_user_online_time.rb', line 40 def stop channel_name @channel_name = channel_name if data[start_key] data[spend_key] ||= 0 data[spend_key] += (Time.now - Time.parse(data[start_key])).to_i data.delete start_key resave! end end |
#uid ⇒ Object
58 |
# File 'app/models/faye_user_online_time.rb', line 58 def uid; self.user_id; end |