App.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import {
  2. ActivityIcon,
  3. CheckCircle2Icon,
  4. ClockIcon,
  5. CpuIcon,
  6. GaugeIcon,
  7. HardDriveIcon,
  8. MemoryStickIcon,
  9. XCircleIcon,
  10. } from "lucide-react";
  11. import { useEffect, useState } from "react";
  12. import type { ReactNode } from "react";
  13. import type {
  14. E2EReportSummary,
  15. TestResultSummary,
  16. } from "../shared/types.js";
  17. import { TrendPanels } from "./ChartPanels.js";
  18. import { CoveragePanel } from "./CoveragePanel.js";
  19. import {
  20. formatBytes,
  21. formatDateTime,
  22. formatDuration,
  23. formatNumber,
  24. } from "./format.js";
  25. import { loadReportSummary } from "./reportData.js";
  26. import { loadPerformanceTrends } from "./trendData.js";
  27. import type { PerformanceTrends } from "./trendData.js";
  28. interface ReportState {
  29. summary: E2EReportSummary;
  30. trends: PerformanceTrends | null;
  31. trendError: string | null;
  32. }
  33. export function App() {
  34. const [report, setReport] = useState<ReportState | null>(null);
  35. const [error, setError] = useState<string | null>(null);
  36. useEffect(() => {
  37. void loadReportSummary()
  38. .then(async ({ summary, summaryUrl }) => {
  39. try {
  40. const trends = await loadPerformanceTrends(summary, summaryUrl);
  41. setReport({ summary, trendError: null, trends });
  42. } catch (loadError: unknown) {
  43. setReport({
  44. summary,
  45. trendError:
  46. loadError instanceof Error ? loadError.message : String(loadError),
  47. trends: null,
  48. });
  49. }
  50. })
  51. .catch((loadError: unknown) => {
  52. setError(loadError instanceof Error ? loadError.message : String(loadError));
  53. });
  54. }, []);
  55. if (error) {
  56. return <StateScreen title="报告加载失败" detail={error} />;
  57. }
  58. if (!report) {
  59. return <StateScreen title="报告加载中" detail="Reading summary.json" />;
  60. }
  61. return <ReportView report={report} />;
  62. }
  63. function ReportView({ report }: { report: ReportState }) {
  64. const { summary } = report;
  65. const failed = summary.exitCode !== 0 || summary.tests.failed > 0;
  66. const uiCoverage = summary.coverage?.uiRenderer;
  67. return (
  68. <main className="shell">
  69. <header className="hero">
  70. <div>
  71. <p className="eyebrow">Desktop E2E Report</p>
  72. <h1>{summary.runId}</h1>
  73. <p className="path">{summary.artifactDir}</p>
  74. </div>
  75. <div className="heroMeta">
  76. <span className={failed ? "status fail" : "status pass"}>
  77. {failed ? <XCircleIcon size={16} /> : <CheckCircle2Icon size={16} />}
  78. {failed ? "FAILED" : "PASSED"}
  79. </span>
  80. <span>开始 {formatDateTime(summary.startedAt)}</span>
  81. <span>完成 {formatDateTime(summary.completedAt)}</span>
  82. </div>
  83. </header>
  84. <section className="metricGrid">
  85. <MetricCard
  86. detail={`${summary.tests.failed} failed`}
  87. icon={<CheckCircle2Icon />}
  88. label="测试"
  89. value={`${summary.tests.passed}/${summary.tests.total}`}
  90. />
  91. <MetricCard
  92. detail={`exit ${summary.exitCode}`}
  93. icon={<ClockIcon />}
  94. label="总耗时"
  95. value={formatDuration(summary.durationMs)}
  96. />
  97. <MetricCard
  98. detail={
  99. uiCoverage
  100. ? `${uiCoverage.sourceFileCount ?? uiCoverage.rendererEntryCount} ${
  101. uiCoverage.sourceFileCount === undefined
  102. ? "renderer scripts"
  103. : "source files"
  104. }`
  105. : "disabled"
  106. }
  107. icon={<GaugeIcon />}
  108. label="UI 覆盖率"
  109. value={statsValue(
  110. uiCoverage?.totals.lines?.pct ?? uiCoverage?.totals.percent,
  111. "%",
  112. )}
  113. />
  114. <MetricCard
  115. detail={sampleHint(summary.performance.processSamples)}
  116. icon={<CpuIcon />}
  117. label="Electron CPU P95"
  118. value={statsValue(summary.performance.processSamples?.cpuTotalPercent.p95, "%")}
  119. />
  120. <MetricCard
  121. detail={sampleHint(summary.performance.processSamples)}
  122. icon={<MemoryStickIcon />}
  123. label="Electron 内存峰值"
  124. value={statsValue(summary.performance.processSamples?.memoryWorkingSetMb.peak, "MB")}
  125. />
  126. <MetricCard
  127. detail={sampleHint(summary.performance.containerSamples)}
  128. icon={<GaugeIcon />}
  129. label="容器 CPU P95"
  130. value={statsValue(summary.performance.containerSamples?.cpuPercent.p95, "%")}
  131. />
  132. <MetricCard
  133. detail={summary.containerE2E ? "docker stats" : "non-container"}
  134. icon={<HardDriveIcon />}
  135. label="容器块写入峰值"
  136. value={statsValue(summary.performance.containerSamples?.blockWriteMb.peak, "MB")}
  137. />
  138. </section>
  139. <TrendPanels trends={report.trends} trendError={report.trendError} />
  140. <CoveragePanel coverage={uiCoverage} />
  141. <TestsPanel results={summary.tests.results} />
  142. <section className="gridTwo">
  143. <InfoPanel title="Specs" rows={specRows(summary)} />
  144. <InfoPanel title="Artifacts" rows={artifactRows(summary)} />
  145. <InfoPanel title="Environment" rows={environmentRows(summary)} />
  146. </section>
  147. </main>
  148. );
  149. }
  150. function MetricCard(props: {
  151. detail: string;
  152. icon: ReactNode;
  153. label: string;
  154. value: string;
  155. }) {
  156. return (
  157. <article className="metric">
  158. <div className="metricIcon">{props.icon}</div>
  159. <span>{props.label}</span>
  160. <strong>{props.value}</strong>
  161. <small>{props.detail}</small>
  162. </article>
  163. );
  164. }
  165. function TestsPanel({ results }: { results: TestResultSummary[] }) {
  166. return (
  167. <Panel title="Tests" meta={`${results.length} recorded`} wide>
  168. <div className="tableWrap">
  169. <table>
  170. <thead>
  171. <tr>
  172. <th>Status</th>
  173. <th>Duration</th>
  174. <th>Test</th>
  175. <th>File</th>
  176. </tr>
  177. </thead>
  178. <tbody>
  179. {(results.length > 0 ? results : [emptyResult()]).map((result) => (
  180. <tr key={`${result.fullTitle}-${result.capturedAt}`}>
  181. <td>
  182. <span className={result.passed ? "pill pass" : "pill fail"}>
  183. {result.passed ? "PASS" : "FAIL"}
  184. </span>
  185. </td>
  186. <td>{formatDuration(result.durationMs)}</td>
  187. <td>
  188. {result.fullTitle}
  189. {result.error ? <pre>{result.error}</pre> : null}
  190. </td>
  191. <td>
  192. <code>{result.file ?? "n/a"}</code>
  193. </td>
  194. </tr>
  195. ))}
  196. </tbody>
  197. </table>
  198. </div>
  199. </Panel>
  200. );
  201. }
  202. function InfoPanel({ rows, title }: { rows: Array<[string, string]>; title: string }) {
  203. return (
  204. <Panel title={title}>
  205. <dl className="infoList">
  206. {rows.map(([label, value]) => (
  207. <div key={`${label}-${value}`}>
  208. <dt>{label}</dt>
  209. <dd>{value}</dd>
  210. </div>
  211. ))}
  212. </dl>
  213. </Panel>
  214. );
  215. }
  216. function Panel(props: {
  217. children: ReactNode;
  218. meta?: string;
  219. title: string;
  220. wide?: boolean;
  221. }) {
  222. return (
  223. <section className={props.wide ? "panel wide" : "panel"}>
  224. <div className="panelHeader">
  225. <h2>{props.title}</h2>
  226. {props.meta ? <span>{props.meta}</span> : null}
  227. </div>
  228. {props.children}
  229. </section>
  230. );
  231. }
  232. function StateScreen({ detail, title }: { detail: string; title: string }) {
  233. return (
  234. <main className="stateScreen">
  235. <ActivityIcon size={28} />
  236. <h1>{title}</h1>
  237. <p>{detail}</p>
  238. </main>
  239. );
  240. }
  241. function specRows(summary: E2EReportSummary): Array<[string, string]> {
  242. const specs =
  243. summary.requestedSpecs.length > 0
  244. ? summary.requestedSpecs
  245. : ["all configured desktop e2e specs"];
  246. return specs.map((spec, index) => [`Spec ${index + 1}`, spec]);
  247. }
  248. function artifactRows(summary: E2EReportSummary): Array<[string, string]> {
  249. return [
  250. ["Viewer URL", summary.files.viewerUrl ?? window.location.href],
  251. ["Summary JSON", summary.files.summaryJson],
  252. ["Summary Markdown", summary.files.summaryMd],
  253. ["Coverage heatmap", summary.files.coverageHeatmap ?? "n/a"],
  254. ["UI renderer coverage", summary.files.uiRendererCoverage ?? "n/a"],
  255. ["Manifest", summary.files.manifest],
  256. ["Test results", summary.files.tests],
  257. ["Process samples", summary.files.processSamplesGlob],
  258. ["Container samples", summary.files.containerSamples],
  259. ];
  260. }
  261. function environmentRows(summary: E2EReportSummary): Array<[string, string]> {
  262. return [
  263. ["Platform", `${summary.environment.platform} / ${summary.environment.arch}`],
  264. ["Node", summary.environment.node],
  265. ["CPU", summary.environment.cpuModel],
  266. ["Memory", formatBytes(summary.environment.totalMemoryBytes)],
  267. ["Container", summary.containerE2E ? "yes" : "no"],
  268. ];
  269. }
  270. function emptyResult(): TestResultSummary {
  271. return {
  272. capturedAt: "",
  273. durationMs: 0,
  274. fullTitle: "No test results were recorded",
  275. passed: true,
  276. status: "unknown",
  277. title: "No test results",
  278. };
  279. }
  280. function sampleHint(summary: { sampleCount: number } | undefined) {
  281. return summary ? `${summary.sampleCount} samples` : "no samples";
  282. }
  283. function statsValue(value: number | undefined, suffix: string) {
  284. return value === undefined ? "n/a" : formatNumber(value, suffix);
  285. }