Flows and dashboards get ids: the store is addressed by id with a key index, the first start gives every document its key as id, fluksio store migrate --dry-run and ls; the rename routes are gone

This commit is contained in:
2026-11-02 16:54:11 +01:00
parent de13c48c90
commit fae85f5ecb
20 changed files with 1287 additions and 420 deletions
+67 -32
View File
@@ -25,13 +25,15 @@ from typing import Any, Literal
from pydantic import BaseModel, Field, field_validator, model_validator
from fluksio.flow.messages import DType, qualify
from fluksio.flow.schemas import FlowDef, _validate_name
from fluksio.flow.schemas import FlowDef, _validate_id, _validate_name
from fluksio.flow.store import (
FlowStore,
StaleVersion,
_updated_at,
checked,
claim,
copy_name,
read_keys,
)
#: Sibling of the shared-node library, and likewise not a flow.
@@ -500,6 +502,11 @@ def _flatten_pages(pages: list[Any]) -> list[dict[str, Any]]:
class DashboardDef(BaseModel):
"""A dashboard as stored, and as the API hands it over."""
#: Which dashboard this is, like a flow's (``FlowDef.id``), in an id space
#: of its own: the directory, never changed once given, the key for one
#: from before ids. Optional on the way in, always set on the way out.
id: str = ""
#: The key, unique among dashboards. Nothing binds to it, so it is a label.
name: str
title: str = ""
#: How many columns the grid is cut into, so a dashboard can be matched to
@@ -550,6 +557,11 @@ class DashboardDef(BaseModel):
data["widgets"] = _flatten_pages(pages)
return data
@field_validator("id")
@classmethod
def _check_id(cls, value: str) -> str:
return _validate_id(value)
@field_validator("name")
@classmethod
def _check_name(cls, value: str) -> str:
@@ -610,6 +622,7 @@ class DashboardDef(BaseModel):
class DashboardSummary(BaseModel):
"""A dashboard in a list, without its contents."""
id: str = ""
name: str
title: str = ""
#: The glyph this dashboard draws on a panel's rail, so a list can show it
@@ -648,6 +661,9 @@ class DashboardExists(ValueError):
super().__init__(name)
self.name = name
def __str__(self) -> str:
return f"There is already a dashboard named '{self.name}'"
class DashboardStore:
"""Dashboards in the flow store's repository, invisible to the flow listing.
@@ -665,6 +681,8 @@ class DashboardStore:
self._published: dict[str, DashboardDef | None] = {}
self._published_at = -1
self._published_lock = threading.Lock()
#: `key_index` and the flow store revision it was read at.
self._keys: tuple[int, dict[str, str]] = (-1, {})
def _file(self, name: str) -> Path:
return self.root / checked(name) / "dashboard.json"
@@ -689,6 +707,7 @@ class DashboardStore:
continue
summaries.append(
DashboardSummary(
id=defn.id,
name=defn.name,
title=defn.title,
icon=defn.icon,
@@ -725,10 +744,45 @@ class DashboardStore:
path = self._file(name)
if not path.exists():
raise DashboardNotFound(name)
return DashboardDef.model_validate_json(path.read_text()).model_copy(
update={"has_draft": self.has_draft(name)}
defn = DashboardDef.model_validate_json(path.read_text())
# One from before ids is known by its directory, which is its key.
return defn.model_copy(
update={"id": defn.id or name, "has_draft": self.has_draft(name)}
)
def key_index(self) -> dict[str, str]:
"""Every dashboard's key, to its id, whether published or only a draft.
Read again after any commit of the store, as `FlowStore.key_index`.
Shared: read it, never change it.
"""
at, keys = self._keys
if at == self.flows.revision:
return keys
at = self.flows.revision
found = {p.parent for p in self.root.glob("*/dashboard.json")}
found |= {p.parent for p in self.root.glob("*/dashboard.draft.json")}
keys = read_keys(sorted(found), "dashboard")
self._keys = (at, keys)
return keys
def _claim(self, defn: DashboardDef) -> DashboardDef:
"""*defn* with its id, once it is certain to be a save the store allows.
Under the flow store's write lock; see `claim`. An id left out is the
key, which is how every dashboard is created for now.
"""
dash_id = checked(defn.id or defn.name)
current = self.read(dash_id, draft=True) if self.exists(dash_id) else None
claim(
dash_id,
defn.name,
(current.id, current.name) if current else None,
self.key_index(),
DashboardExists,
)
return defn.model_copy(update={"id": dash_id})
def published(self, name: str) -> DashboardDef | None:
"""The published document, or None, as of the store's last commit.
@@ -756,7 +810,8 @@ class DashboardStore:
caller that already has the finished document — a test, or a seed.
"""
with self._lock, self.flows._write_lock:
path = self._file(defn.name)
defn = self._claim(defn)
path = self._file(defn.id)
current = 0
if path.exists():
current = DashboardDef.model_validate_json(path.read_text()).version
@@ -779,14 +834,15 @@ class DashboardStore:
does not exist yet: creating one is its first draft.
"""
with self._lock, self.flows._write_lock:
defn = self._claim(defn)
current = 0
if self.exists(defn.name):
current = self.read(defn.name, draft=True).version
if self.exists(defn.id):
current = self.read(defn.id, draft=True).version
if base_version is not None and base_version != current:
raise StaleVersion(defn.name, current)
saved = defn.model_copy(update={"version": current + 1, "has_draft": True})
path = self._draft_file(defn.name)
path = self._draft_file(defn.id)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(self._dump(saved))
self.flows._commit(f"Update draft of dashboard '{defn.name}'")
@@ -828,30 +884,6 @@ class DashboardStore:
pass
self.flows._commit(f"Delete dashboard '{name}'")
def rename(self, name: str, new_name: str) -> DashboardDef:
defn = self.read(name, draft=True)
if self.exists(new_name):
raise DashboardExists(new_name)
with self.flows._write_lock:
renamed = defn.model_copy(update={"name": new_name})
self._file(new_name).parent.mkdir(parents=True, exist_ok=True)
# Whichever files the dashboard has move; one nobody published yet
# has only the draft, and renaming it must not publish it.
if self.is_published(name):
published = self.read(name).model_copy(update={"name": new_name})
self._file(new_name).write_text(self._dump(published))
self._file(name).unlink()
# An unpublished edit belongs to the dashboard, so it moves too.
if self.has_draft(name):
self._draft_file(new_name).write_text(self._dump(renamed))
self._draft_file(name).unlink()
try:
self._file(name).parent.rmdir()
except OSError:
pass
self.flows._commit(f"Rename dashboard '{name}' to '{new_name}'")
return renamed
def duplicate(self, name: str) -> DashboardDef:
"""Copy the working copy into a new dashboard, as a draft only.
@@ -860,9 +892,12 @@ class DashboardStore:
"""
with self._lock, self.flows._write_lock:
source = self.read(name, draft=True)
new_name = copy_name(name, self.exists)
# The copy's id is its key, so it has to be free as both.
keys = self.key_index()
new_name = copy_name(source.name, lambda n: self.exists(n) or n in keys)
copy = source.model_copy(
update={
"id": new_name,
"name": new_name,
"title": f"{source.title} (copy)" if source.title else "",
"version": 1,