GOAT-Evolution / backend /api /logs_explorer.py
rawknee-69's picture
Add interactive Logs Explorer tab for viewing raw files
8e45c5a
Raw History Blame Contribute Delete
3.28 kB
from __future__ import annotations
import os
from pathlib import Path
from fastapi import APIRouter
from fastapi.responses import FileResponse, JSONResponse
router = APIRouter()
def _get_public_logs_dir() -> Path:
preferred = Path(os.getenv("PUBLIC_LOGS_DIR", "/data/goat-logs"))
if preferred.exists() or os.getenv("SPACE_ID"):
preferred.mkdir(parents=True, exist_ok=True)
return preferred
fallback = Path("logs/public")
fallback.mkdir(parents=True, exist_ok=True)
return fallback
def _get_app_logs_dir() -> Path:
# Internal app logs
preferred = Path("/data/app-logs")
if preferred.exists() or os.getenv("SPACE_ID"):
preferred.mkdir(parents=True, exist_ok=True)
return preferred
fallback = Path("logs")
fallback.mkdir(parents=True, exist_ok=True)
return fallback
def _get_artifacts_dir() -> Path:
preferred = Path(os.getenv("ARTIFACTS_DIR", "/data/goat-artifacts"))
if preferred.exists() or os.getenv("SPACE_ID"):
preferred.mkdir(parents=True, exist_ok=True)
return preferred
fallback = Path("training/artifacts")
fallback.mkdir(parents=True, exist_ok=True)
return fallback
def _build_tree(root: Path, base_url: str) -> dict:
if not root.exists():
return {"type": "directory", "name": root.name, "children": []}
tree = {"type": "directory", "name": root.name, "children": []}
try:
for path in sorted(root.iterdir(), key=lambda p: (not p.is_dir(), p.name)):
if path.name.startswith("."):
continue
if path.is_dir():
child_tree = _build_tree(path, f"{base_url}/{path.name}")
if child_tree["children"]: # Only add non-empty directories
tree["children"].append(child_tree)
else:
ext = path.suffix.lower()
if ext in {".json", ".jsonl", ".log", ".txt", ".md", ".csv", ".png", ".jpg"}:
tree["children"].append({
"type": "file",
"name": path.name,
"url": f"{base_url}/{path.name}",
"size": path.stat().st_size
})
except PermissionError:
pass
return tree
@router.get("/tree")
async def get_logs_tree():
"""Returns a hierarchical tree of all available logs and artifacts."""
public_logs = _build_tree(_get_public_logs_dir(), "/raw-logs/public")
public_logs["name"] = "Public Battle Logs"
app_logs = _build_tree(_get_app_logs_dir(), "/raw-logs/app")
app_logs["name"] = "Application Logs"
artifacts = _build_tree(_get_artifacts_dir(), "/raw-logs/artifacts")
artifacts["name"] = "Training Artifacts"
# Check for baseline scores specifically requested by user
baseline_file = Path("training/baseline_scores.json")
if baseline_file.exists():
artifacts["children"].insert(0, {
"type": "file",
"name": "baseline_scores.json",
"url": "/raw-logs/baseline_scores.json",
"size": baseline_file.stat().st_size
})
return {
"tree": [
public_logs,
app_logs,
artifacts
]
}