File size: 1,714 Bytes
1c75c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# tracklight_server/api/dashboard.py

from fastapi import APIRouter, Depends, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from ..db import duckdb
from .auth import verify_token
import os

router = APIRouter()

# Get the absolute path to the templates directory
SERVER_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATES_DIR = os.path.join(SERVER_DIR, "templates")
templates = Jinja2Templates(directory=TEMPLATES_DIR)

@router.get("/login", response_class=HTMLResponse)
async def get_login_page(request: Request):
    """
    Serves the login page.
    """
    return templates.TemplateResponse("login.html", {"request": request})

@router.get("/dashboard", response_class=HTMLResponse)
async def get_dashboard(request: Request):
    """
    Serves the main dashboard page with a list of projects.
    """
    projects = duckdb.get_projects()
    return templates.TemplateResponse("project_list.html", {"request": request, "projects": projects})

@router.get("/project/{project_name}", response_class=HTMLResponse)
async def get_project_page(request: Request, project_name: str, user: str):
    """
    Serves the project-specific page with a list of runs.
    """
    runs = duckdb.get_runs(project_name, user)
    return templates.TemplateResponse("dashboard.html", {"request": request, "project": project_name, "user": user, "runs": runs})

@router.get("/api/metrics")
async def get_metrics(run_id: str, token: str = Depends(verify_token)):
    """
    Returns metrics and config for a given run.
    """
    metrics = duckdb.get_metrics_for_run(run_id)
    config = duckdb.get_config_for_run(run_id)
    return {"metrics": metrics, "config": config}