Add persistent web chat log
This commit is contained in:
1 parent
9be381c2de
commit
492238aa22
12 files changed
+506
-51
No files matched your search
@@ -0,0 +1,35 @@
|
||||
"""Small shared in-memory chat history for the web dashboard."""
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
MAX_CHAT_EVENTS = 200
|
||||
_lock = threading.RLock()
|
||||
_last_event_id = 0
|
||||
|
||||
|
||||
def append_chat_event(global_state, event_type, message, sender=None):
|
||||
global _last_event_id
|
||||
with _lock:
|
||||
event_id = max(time.time_ns(), _last_event_id + 1)
|
||||
_last_event_id = event_id
|
||||
event = {
|
||||
'id': str(event_id),
|
||||
'timestamp': time.time(),
|
||||
'type': event_type,
|
||||
'message': str(message),
|
||||
}
|
||||
if sender:
|
||||
event['sender'] = str(sender)
|
||||
events = global_state.get('chat_log')
|
||||
if not isinstance(events, list):
|
||||
events = []
|
||||
global_state.chat_log = events
|
||||
events.append(event)
|
||||
del events[:-MAX_CHAT_EVENTS]
|
||||
return event
|
||||
|
||||
|
||||
def snapshot_chat_log(global_state):
|
||||
with _lock:
|
||||
return [dict(event) for event in global_state.get('chat_log', ())]
|
||||
Reference in new issue
Block a user