Both overviews carried the same toolbar twice, left-aligned, with a search
field permanently taking a row of width. One `OverviewToolbar` now serves
them: the search folds into an icon and expands again on click (Escape puts
it away and hands focus back), create is a `+`, and everything sits right of
the page. Each page keeps its own create dialog — the toolbar only renders
the trigger — so the testids the runtime spec and the capture script drive
stayed where they were.
Dashboards get the flow store's draft/publish split. The editor autosaves
`dashboard.draft.json` beside `dashboard.json`; `/view/{name}`, `bindings_for`
and `history_requirements` keep reading the published file, so a wall panel
sees an edit only once someone publishes it. `POST /dashboards/{name}/publish`
and `/discard` mirror the flow routes down to the version precondition and the
409, `GET /dashboards/{name}?draft=true` is what the editor asks for, and the
dock grows the same Publish button — which flushes a queued save first, so an
autosave in flight is not published around. Creating a dashboard still writes
the published file directly: an empty document on a panel is harmless, and it
keeps the store free of a never-published case.
"Publish all" is a checkmark in the toolbar, live only when something actually
has `has_draft`. A summary carries no version and publish needs the one it is
based on, so each document's detail is read immediately before its publish —
honest against a stale list, and no version-less backend path to maintain.
Failures are counted rather than swallowed: three of five fails says so and
names the three.
165 lines
5.8 KiB
Python
165 lines
5.8 KiB
Python
"""Dashboards: documents of widgets bound to message names."""
|
|
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.concurrency import run_in_threadpool
|
|
from pydantic import BaseModel
|
|
|
|
from app.api.deps import DashboardStoreDep, FlowControllerDep, get_current_user
|
|
from app.flow.dashboards import (
|
|
DashboardDef,
|
|
DashboardExists,
|
|
DashboardNotFound,
|
|
DashboardsPublic,
|
|
default_dashboard,
|
|
)
|
|
from app.flow.store import StaleVersion
|
|
from app.models import Message
|
|
|
|
router = APIRouter(
|
|
prefix="/dashboards", tags=["dashboards"], dependencies=[Depends(get_current_user)]
|
|
)
|
|
|
|
|
|
class RenameRequest(BaseModel):
|
|
name: str
|
|
|
|
|
|
class PublishRequest(BaseModel):
|
|
version: int
|
|
|
|
|
|
def _apply_history_limits(store: Any, controller: Any) -> None:
|
|
"""Tell the engine how much past each charted message needs kept."""
|
|
controller.set_history_limits(store.history_requirements())
|
|
|
|
|
|
@router.get("/", response_model=DashboardsPublic)
|
|
async def read_dashboards(store: DashboardStoreDep) -> Any:
|
|
"""Every dashboard, without its contents."""
|
|
summaries = await run_in_threadpool(store.list)
|
|
return DashboardsPublic(data=summaries, count=len(summaries))
|
|
|
|
|
|
@router.get("/{name}", response_model=DashboardDef)
|
|
async def read_dashboard(
|
|
name: str, store: DashboardStoreDep, draft: bool = False
|
|
) -> Any:
|
|
"""What a panel shows, or with ``draft`` the copy the editor is on."""
|
|
try:
|
|
return await run_in_threadpool(store.read, name, draft)
|
|
except DashboardNotFound:
|
|
raise HTTPException(status_code=404, detail=f"No dashboard named '{name}'")
|
|
|
|
|
|
@router.post("/{name}", response_model=DashboardDef)
|
|
async def create_dashboard(
|
|
name: str, store: DashboardStoreDep, controller: FlowControllerDep
|
|
) -> Any:
|
|
"""Start a dashboard: one page, one section, nothing on it yet."""
|
|
if await run_in_threadpool(store.exists, name):
|
|
raise HTTPException(status_code=409, detail=f"'{name}' already exists")
|
|
try:
|
|
defn = default_dashboard(name)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=422, detail=str(exc))
|
|
saved = await run_in_threadpool(store.write, defn, 0)
|
|
await run_in_threadpool(_apply_history_limits, store, controller)
|
|
return saved
|
|
|
|
|
|
@router.put("/{name}", response_model=DashboardDef)
|
|
async def save_dashboard(
|
|
name: str,
|
|
body: DashboardDef,
|
|
store: DashboardStoreDep,
|
|
) -> Any:
|
|
"""Save unpublished changes, refusing a save someone else has moved past.
|
|
|
|
This writes a draft: panels keep showing the published document until
|
|
someone publishes, so nothing here can change what a wall is displaying —
|
|
which is also why the engine's history limits are left alone.
|
|
"""
|
|
if body.name != name:
|
|
raise HTTPException(status_code=422, detail="The name in the body must match")
|
|
try:
|
|
saved = await run_in_threadpool(store.write_draft, body, body.version)
|
|
except DashboardNotFound:
|
|
raise HTTPException(status_code=404, detail=f"No dashboard named '{name}'")
|
|
except StaleVersion as exc:
|
|
raise HTTPException(
|
|
status_code=409,
|
|
detail={
|
|
"message": "Someone else saved this dashboard first",
|
|
"current_version": exc.current,
|
|
},
|
|
)
|
|
return saved
|
|
|
|
|
|
@router.post("/{name}/publish", response_model=DashboardDef)
|
|
async def publish_dashboard(
|
|
name: str,
|
|
body: PublishRequest,
|
|
store: DashboardStoreDep,
|
|
controller: FlowControllerDep,
|
|
) -> Any:
|
|
"""Put the unpublished changes on the panels."""
|
|
if not await run_in_threadpool(store.exists, name):
|
|
raise HTTPException(status_code=404, detail=f"No dashboard named '{name}'")
|
|
if not await run_in_threadpool(store.has_draft, name):
|
|
raise HTTPException(
|
|
status_code=400, detail=f"Dashboard '{name}' has no unpublished changes"
|
|
)
|
|
try:
|
|
published = await run_in_threadpool(store.publish, name, body.version)
|
|
except StaleVersion as exc:
|
|
raise HTTPException(
|
|
status_code=409,
|
|
detail={
|
|
"message": "Someone else saved this dashboard first",
|
|
"current_version": exc.current,
|
|
},
|
|
)
|
|
await run_in_threadpool(_apply_history_limits, store, controller)
|
|
return published
|
|
|
|
|
|
@router.post("/{name}/discard", response_model=DashboardDef)
|
|
async def discard_dashboard_draft(name: str, store: DashboardStoreDep) -> Any:
|
|
"""Throw the unpublished changes away and go back to what is shown."""
|
|
if not await run_in_threadpool(store.exists, name):
|
|
raise HTTPException(status_code=404, detail=f"No dashboard named '{name}'")
|
|
if not await run_in_threadpool(store.has_draft, name):
|
|
raise HTTPException(
|
|
status_code=400, detail=f"Dashboard '{name}' has no unpublished changes"
|
|
)
|
|
return await run_in_threadpool(store.discard_draft, name)
|
|
|
|
|
|
@router.delete("/{name}", response_model=Message)
|
|
async def delete_dashboard(
|
|
name: str, store: DashboardStoreDep, controller: FlowControllerDep
|
|
) -> Any:
|
|
try:
|
|
await run_in_threadpool(store.delete, name)
|
|
except DashboardNotFound:
|
|
raise HTTPException(status_code=404, detail=f"No dashboard named '{name}'")
|
|
await run_in_threadpool(_apply_history_limits, store, controller)
|
|
return Message(message=f"Deleted dashboard '{name}'")
|
|
|
|
|
|
@router.post("/{name}/rename", response_model=DashboardDef)
|
|
async def rename_dashboard(
|
|
name: str, body: RenameRequest, store: DashboardStoreDep
|
|
) -> Any:
|
|
try:
|
|
return await run_in_threadpool(store.rename, name, body.name)
|
|
except DashboardNotFound:
|
|
raise HTTPException(status_code=404, detail=f"No dashboard named '{name}'")
|
|
except DashboardExists:
|
|
raise HTTPException(status_code=409, detail=f"'{body.name}' already exists")
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=422, detail=str(exc))
|