38 lines
1 KiB
Python
38 lines
1 KiB
Python
import asyncio
|
|
|
|
from fastapi import APIRouter
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
from app.collect import procs as proc_col
|
|
from app.render import render
|
|
|
|
router = APIRouter(prefix="/api", tags=["processes"])
|
|
|
|
SORT_KEYS = ("pid", "name", "cpu", "rss", "mem_pct", "io_read", "io_write", "gpu")
|
|
|
|
|
|
@router.get("/processes")
|
|
async def processes(q: str = "", sort: str = "cpu", order: str = "desc"):
|
|
if sort not in SORT_KEYS:
|
|
sort = "cpu"
|
|
if order not in ("asc", "desc"):
|
|
order = "desc"
|
|
procs = await asyncio.to_thread(proc_col.sample)
|
|
if q:
|
|
ql = q.lower()
|
|
procs = [p for p in procs if ql in p["name"].lower() or str(p["pid"]) == ql]
|
|
reverse = order == "desc"
|
|
try:
|
|
procs.sort(key=lambda p: (p[sort] is None, p[sort]), reverse=reverse)
|
|
except (KeyError, TypeError):
|
|
pass
|
|
return HTMLResponse(
|
|
render(
|
|
"processes.html",
|
|
procs=procs[:300],
|
|
total=len(procs),
|
|
q=q,
|
|
sort=sort,
|
|
order=order,
|
|
)
|
|
)
|