records.py 3.2 KB

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