04/24 fix bugs
This commit is contained in:
@@ -805,20 +805,47 @@ def update_check_note(user_id, website_id, user_note):
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_activity_log(limit=200):
|
||||
def get_activity_log(limit=200, search: str = ""):
|
||||
"""
|
||||
Return recent activity_log entries, newest first.
|
||||
The DB column is created_at; we alias it to logged_at for a consistent
|
||||
key across both log tables so the view layer never needs to care.
|
||||
"""
|
||||
conn = None
|
||||
try:
|
||||
conn = get_connection()
|
||||
cur = conn.cursor(dictionary=True)
|
||||
cur.execute(
|
||||
|
||||
where = ""
|
||||
params: list = []
|
||||
if search:
|
||||
where = """
|
||||
WHERE al.action LIKE %s
|
||||
OR u.username LIKE %s
|
||||
OR al.entity LIKE %s
|
||||
OR al.detail LIKE %s
|
||||
"""
|
||||
SELECT al.*, u.username
|
||||
like = f"%{search}%"
|
||||
params.extend([like, like, like, like])
|
||||
params.append(limit)
|
||||
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT al.id,
|
||||
al.user_id,
|
||||
al.action,
|
||||
al.entity,
|
||||
al.entity_id,
|
||||
al.detail,
|
||||
al.logged_at,
|
||||
u.username
|
||||
FROM activity_log al
|
||||
LEFT JOIN users u ON u.id = al.user_id
|
||||
{where}
|
||||
ORDER BY al.logged_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
(limit,)
|
||||
params,
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
cur.close()
|
||||
@@ -2113,3 +2140,73 @@ def delete_bid_update(user_id: int, update_id: int):
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
# ─── Application Log (DB-backed logging) ─────────────────────────────────────
|
||||
|
||||
def get_app_log(limit: int = 500, level_filter: str = "",
|
||||
search: str = "") -> list:
|
||||
"""
|
||||
Return recent application log entries from app_log.
|
||||
level_filter : one of DEBUG/INFO/WARNING/ERROR/CRITICAL — empty = all
|
||||
search : substring match against logger_name or message
|
||||
"""
|
||||
conn = None
|
||||
try:
|
||||
conn = get_connection()
|
||||
cur = conn.cursor(dictionary=True)
|
||||
|
||||
conditions = []
|
||||
params: list = []
|
||||
|
||||
if level_filter:
|
||||
conditions.append("level = %s")
|
||||
params.append(level_filter)
|
||||
if search:
|
||||
conditions.append("(logger_name LIKE %s OR message LIKE %s)")
|
||||
like = f"%{search}%"
|
||||
params.extend([like, like])
|
||||
|
||||
where = ("WHERE " + " AND ".join(conditions)) if conditions else ""
|
||||
params.append(limit)
|
||||
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT id, level, logger_name, message, logged_at
|
||||
FROM app_log
|
||||
{where}
|
||||
ORDER BY logged_at DESC
|
||||
LIMIT %s
|
||||
""",
|
||||
params,
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
cur.close()
|
||||
return rows
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
def purge_app_log(older_than_days: int = 30):
|
||||
"""
|
||||
Delete app_log entries older than older_than_days days.
|
||||
Called from the admin log view's Purge button.
|
||||
"""
|
||||
conn = None
|
||||
try:
|
||||
conn = get_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"DELETE FROM app_log WHERE logged_at < NOW() - INTERVAL %s DAY",
|
||||
(older_than_days,),
|
||||
)
|
||||
deleted = cur.rowcount
|
||||
conn.commit()
|
||||
cur.close()
|
||||
logger.info(f"App log purged: {deleted} records older than "
|
||||
f"{older_than_days} days deleted.")
|
||||
return deleted
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user