Spaces:
Build error
Build error
Update utils/audit.py
Browse files- utils/audit.py +35 -28
utils/audit.py
CHANGED
|
@@ -1,37 +1,40 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from datetime import datetime
|
| 3 |
-
import uuid
|
| 4 |
-
import json
|
| 5 |
import logging
|
|
|
|
|
|
|
|
|
|
| 6 |
from typing import Optional, Any
|
| 7 |
|
| 8 |
class AuditLogger:
|
| 9 |
def __init__(self):
|
|
|
|
| 10 |
self._setup_logging()
|
| 11 |
-
|
| 12 |
-
def initialize_with_connection(self, conn):
|
| 13 |
-
self.conn = conn
|
| 14 |
|
| 15 |
-
audit_logger = AuditLogger()
|
| 16 |
-
|
| 17 |
def _setup_logging(self):
|
| 18 |
logging.basicConfig(
|
| 19 |
filename='audit.log',
|
| 20 |
level=logging.INFO,
|
| 21 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 22 |
)
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
def log_activity(
|
| 25 |
-
self,
|
| 26 |
-
user_id: str,
|
| 27 |
-
action: str,
|
| 28 |
ip_address: Optional[str] = None,
|
| 29 |
details: Any = None,
|
| 30 |
severity: str = 'info'
|
| 31 |
):
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
try:
|
|
|
|
| 35 |
cursor.execute("""
|
| 36 |
INSERT INTO audit_logs (
|
| 37 |
id, user_id, action, ip_address, timestamp, details, severity
|
|
@@ -46,23 +49,27 @@ audit_logger = AuditLogger()
|
|
| 46 |
severity
|
| 47 |
))
|
| 48 |
self.conn.commit()
|
| 49 |
-
|
| 50 |
logging.info(f"Audit log: {action} by user {user_id}")
|
| 51 |
-
|
| 52 |
except Exception as e:
|
| 53 |
logging.error(f"Failed to create audit log: {str(e)}")
|
| 54 |
-
|
| 55 |
def get_user_activity(self, user_id: str, limit: int = 50):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
# Create a global instance without connection
|
| 68 |
audit_logger = AuditLogger()
|
|
|
|
| 1 |
+
# utils/audit.py
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 3 |
import logging
|
| 4 |
+
import json
|
| 5 |
+
import uuid
|
| 6 |
+
from datetime import datetime
|
| 7 |
from typing import Optional, Any
|
| 8 |
|
| 9 |
class AuditLogger:
|
| 10 |
def __init__(self):
|
| 11 |
+
self.conn = None
|
| 12 |
self._setup_logging()
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
| 14 |
def _setup_logging(self):
|
| 15 |
logging.basicConfig(
|
| 16 |
filename='audit.log',
|
| 17 |
level=logging.INFO,
|
| 18 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 19 |
)
|
| 20 |
+
|
| 21 |
+
def initialize_with_connection(self, conn):
|
| 22 |
+
self.conn = conn
|
| 23 |
+
|
| 24 |
def log_activity(
|
| 25 |
+
self,
|
| 26 |
+
user_id: str,
|
| 27 |
+
action: str,
|
| 28 |
ip_address: Optional[str] = None,
|
| 29 |
details: Any = None,
|
| 30 |
severity: str = 'info'
|
| 31 |
):
|
| 32 |
+
if not self.conn:
|
| 33 |
+
logging.error("Database connection not initialized")
|
| 34 |
+
return
|
| 35 |
+
|
| 36 |
try:
|
| 37 |
+
cursor = self.conn.cursor()
|
| 38 |
cursor.execute("""
|
| 39 |
INSERT INTO audit_logs (
|
| 40 |
id, user_id, action, ip_address, timestamp, details, severity
|
|
|
|
| 49 |
severity
|
| 50 |
))
|
| 51 |
self.conn.commit()
|
|
|
|
| 52 |
logging.info(f"Audit log: {action} by user {user_id}")
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
logging.error(f"Failed to create audit log: {str(e)}")
|
| 55 |
+
|
| 56 |
def get_user_activity(self, user_id: str, limit: int = 50):
|
| 57 |
+
if not self.conn:
|
| 58 |
+
return []
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
cursor = self.conn.cursor()
|
| 62 |
+
cursor.execute("""
|
| 63 |
+
SELECT action, timestamp, details, severity
|
| 64 |
+
FROM audit_logs
|
| 65 |
+
WHERE user_id = ?
|
| 66 |
+
ORDER BY timestamp DESC
|
| 67 |
+
LIMIT ?
|
| 68 |
+
""", (user_id, limit))
|
| 69 |
+
return cursor.fetchall()
|
| 70 |
+
except Exception as e:
|
| 71 |
+
logging.error(f"Failed to fetch user activity: {str(e)}")
|
| 72 |
+
return []
|
| 73 |
|
| 74 |
+
# Create a single instance
|
|
|
|
| 75 |
audit_logger = AuditLogger()
|