// 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.`}
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 (
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` : ''}