Files

197 lines
6.2 KiB
TypeScript

/**
* fleet/boardSource.ts — Read-only Zugriff auf die Board-Ablage (PVC fleet-board).
*
* Der Tower mountet das Board-PVC seit 0.19 READ-ONLY; geschrieben wird
* weiterhin ausschliesslich ueber den board-service (Single-Writer-Prinzip).
* Zweck: NAR-Historie (Rueckbau-Karten + Vollzug-Nachweise) und der
* Release-Notes-Feed brauchen die Board-Dateien, fuer die es kein MCP-Tool gibt.
*
* Datenformate:
* - <vs>/ideen-register.md Markdown-Tabelle | karte_id | titel | score | status | ursprung | epoch |
* - <vs>/release-notes.md Zeilen "- [ISO-8601] text (von agent)"
*/
import { readFileSync, readdirSync, existsSync, statSync } from "node:fs"
import path from "node:path"
export const FLEET_BOARD_DIR = process.env.FLEET_BOARD_DIR || "/srv/fleet/board"
export type BoardCard = {
karte_id: string
titel: string
score: number | null
status: string
ursprung: string
created: number | null
vs: string
}
export type ReleaseLine = {
iso: string
epoch: number | null
text: string
agent: string
vs: string
}
function cell(value: string): string {
return value.trim().replace(/^`|`$/g, "").trim()
}
/** Wertstreams = Unterverzeichnisse mit ideen-register.md (fail-loud-los: leere Liste). */
export function listBoardValueStreams(): string[] {
if (!existsSync(FLEET_BOARD_DIR)) return []
return readdirSync(FLEET_BOARD_DIR)
.filter((name) => {
const p = path.join(FLEET_BOARD_DIR, name)
try {
return statSync(p).isDirectory() && existsSync(path.join(p, "ideen-register.md"))
} catch {
return false
}
})
.sort()
}
/** Karten eines Wertstreams aus ideen-register.md (kaputte Zeilen werden uebersprungen). */
export function loadBoardCards(vs: string): BoardCard[] {
const file = path.join(FLEET_BOARD_DIR, vs, "ideen-register.md")
if (!existsSync(file)) return []
const out: BoardCard[] = []
for (const raw of readFileSync(file, "utf8").split("\n")) {
const line = raw.trim()
if (!line.startsWith("|")) continue
const cols = line.split("|").slice(1, -1).map(cell)
if (cols.length < 5) continue
if (/^karte.?id$/i.test(cols[0]) || /^-+$/.test(cols[0])) continue
const scoreRaw = Number(cols[2])
const createdRaw = Number(cols[5])
out.push({
karte_id: cols[0],
titel: cols[1],
score: Number.isFinite(scoreRaw) ? scoreRaw : null,
status: cols[3],
ursprung: cols[4],
created: Number.isFinite(createdRaw) && createdRaw > 0 ? createdRaw : null,
vs,
})
}
return out
}
/** Release-Notes-Zeilen eines Wertstreams, neueste zuerst. */
export function loadReleaseLines(vs: string): ReleaseLine[] {
const file = path.join(FLEET_BOARD_DIR, vs, "release-notes.md")
if (!existsSync(file)) return []
const out: ReleaseLine[] = []
for (const raw of readFileSync(file, "utf8").split("\n")) {
const line = raw.trim()
const m = line.match(/^-\s*\[([^\]]+)\]\s*(.*)$/)
if (!m) continue
const iso = m[1].trim()
let text = m[2].trim()
const t = Date.parse(iso)
let agent = ""
const von = text.match(/\(von\s+([^)]+)\)\s*$/)
if (von) {
agent = von[1].trim()
text = text.slice(0, von.index).trim()
} else {
const dash = text.match(/—\s*([A-Za-z0-9._-]+)\s*$/)
if (dash) {
agent = dash[1].trim()
text = text.slice(0, dash.index).trim()
}
}
out.push({
iso,
epoch: Number.isNaN(t) ? null : Math.floor(t / 1000),
text,
agent,
vs,
})
}
return out.sort((a, b) => (b.epoch ?? 0) - (a.epoch ?? 0))
}
export type NarEntry = {
karte: string
titel: string
datum: string
datum_offen: boolean
faellig_in_tagen: number | null
status: string
vollzogen: boolean
ueberfaellig: boolean
grant: string
ziel: string
agent: string
nachweis: string
vs: string
}
const TARGET_RE = /\b(\d{1,3}(?:\.\d{1,3}){3}(?::\d{1,5})?)\b/
const DATE_RE = /\b(\d{4}-\d{2}-\d{2})\b/
const GRANT_RE = /(?:NAR-Grant|Grant)\s+'?([A-Za-z0-9._-]{3,60})'?/i
/**
* NAR-Historie: Rueckbau-Karten (Grant-Lebenszyklus) + Vollzug-Nachweise aus
* den Release-Notes. Datenquelle sind ausschliesslich vorhandene Board-Daten —
* keine erfundenen Eintraege: fehlt ein Datum, wird datum_offen=true gesetzt.
*/
export function loadNarHistory(vsList?: string[]): {
history: NarEntry[]
ausstehend: NarEntry[]
today: string
} {
const streams = vsList && vsList.length ? vsList : listBoardValueStreams()
const todayIso = new Date().toISOString().slice(0, 10)
const todayMs = Date.parse(`${todayIso}T00:00:00Z`)
const all: NarEntry[] = []
for (const vs of streams) {
const cards = loadBoardCards(vs).filter(
(c) =>
c.karte_id.startsWith("rueckbau-") ||
/rueckbau/i.test(c.titel) ||
/NAR-Grant/i.test(c.titel)
)
const notes = loadReleaseLines(vs)
for (const c of cards) {
const dateMatch = c.titel.match(DATE_RE)
const datum = dateMatch ? dateMatch[1] : ""
let faellig: number | null = null
if (datum) {
const ms = Date.parse(`${datum}T00:00:00Z`)
if (!Number.isNaN(ms)) faellig = Math.round((ms - todayMs) / 86400000)
}
const grantMatch = c.titel.match(GRANT_RE)
const targetMatch = c.titel.match(TARGET_RE)
const status = c.status
const vollzogen = /^fertig$/i.test(status)
// Nachweis: Release-Note, die die Karten-ID nennt (Vollzug) — erste Uebereinstimmung.
const proof = notes.find((n) => n.text.includes(c.karte_id))
all.push({
karte: c.karte_id,
titel: c.titel,
datum,
datum_offen: !datum,
faellig_in_tagen: faellig,
status,
vollzogen,
ueberfaellig: !vollzogen && faellig !== null && faellig < 0,
grant: grantMatch ? grantMatch[1] : "",
ziel: targetMatch ? targetMatch[1] : "",
agent: proof ? proof.agent : "",
nachweis: proof ? proof.text.slice(0, 300) : "",
vs,
})
}
}
const history = all
.filter((e) => e.vollzogen)
.sort((a, b) => (b.datum || "").localeCompare(a.datum || ""))
const ausstehend = all
.filter((e) => !e.vollzogen)
.sort((a, b) => (a.faellig_in_tagen ?? 9e9) - (b.faellig_in_tagen ?? 9e9))
return { history, ausstehend, today: todayIso }
}