from hub import methods, Global import threading import time import json import os class UserWorkRecord(object): """ """ users = dict() # {: {log_uuid: , start_at, end_at}} @classmethod def init(cls): """初始化""" for item in Global.mdb.get_all('UserWorkRecordList', sort_field=[('start_at', -1)]): if item.get('user_uuid') not in cls.users: cls.users[item.get('user_uuid')] = { 'log_uuid': str(item.get('_id')), 'start_at': item.get('start_at'), 'end_at': item.get('end_at'), } if item.get('user_uuid') in cls.users: continue @classmethod def get_log_uuid(cls, user_uuid, log_time, vehicle_id, cockpit_id): """ UserWorkRecordList: 用户操作记录 UserWorkRecordList.uuid: 记录标识 UserWorkRecordList.user_uuid: 用户标识 UserWorkRecordList.start_at: 操作开始时间 UserWorkRecordList.end_at: 操作结束时间 UserWorkRecordList.cockpit_id: 舱端id UserWorkRecordList.vehicle_id: 车端id """ # --- check --- if not user_uuid or not log_time: methods.debug_log(f"UserWorkRecord|40", f"#error40, user_uuid: {user_uuid}, log_time: {log_time}") return None # --- check --- r1 = user_uuid not in cls.users or log_time > cls.users[user_uuid].get('end_at') + 300 # 距离上次操作间隔300s,当做新的记录 if r1: # --- add --- data = { 'user_uuid': user_uuid, 'start_at': log_time, 'end_at': log_time, 'vehicle_id': vehicle_id, 'cockpit_id': cockpit_id, } log_uuid = Global.mdb.add('UserWorkRecordList', data) # --- update --- cls.users[user_uuid] = { 'log_uuid': log_uuid, 'start_at': log_time, 'end_at': log_time, } return log_uuid # --- update --- 距离上次操作间隔60s,更新一下结束时间 log_uuid = cls.users[user_uuid].get('log_uuid') if log_time > cls.users[user_uuid].get('end_at') + 60: Global.mdb.update_one_by_id('UserWorkRecordList', log_uuid, {'end_at': log_time}) cls.users[user_uuid]['end_at'] = log_time return log_uuid