|
| 1 | +import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises' |
| 2 | +import path from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | + |
| 5 | +import type { |
| 6 | + FailureKind, |
| 7 | + RepoFailureReportSummary, |
| 8 | + RepoInstall, |
| 9 | + RepoRuntime, |
| 10 | + WorkspaceRepo, |
| 11 | +} from '../src/types/workspace.ts' |
| 12 | + |
| 13 | +type FailureSnapshotRecord = { |
| 14 | + command: string | null |
| 15 | + endedAt: string | null |
| 16 | + exitCode: number | null |
| 17 | + logTail: string[] |
| 18 | + message: string | null |
| 19 | + signal: string | null |
| 20 | + startedAt: string | null |
| 21 | + status: string |
| 22 | + updatedAt: string | null |
| 23 | +} |
| 24 | + |
| 25 | +type FailureReportFile = { |
| 26 | + generatedAt: string |
| 27 | + git: WorkspaceRepo['git'] |
| 28 | + health: WorkspaceRepo['health'] |
| 29 | + kind: FailureKind |
| 30 | + repo: { |
| 31 | + collection: string |
| 32 | + name: string |
| 33 | + packageManager: string |
| 34 | + path: string |
| 35 | + preferredMode: WorkspaceRepo['preferredMode'] |
| 36 | + previewUrl: string | null |
| 37 | + relativePath: string |
| 38 | + slug: string |
| 39 | + type: WorkspaceRepo['type'] |
| 40 | + } |
| 41 | + snapshot: FailureSnapshotRecord |
| 42 | +} |
| 43 | + |
| 44 | +type StoredFailureReport = FailureReportFile & { |
| 45 | + filePath: string |
| 46 | + workspaceRelativePath: string |
| 47 | +} |
| 48 | + |
| 49 | +const serverFile = fileURLToPath(import.meta.url) |
| 50 | +const serverDir = path.dirname(serverFile) |
| 51 | +const appRoot = path.resolve(serverDir, '..') |
| 52 | +const configuredWorkspaceRoot = process.env.WORKSPACE_HUB_WORKSPACE_ROOT?.trim() |
| 53 | +const workspaceRoot = configuredWorkspaceRoot |
| 54 | + ? path.resolve(configuredWorkspaceRoot) |
| 55 | + : path.resolve(appRoot, '..', '..') |
| 56 | +const reportsRoot = path.join(appRoot, 'data', 'failure-reports') |
| 57 | + |
| 58 | +function sanitizeFileSegment(value: string) { |
| 59 | + return value |
| 60 | + .trim() |
| 61 | + .toLowerCase() |
| 62 | + .replace(/[^a-z0-9]+/g, '-') |
| 63 | + .replace(/^-+|-+$/g, '') |
| 64 | + .slice(0, 80) || 'report' |
| 65 | +} |
| 66 | + |
| 67 | +function buildSnapshotRecord( |
| 68 | + kind: FailureKind, |
| 69 | + snapshot: RepoInstall | RepoRuntime, |
| 70 | +): FailureSnapshotRecord { |
| 71 | + const endedAt = |
| 72 | + kind === 'install' |
| 73 | + ? (snapshot as RepoInstall).finishedAt |
| 74 | + : (snapshot as RepoRuntime).stoppedAt |
| 75 | + |
| 76 | + return { |
| 77 | + command: snapshot.command, |
| 78 | + endedAt, |
| 79 | + exitCode: snapshot.lastExitCode, |
| 80 | + logTail: snapshot.logTail, |
| 81 | + message: snapshot.message, |
| 82 | + signal: snapshot.lastSignal, |
| 83 | + startedAt: snapshot.startedAt, |
| 84 | + status: snapshot.status, |
| 85 | + updatedAt: snapshot.updatedAt, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function toStoredFailureReport( |
| 90 | + filePath: string, |
| 91 | + report: FailureReportFile, |
| 92 | +): StoredFailureReport { |
| 93 | + return { |
| 94 | + ...report, |
| 95 | + filePath, |
| 96 | + workspaceRelativePath: path.relative(workspaceRoot, filePath), |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function toFailureReportSummary( |
| 101 | + report: StoredFailureReport, |
| 102 | +): RepoFailureReportSummary { |
| 103 | + return { |
| 104 | + command: report.snapshot.command, |
| 105 | + exitCode: report.snapshot.exitCode, |
| 106 | + filePath: report.filePath, |
| 107 | + generatedAt: report.generatedAt, |
| 108 | + kind: report.kind, |
| 109 | + message: report.snapshot.message, |
| 110 | + signal: report.snapshot.signal, |
| 111 | + workspaceRelativePath: report.workspaceRelativePath, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +export async function writeFailureReport( |
| 116 | + repo: WorkspaceRepo, |
| 117 | + kind: FailureKind, |
| 118 | + snapshot: RepoInstall | RepoRuntime, |
| 119 | +) { |
| 120 | + const generatedAt = new Date().toISOString() |
| 121 | + const report: FailureReportFile = { |
| 122 | + generatedAt, |
| 123 | + git: repo.git, |
| 124 | + health: repo.health, |
| 125 | + kind, |
| 126 | + repo: { |
| 127 | + collection: repo.collection, |
| 128 | + name: repo.name, |
| 129 | + packageManager: repo.packageManager, |
| 130 | + path: repo.path, |
| 131 | + preferredMode: repo.preferredMode, |
| 132 | + previewUrl: repo.previewUrl, |
| 133 | + relativePath: repo.relativePath, |
| 134 | + slug: repo.slug, |
| 135 | + type: repo.type, |
| 136 | + }, |
| 137 | + snapshot: buildSnapshotRecord(kind, snapshot), |
| 138 | + } |
| 139 | + |
| 140 | + const fileName = [ |
| 141 | + generatedAt.replace(/[:.]/g, '-'), |
| 142 | + sanitizeFileSegment(repo.relativePath), |
| 143 | + kind, |
| 144 | + ].join('-') + '.json' |
| 145 | + const filePath = path.join(reportsRoot, fileName) |
| 146 | + |
| 147 | + await mkdir(reportsRoot, { recursive: true }) |
| 148 | + await writeFile(filePath, `${JSON.stringify(report, null, 2)}\n`, 'utf8') |
| 149 | + |
| 150 | + return toStoredFailureReport(filePath, report) |
| 151 | +} |
| 152 | + |
| 153 | +async function readFailureReportFile(filePath: string) { |
| 154 | + try { |
| 155 | + const report = JSON.parse(await readFile(filePath, 'utf8')) as FailureReportFile |
| 156 | + |
| 157 | + if ( |
| 158 | + typeof report.generatedAt !== 'string' || |
| 159 | + typeof report.kind !== 'string' || |
| 160 | + typeof report.repo?.relativePath !== 'string' |
| 161 | + ) { |
| 162 | + return null |
| 163 | + } |
| 164 | + |
| 165 | + return toStoredFailureReport(filePath, report) |
| 166 | + } catch { |
| 167 | + return null |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +export async function readFailureReports() { |
| 172 | + try { |
| 173 | + const entries = await readdir(reportsRoot, { withFileTypes: true }) |
| 174 | + const files = entries |
| 175 | + .filter((entry) => entry.isFile() && entry.name.endsWith('.json')) |
| 176 | + .map((entry) => path.join(reportsRoot, entry.name)) |
| 177 | + |
| 178 | + const reports = await Promise.all(files.map((filePath) => readFailureReportFile(filePath))) |
| 179 | + return reports.filter((report): report is StoredFailureReport => Boolean(report)) |
| 180 | + } catch { |
| 181 | + return [] |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +export async function readLatestFailureReports() { |
| 186 | + const reports = await readFailureReports() |
| 187 | + const latest = new Map<string, RepoFailureReportSummary>() |
| 188 | + |
| 189 | + for (const report of reports) { |
| 190 | + const current = latest.get(report.repo.relativePath) |
| 191 | + if (!current || Date.parse(report.generatedAt) > Date.parse(current.generatedAt)) { |
| 192 | + latest.set(report.repo.relativePath, toFailureReportSummary(report)) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return latest |
| 197 | +} |
0 commit comments