Add search for activity logs

This commit is contained in:
Peter Stockings
2026-02-13 00:28:12 +11:00
parent 8c08140ad0
commit 67009c9603
5 changed files with 67 additions and 17 deletions

View File

@@ -19,9 +19,27 @@ class Activity:
# We don't want logging to break the main application flow
current_app.logger.error(f"Error logging activity: {e}")
def get_recent_logs(self, limit=50, offset=0):
"""Fetches recent activity logs with person names, supporting pagination."""
query = """
def get_recent_logs(self, limit=50, offset=0, search_query=None):
"""Fetches recent activity logs with person names, supporting pagination and search."""
params = [limit, offset]
search_clause = ""
if search_query:
# Add wildcard percentages for partial matching
term = f"%{search_query}%"
search_clause = """
WHERE
p.name ILIKE %s OR
al.action ILIKE %s OR
al.entity_type ILIKE %s OR
al.details ILIKE %s
"""
# Prepend search terms to params list (limit/offset must change position if we were using ? placeholders
# but with %s list, order matters. Let's reconstruct consistent order).
# Actually, LIMIT/OFFSET are at the end. Search params come before.
params = [term, term, term, term, limit, offset]
query = f"""
SELECT
al.id,
al.person_id,
@@ -35,7 +53,8 @@ class Activity:
al.timestamp
FROM activity_log al
LEFT JOIN person p ON al.person_id = p.person_id
{search_clause}
ORDER BY al.timestamp DESC
LIMIT %s OFFSET %s
"""
return self.execute(query, [limit, offset])
return self.execute(query, params)