UserWorkRecord.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from hub import methods, Global
  2. import threading
  3. import time
  4. import json
  5. import os
  6. class UserWorkRecord(object):
  7. """
  8. """
  9. users = dict() # {<user_uuid>: {log_uuid: <UserWorkRecordList.uuid>, start_at, end_at}}
  10. @classmethod
  11. def init(cls):
  12. """初始化"""
  13. for item in Global.mdb.get_all('UserWorkRecordList', sort_field=[('start_at', -1)]):
  14. if item.get('user_uuid') not in cls.users:
  15. cls.users[item.get('user_uuid')] = {
  16. 'log_uuid': str(item.get('_id')),
  17. 'start_at': item.get('start_at'),
  18. 'end_at': item.get('end_at'),
  19. }
  20. if item.get('user_uuid') in cls.users:
  21. continue
  22. @classmethod
  23. def get_log_uuid(cls, user_uuid, log_time, vehicle_id, cockpit_id):
  24. """
  25. UserWorkRecordList: 用户操作记录
  26. UserWorkRecordList.uuid: 记录标识
  27. UserWorkRecordList.user_uuid: 用户标识
  28. UserWorkRecordList.start_at: 操作开始时间
  29. UserWorkRecordList.end_at: 操作结束时间
  30. UserWorkRecordList.cockpit_id: 舱端id
  31. UserWorkRecordList.vehicle_id: 车端id
  32. """
  33. # --- check ---
  34. if not user_uuid or not log_time:
  35. methods.debug_log(f"UserWorkRecord|40", f"#error40, user_uuid: {user_uuid}, log_time: {log_time}")
  36. return None
  37. # --- check ---
  38. r1 = user_uuid not in cls.users or log_time > cls.users[user_uuid].get('end_at') + 300 # 距离上次操作间隔300s,当做新的记录
  39. if r1:
  40. # --- add ---
  41. data = {
  42. 'user_uuid': user_uuid,
  43. 'start_at': log_time,
  44. 'end_at': log_time,
  45. 'vehicle_id': vehicle_id,
  46. 'cockpit_id': cockpit_id,
  47. }
  48. log_uuid = Global.mdb.add('UserWorkRecordList', data)
  49. # --- update ---
  50. cls.users[user_uuid] = {
  51. 'log_uuid': log_uuid,
  52. 'start_at': log_time,
  53. 'end_at': log_time,
  54. }
  55. return log_uuid
  56. # --- update --- 距离上次操作间隔60s,更新一下结束时间
  57. log_uuid = cls.users[user_uuid].get('log_uuid')
  58. if log_time > cls.users[user_uuid].get('end_at') + 60:
  59. Global.mdb.update_one_by_id('UserWorkRecordList', log_uuid, {'end_at': log_time})
  60. cls.users[user_uuid]['end_at'] = log_time
  61. return log_uuid