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:
@@ -119,6 +119,9 @@ class Endpoint(BaseModel):
|
||||
kind: str
|
||||
#: Stable within its kind, and used as the canvas node id.
|
||||
id: str
|
||||
#: The id of the dashboard or flow it sits in, which is what opening it
|
||||
#: goes by. Never read it off ``id``, whose flow part is a key.
|
||||
owner_id: str = ""
|
||||
label: str
|
||||
#: What sort of widget, or the node type in the other flow.
|
||||
detail: str = ""
|
||||
@@ -194,24 +197,31 @@ def _endpoints(controller: FlowController, definition: FlowDef) -> list[Endpoint
|
||||
found[f"dashboard:{binding['dashboard']}:{binding['widget']}"] = Endpoint(
|
||||
kind="dashboard",
|
||||
id=f"dashboard:{binding['dashboard']}:{binding['widget']}",
|
||||
owner_id=binding["dashboard"],
|
||||
label=binding["title"],
|
||||
detail=binding["type"],
|
||||
provides=[binding["provides"]] if binding["provides"] else [],
|
||||
requires=binding["requires"],
|
||||
)
|
||||
|
||||
def _flow_endpoint(key: str, detail: str) -> Endpoint:
|
||||
def _flow_endpoint(key: str, detail: str, owner_id: str = "") -> Endpoint:
|
||||
"""One label per node on the far side, however many names reach it."""
|
||||
return found.setdefault(
|
||||
f"flow:{key}",
|
||||
Endpoint(kind="flow", id=f"flow:{key}", label=key, detail=detail),
|
||||
Endpoint(
|
||||
kind="flow",
|
||||
id=f"flow:{key}",
|
||||
owner_id=owner_id,
|
||||
label=key,
|
||||
detail=detail,
|
||||
),
|
||||
)
|
||||
|
||||
# An outsider reaching into this flow.
|
||||
for other, node_id, provides, requires in controller.cross_flow_nodes(flow):
|
||||
endpoint = _flow_endpoint(f"{other}.{node_id}", "flow")
|
||||
endpoint.provides += provides
|
||||
endpoint.requires += requires
|
||||
for other in controller.cross_flow_nodes(flow):
|
||||
endpoint = _flow_endpoint(f"{other.flow}.{other.node}", "flow", other.flow_id)
|
||||
endpoint.provides += other.provides
|
||||
endpoint.requires += other.requires
|
||||
|
||||
# And this flow reaching out: its own ports bound to a message of another
|
||||
# flow. Read from the working document rather than from the store, so a
|
||||
@@ -235,6 +245,7 @@ def _endpoints(controller: FlowController, definition: FlowDef) -> list[Endpoint
|
||||
endpoint = _flow_endpoint(
|
||||
f"{far.flow}.{far.node}" if far else message,
|
||||
far.type if far else "flow",
|
||||
far.flow_id if far else "",
|
||||
)
|
||||
# An endpoint publishing into this flow is what this flow reads.
|
||||
side = endpoint.requires if ours_publishes else endpoint.provides
|
||||
@@ -289,14 +300,15 @@ def _read_flow(controller: FlowController, flow_id: str) -> FlowDef:
|
||||
raise _missing(flow_id)
|
||||
|
||||
|
||||
def _audit(action: str, flow: str, user: CurrentUser) -> None:
|
||||
"""Record who changed what, by the flow's key. The collector writes it
|
||||
down; the bus carries it."""
|
||||
def _audit(action: str, key: str, flow_id: str, user: CurrentUser) -> None:
|
||||
"""Record who changed what. The collector writes it down under the flow's
|
||||
id; the bus carries the key with it, as it does for every event."""
|
||||
event_bus.publish(
|
||||
{
|
||||
"type": "audit",
|
||||
"action": action,
|
||||
"flow": flow,
|
||||
"flow": key,
|
||||
"flow_id": flow_id,
|
||||
"user": user.email,
|
||||
"ts": time.time(),
|
||||
}
|
||||
@@ -415,13 +427,12 @@ async def delete_shared_node(
|
||||
first, so the source can be retired while flows still use it.
|
||||
"""
|
||||
if detach:
|
||||
running = await run_in_threadpool(controller.store.usages, lib_name, False)
|
||||
running = await run_in_threadpool(controller.store.lib_users, lib_name)
|
||||
try:
|
||||
await run_in_threadpool(controller.store.detach_lib, lib_name)
|
||||
except LibNotFound:
|
||||
raise HTTPException(status_code=404, detail=f"No shared node '{lib_name}'")
|
||||
# `usages` qualifies with the flow's id.
|
||||
for flow_id in sorted({usage.split(".", 1)[0] for usage in running}):
|
||||
for flow_id in running:
|
||||
await controller.reload_flow(flow_id)
|
||||
return Message(message=f"Deleted shared node '{lib_name}'")
|
||||
# The published flows too: a draft that unshared a node still runs it.
|
||||
@@ -643,7 +654,7 @@ async def publish_flow(
|
||||
status_code=409,
|
||||
detail={"message": str(exc), "current_version": exc.current},
|
||||
)
|
||||
_audit("published", definition.name, user)
|
||||
_audit("published", definition.name, flow_id, user)
|
||||
await controller.reload_flow(flow_id)
|
||||
return await run_in_threadpool(_detail, controller, published)
|
||||
|
||||
@@ -680,7 +691,7 @@ async def delete_flow(
|
||||
"""
|
||||
live = session.exec(
|
||||
select(Run.id).where(
|
||||
col(Run.flow) == flow_id, col(Run.status).in_(("running", "queued"))
|
||||
col(Run.flow_id) == flow_id, col(Run.status).in_(("running", "queued"))
|
||||
)
|
||||
).first()
|
||||
if live is not None:
|
||||
@@ -695,7 +706,7 @@ async def delete_flow(
|
||||
key = await run_in_threadpool(controller.store.delete_flow, flow_id)
|
||||
except FlowNotFound:
|
||||
raise _missing(flow_id)
|
||||
_audit("deleted", key, user)
|
||||
_audit("deleted", key, flow_id, user)
|
||||
# Its nodes first, or they would put values and work straight back.
|
||||
# Its runs stay: a run record is a record of what ran, and surviving the
|
||||
# flow it belonged to is the point of keeping one. `DELETE /runs?flow=`
|
||||
@@ -824,7 +835,7 @@ async def save_node_source(
|
||||
controller.store.write_lib_source, ref, source.code
|
||||
)
|
||||
if changed:
|
||||
_audit(f"edited shared node '{ref}'", name, user)
|
||||
_audit(f"edited shared node '{ref}'", name, flow_id, user)
|
||||
await controller.reload_lib_users(ref)
|
||||
else:
|
||||
await run_in_threadpool(
|
||||
@@ -905,7 +916,7 @@ async def start_flow(
|
||||
"""Let the engine run this flow again."""
|
||||
definition = _read_flow(controller, flow_id)
|
||||
await controller.set_enabled(flow_id, definition.name, True)
|
||||
_audit("started", definition.name, user)
|
||||
_audit("started", definition.name, flow_id, user)
|
||||
return await run_in_threadpool(_detail, controller, definition)
|
||||
|
||||
|
||||
@@ -916,7 +927,7 @@ async def stop_flow(
|
||||
"""Take this flow off the engine: no subscriptions, schedules or webhooks."""
|
||||
definition = _read_flow(controller, flow_id)
|
||||
await controller.set_enabled(flow_id, definition.name, False)
|
||||
_audit("stopped", definition.name, user)
|
||||
_audit("stopped", definition.name, flow_id, user)
|
||||
return await run_in_threadpool(_detail, controller, definition)
|
||||
|
||||
|
||||
@@ -1123,9 +1134,14 @@ async def train_node(
|
||||
if service is None:
|
||||
raise HTTPException(status_code=503, detail="Runs are not available")
|
||||
try:
|
||||
# Registered by `fluksio sync`, which gives it whatever id it gets: the
|
||||
# built-in flow is known by its key.
|
||||
train_id = controller.store.key_index().get(TRAIN_FLOW)
|
||||
if train_id is None:
|
||||
raise FlowNotFound(TRAIN_FLOW)
|
||||
run = await run_in_threadpool(
|
||||
service.submit,
|
||||
TRAIN_FLOW,
|
||||
train_id,
|
||||
params={"installation": orjson.dumps(wren.installation()).decode()},
|
||||
cause="train",
|
||||
actor=user.email,
|
||||
@@ -1153,11 +1169,15 @@ def read_training(
|
||||
controller: FlowControllerDep,
|
||||
session: SessionDep,
|
||||
) -> Any:
|
||||
"""A wren node's latest training run, or null when it has none."""
|
||||
"""A wren node's latest training run, or null when it has none.
|
||||
|
||||
Found by the node it trains alone: only a training run names one, and
|
||||
the training flow may have been registered again under another id since.
|
||||
"""
|
||||
_node_def(controller, flow_id, node_id)
|
||||
run = session.exec(
|
||||
select(Run)
|
||||
.where(col(Run.flow) == TRAIN_FLOW, col(Run.for_node) == f"{flow_id}.{node_id}")
|
||||
.where(col(Run.for_node) == f"{flow_id}.{node_id}")
|
||||
.order_by(col(Run.created_at).desc())
|
||||
).first()
|
||||
if run is None:
|
||||
|
||||
Reference in New Issue
Block a user