From 65df3368f3d072518babdc24f22e193bebe1c7d1 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sun, 12 Jan 2025 06:57:06 -0500 Subject: [PATCH] the ability to search is done. modified: .gitignore modified: app.py modified: templates/base.html.j2 modified: templates/view_namespace_logs.html.j2 modified: test_agent.sh --- .gitignore | 4 + app.py | 38 +++++++-- templates/base.html.j2 | 5 ++ templates/view_namespace_logs.html.j2 | 111 +++++++++++++++++++++----- test_agent.sh | 5 +- 5 files changed, 131 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 63ad4cf..48a78ee 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,8 @@ *.swp env/ __pycache__/ + cookies.txt +jwt_secret.txt +pyralogs_secret.txt + diff --git a/app.py b/app.py index c2c720d..1f947ce 100644 --- a/app.py +++ b/app.py @@ -36,6 +36,7 @@ from sqlalchemy import ( ForeignKey, Table, Text, + or_, ) from sqlalchemy.orm import declarative_base from sqlalchemy.orm import sessionmaker @@ -143,11 +144,11 @@ def get_gravatar_url(email, size=100): def send_email(to_email, subject, body): - log.debug("======= Email Sent =======") - log.debug(f"To: {to_email}") - log.debug(f"Subject: {subject}") - log.debug(f"Body:\n{body}") - log.debug("==========================") + log.info("======= Email Sent =======") + log.info(f"To: {to_email}") + log.info(f"Subject: {subject}") + log.info(f"Body:\n{body}") + log.info("==========================") msg = MIMEText(body) msg["Subject"] = subject @@ -1039,18 +1040,39 @@ def log_webhook_view(request): return Response("Log entry created.", status=201) -@view_config(route_name="view_namespace_logs", renderer="view_namespace_logs.html.j2") + +@view_config( + route_name="view_namespace_logs", renderer="view_namespace_logs.html.j2" +) @reader_required def view_namespace_logs_view(request): namespace = request.namespace - namespace_dbsession = request.namespace_dbsession - logs = namespace_dbsession.query(LogEntry).order_by(LogEntry.timestamp.desc()).all() + + # Get the search query from the request parameters + search_query = request.params.get('q', '').strip() + + # Start building the query + query = namespace_dbsession.query(LogEntry) + + if search_query: + # Create a search pattern for case-insensitive search + search_pattern = f"%{search_query}%" + query = query.filter( + or_( + LogEntry.message.ilike(search_pattern), + LogEntry.level.ilike(search_pattern), + LogEntry.log_metadata.ilike(search_pattern), + ) + ) + + logs = query.order_by(LogEntry.timestamp.desc()).all() return { "request": request, "namespace": namespace, "logs": logs, + "search_query": search_query, } diff --git a/templates/base.html.j2 b/templates/base.html.j2 index b959141..f6c85f6 100644 --- a/templates/base.html.j2 +++ b/templates/base.html.j2 @@ -39,6 +39,11 @@ margin: 0; } + mark { + background-color: yellow; + color: black; + } + /* Hamburger Menu Button */ #mobile-menu-button { display: none; diff --git a/templates/view_namespace_logs.html.j2 b/templates/view_namespace_logs.html.j2 index 2425559..0187dec 100644 --- a/templates/view_namespace_logs.html.j2 +++ b/templates/view_namespace_logs.html.j2 @@ -1,33 +1,100 @@ {% extends 'base.html.j2' %} -{% block title %}Namespace Logs{% endblock %} +{% block title %}Logs for {{ namespace.name }}{% endblock %} {% block content %}

Logs for Namespace: {{ namespace.name }}

-{% if logs %} - - - - - - - - - - - {% for log in logs %} + + + + + {% if search_query %} + Clear Search + {% endif %} + + + +
+ {% if logs %} +
TimestampLevelMessageMetadata
+ - - - - + + + + - {% endfor %} - -
{{ log.timestamp.strftime('%Y-%m-%d %H:%M:%S') }}{{ log.level }}{{ log.message }}{{ log.log_metadata }}TimestampLevelMessageMetadata
-{% else %} -

No logs available.

+ + + {% for log in logs %} + + {{ log.timestamp.strftime('%Y-%m-%d %H:%M:%S') }} + {{ log.level }} + {{ log.message }} + + {% if log.log_metadata %} +
{{ log.log_metadata }}
+ {% else %} + N/A + {% endif %} + + + {% endfor %} + + + {% else %} +

No logs found.

+ {% endif %} + + + +{% if search_query %} + {% endif %} {% endblock %} diff --git a/test_agent.sh b/test_agent.sh index 172cf9b..587e372 100644 --- a/test_agent.sh +++ b/test_agent.sh @@ -9,7 +9,7 @@ # Variables BASE_URL="http://127.0.0.1:6544" -JWT_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZ2VudF9pZCI6IjcxZmQ0ZjA0LTQ2NzgtNGEyNi05ZmU4LTkxNjU2OTNmOTBmOSIsImFnZW50X25hbWUiOiJ0ZXN0LTkwMDAiLCJuYW1lc3BhY2VfaWQiOiJhZGQwYWQ1YS0zNjE0LTRhODgtOTRmZC0xNGQ4ZDBkYjIxYjUiLCJ0b2tlbl92ZXJzaW9uIjozLCJpYXQiOjE3MzY2NDYxNzV9.BzTXP6W6dcKQEwB970zTT5JPaJJsb0rybQv774wQy-s" +JWT_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZ2VudF9pZCI6IjcxZmQ0ZjA0LTQ2NzgtNGEyNi05ZmU4LTkxNjU2OTNmOTBmOSIsImFnZW50X25hbWUiOiJ0ZXN0LTkwMDAiLCJuYW1lc3BhY2VfaWQiOiJhZGQwYWQ1YS0zNjE0LTRhODgtOTRmZC0xNGQ4ZDBkYjIxYjUiLCJ0b2tlbl92ZXJzaW9uIjo1LCJpYXQiOjE3MzY2ODIxMTV9.p2HQmjFBtL9XLePtVCu3HyPhw89993BO4b6E82SwyEM" # Log message from the first argument LOG_MESSAGE="$1" @@ -26,7 +26,8 @@ RESPONSE=$(curl -s -X POST "$BASE_URL/webhook" \ -H "Authorization: Bearer $JWT_TOKEN" \ -d "{ \"message\": \"$LOG_MESSAGE\", - \"level\": \"INFO\" + \"level\": \"INFO\", + \"metadata\": {\"w\": 1} }") # Output the server's response