Compare commits
11 changed files with 33 additions and 349 deletions
|
|
@ -204,17 +204,14 @@ counts as an agent.
|
||||||
**The API.** The router answers two NATS requests from swarm-controller, which
|
**The API.** The router answers two NATS requests from swarm-controller, which
|
||||||
serves them next to `GET /api/notify/flows`:
|
serves them next to `GET /api/notify/flows`:
|
||||||
|
|
||||||
| Route | Router subject | Answers |
|
| Route | Router subject | Answers |
|
||||||
| ---------------------------- | ------------------------------ | -------------------------------------------------------------------------------------------- |
|
| ---------------------------- | ------------------------------ | ------------------------------------------------------------------ |
|
||||||
| `GET /api/notify/deliveries` | `$SWARM.router.api.deliveries` | the newest rows, newest first; `?limit=` up to 500, 100 by default |
|
| `GET /api/notify/deliveries` | `$SWARM.router.api.deliveries` | the newest rows, newest first; `?limit=` up to 500, 100 by default |
|
||||||
| `GET /api/notify/dry-run` | `$SWARM.router.api.dry-run` | the saved flows document, run over the newest 50 events |
|
| `GET /api/notify/dry-run` | `$SWARM.router.api.dry-run` | the saved flows document, run over the newest 50 events |
|
||||||
| `POST /api/notify/dry-run` | `$SWARM.router.api.dry-run` | the body's draft flows document, checked as a `PUT` checks it, run over the newest 50 events |
|
|
||||||
|
|
||||||
The dry-run request carries a whole document. The `GET` sends the saved one;
|
The dry-run request carries a whole document, so a draft runs the same way.
|
||||||
the `POST` takes a `PUT` body (it ignores a `revision`), answers 400
|
The controller sends the saved one. [`/notifications`](ui.md#notification-flows)
|
||||||
with the `PUT` error for a document that fails the check, and forwards a valid one.
|
shows both: the recent deliveries, and the counts beside each node.
|
||||||
[`/notifications`](ui.md#notification-flows) shows both: the recent deliveries,
|
|
||||||
and the counts beside each node.
|
|
||||||
|
|
||||||
**Deploying in any order.** A router that predates these subjects doesn't
|
**Deploying in any order.** A router that predates these subjects doesn't
|
||||||
subscribe to them, and the queue answers the controller's request at once with
|
subscribe to them, and the queue answers the controller's request at once with
|
||||||
|
|
|
||||||
|
|
@ -296,12 +296,6 @@ editor offers to reload and drop your edits, or to keep them and replace this
|
||||||
flow's latest version with them. For any other refusal the editor shows the
|
flow's latest version with them. For any other refusal the editor shows the
|
||||||
controller's own message. The router runs a saved flow from its next event.
|
controller's own message. The router runs a saved flow from its next event.
|
||||||
|
|
||||||
**preview** runs the draft, saved or not, over the newest forge events and
|
|
||||||
shows on each box how many of them reached it, as a dry run does for the saved
|
|
||||||
flows in the list. It needs no save, so you can try a half-built flow. The
|
|
||||||
numbers go as soon as you edit the flow. If the draft would not save, or the
|
|
||||||
router can't run a dry run yet, the controller's own message shows instead.
|
|
||||||
|
|
||||||
Leaving the page with unsaved edits asks first, whether you close or reload the
|
Leaving the page with unsaved edits asks first, whether you close or reload the
|
||||||
tab, follow a link in the app, or press the browser's back button. Choose
|
tab, follow a link in the app, or press the browser's back button. Choose
|
||||||
**leave and drop edits** to go on, or cancel to stay and save.
|
**leave and drop edits** to go on, or cancel to stay and save.
|
||||||
|
|
|
||||||
|
|
@ -10,14 +10,6 @@ import { NODE_HEIGHT, NODE_WIDTH } from "./FlowLayout.js";
|
||||||
|
|
||||||
const PAD = 10;
|
const PAD = 10;
|
||||||
|
|
||||||
// The long kind name would run into the aside beside it.
|
|
||||||
const SHORT_KIND: Record<string, string> = { request_reviewer: "reviewer" };
|
|
||||||
|
|
||||||
// "1 hit", "0 hits": how the box says how many events reached it.
|
|
||||||
export function hitsText(hits: number): string {
|
|
||||||
return `${hits} ${hits === 1 ? "hit" : "hits"}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function BoxFace({
|
export function BoxFace({
|
||||||
kind,
|
kind,
|
||||||
text,
|
text,
|
||||||
|
|
@ -31,14 +23,17 @@ export function BoxFace({
|
||||||
// The events of the last dry run that reached the node, when one covers it.
|
// The events of the last dry run that reached the node, when one covers it.
|
||||||
hits?: number;
|
hits?: number;
|
||||||
}) {
|
}) {
|
||||||
const aside = [order, hits === undefined ? undefined : hitsText(hits)]
|
const aside = [
|
||||||
|
order,
|
||||||
|
hits === undefined ? undefined : `${hits} ${hits === 1 ? "hit" : "hits"}`,
|
||||||
|
]
|
||||||
.filter((part) => part !== undefined)
|
.filter((part) => part !== undefined)
|
||||||
.join(" · ");
|
.join(" · ");
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<rect width={NODE_WIDTH} height={NODE_HEIGHT} rx="6" />
|
<rect width={NODE_WIDTH} height={NODE_HEIGHT} rx="6" />
|
||||||
<text class="notify-box-kind" x={PAD} y="17">
|
<text class="notify-box-kind" x={PAD} y="17">
|
||||||
{SHORT_KIND[kind] ?? kind}
|
{kind}
|
||||||
</text>
|
</text>
|
||||||
{aside ? (
|
{aside ? (
|
||||||
<text
|
<text
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ import {
|
||||||
type Edit,
|
type Edit,
|
||||||
type Limits,
|
type Limits,
|
||||||
} from "./FlowDraft.js";
|
} from "./FlowDraft.js";
|
||||||
import { BoxFace, hitsText } from "./FlowBox.js";
|
import { BoxFace } from "./FlowBox.js";
|
||||||
import { layoutFlow, neighbour, nodeAt } from "./FlowLayout.js";
|
import { layoutFlow, neighbour, nodeAt } from "./FlowLayout.js";
|
||||||
import { describeNode, ordinal, type Flow } from "./FlowTypes.js";
|
import { describeNode, ordinal, type Flow } from "./FlowTypes.js";
|
||||||
|
|
||||||
|
|
@ -66,15 +66,12 @@ export function FlowCanvas({
|
||||||
selected,
|
selected,
|
||||||
limits,
|
limits,
|
||||||
palette,
|
palette,
|
||||||
hits,
|
|
||||||
...edits
|
...edits
|
||||||
}: {
|
}: {
|
||||||
flow: Flow;
|
flow: Flow;
|
||||||
selected: string | null;
|
selected: string | null;
|
||||||
limits: Limits;
|
limits: Limits;
|
||||||
palette: PaletteKind[];
|
palette: PaletteKind[];
|
||||||
// The events of a preview that reached each node, when one is showing.
|
|
||||||
hits?: Map<string, number>;
|
|
||||||
} & CanvasEdits) {
|
} & CanvasEdits) {
|
||||||
const layout = layoutFlow(flow);
|
const layout = layoutFlow(flow);
|
||||||
const byId = new Map(flow.nodes.map((n) => [n.id, n]));
|
const byId = new Map(flow.nodes.map((n) => [n.id, n]));
|
||||||
|
|
@ -341,7 +338,7 @@ export function FlowCanvas({
|
||||||
tabIndex={p.id === tabStop ? 0 : -1}
|
tabIndex={p.id === tabStop ? 0 : -1}
|
||||||
role="button"
|
role="button"
|
||||||
aria-pressed={p.id === selected}
|
aria-pressed={p.id === selected}
|
||||||
aria-label={`${node.kind} ${p.id}: ${text}${order}${hits ? `, ${hitsText(hits.get(p.id) ?? 0)}` : ""}`}
|
aria-label={`${node.kind} ${p.id}: ${text}${order}`}
|
||||||
onPointerDown={(e) => {
|
onPointerDown={(e) => {
|
||||||
if (node.kind === "source") {
|
if (node.kind === "source") {
|
||||||
// The source cannot move; a press just picks it.
|
// The source cannot move; a press just picks it.
|
||||||
|
|
@ -362,7 +359,6 @@ export function FlowCanvas({
|
||||||
order={
|
order={
|
||||||
pos.count > 1 ? `runs ${ordinal(pos.index + 1)}` : undefined
|
pos.count > 1 ? `runs ${ordinal(pos.index + 1)}` : undefined
|
||||||
}
|
}
|
||||||
hits={hits ? (hits.get(p.id) ?? 0) : undefined}
|
|
||||||
/>
|
/>
|
||||||
</g>
|
</g>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -429,19 +429,6 @@ export function saveRequest(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// The `POST /api/notify/dry-run` request for the draft: the same document a
|
|
||||||
// save sends, without a revision since nothing is stored.
|
|
||||||
export function previewRequest(
|
|
||||||
flows: Flow[],
|
|
||||||
disabledBuiltins: string[],
|
|
||||||
): { method: "POST"; headers: Record<string, string>; body: string } {
|
|
||||||
return {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
body: JSON.stringify({ disabled_builtins: disabledBuiltins, flows }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export type SaveOutcome = "saved" | "conflict" | "invalid" | "error";
|
export type SaveOutcome = "saved" | "conflict" | "invalid" | "error";
|
||||||
|
|
||||||
// What a save's HTTP status means to the editor: 409 is a stale revision (read
|
// What a save's HTTP status means to the editor: 409 is a stale revision (read
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,6 @@ import {
|
||||||
reparent,
|
reparent,
|
||||||
savedAs,
|
savedAs,
|
||||||
saveOutcome,
|
saveOutcome,
|
||||||
previewRequest,
|
|
||||||
saveRequest,
|
saveRequest,
|
||||||
seedFlow,
|
seedFlow,
|
||||||
parentOf,
|
parentOf,
|
||||||
|
|
@ -49,7 +48,6 @@ import {
|
||||||
import { useLeaveGuard } from "./useLeaveGuard.js";
|
import { useLeaveGuard } from "./useLeaveGuard.js";
|
||||||
import { NodeInspector } from "./NodeInspector.js";
|
import { NodeInspector } from "./NodeInspector.js";
|
||||||
import { FALLBACK_SCHEMA, type Flow, type FlowsView } from "./FlowTypes.js";
|
import { FALLBACK_SCHEMA, type Flow, type FlowsView } from "./FlowTypes.js";
|
||||||
import { DRY_RUN_URL, hitsOf, type DryRun } from "./RouterLive.js";
|
|
||||||
import "./NotificationsPage.css";
|
import "./NotificationsPage.css";
|
||||||
|
|
||||||
const FLOWS_URL = "/api/notify/flows";
|
const FLOWS_URL = "/api/notify/flows";
|
||||||
|
|
@ -77,14 +75,6 @@ export function FlowEditorPage({
|
||||||
const [saveError, setSaveError] = useState<ProblemDetails | null>(null);
|
const [saveError, setSaveError] = useState<ProblemDetails | null>(null);
|
||||||
const [stale, setStale] = useState(false);
|
const [stale, setStale] = useState(false);
|
||||||
const [notice, setNotice] = useState<string | null>(null);
|
const [notice, setNotice] = useState<string | null>(null);
|
||||||
// The last preview and the draft it ran, which stops counting once the
|
|
||||||
// draft is edited.
|
|
||||||
const [preview, setPreview] = useState<{
|
|
||||||
draft: Flow;
|
|
||||||
flowId: string;
|
|
||||||
dry: DryRun;
|
|
||||||
} | null>(null);
|
|
||||||
const [previewError, setPreviewError] = useState<ProblemDetails | null>(null);
|
|
||||||
const [busy, setBusy] = useState(false);
|
const [busy, setBusy] = useState(false);
|
||||||
const [confirmDelete, setConfirmDelete] = useState(false);
|
const [confirmDelete, setConfirmDelete] = useState(false);
|
||||||
|
|
||||||
|
|
@ -158,32 +148,6 @@ export function FlowEditorPage({
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the draft, saved or not, over the newest forge events, and put the
|
|
||||||
// number each node would have caught on its box.
|
|
||||||
async function runPreview() {
|
|
||||||
if (!view || !draft) return;
|
|
||||||
const flow = isNew ? savedAs(view, draft) : draft;
|
|
||||||
setBusy(true);
|
|
||||||
setPreviewError(null);
|
|
||||||
try {
|
|
||||||
const r = await fetch(
|
|
||||||
DRY_RUN_URL,
|
|
||||||
previewRequest(withFlow(view.flows, flow), view.disabled_builtins),
|
|
||||||
);
|
|
||||||
if (r.ok) {
|
|
||||||
setPreview({ draft, flowId: flow.id, dry: (await r.json()) as DryRun });
|
|
||||||
} else {
|
|
||||||
setPreview(null);
|
|
||||||
setPreviewError(await readApiError(r));
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
setPreview(null);
|
|
||||||
setPreviewError({ detail: String(err) });
|
|
||||||
} finally {
|
|
||||||
setBusy(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function onEdit(result: Edit, select?: string) {
|
function onEdit(result: Edit, select?: string) {
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
setNotice(result.reason);
|
setNotice(result.reason);
|
||||||
|
|
@ -253,8 +217,6 @@ export function FlowEditorPage({
|
||||||
const schema = view.schema ?? FALLBACK_SCHEMA;
|
const schema = view.schema ?? FALLBACK_SCHEMA;
|
||||||
const limits = limitsOf(view.schema);
|
const limits = limitsOf(view.schema);
|
||||||
const node = draft.nodes.find((n) => n.id === selected) ?? null;
|
const node = draft.nodes.find((n) => n.id === selected) ?? null;
|
||||||
const shown = preview?.draft === draft ? preview : null;
|
|
||||||
const hits = shown ? hitsOf(shown.dry, shown.flowId) : undefined;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Panel
|
<Panel
|
||||||
|
|
@ -279,12 +241,6 @@ export function FlowEditorPage({
|
||||||
{saveError ? (
|
{saveError ? (
|
||||||
<ApiErrorPanel context="the flow was not saved" problem={saveError} />
|
<ApiErrorPanel context="the flow was not saved" problem={saveError} />
|
||||||
) : null}
|
) : null}
|
||||||
{previewError ? (
|
|
||||||
<ApiErrorPanel
|
|
||||||
context="the preview did not run"
|
|
||||||
problem={previewError}
|
|
||||||
/>
|
|
||||||
) : null}
|
|
||||||
{notice ? (
|
{notice ? (
|
||||||
<p class="notify-notice" role="status">
|
<p class="notify-notice" role="status">
|
||||||
{notice}
|
{notice}
|
||||||
|
|
@ -332,9 +288,6 @@ export function FlowEditorPage({
|
||||||
>
|
>
|
||||||
discard edits
|
discard edits
|
||||||
</Button>
|
</Button>
|
||||||
<Button disabled={busy} onClick={() => void runPreview()}>
|
|
||||||
preview
|
|
||||||
</Button>
|
|
||||||
{isNew ? null : (
|
{isNew ? null : (
|
||||||
<Button disabled={busy} onClick={() => setConfirmDelete(true)}>
|
<Button disabled={busy} onClick={() => setConfirmDelete(true)}>
|
||||||
delete flow
|
delete flow
|
||||||
|
|
@ -342,13 +295,6 @@ export function FlowEditorPage({
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{shown ? (
|
|
||||||
<p class="notify-hint" role="status">
|
|
||||||
{hits
|
|
||||||
? `Preview: each box shows how many of the newest ${shown.dry.events} forge events reached it. Edit the flow and the numbers go.`
|
|
||||||
: "The preview skipped this flow because it is off."}
|
|
||||||
</p>
|
|
||||||
) : null}
|
|
||||||
|
|
||||||
<div class="notify-editor-body">
|
<div class="notify-editor-body">
|
||||||
<FlowCanvas
|
<FlowCanvas
|
||||||
|
|
@ -356,7 +302,6 @@ export function FlowEditorPage({
|
||||||
selected={selected}
|
selected={selected}
|
||||||
limits={limits}
|
limits={limits}
|
||||||
palette={paletteKinds(schema)}
|
palette={paletteKinds(schema)}
|
||||||
hits={hits}
|
|
||||||
onSelect={setSelected}
|
onSelect={setSelected}
|
||||||
onAdd={(kind, parentId) => {
|
onAdd={(kind, parentId) => {
|
||||||
const added = blankNode(draft, kind);
|
const added = blankNode(draft, kind);
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
// Wire types and helpers for what the notification router reports about
|
// Wire types and helpers for what the notification router reports about
|
||||||
// itself: its deliveries log and a dry run of the saved flows, or with
|
// itself: its deliveries log and a dry run of the saved flows. Backed by
|
||||||
// `POST` of a draft. Backed by swarm-controller's
|
// swarm-controller's `GET /api/notify/deliveries` and
|
||||||
// `GET /api/notify/deliveries` and `GET`/`POST /api/notify/dry-run`, which
|
// `GET /api/notify/dry-run`, which answer 503 while the running router
|
||||||
// answer 503 while the running router predates them.
|
// predates them.
|
||||||
import type { ProblemDetails } from "@hive/shared/api-error.js";
|
import type { ProblemDetails } from "@hive/shared/api-error.js";
|
||||||
|
|
||||||
export const DELIVERIES_URL = "/api/notify/deliveries";
|
export const DELIVERIES_URL = "/api/notify/deliveries";
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ import {
|
||||||
replaceNode,
|
replaceNode,
|
||||||
savedAs,
|
savedAs,
|
||||||
saveOutcome,
|
saveOutcome,
|
||||||
previewRequest,
|
|
||||||
saveRequest,
|
saveRequest,
|
||||||
seedFlow,
|
seedFlow,
|
||||||
siblingPosition,
|
siblingPosition,
|
||||||
|
|
@ -247,15 +246,6 @@ test("saveRequest sends the whole document at the revision read", () => {
|
||||||
assert.equal("builtins" in body, false, "built-ins are never sent");
|
assert.equal("builtins" in body, false, "built-ins are never sent");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("previewRequest posts the document without a revision", () => {
|
|
||||||
const req = previewRequest([flow()], ["b1"]);
|
|
||||||
assert.equal(req.method, "POST");
|
|
||||||
const body = JSON.parse(req.body);
|
|
||||||
assert.deepEqual(body.disabled_builtins, ["b1"]);
|
|
||||||
assert.equal(body.flows[0].id, "f");
|
|
||||||
assert.equal("revision" in body, false, "nothing is stored, so no revision");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("saveOutcome tells a stale revision from a refused document", () => {
|
test("saveOutcome tells a stale revision from a refused document", () => {
|
||||||
assert.equal(saveOutcome(200), "saved");
|
assert.equal(saveOutcome(200), "saved");
|
||||||
assert.equal(saveOutcome(409), "conflict");
|
assert.equal(saveOutcome(409), "conflict");
|
||||||
|
|
|
||||||
|
|
@ -165,20 +165,6 @@ pub(crate) fn unreadable(why: &DecodeError) -> ProblemDetails {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The document a body describes, or the first way it fails
|
|
||||||
/// [`Document::check`].
|
|
||||||
pub(crate) fn checked_document(
|
|
||||||
disabled_builtins: Vec<String>,
|
|
||||||
flows: Vec<Flow>,
|
|
||||||
) -> Result<Document, String> {
|
|
||||||
let doc = Document {
|
|
||||||
version: notify_flows::VERSION,
|
|
||||||
disabled_builtins,
|
|
||||||
flows,
|
|
||||||
};
|
|
||||||
doc.check().map(|()| doc)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Replace the flows.
|
/// Replace the flows.
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
put,
|
put,
|
||||||
|
|
@ -210,7 +196,12 @@ pub(crate) async fn put_flows(
|
||||||
body: Result<Json<PutFlows>, JsonRejection>,
|
body: Result<Json<PutFlows>, JsonRejection>,
|
||||||
) -> Result<Json<FlowsView>, ProblemDetails> {
|
) -> Result<Json<FlowsView>, ProblemDetails> {
|
||||||
let Json(put) = body.map_err(|e| error_problem(StatusCode::BAD_REQUEST, &e.body_text()))?;
|
let Json(put) = body.map_err(|e| error_problem(StatusCode::BAD_REQUEST, &e.body_text()))?;
|
||||||
let doc = checked_document(put.disabled_builtins, put.flows)
|
let doc = Document {
|
||||||
|
version: notify_flows::VERSION,
|
||||||
|
disabled_builtins: put.disabled_builtins,
|
||||||
|
flows: put.flows,
|
||||||
|
};
|
||||||
|
doc.check()
|
||||||
.map_err(|why| error_problem(StatusCode::BAD_REQUEST, &why))?;
|
.map_err(|why| error_problem(StatusCode::BAD_REQUEST, &why))?;
|
||||||
let client = queue(&state).map_err(refused)?;
|
let client = queue(&state).map_err(refused)?;
|
||||||
let store = open(&client).await.map_err(|e| queue_problem(&e))?;
|
let store = open(&client).await.map_err(|e| queue_problem(&e))?;
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,28 @@
|
||||||
//! `GET /api/notify/deliveries`, `GET /api/notify/dry-run` and
|
//! `GET /api/notify/deliveries` and `GET /api/notify/dry-run`: what the
|
||||||
//! `POST /api/notify/dry-run`: what the
|
//! notification router has delivered, and what the saved flows would do with
|
||||||
//! notification router has delivered, and what the saved flows, or a draft,
|
|
||||||
//! would do with
|
|
||||||
//! the newest forge events.
|
//! the newest forge events.
|
||||||
//!
|
//!
|
||||||
//! The router keeps the log and the events, so each is a NATS request to it
|
//! The router keeps the log and the events, so each is a NATS request to it
|
||||||
//! (`swarm_wire::router_api`). A router that predates them, or none at all,
|
//! (`swarm_wire::router_api`). A router that predates them, or none at all,
|
||||||
//! has no subscriber: the queue answers "no responders" at once and these
|
//! has no subscriber: the queue answers "no responders" at once and these
|
||||||
//! answer 503 saying so, as they do when it hears the request and stays
|
//! answer 503 saying so, as they do when it hears the request and stays
|
||||||
//! silent. The saved document is what `GET /api/notify/flows` shows; the `POST`
|
//! silent. The saved document is what `GET /api/notify/flows` shows; a draft
|
||||||
//! sends the same request with the body's document, checked as a `PUT` checks
|
//! is run by sending the same request with another document.
|
||||||
//! it.
|
|
||||||
|
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
use axum::extract::rejection::JsonRejection;
|
|
||||||
use axum::extract::{Query, State};
|
use axum::extract::{Query, State};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use problem_details::ProblemDetails;
|
use problem_details::ProblemDetails;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use swarm_wire::notify_flows::{Document, Flow};
|
use swarm_wire::notify_flows::Document;
|
||||||
use swarm_wire::router_api::{
|
use swarm_wire::router_api::{
|
||||||
DELIVERIES_SUBJECT, DRY_RUN_SUBJECT, Deliveries, DeliveriesRequest, DryRun, DryRunRequest,
|
DELIVERIES_SUBJECT, DRY_RUN_SUBJECT, Deliveries, DeliveriesRequest, DryRun, DryRunRequest,
|
||||||
REPLY_DEADLINE, Refused,
|
REPLY_DEADLINE, Refused,
|
||||||
};
|
};
|
||||||
use utoipa::{IntoParams, ToSchema};
|
use utoipa::IntoParams;
|
||||||
|
|
||||||
use crate::notify_flows::{Stored, checked_document, open, queue_problem, stored, unreadable};
|
use crate::notify_flows::{Stored, open, queue_problem, stored, unreadable};
|
||||||
use crate::routes::{AppState, error_problem};
|
use crate::routes::{AppState, error_problem};
|
||||||
use crate::schedules::{queue, refused};
|
use crate::schedules::{queue, refused};
|
||||||
|
|
||||||
|
|
@ -140,56 +136,8 @@ pub(crate) async fn get_dry_run(
|
||||||
ask(&client, DRY_RUN_SUBJECT, body).await.map(Json)
|
ask(&client, DRY_RUN_SUBJECT, body).await.map(Json)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The body of `POST /api/notify/dry-run`: the document of a `PUT`, whose
|
|
||||||
/// `revision` may be present and is ignored.
|
|
||||||
#[derive(Debug, Clone, Deserialize, ToSchema)]
|
|
||||||
#[serde(deny_unknown_fields)]
|
|
||||||
pub(crate) struct DraftFlows {
|
|
||||||
/// Ignored.
|
|
||||||
#[serde(default, rename = "revision")]
|
|
||||||
_revision: u64,
|
|
||||||
/// The ids of the built-in flows to switch off.
|
|
||||||
#[serde(default)]
|
|
||||||
disabled_builtins: Vec<String>,
|
|
||||||
/// The custom flows.
|
|
||||||
#[serde(default)]
|
|
||||||
flows: Vec<Flow>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A draft flows document run over the newest 50 forge events, delivering and
|
|
||||||
/// saving nothing: how many events reached each node.
|
|
||||||
#[utoipa::path(
|
|
||||||
post,
|
|
||||||
path = "/api/notify/dry-run",
|
|
||||||
request_body = DraftFlows,
|
|
||||||
responses(
|
|
||||||
(status = 200, description = "per flow that would run, the number of the newest 50 \
|
|
||||||
events that reached each node", body = DryRun),
|
|
||||||
(status = 400, description = "not JSON or not this shape, or the document fails the \
|
|
||||||
check `PUT /api/notify/flows` applies (problem+json)", body = String),
|
|
||||||
(status = 502, description = "the router refused, or its answer did not decode \
|
|
||||||
(problem+json)", body = String),
|
|
||||||
(status = 503, description = "no swarm queue is configured or connected here, or the \
|
|
||||||
router does not answer: it is not running, or it predates the dry run \
|
|
||||||
(problem+json)", body = String),
|
|
||||||
),
|
|
||||||
tag = "notify"
|
|
||||||
)]
|
|
||||||
pub(crate) async fn post_dry_run(
|
|
||||||
State(state): State<AppState>,
|
|
||||||
body: Result<Json<DraftFlows>, JsonRejection>,
|
|
||||||
) -> Result<Json<DryRun>, ProblemDetails> {
|
|
||||||
let Json(draft) = body.map_err(|e| error_problem(StatusCode::BAD_REQUEST, &e.body_text()))?;
|
|
||||||
let document = checked_document(draft.disabled_builtins, draft.flows)
|
|
||||||
.map_err(|why| error_problem(StatusCode::BAD_REQUEST, &why))?;
|
|
||||||
let client = queue(&state).map_err(refused)?;
|
|
||||||
let body = serde_json::to_vec(&DryRunRequest { document }).expect("a request encodes");
|
|
||||||
ask(&client, DRY_RUN_SUBJECT, body).await.map(Json)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use axum::extract::rejection::MissingJsonContentType;
|
|
||||||
use futures_util::StreamExt as _;
|
use futures_util::StreamExt as _;
|
||||||
use swarm_wire::notify_flows::{self, Document};
|
use swarm_wire::notify_flows::{self, Document};
|
||||||
use swarm_wire::router_api::{FlowHits, NodeHits};
|
use swarm_wire::router_api::{FlowHits, NodeHits};
|
||||||
|
|
@ -384,160 +332,4 @@ mod tests {
|
||||||
let unreadable = get_dry_run(State(state)).await.expect_err("version 2");
|
let unreadable = get_dry_run(State(state)).await.expect_err("version 2");
|
||||||
assert_eq!(unreadable.status, Some(StatusCode::CONFLICT));
|
assert_eq!(unreadable.status, Some(StatusCode::CONFLICT));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draft(flows: &serde_json::Value) -> Json<DraftFlows> {
|
|
||||||
Json(
|
|
||||||
serde_json::from_value(serde_json::json!({
|
|
||||||
"revision": 7,
|
|
||||||
"disabled_builtins": ["closed"],
|
|
||||||
"flows": flows,
|
|
||||||
}))
|
|
||||||
.expect("a draft"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sink_flow(id: &str) -> serde_json::Value {
|
|
||||||
serde_json::json!({
|
|
||||||
"id": id, "name": "n", "enabled": true,
|
|
||||||
"nodes": [
|
|
||||||
{"id": "source", "kind": "source", "events": ["issues"]},
|
|
||||||
{"id": "sink", "kind": "todo", "to": {"agent": "triage"}, "op": "upsert",
|
|
||||||
"key": "k", "prefix": false, "text": "t"},
|
|
||||||
],
|
|
||||||
"edges": [{"from": "source", "to": "sink"}],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A valid draft reaches the router as the request's document, with the
|
|
||||||
/// body's `revision` dropped, and the router's counts come back.
|
|
||||||
#[tokio::test]
|
|
||||||
async fn a_valid_draft_is_forwarded_and_the_counts_come_back() {
|
|
||||||
let nats = TestNats::start();
|
|
||||||
let client = nats.client().await;
|
|
||||||
let state = crate::agent_limits::tests::state(&client);
|
|
||||||
let reply = DryRun {
|
|
||||||
events: 2,
|
|
||||||
flows: vec![FlowHits {
|
|
||||||
flow: "mine".to_owned(),
|
|
||||||
nodes: vec![NodeHits {
|
|
||||||
node: "source".to_owned(),
|
|
||||||
hits: 2,
|
|
||||||
}],
|
|
||||||
}],
|
|
||||||
};
|
|
||||||
let router = answer(
|
|
||||||
&client,
|
|
||||||
DRY_RUN_SUBJECT,
|
|
||||||
serde_json::to_vec(&reply).unwrap(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
let Json(got) = post_dry_run(
|
|
||||||
State(state),
|
|
||||||
Ok(draft(&serde_json::json!([sink_flow("mine")]))),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.expect("answered");
|
|
||||||
assert_eq!(got, reply);
|
|
||||||
let sent: DryRunRequest =
|
|
||||||
serde_json::from_slice(&router.await.expect("task")).expect("a request");
|
|
||||||
assert_eq!(sent.document.disabled_builtins, ["closed"]);
|
|
||||||
assert_eq!(sent.document.flows.len(), 1);
|
|
||||||
assert_eq!(sent.document.flows[0].id, "mine");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A tree that fails the check is 400 with `PUT`'s message and never
|
|
||||||
/// reaches the router; a body that is not a document is 400 too. The
|
|
||||||
/// control is the valid draft above, which the same router subscription
|
|
||||||
/// would have received.
|
|
||||||
#[tokio::test]
|
|
||||||
async fn an_invalid_draft_is_400_and_never_forwarded() {
|
|
||||||
let nats = TestNats::start();
|
|
||||||
let client = nats.client().await;
|
|
||||||
let state = crate::agent_limits::tests::state(&client);
|
|
||||||
let mut requests = client.subscribe(DRY_RUN_SUBJECT).await.expect("subscribed");
|
|
||||||
client.flush().await.expect("flushed");
|
|
||||||
|
|
||||||
let mut orphan = sink_flow("orphan");
|
|
||||||
orphan["edges"] = serde_json::json!([]);
|
|
||||||
let refused = post_dry_run(
|
|
||||||
State(state.clone()),
|
|
||||||
Ok(draft(&serde_json::json!([orphan]))),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.expect_err("an orphan node");
|
|
||||||
assert_eq!(refused.status, Some(StatusCode::BAD_REQUEST));
|
|
||||||
let put = checked_document(
|
|
||||||
Vec::new(),
|
|
||||||
serde_json::from_value(serde_json::json!([{
|
|
||||||
"id": "orphan", "name": "n", "enabled": true,
|
|
||||||
"nodes": [
|
|
||||||
{"id": "source", "kind": "source", "events": ["issues"]},
|
|
||||||
{"id": "sink", "kind": "todo", "to": {"agent": "triage"}, "op": "upsert",
|
|
||||||
"key": "k", "prefix": false, "text": "t"},
|
|
||||||
],
|
|
||||||
"edges": [],
|
|
||||||
}]))
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.expect_err("the same check");
|
|
||||||
assert_eq!(
|
|
||||||
refused.detail.as_deref(),
|
|
||||||
Some(put.as_str()),
|
|
||||||
"PUT's message"
|
|
||||||
);
|
|
||||||
|
|
||||||
let typo: Result<Json<DraftFlows>, JsonRejection> = Err(
|
|
||||||
JsonRejection::MissingJsonContentType(MissingJsonContentType::default()),
|
|
||||||
);
|
|
||||||
let refused = post_dry_run(State(state.clone()), typo)
|
|
||||||
.await
|
|
||||||
.expect_err("not json");
|
|
||||||
assert_eq!(refused.status, Some(StatusCode::BAD_REQUEST));
|
|
||||||
|
|
||||||
// Nothing was forwarded: the next request on the subject is the
|
|
||||||
// control's, not either refusal's.
|
|
||||||
let control = post_dry_run(
|
|
||||||
State(state),
|
|
||||||
Ok(draft(&serde_json::json!([sink_flow("ok")]))),
|
|
||||||
);
|
|
||||||
let (sent, got) = tokio::join!(
|
|
||||||
async {
|
|
||||||
let m = requests.next().await.expect("the control's request");
|
|
||||||
let sent: DryRunRequest = serde_json::from_slice(&m.payload).expect("request");
|
|
||||||
client
|
|
||||||
.publish(
|
|
||||||
m.reply.expect("reply"),
|
|
||||||
serde_json::to_vec(&DryRun {
|
|
||||||
events: 0,
|
|
||||||
flows: Vec::new(),
|
|
||||||
})
|
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.expect("answered");
|
|
||||||
sent
|
|
||||||
},
|
|
||||||
control
|
|
||||||
);
|
|
||||||
assert_eq!(sent.document.flows[0].id, "ok");
|
|
||||||
assert!(got.is_ok());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// With no router the draft is 503; with no queue it is 503 too.
|
|
||||||
#[tokio::test]
|
|
||||||
async fn a_draft_without_a_router_is_unavailable() {
|
|
||||||
let nats = TestNats::start();
|
|
||||||
let client = nats.client().await;
|
|
||||||
let state = crate::agent_limits::tests::state(&client);
|
|
||||||
let b = post_dry_run(State(state), Ok(draft(&serde_json::json!([]))))
|
|
||||||
.await
|
|
||||||
.expect_err("nobody answers");
|
|
||||||
assert_eq!(b.status, Some(StatusCode::SERVICE_UNAVAILABLE));
|
|
||||||
let (state, _) = crate::routes::tests::state_with_roster();
|
|
||||||
let b = post_dry_run(State(state), Ok(draft(&serde_json::json!([]))))
|
|
||||||
.await
|
|
||||||
.expect_err("no queue");
|
|
||||||
assert_eq!(b.status, Some(StatusCode::SERVICE_UNAVAILABLE));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -645,10 +645,7 @@ pub(crate) fn build_app(state: AppState) -> axum::Router {
|
||||||
))
|
))
|
||||||
.routes(routes!(notify_flows::get_flows, notify_flows::put_flows))
|
.routes(routes!(notify_flows::get_flows, notify_flows::put_flows))
|
||||||
.routes(routes!(notify_router_api::get_deliveries))
|
.routes(routes!(notify_router_api::get_deliveries))
|
||||||
.routes(routes!(
|
.routes(routes!(notify_router_api::get_dry_run))
|
||||||
notify_router_api::get_dry_run,
|
|
||||||
notify_router_api::post_dry_run
|
|
||||||
))
|
|
||||||
.routes(routes!(webhook::post_webhook_forge))
|
.routes(routes!(webhook::post_webhook_forge))
|
||||||
.split_for_parts();
|
.split_for_parts();
|
||||||
// Just the JSON, not the UI — Swagger UI itself is nginx-hosted from
|
// Just the JSON, not the UI — Swagger UI itself is nginx-hosted from
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue