trying to make the logs view to have better date format.
This commit is contained in:
42
app/main.py
42
app/main.py
@ -2,7 +2,7 @@ import os
|
||||
import uuid
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from fastapi import FastAPI, Request, status, Query
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
@ -12,6 +12,7 @@ from typing import Dict, List, Annotated
|
||||
import uuid as uuid_lib
|
||||
|
||||
from collections import deque
|
||||
from dateutil.parser import isoparse
|
||||
|
||||
from pythonjsonlogger import jsonlogger
|
||||
import sys
|
||||
@ -61,7 +62,7 @@ class LogBuffer:
|
||||
# Apply level filter
|
||||
if level and level.strip():
|
||||
level = level.upper()
|
||||
valid_levels = {'INFO', 'WARNING', 'ERROR', 'DEBUG'} # Added DEBUG for completeness
|
||||
valid_levels = {'INFO', 'WARNING', 'ERROR', 'DEBUG'}
|
||||
if level in valid_levels:
|
||||
logs = [log for log in logs if log['level'].upper() == level]
|
||||
else:
|
||||
@ -69,8 +70,16 @@ class LogBuffer:
|
||||
# Apply since filter
|
||||
if since:
|
||||
try:
|
||||
# Handle 'Z' for UTC and ensure timezone awareness for comparison
|
||||
since_dt = datetime.fromisoformat(since.replace('Z', '+00:00')).astimezone(timezone.utc)
|
||||
# Use isoparse for robust parsing of ISO 8601 strings
|
||||
since_dt = isoparse(since)
|
||||
|
||||
# If the parsed datetime is naive (no timezone info), assume it's UTC
|
||||
if since_dt.tzinfo is None:
|
||||
since_dt = since_dt.replace(tzinfo=timezone.utc)
|
||||
else:
|
||||
# If it has timezone info, convert it to UTC for consistent comparison
|
||||
since_dt = since_dt.astimezone(timezone.utc)
|
||||
|
||||
logs = [log for log in logs if
|
||||
datetime.fromisoformat(log['timestamp'].replace('Z', '+00:00')).astimezone(timezone.utc) >= since_dt]
|
||||
except ValueError:
|
||||
@ -90,6 +99,13 @@ if not logger.handlers:
|
||||
logger.addHandler(logHandler)
|
||||
logger.addHandler(BufferHandler())
|
||||
|
||||
# Configure Uvicorn's loggers to propagate to the root logger
|
||||
# This ensures Uvicorn's startup and access logs are captured by our BufferHandler
|
||||
logging.getLogger("uvicorn").propagate = True
|
||||
logging.getLogger("uvicorn.access").propagate = True
|
||||
logging.getLogger("uvicorn.error").propagate = True
|
||||
|
||||
|
||||
# --- FastAPI Application ---
|
||||
app = FastAPI(
|
||||
title="Node Monitoring System",
|
||||
@ -139,6 +155,7 @@ LOAD_AVG_CRITICAL_THRESHOLD = 3.0
|
||||
MEMORY_WARNING_THRESHOLD = 75.0
|
||||
MEMORY_CRITICAL_THRESHOLD = 90.0
|
||||
LAST_SEEN_CRITICAL_THRESHOLD_SECONDS = 30
|
||||
NODE_INACTIVE_REMOVAL_THRESHOLD_SECONDS = 300 # Remove node from UI after 5 minutes of inactivity
|
||||
|
||||
def get_node_health(node_data: Dict) -> str:
|
||||
last_seen_str = node_data.get("last_seen")
|
||||
@ -330,13 +347,25 @@ async def update_node_status(
|
||||
@app.get("/nodes/status")
|
||||
async def get_all_nodes_status():
|
||||
logger.info("Fetching all nodes status for UI.")
|
||||
response_nodes = []
|
||||
|
||||
# Prune inactive nodes from known_nodes_db before processing
|
||||
current_time_utc = datetime.now(timezone.utc)
|
||||
nodes_to_remove = []
|
||||
for node_uuid, data in known_nodes_db.items():
|
||||
last_seen_dt = datetime.fromisoformat(data["last_seen"]).replace(tzinfo=timezone.utc)
|
||||
if (current_time_utc - last_seen_dt).total_seconds() > NODE_INACTIVE_REMOVAL_THRESHOLD_SECONDS:
|
||||
nodes_to_remove.append(node_uuid)
|
||||
logger.info(f"Removing inactive node {node_uuid} from known_nodes_db.")
|
||||
|
||||
for node_uuid in nodes_to_remove:
|
||||
known_nodes_db.pop(node_uuid)
|
||||
|
||||
response_nodes = []
|
||||
for node_uuid, data in known_nodes_db.items():
|
||||
current_health = get_node_health(data)
|
||||
|
||||
connections = {}
|
||||
for target_uuid in known_nodes_db:
|
||||
for target_uuid in known_nodes_db: # Only iterate over currently active nodes
|
||||
if target_uuid != node_uuid:
|
||||
ping_data = database.get_ping_data(node_uuid, target_uuid, start_time="-300s")
|
||||
latency_ms = None
|
||||
@ -363,3 +392,4 @@ async def get_all_nodes_status():
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
return {"status": "ok"}
|
||||
# --- END OF FILE main.py ---
|
||||
|
Reference in New Issue
Block a user