History is kept by flow id: runs, cascades, rollups and events file the id in their flow column and nodes as <flow id>.<node>, the API answers flow plus flow_name and shows and takes nodes as <key>.<node>; events carry flow_id from the moment they are published; run queue items, the stage cache and cached curves keep ids and keys apart; endpoints, the brain graph, device bindings and search name the id to open
This commit is contained in:
@@ -5,6 +5,10 @@ flow's id is opaque, so a tombstone keeps the key it last had, and `names_of`
|
||||
reads that back for anything showing the history. A flow deleted before
|
||||
tombstones existed has its old key as its id, which already reads right.
|
||||
|
||||
History names a node ``<flow id>.<node>``. The API shows and takes it as
|
||||
``<key>.<node>``, the way the engine names it, and `shown_nodes` and
|
||||
`stored_node` translate between the two.
|
||||
|
||||
The flow store is the one writer: it buries a flow before removing its files,
|
||||
asks before issuing an id, and lifts the tombstone of a flow restored under a
|
||||
pinned id once the restored document is on disk.
|
||||
@@ -35,20 +39,20 @@ def referenced(session: Session, flow_id: str) -> bool:
|
||||
queries = [
|
||||
select(Run.id).where(
|
||||
or_(
|
||||
col(Run.flow) == flow_id,
|
||||
col(Run.flow_id) == flow_id,
|
||||
col(Run.for_node).startswith(nodes, autoescape=True),
|
||||
)
|
||||
),
|
||||
select(FlowRun.id).where(col(FlowRun.flow) == flow_id),
|
||||
select(MetricBucket.flow).where(
|
||||
select(FlowRun.id).where(col(FlowRun.flow_id) == flow_id),
|
||||
select(MetricBucket.flow_id).where(
|
||||
or_(
|
||||
col(MetricBucket.flow) == flow_id,
|
||||
col(MetricBucket.flow_id) == flow_id,
|
||||
col(MetricBucket.node).startswith(nodes, autoescape=True),
|
||||
)
|
||||
),
|
||||
select(EngineEvent.id).where(
|
||||
or_(
|
||||
col(EngineEvent.flow) == flow_id,
|
||||
col(EngineEvent.flow_id) == flow_id,
|
||||
col(EngineEvent.node).startswith(nodes, autoescape=True),
|
||||
)
|
||||
),
|
||||
@@ -118,6 +122,44 @@ def name_of(flow_id: str, store: FlowStore) -> str:
|
||||
return names_of([flow_id], store)[flow_id]
|
||||
|
||||
|
||||
def id_of(key: str, store: FlowStore) -> str:
|
||||
"""The flow id behind *key*: the live flow's, else that of the one deleted
|
||||
flow that last held it, else *key* itself, as for history from before ids.
|
||||
"""
|
||||
live = store.key_index().get(key)
|
||||
if live is not None:
|
||||
return live
|
||||
with Session(engine) as session:
|
||||
graves = session.exec(
|
||||
select(FlowTombstone.id).where(col(FlowTombstone.name) == key).limit(2)
|
||||
).all()
|
||||
return graves[0] if len(graves) == 1 else key
|
||||
|
||||
|
||||
def shown_nodes(nodes: Iterable[str], store: FlowStore) -> dict[str, str]:
|
||||
"""Each stored ``<flow id>.<node>`` as ``<key>.<node>``, with one lookup."""
|
||||
split = {node: node.partition(".") for node in nodes}
|
||||
names = names_of({flow for flow, dot, _ in split.values() if dot}, store)
|
||||
return {
|
||||
node: f"{names[flow]}.{rest}" if dot else node
|
||||
for node, (flow, dot, rest) in split.items()
|
||||
}
|
||||
|
||||
|
||||
def stored_node(node: str, store: FlowStore, flow_id: str = "") -> str:
|
||||
"""A ``<key>.<node>`` as history stores it, ``<flow id>.<node>``.
|
||||
|
||||
With *flow_id*, a key that is that flow's own resolves to it, which is how
|
||||
a node of a deleted or renamed flow is still found by the name it shows.
|
||||
"""
|
||||
key, dot, rest = node.partition(".")
|
||||
if not dot:
|
||||
return node
|
||||
if flow_id and name_of(flow_id, store) == key:
|
||||
return f"{flow_id}.{rest}"
|
||||
return f"{id_of(key, store)}.{rest}"
|
||||
|
||||
|
||||
def prune(session: Session) -> int:
|
||||
"""Drop the tombstones no history row names any more. Returns how many."""
|
||||
stale = [
|
||||
|
||||
Reference in New Issue
Block a user