records.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #*************************************************************************
  2. # Copyright © 2015 JiangLin. All rights reserved.
  3. # File Name: mark_online.py
  4. # Author:JiangLin
  5. # Mail:xiyang0807@gmail.com
  6. # Created Time: 2016-01-07 21:04:12
  7. #*************************************************************************
  8. #!/usr/bin/env python
  9. # -*- coding=UTF-8 -*-
  10. from time import time
  11. from datetime import datetime
  12. from maple import redis_data
  13. from flask import current_app,g
  14. def mark_online(user_ip):
  15. '''记录在线用户'''
  16. pipe = redis_data.pipeline()
  17. config = current_app.config
  18. now_time = int(time()) + 28800
  19. expires = config['ONLINE_LAST_MINUTES'] * 60
  20. '''分钟'''
  21. online_users = 'online_users:%d' % (now_time // 60)
  22. active_users = 'active_users:%s' % user_ip
  23. '''注册用户'''
  24. if g.user is not None and g.user.is_authenticated:
  25. online_sign_users = 'online_sign_users:%d' % (now_time // 60)
  26. active_sign_users = 'active_sign_users:%s' % user_ip
  27. pipe.sadd(online_sign_users,user_ip)
  28. pipe.set(active_sign_users, now_time)
  29. pipe.expire(online_sign_users, expires)
  30. pipe.expire(active_sign_users, expires)
  31. pipe.sadd(online_users,user_ip)
  32. pipe.set(active_users, now_time)
  33. pipe.expire(online_users, expires)
  34. pipe.expire(active_users, expires)
  35. high = redis_data.hget('online_users','high:counts')
  36. if not high:
  37. pipe.hset('online_users','high:counts',1)
  38. high_time = redis_data.hget('online_users','high:time')
  39. if not high_time:
  40. pipe.hset('online_users','high:time',now_time)
  41. pipe.execute()
  42. # def load_user_last_activity(user_ip):
  43. # last_active = redis_data.get('active_users:%s' % user_ip)
  44. # if last_active is None:
  45. # last_active = time() + 28800
  46. # return datetime.utcfromtimestamp(int(last_active))
  47. def load_online_users(mode):
  48. if mode == 'counts':
  49. counts = len(load_online_all_users()) - len(load_online_sign_users())
  50. return counts
  51. if mode == 'all_counts':
  52. counts = len(load_online_all_users())
  53. high = redis_data.hget('online_users','high:counts')
  54. if counts > int(high):
  55. redis_data.hset('online_users','high:counts',counts)
  56. redis_data.hset('online_users','high:time',int(time()) + 28800)
  57. return counts
  58. if mode == 'sign_counts':
  59. return len(load_online_sign_users())
  60. if mode == 'high':
  61. counts = redis_data.hget('online_users','high:counts')
  62. return int(counts)
  63. if mode == 'high_time':
  64. high_time = redis_data.hget('online_users','high:time')
  65. return datetime.utcfromtimestamp(int(high_time))
  66. def load_online_all_users():
  67. config = current_app.config
  68. current = (int(time()) + 28800) // 60
  69. minutes = range(config['ONLINE_LAST_MINUTES'])
  70. return redis_data.sunion(['online_users:%d' % (current - x)
  71. for x in minutes])
  72. def load_online_sign_users():
  73. config = current_app.config
  74. current = (int(time()) + 28800) // 60
  75. minutes = range(config['ONLINE_LAST_MINUTES'])
  76. return redis_data.sunion(['online_sign_users:%d' % (current - x)
  77. for x in minutes])