// Processes: every worker thread with what it does, its state, start/stop, autostart and recent activity; the event log // (fly_trader/ui/api/processes.py). Dashtreme layout: a KPI row of colour widgets, one colour widget card per worker // (its colour is its state), the event log as a DataTable beside the last 24 hours by level. import { useState, type ReactNode } from 'react' import { action } from '../lib/api' import { useNow, useStored, useTopic } from '../lib/hooks' import { ago, dt, hm, int } from '../lib/format' import { Callout, Caption, Card, Empty, Icon, KVList, Loaded, Pills, SectionTitle, Sparkline, Toggle, Widget, WidgetGrid, type WidgetColor } from '../components/ui' import { DataTable, Pill, type Col } from '../components/DataTable' import { DonutChart } from '../components/Charts' import { ActionButton, Modal, useFeedback } from '../components/feedback' import { PageHead } from '../components/ChainPicker' type WorkerState = 'running' | 'stopping' | 'crashed' | 'stopped' | 'external' type Worker = { name: string; title: string; icon: string; desc: string; role: string; role_label: string; group: string state: WorkerState; alive: boolean; stopping: boolean; started_at: string | null; stopped_at: string | null; error: string | null external: boolean; autostart: boolean; ext_pid: number | null; ext_started_at: string | null; start_note: string | null; stop_note: string | null } type External = { name: string; title: string; pid: number; started_at: string | null } type Counts = { running: number; stopping: number; crashed: number; stopped: number; external: number; total: number; autostart: number; trade_needed: number; trade_up: number } type Workers = { pid: number; observer: boolean; host_pid: number | null; external: External[]; groups: { key: string; title: string; workers: Worker[] }[]; counts: Counts } type EventStats = { levels: Record; hours: string[]; warning: number[]; error: number[] } type LogLine = { time: string; level: string; what: string } type Ev = { ts: string; level: string; source: string; message: string } const STATE: Record = { running: { color: 'success', label: 'Running', icon: 'bx-play-circle' }, stopping: { color: 'warning', label: 'Stopping', icon: 'bx-loader-circle' }, crashed: { color: 'danger', label: 'Crashed', icon: 'bx-error-circle' }, stopped: { color: 'dark', label: 'Stopped', icon: 'bx-stop-circle' }, external: { color: 'info', label: 'Running outside', icon: 'bx-terminal' }, } const LEVELS = ['info', 'warning', 'error'] as const const LEVEL_PILL: Record = { info: 'gray', warning: 'orange', error: 'red', INFO: 'gray', WARNING: 'orange', ERROR: 'red', CRITICAL: 'red', DEBUG: 'gray' } const LEVEL_DOT: Record = { info: 'rgba(255,255,255,0.35)', warning: '#ffc107', error: '#f0435a' } export default function Processes() { const s = useTopic('processes.workers') const stats = useTopic('processes.event_stats') return ( <> {(d) => ( <> {d.observer ? `This console only observes (fly-trader ui --observe): the workers are threads inside the console that hosts them (pid ${d.host_pid ?? 'unknown'}); start and stop them there.` : `All workers are threads inside this console process (pid ${d.pid}); stopping one waits for it to finish its current step.`} {d.external.length > 0 && } {d.groups.map((g) => { const up = g.workers.filter((w) => w.state === 'running' || w.state === 'external').length return (
{g.title} · {up} of {g.workers.length} running
{g.workers.map((w) =>
)}
) })} )}
Events
) } function Kpis({ d, stats }: { d: Workers; stats?: EventStats }) { const c = d.counts const errors = stats?.levels.error ?? null return ( ) } function ExternalBanner({ ext, observer }: { ext: External[]; observer: boolean }) { const now = useNow(5000) return (
Worker processes are running outside the console (started from the command line). Stop them to keep everything in one place.
{ext.map((a) => (
External {a.name} · pid {a.pid} · started {ago(a.started_at, now)}
))}
) } function StopExternal({ name, title, pid, observer }: { name: string; title: string; pid: number | null; observer: boolean }) { return ( Sends SIGTERM to pid {pid ?? '?'}, waits up to 30 s for it to finish, then kills it (SIGKILL). The console can then run {title} as one of its threads.

, }} /> ) } /** A pill on a colour widget (role, autostart, thread). */ function WPill({ children, title }: { children: ReactNode; title?: string }) { return ( {children} ) } function WorkerCard({ w, observer }: { w: Worker; observer: boolean }) { const now = useNow(5000) const fb = useFeedback() const [activity, setActivity] = useState(false) const [busy, setBusy] = useState(false) const st = STATE[w.state] const ink = st.color === 'warning' ? '#241a00' : '#fff' const since = w.alive && w.started_at ? `running since ${ago(w.started_at, now)}` : w.stopped_at ? `stopped ${ago(w.stopped_at, now)}` : 'not started' const sub = w.state === 'external' ? `pid ${w.ext_pid ?? '?'} · started ${ago(w.ext_started_at, now)}` : since const setAutostart = async (on: boolean) => { const ok = await fb.confirm({ title: on ? `Autostart ${w.title}?` : `Stop autostarting ${w.title}?`, body:

{on ? `${w.title} will start whenever the console starts.` : `${w.title} will no longer start with the console; this does not stop it now.`}

, confirmLabel: on ? 'Turn on' : 'Turn off', }) if (!ok) return setBusy(true) try { const r = await action('processes.autostart', { name: w.name, on }) fb.toast(r.message || 'Done', 'success') } catch (e) { fb.toast((e as Error).message, 'error', 10000) } finally { setBusy(false) } } return (

{w.title}

{st.label}

{sub}

{w.role_label} {w.autostart ? 'autostart on' : 'autostart off'} thread {w.name}
{w.desc}
{w.error && (
{w.error}
)}

Starts {w.title} as a thread inside this console: {w.desc}

{w.start_note && {w.start_note}} ), }} /> {w.state === 'external' ? : (

Asks the {w.title} thread to stop; it finishes its current step first, which can take a while.

{w.stop_note &&

{w.stop_note}

} ), }} /> )}
{activity && setActivity(false)} />}
) } const LOG_COLS: Col[] = [ { key: 'time', label: 'time', width: 90 }, { key: 'level', label: 'level', width: 100, render: (r) => (r.level ? {r.level} : '') }, { key: 'what', label: 'what', wrap: true }, ] function Activity({ name, title, onClose }: { name: string; title: string; onClose: () => void }) { const s = useTopic<{ name: string; rows: LogLine[] }>('processes.activity', { worker: name }) return ( {(d) => (d.rows.length ? : Nothing logged yet.)} ) } const EV_COLS: Col[] = [ { key: 'ts', label: 'time', width: 150, render: (r) => dt(r.ts, true) }, { key: 'level', label: 'level', width: 100, render: (r) => {r.level} }, { key: 'source', label: 'source', width: 130 }, { key: 'message', label: 'message', wrap: true }, ] function EventLog() { const [stored, setStored] = useStored('ev_levels', ['warning', 'error']) const levels = LEVELS.filter((l) => stored.includes(l)) const s = useTopic<{ levels: string[]; rows: Ev[] }>('processes.events', { levels }) return ( setStored(v)} />}> {(d) => (d.rows.length ? : Nothing at these levels.)} ) } function EventSummary({ s }: { s?: EventStats }) { if (!s) return
const total = Object.values(s.levels).reduce((a, b) => a + b, 0) const lv = LEVELS.map((l) => ({ l, n: s.levels[l] ?? 0 })) const hourly = s.warning.map((w, i) => w + (s.error[i] ?? 0)) return ( {total ? x.l)} values={lv.map((x) => x.n)} center={int(total)} centerLabel="events" height={250} colors={lv.map((x) => LEVEL_DOT[x.l])} /> : No events in the last 24 hours.} ({ label: x.l, value: `${int(x.n)}${total ? ` · ${((x.n / total) * 100).toFixed(0)}%` : ''}`, dot: LEVEL_DOT[x.l] }))} />
Warnings and errors per hour, {hm(s.hours[0])}–{hm(s.hours[s.hours.length - 1])} UTC{hourly.some((v) => v > 0) ? ` · peak ${int(Math.max(...hourly))} in an hour` : ''}
{hourly.some((v) => v > 0) ? : None.}
) }