Initial commit: add .gitignore and README
This commit is contained in:
29
fusionagi/governance/audit_log.py
Normal file
29
fusionagi/governance/audit_log.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""Structured audit log for AGI."""
|
||||
from typing import Any
|
||||
from fusionagi.schemas.audit import AuditEntry, AuditEventType
|
||||
from fusionagi._logger import logger
|
||||
import uuid
|
||||
|
||||
class AuditLog:
|
||||
def __init__(self, max_entries=100000):
|
||||
self._entries = []
|
||||
self._max_entries = max_entries
|
||||
self._by_task = {}
|
||||
self._by_type = {}
|
||||
def append(self, event_type, actor, action="", task_id=None, payload=None, outcome=""):
|
||||
entry_id = str(uuid.uuid4())
|
||||
entry = AuditEntry(entry_id=entry_id, event_type=event_type, actor=actor, task_id=task_id, action=action, payload=payload or {}, outcome=outcome)
|
||||
if len(self._entries) >= self._max_entries:
|
||||
self._entries.pop(0)
|
||||
idx = len(self._entries)
|
||||
self._entries.append(entry)
|
||||
if entry.task_id:
|
||||
self._by_task.setdefault(entry.task_id, []).append(idx)
|
||||
self._by_type.setdefault(entry.event_type.value, []).append(idx)
|
||||
return entry_id
|
||||
def get_by_task(self, task_id, limit=100):
|
||||
indices = self._by_task.get(task_id, [])[-limit:]
|
||||
return [self._entries[i] for i in indices if i < len(self._entries)]
|
||||
def get_by_type(self, event_type, limit=100):
|
||||
indices = self._by_type.get(event_type.value, [])[-limit:]
|
||||
return [self._entries[i] for i in indices if i < len(self._entries)]
|
||||
Reference in New Issue
Block a user