// Live activity while events are handled. LiveTicker (Overview): the newest events first, with the ones
// being read now. RunTimeline (Event Center): each event's steps, received -> read -> traced -> acted.
import { Icon } from "../lib/icons.jsx";
import { KIND_LABEL, fmt, kindsOf } from "../lib/constants.js";
import { useData } from "../lib/data.jsx";
import { useUi, go } from "../lib/ui.jsx";
import { CardHead, Status } from "./common.jsx";
import { tr } from "../lib/i18n.js";
// The run reports "acted", "escalated" or "no action"; the status badge says "acted", "esc" or "none".
const OUTCOME = { acted: "acted", escalated: "esc", "no action": "none" };
const reading = (job) => (job?.current ? String(job.current).split(", ").filter(Boolean) : []);
function runLine(job) {
if (!job) return tr("Waiting for events.");
if (job.running) return tr("{done} of {total} events handled", { done: fmt(job.done), total: fmt(job.total) });
if (job.error) return tr("Stopped after {done} of {total} events.", { done: fmt(job.done), total: fmt(job.total) });
if (job.done) return job.elapsed ? tr("Last run: {n} events in {s} s", { n: fmt(job.done), s: fmt(job.elapsed) }) : tr("Last run: {n} events", { n: fmt(job.done) });
return tr("No run yet. Run the demo, or add an event.");
}
export function LiveTicker({ job }) {
const { set } = useUi();
const now = reading(job);
const rows = [...(job?.events || [])].reverse().slice(0, 7);
const open = (id) => { set({ sel: id, openEvent: id }); go("#/events"); };
return (
{tr("Live activity")}}
sub={runLine(job)} />
{job?.running &&
}
{now.map((id) => (
-
{id}{tr("Reading the messageā¦")}
))}
{rows.map((e) => (
- open(e.id)}>
{e.id}{e.summary}
))}
{!now.length && !rows.length && - {tr("Events appear here as the tower handles them.")}
}
);
}
// One event's steps, from what the tower recorded for it.
function steps(d, id, state) {
if (state === "queued") return [["wait", tr("Waiting")]];
if (state === "reading") return [["ok", tr("Received")], ["now", tr("Reading the message")]];
const t = d.traceById[id];
const rows = d.byEvent[id] || [];
const kinds = t ? kindsOf(t).map((k) => KIND_LABEL[k]).filter(Boolean) : [];
const paths = t ? (t.results || []).reduce((a, r) => a + (r.graph_paths || []).length, 0) : 0;
const writes = rows.filter((r) => r.ok && r.tool !== "escalate").length;
const escalated = rows.some((r) => r.tool === "escalate");
return [
["ok", tr("Received")],
["ok", tr("Read as {kinds}", { kinds: kinds.join(" + ") || tr("noise") })],
["ok", paths ? tr("Traced {n} paths", { n: fmt(paths) }) : tr("Nothing to trace")],
["ok", writes ? tr("Took {n} actions", { n: fmt(writes) }) : escalated ? tr("Sent to a person") : tr("No action needed")],
];
}
export function RunTimeline({ job }) {
const d = useData();
const { set } = useUi();
if (!job || (!job.running && !job.events?.length)) return null;
const now = reading(job);
const done = job.events || [];
// The events of this run in order: finished, being read, then a few still waiting.
const ids = (job.running ? [...done.map((e) => e.id), ...now] : done.map((e) => e.id)).slice(-6);
const state = (id) => (now.includes(id) ? "reading" : done.some((e) => e.id === id) ? "done" : "queued");
const rows = (
{ids.map((id) => (
set({ sel: id, evView: "feed" })}>
{id}
{steps(d, id, state(id)).map(([s, label], k) => (
-
{s === "ok" ? : s === "now" ? : }
{label}
))}
))}
);
// While a run is going: open, step by step. Afterwards: one line that opens to the same steps.
if (!job.running) return (
{tr("Last run")}{runLine(job).replace(/^Last run: /, "")}
{tr("Show steps")}
{rows}
);
return (
{tr("Live run")}} sub={runLine(job)} />
{rows}
);
}