fleet(tower): 0.18 — Tasks-View (Spool/Dead-Letter/Alarm) + Task-Detail-Modal + /healthz + /api/status; Fleet-Views 0.13-0.18 erstmals versioniert
This commit is contained in:
@@ -0,0 +1,330 @@
|
||||
/**
|
||||
* fleet/taskSource.ts — Task-Envelope-Leser für die Task-Detail-Ansicht.
|
||||
*
|
||||
* Karte tower-task-detail (Change-Order 2026-10-30 aus devops-Sandbox,
|
||||
* /sandbox/fleet-change-orders/tower-task-detail/): Klick auf einen Eintrag der
|
||||
* Listen Spool-Reifegrad / Alarm-Feed / Dead-Letter zeigt den VOLLEN
|
||||
* Task-Envelope (from/to/type/status/priority/created/Alter/topic/body/...).
|
||||
*
|
||||
* Datenquelle: Spool-PVC ro (FLEET_SPOOL_DIR), dieselbe Quelle wie
|
||||
* countInbox()/readJournalTail(). Kein Egress, keine Secrets, kein neuer Grant.
|
||||
*
|
||||
* Abnahme-Regeln aus der Change-Order:
|
||||
* - Fehlende Felder -> "-" (missing_fields listet sie zusaetzlich).
|
||||
* - found:false (synthetisches Ereignis, z.B. Beacon-Event mit ref=agent_id)
|
||||
* liefert alle Kernfelder als "-" und die angefragte id, KEIN Crash.
|
||||
* - Body UNVERAENDERT (keine Maskierung) — XSS-Escaping nur im Panel.
|
||||
* - Nie 5xx fuer unbekannte ids; tote Quelle -> degraded statt Throw.
|
||||
*/
|
||||
import { readFileSync, existsSync, readdirSync, statSync } from "node:fs"
|
||||
import path from "node:path"
|
||||
import { parse as parseYaml } from "yaml"
|
||||
import { FLEET_SPOOL_DIR, readJournalTail, type JournalLine } from "./fleetSource"
|
||||
|
||||
export const MISSING = "-"
|
||||
const DEAD_LETTER_DAYS = 7
|
||||
const DETAIL_STALE_SECONDS = Number(process.env.FLEET_DETAIL_STALE_SECONDS || "900")
|
||||
|
||||
const CORE_FIELDS = [
|
||||
"task_id", "from", "to", "type", "status", "priority", "created",
|
||||
"created_iso", "age_seconds", "age_human", "topic", "body", "thread_id",
|
||||
"karte_id", "reply_to", "vs", "claimed_at", "status_label",
|
||||
] as const
|
||||
|
||||
const STATUS_LABEL: Record<string, string> = {
|
||||
submitted: "eingereicht", working: "in Arbeit", open: "offen",
|
||||
completed: "abgeschlossen", failed: "fehlgeschlagen", blocked: "blockiert",
|
||||
}
|
||||
|
||||
export type TaskEnvelope = {
|
||||
task_id: string
|
||||
from: string
|
||||
to: string
|
||||
type: string
|
||||
status: string
|
||||
priority: string
|
||||
created: number | null
|
||||
created_iso: string
|
||||
age_seconds: number | null
|
||||
age_human: string
|
||||
topic: string
|
||||
body: string
|
||||
thread_id: string
|
||||
karte_id: string
|
||||
reply_to: string
|
||||
vs: string
|
||||
claimed_at: string
|
||||
status_label: string
|
||||
file: string
|
||||
}
|
||||
|
||||
export type TaskDetail = {
|
||||
generated_at: string
|
||||
now: number
|
||||
source: string
|
||||
refresh_seconds: number
|
||||
entry_kind: string
|
||||
found: boolean
|
||||
query_task_id: string
|
||||
task: Record<string, string | number | null>
|
||||
missing_fields: string[]
|
||||
extra_fields: Record<string, unknown>
|
||||
raw_keys: string[]
|
||||
body_note: string
|
||||
degraded: boolean
|
||||
degraded_reason: string
|
||||
data_ts: string | number | null
|
||||
data_age_seconds: number | null
|
||||
stale: boolean
|
||||
error: string
|
||||
}
|
||||
|
||||
function iso(epoch: number | null): string {
|
||||
return epoch === null ? MISSING : new Date(epoch * 1000).toISOString()
|
||||
}
|
||||
|
||||
function humanize(seconds: number | null): string {
|
||||
if (seconds === null) return MISSING
|
||||
const s = Math.max(0, Math.floor(seconds))
|
||||
const d = Math.floor(s / 86400)
|
||||
const h = Math.floor((s % 86400) / 3600)
|
||||
const m = Math.floor((s % 3600) / 60)
|
||||
if (d) return `${d}d ${h}h`
|
||||
if (h) return `${h}h ${m}m`
|
||||
return `${m}m`
|
||||
}
|
||||
|
||||
function toEpoch(value: unknown): number | null {
|
||||
if (value === null || value === undefined || typeof value === "boolean") return null
|
||||
if (typeof value === "number") return Number.isFinite(value) ? Math.floor(value) : null
|
||||
const s = String(value).trim()
|
||||
if (!s) return null
|
||||
if (/^\d+$/.test(s)) return Number(s)
|
||||
const t = Date.parse(s)
|
||||
return Number.isNaN(t) ? null : Math.floor(t / 1000)
|
||||
}
|
||||
|
||||
function str(value: unknown): string {
|
||||
if (value === null || value === undefined) return MISSING
|
||||
if (typeof value === "string") return value.trim() === "" ? MISSING : value
|
||||
if (typeof value === "object") return JSON.stringify(value)
|
||||
return String(value)
|
||||
}
|
||||
|
||||
function normalizeEnvelope(doc: Record<string, unknown>, now: number, file: string): TaskEnvelope {
|
||||
const taskId = String(doc.task_id ?? doc.id ?? "")
|
||||
const created = toEpoch(doc.created ?? doc.created_at)
|
||||
const age = created === null ? null : Math.max(0, now - created)
|
||||
const status = String(doc.status ?? "").toLowerCase()
|
||||
const claimedRaw = doc.claimed_at ?? doc.working_since ?? doc.claimed
|
||||
return {
|
||||
task_id: taskId || MISSING,
|
||||
from: str(doc.from ?? doc.from_agent),
|
||||
to: str(doc.to),
|
||||
type: str(doc.type),
|
||||
status: status || MISSING,
|
||||
priority: str(doc.priority),
|
||||
created,
|
||||
created_iso: iso(created),
|
||||
age_seconds: age,
|
||||
age_human: humanize(age),
|
||||
topic: str(doc.topic),
|
||||
body: str(doc.body),
|
||||
thread_id: str(doc.thread_id),
|
||||
karte_id: str(doc.karte_id),
|
||||
reply_to: str(doc.reply_to),
|
||||
vs: str(doc.vs),
|
||||
claimed_at: claimedRaw === undefined || claimedRaw === null ? MISSING : iso(toEpoch(claimedRaw)),
|
||||
status_label: STATUS_LABEL[status] ?? (status || MISSING),
|
||||
file,
|
||||
}
|
||||
}
|
||||
|
||||
type RawEnvelope = { doc: Record<string, unknown>; envelope: TaskEnvelope }
|
||||
|
||||
function readAllEnvelopes(now: number): RawEnvelope[] {
|
||||
if (!existsSync(FLEET_SPOOL_DIR)) {
|
||||
throw new Error(`Spool-Dir fehlt: ${FLEET_SPOOL_DIR} (fleet-spool-PVC ro gemountet?)`)
|
||||
}
|
||||
const out: RawEnvelope[] = []
|
||||
for (const f of readdirSync(FLEET_SPOOL_DIR)) {
|
||||
if (!f.endsWith(".yaml")) continue
|
||||
const full = path.join(FLEET_SPOOL_DIR, f)
|
||||
try {
|
||||
if (!statSync(full).isFile()) continue
|
||||
const doc = parseYaml(readFileSync(full, "utf8")) as Record<string, unknown> | null
|
||||
if (!doc || typeof doc !== "object") continue
|
||||
if (!doc.task_id && !doc.id) continue
|
||||
out.push({ doc, envelope: normalizeEnvelope(doc, now, f) })
|
||||
} catch {
|
||||
// Unparsebare Datei: nicht Teil der Ansicht — erscheint im Journal als rejected.
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** Spool-Reifegrad: offene Envelopes (submitted/working), aelteste zuerst. */
|
||||
export function listSpoolTasks(): TaskEnvelope[] {
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
const open = readAllEnvelopes(now).filter(({ envelope }) =>
|
||||
["submitted", "working", "running", "open"].includes(envelope.status))
|
||||
return open.map((r) => r.envelope).sort((a, b) => (b.age_seconds ?? 0) - (a.age_seconds ?? 0))
|
||||
}
|
||||
|
||||
/** Dead-Letter: fehlgeschlagene Envelopes der letzten 7 Tage, aelteste zuerst. */
|
||||
export function listDeadLetterTasks(): TaskEnvelope[] {
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
const cutoff = now - DEAD_LETTER_DAYS * 86400
|
||||
return readAllEnvelopes(now)
|
||||
.filter(({ envelope }) => envelope.status === "failed")
|
||||
.filter(({ envelope }) => envelope.created !== null && envelope.created >= cutoff)
|
||||
.map((r) => r.envelope)
|
||||
.sort((a, b) => (b.age_seconds ?? 0) - (a.age_seconds ?? 0))
|
||||
}
|
||||
|
||||
export type AlarmRow = {
|
||||
time_epoch: number | null
|
||||
time_iso: string
|
||||
age_human: string
|
||||
event: string
|
||||
ref: string
|
||||
from: string
|
||||
to: string
|
||||
task_id: string
|
||||
note: string
|
||||
}
|
||||
|
||||
const ALARM_EVENT_RE = /deadman|alarm|stall|beacon|gateway|reject|invalid|orphan|escalat|blocked|timeout|fail|_warn/i
|
||||
|
||||
/** Alarm-Feed: Stoerungs-/Zustandsereignisse aus dem Spool-Journal (letzte 24 h). */
|
||||
export function listAlarmEvents(limit = 120): AlarmRow[] {
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
const cutoff = now - 24 * 3600
|
||||
const lines: JournalLine[] = readJournalTail(1500)
|
||||
const rows: AlarmRow[] = []
|
||||
for (const line of lines) {
|
||||
const event = String(line.event ?? "")
|
||||
if (!event || !ALARM_EVENT_RE.test(event)) continue
|
||||
const t = toEpoch(line.time_epoch)
|
||||
if (t !== null && t < cutoff) continue
|
||||
rows.push({
|
||||
time_epoch: t,
|
||||
time_iso: iso(t),
|
||||
age_human: humanize(t === null ? null : now - t),
|
||||
event,
|
||||
ref: String(line.task_id ?? line.ref ?? line.agent_id ?? line.agent ?? MISSING),
|
||||
from: String(line.from ?? line.by ?? MISSING),
|
||||
to: String(line.to ?? MISSING),
|
||||
task_id: String(line.task_id ?? MISSING),
|
||||
note: String(line.note ?? line.reason ?? ""),
|
||||
})
|
||||
}
|
||||
return rows.slice(-limit).reverse()
|
||||
}
|
||||
|
||||
/** Vollstaendiger Envelope fuer die Detailansicht. Wirft NICHT bei unbekannter id. */
|
||||
export function getTaskDetail(queryId: string, kind = ""): TaskDetail {
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
const base: TaskDetail = {
|
||||
generated_at: new Date(now * 1000).toISOString(),
|
||||
now,
|
||||
source: FLEET_SPOOL_DIR,
|
||||
refresh_seconds: 30,
|
||||
entry_kind: kind || "unknown",
|
||||
found: false,
|
||||
query_task_id: queryId,
|
||||
task: Object.fromEntries(CORE_FIELDS.map((k) => [k, MISSING])),
|
||||
missing_fields: [...CORE_FIELDS],
|
||||
extra_fields: {},
|
||||
raw_keys: [],
|
||||
body_note: "Body unveraendert aus dem Spool (keine Maskierung).",
|
||||
degraded: false,
|
||||
degraded_reason: "",
|
||||
data_ts: null,
|
||||
data_age_seconds: null,
|
||||
stale: false,
|
||||
error: "",
|
||||
}
|
||||
if (!queryId) {
|
||||
base.error = "Parameter id fehlt"
|
||||
return base
|
||||
}
|
||||
if (!existsSync(FLEET_SPOOL_DIR)) {
|
||||
base.degraded = true
|
||||
base.degraded_reason = "quelle-tot"
|
||||
base.error = `Spool-Dir fehlt: ${FLEET_SPOOL_DIR}`
|
||||
return base
|
||||
}
|
||||
try {
|
||||
const wanted = queryId.trim().toLowerCase()
|
||||
const hits = readAllEnvelopes(now).filter(({ envelope }) =>
|
||||
envelope.task_id.toLowerCase() === wanted)
|
||||
if (hits.length === 0) {
|
||||
base.task.task_id = queryId
|
||||
return base
|
||||
}
|
||||
const best = hits.sort((a, b) => (b.envelope.created ?? 0) - (a.envelope.created ?? 0))[0]
|
||||
const env = best.envelope
|
||||
const task: Record<string, string | number | null> = {}
|
||||
const missing: string[] = []
|
||||
for (const k of CORE_FIELDS) {
|
||||
const v = env[k as keyof TaskEnvelope]
|
||||
if (v === MISSING || v === null || v === "") {
|
||||
task[k] = MISSING
|
||||
missing.push(k)
|
||||
} else {
|
||||
task[k] = v as string | number
|
||||
}
|
||||
}
|
||||
const extra: Record<string, unknown> = {}
|
||||
const known = new Set<string>([...CORE_FIELDS, "id", "file", "created_at", "working_since", "claimed"])
|
||||
for (const [k, v] of Object.entries(best.doc)) {
|
||||
if (!known.has(k)) extra[k] = v
|
||||
}
|
||||
base.found = true
|
||||
base.task = task
|
||||
base.missing_fields = missing
|
||||
base.extra_fields = extra
|
||||
base.raw_keys = Object.keys(best.doc).sort()
|
||||
base.data_ts = env.created
|
||||
base.data_age_seconds = env.age_seconds
|
||||
base.stale = env.age_seconds !== null && env.age_seconds > DETAIL_STALE_SECONDS
|
||||
return base
|
||||
} catch (e) {
|
||||
base.degraded = true
|
||||
base.degraded_reason = "lesefehler"
|
||||
base.error = e instanceof Error ? e.message : String(e)
|
||||
return base
|
||||
}
|
||||
}
|
||||
|
||||
export type TaskOverview = {
|
||||
generated_at: string
|
||||
spool: TaskEnvelope[]
|
||||
dead_letter: TaskEnvelope[]
|
||||
alarm: AlarmRow[]
|
||||
counts: { spool: number; dead_letter: number; alarm: number }
|
||||
degraded: boolean
|
||||
degraded_reason: string
|
||||
}
|
||||
|
||||
/** Alle drei Listen in einem Call (Panel-Initialisierung + 30s-Refresh). */
|
||||
export function loadTaskOverview(): TaskOverview {
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
const out: TaskOverview = {
|
||||
generated_at: new Date(now * 1000).toISOString(),
|
||||
spool: [], dead_letter: [], alarm: [],
|
||||
counts: { spool: 0, dead_letter: 0, alarm: 0 },
|
||||
degraded: false, degraded_reason: "",
|
||||
}
|
||||
try {
|
||||
out.spool = listSpoolTasks()
|
||||
out.dead_letter = listDeadLetterTasks()
|
||||
out.alarm = listAlarmEvents()
|
||||
out.counts = { spool: out.spool.length, dead_letter: out.dead_letter.length, alarm: out.alarm.length }
|
||||
} catch (e) {
|
||||
out.degraded = true
|
||||
out.degraded_reason = e instanceof Error ? e.message : String(e)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user