records.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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:星期六 2017-3-25 20:32:24 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from time import time
  14. from datetime import datetime
  15. from forums.extension import redis_data
  16. from flask import current_app, g
  17. def mark_online(user_ip):
  18. config = current_app.config
  19. now_time = int(time()) + 28800
  20. expires = config.get('ONLINE_LAST_MINUTES', 5) * 60
  21. online_users = 'online_users:%d' % (now_time // 60)
  22. active_users = 'active_users:%s' % user_ip
  23. pipe = redis_data.pipeline()
  24. if 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_online_users(mode):
  43. if mode == 1:
  44. online_users = load_online_all_users()
  45. high_online = redis_data.hget('online_users', 'high:counts')
  46. count = len(online_users)
  47. if int(high_online) < count:
  48. redis_data.hset('online_users', 'high:counts', count)
  49. redis_data.hset('online_users', 'high:time', int(time()) + 28800)
  50. return count
  51. if mode == 2:
  52. # 'online sign users'
  53. online_users = load_online_sign_users()
  54. return len(online_users)
  55. if mode == 3:
  56. # 'guest users'
  57. online_users = load_online_all_users()
  58. online_sign_users = load_online_sign_users()
  59. return len(online_users) - len(online_sign_users)
  60. if mode == 4:
  61. counts = redis_data.hget('online_users', 'high:counts')
  62. return counts
  63. if mode == 5:
  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) for x in
  71. 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])