JobRunsDialog.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * 册10 SC-03 执行记录与轮次详情。
  3. * 契约:docs/.../10-第10册-定时任务与审计视图/14-页面契约本册逐页.md §14.2、作业§4.4/§4.5。
  4. * 接线现状:`/jobs/runs@v1` [实]——五件筛选(关键字/任务/触发方式/结果/日期区间)、
  5. * `page` 0 基翻页、留存期总数 N 与命中数 M 分列下发;标注行返回空流水说明(⛔ 显 0)。
  6. * 关键字当前只检索轮次字段(平台 R-1 `object_keyword` 未交付)⇒ `W22` 常驻。
  7. */
  8. import { useMemo, useState, type ReactNode } from "react";
  9. import { Check, Search, X } from "lucide-react";
  10. import { OpsDialog, OpsStatus, OpsTableColumns } from "../components/OperationsKit";
  11. import { Button } from "../components/ui/button";
  12. import { FilterSelect } from "../components/ui/filter-select";
  13. import { Input } from "../components/ui/input";
  14. import {
  15. fmtDateTime,
  16. fmtDuration,
  17. last7Days,
  18. useJobRuns,
  19. EMPTY_RUNS_FILTER,
  20. type JobListRow,
  21. type RunsFilter,
  22. } from "../lib/scheduled-jobs";
  23. import {
  24. RESULT_WORDS,
  25. ROUND_DETAIL_EMPTY,
  26. RUNS_EMPTY,
  27. TRIGGER_WORDS,
  28. W18A,
  29. W18B,
  30. W18C,
  31. W18D,
  32. W18E_FROM,
  33. W18E_TO,
  34. W19,
  35. W20,
  36. W21_ABNORMAL,
  37. W21_LAST7,
  38. W21_RESET,
  39. W22,
  40. W23_DETAIL,
  41. W23_GOTO,
  42. W27,
  43. W28,
  44. w16,
  45. w17,
  46. w26,
  47. w29,
  48. } from "../lib/copy-jobs";
  49. import type { JobRunRow } from "../lib/api/types";
  50. import { useNavigate } from "react-router";
  51. export interface JobRunsPreset {
  52. jobKey: string;
  53. jobName: string;
  54. }
  55. export function JobRunsDialog({ preset, rows, onClose }: { preset: JobRunsPreset; rows: JobListRow[]; onClose: () => void }) {
  56. const navigate = useNavigate();
  57. const [filter, setFilter] = useState<RunsFilter>({ ...EMPTY_RUNS_FILTER, jobKey: preset.jobKey });
  58. const [applied, setApplied] = useState<RunsFilter>({ ...EMPTY_RUNS_FILTER, jobKey: preset.jobKey });
  59. const [detail, setDetail] = useState<JobRunRow | null>(null);
  60. const state = useJobRuns(applied);
  61. const jobOptions = useMemo(() => rows.filter((row) => !row.annotation), [rows]);
  62. const recentDates = last7DaysFilter();
  63. const recentSelected = applied.dateFrom === recentDates.dateFrom && applied.dateTo === recentDates.dateTo;
  64. const abnormalSelected = applied.result === "异常";
  65. const apply = (next: RunsFilter) => {
  66. setFilter(next);
  67. setApplied(next);
  68. };
  69. // W16:N 为服务端留存期总数,M 为当前已装载轮次。
  70. const title = w16(state.retentionTotal === null ? "—" : String(state.retentionTotal), state.runs.length);
  71. return (
  72. <OpsDialog title={title} subtitle="按轮次查看执行流水" className="job-runs-dialog" onClose={onClose}>
  73. <div className="job-runs-body">
  74. <p className="ops-import-note">{w17(preset.jobName)}</p>
  75. <div className="ops-toolbar scheduled-jobs-filters">
  76. <label className="ops-search">
  77. <Search size={16} />
  78. <Input
  79. aria-label="关键字"
  80. placeholder={W18A}
  81. value={filter.keyword}
  82. onChange={(event) => setFilter({ ...filter, keyword: event.target.value })}
  83. />
  84. {filter.keyword && (
  85. <button aria-label="清空关键字" type="button" onClick={() => setFilter({ ...filter, keyword: "" })}>
  86. <X size={14} />
  87. </button>
  88. )}
  89. </label>
  90. <FilterSelect icon="task" aria-label="按任务筛选" value={filter.jobKey} onChange={(event) => setFilter({ ...filter, jobKey: event.target.value })}>
  91. <option value="">{W18B}</option>
  92. {jobOptions.map((row) => (
  93. <option key={row.key} value={row.key}>
  94. {row.meta?.displayName ?? row.registryName}
  95. </option>
  96. ))}
  97. </FilterSelect>
  98. <FilterSelect icon="trigger" aria-label="按触发方式筛选" value={filter.trigger} onChange={(event) => setFilter({ ...filter, trigger: event.target.value })}>
  99. <option value="">{W18C}</option>
  100. {TRIGGER_WORDS.map((word) => (
  101. <option key={word}>{word}</option>
  102. ))}
  103. </FilterSelect>
  104. <FilterSelect icon="status" aria-label="按执行结果筛选" value={filter.result} onChange={(event) => setFilter({ ...filter, result: event.target.value })}>
  105. <option value="">{W18D}</option>
  106. {RESULT_WORDS.map((word) => (
  107. <option key={word}>{word}</option>
  108. ))}
  109. <option value="异常">{W19}</option>
  110. </FilterSelect>
  111. <Input aria-label={W18E_FROM} type="date" value={filter.dateFrom} onChange={(event) => setFilter({ ...filter, dateFrom: event.target.value })} />
  112. <Input aria-label={W18E_TO} type="date" value={filter.dateTo} onChange={(event) => setFilter({ ...filter, dateTo: event.target.value })} />
  113. <Button size="sm" type="button" onClick={() => apply(filter)}>
  114. 查询
  115. </Button>
  116. <Button size="sm" type="button" variant="ghost" onClick={() => apply({ ...EMPTY_RUNS_FILTER })}>
  117. {W21_RESET}
  118. </Button>
  119. </div>
  120. <div className="ops-toolbar">
  121. <Button
  122. size="sm"
  123. type="button"
  124. variant={recentSelected ? "default" : "outline"}
  125. aria-pressed={recentSelected}
  126. onClick={() => apply({ ...filter, ...(recentSelected ? { dateFrom: "", dateTo: "" } : recentDates) })}
  127. >
  128. {recentSelected && <Check size={14} aria-hidden="true" />}
  129. {W21_LAST7}
  130. </Button>
  131. <Button
  132. size="sm"
  133. type="button"
  134. variant={abnormalSelected ? "default" : "outline"}
  135. aria-pressed={abnormalSelected}
  136. onClick={() => apply({ ...filter, result: abnormalSelected ? "" : "异常" })}
  137. >
  138. {abnormalSelected && <Check size={14} aria-hidden="true" />}
  139. {W21_ABNORMAL}
  140. </Button>
  141. </div>
  142. {/* 平台 R-1(object_keyword 入参)未交付 ⇒ W22 常驻(契约 作业§4.4.6) */}
  143. <p className="ops-import-note">{W22}</p>
  144. {state.error ? (
  145. <p className="ops-form-error" role="alert">
  146. {W20}:{state.error} <Button size="sm" type="button" variant="outline" onClick={state.reload}>重试</Button>
  147. </p>
  148. ) : state.annotationMessage ? (
  149. <div className="ops-empty">{state.annotationMessage}</div>
  150. ) : state.loading && state.runs.length === 0 ? (
  151. <p className="ops-empty">正在加载…</p>
  152. ) : state.runs.length === 0 ? (
  153. <div className="ops-empty">{RUNS_EMPTY}</div>
  154. ) : (
  155. <div className="ops-list-shell">
  156. <div className="job-runs-table">
  157. <table className="ops-table" role="table" aria-label="执行流水">
  158. <OpsTableColumns widths={[15, 16, 11, 17, 17, 13, 11]} />
  159. <thead role="rowgroup">
  160. <tr role="row">
  161. <th scope="col">执行编号</th>
  162. <th scope="col">任务</th>
  163. <th scope="col">触发 / 执行人</th>
  164. <th scope="col">执行时间</th>
  165. <th scope="col">对象处理<small>扫描 / 命中 / 成功 / 跳过 / 失败</small></th>
  166. <th scope="col">结果 / 处置</th>
  167. <th scope="col">操作</th>
  168. </tr>
  169. </thead>
  170. <tbody role="rowgroup">
  171. {state.runs.map((run) => (
  172. <RunRow key={run.run_id} run={run} rows={rows} onDetail={() => setDetail(run)} onGoto={(page) => { onClose(); navigate(page); }} />
  173. ))}
  174. </tbody>
  175. </table>
  176. </div>
  177. {state.hasMore && (
  178. <div className="ops-toolbar" style={{ justifyContent: "center" }}>
  179. <Button size="sm" type="button" variant="outline" disabled={state.loadingMore} onClick={state.loadMore}>
  180. {state.loadingMore ? "加载中…" : `加载更多(已显示 ${state.runs.length}/${state.matchedTotal ?? "—"} 条命中)`}
  181. </Button>
  182. </div>
  183. )}
  184. </div>
  185. )}
  186. </div>
  187. {detail && <RoundDialog run={detail} rows={rows} onClose={() => setDetail(null)} />}
  188. </OpsDialog>
  189. );
  190. }
  191. function last7DaysFilter(): Pick<RunsFilter, "dateFrom" | "dateTo"> {
  192. const { from, to } = last7Days();
  193. return { dateFrom: from, dateTo: to };
  194. }
  195. function RunCell({ label, children }: { label: string; children: ReactNode }) {
  196. return (
  197. <td role="cell">
  198. <span className="job-run-cell-label" aria-hidden="true">{label}</span>
  199. {children}
  200. </td>
  201. );
  202. }
  203. function RunText({ children }: { children: string }) {
  204. return <span className="job-run-text" title={children}>{children}</span>;
  205. }
  206. function RunRow({ run, rows, onDetail, onGoto }: { run: JobRunRow; rows: JobListRow[]; onDetail: () => void; onGoto: (page: string) => void }) {
  207. const meta = rows.find((row) => row.key === run.job_key);
  208. const gotoPage = run.goto_page;
  209. const executor = run.trigger === "人工" ? run.actor_user_id ?? "—" : "系统";
  210. const duration = fmtDuration(run.started_at, run.finished_at);
  211. const counts = [run.scanned, run.matched, run.succeeded, run.skipped, run.failed].join(" / ");
  212. return (
  213. <tr role="row">
  214. <RunCell label="执行编号"><code className="ops-mono"><RunText>{run.run_id}</RunText></code></RunCell>
  215. <RunCell label="任务">
  216. <RunText>{run.job_name ?? meta?.displayName ?? run.job_key}</RunText>
  217. <small className="ops-mono"><RunText>{run.job_key}</RunText></small>
  218. </RunCell>
  219. <RunCell label="触发 / 执行人">
  220. <RunText>{run.trigger || "—"}</RunText>
  221. <small><RunText>{executor}</RunText></small>
  222. </RunCell>
  223. <RunCell label="执行时间">
  224. <RunText>{fmtDateTime(run.started_at)}</RunText>
  225. {run.finished_at && <small><RunText>{`完成 ${fmtDateTime(run.finished_at)}`}</RunText></small>}
  226. <small><RunText>{`耗时 ${duration}`}</RunText></small>
  227. </RunCell>
  228. <RunCell label="对象处理(扫描 / 命中 / 成功 / 跳过 / 失败)">
  229. <code className="ops-mono"><RunText>{counts}</RunText></code>
  230. </RunCell>
  231. <RunCell label="结果 / 处置">
  232. <OpsStatus tone={run.status === "成功" ? "success" : run.status === "失败" ? "danger" : "warning"}>
  233. <RunText>{run.status}</RunText>
  234. </OpsStatus>
  235. {run.disposition_code !== "none" && run.disposition && <small><RunText>{run.disposition}</RunText></small>}
  236. </RunCell>
  237. <RunCell label="操作">
  238. <div className="ops-table-actions">
  239. <button className="ops-table-action" type="button" onClick={onDetail}>
  240. {W23_DETAIL}
  241. </button>
  242. {run.disposition_code === "pending_rerun" && run.goto_visible && gotoPage && (
  243. <button className="ops-table-action" type="button" onClick={() => onGoto(gotoPage)}>
  244. {W23_GOTO}
  245. </button>
  246. )}
  247. </div>
  248. </RunCell>
  249. </tr>
  250. );
  251. }
  252. /** 轮次弹窗(契约 作业§4.5):计数卡五格+结果说明+对象级明细区。 */
  253. function RoundDialog({ run, rows, onClose }: { run: JobRunRow; rows: JobListRow[]; onClose: () => void }) {
  254. const meta = rows.find((row) => row.key === run.job_key);
  255. const interrupted = run.scanned === 0 && run.matched === 0 && run.status === "失败";
  256. const detail = run.detail ?? [];
  257. const violations = run.violations ?? [];
  258. return (
  259. <OpsDialog title={w26(run.run_id)} className="job-round-dialog" onClose={onClose}>
  260. <div className="job-runs-body">
  261. <p className="ops-import-note">
  262. {run.job_name ?? meta?.displayName ?? run.job_key}
  263. {interrupted ? W28 : W27}
  264. </p>
  265. <div className="ops-stat-strip">
  266. <div><span>扫描</span><strong>{run.scanned}</strong></div>
  267. <div><span>命中</span><strong>{run.matched}</strong></div>
  268. <div><span>成功</span><strong>{run.succeeded}</strong></div>
  269. <div><span>跳过</span><strong>{run.skipped}</strong></div>
  270. <div><span>失败</span><strong>{run.failed}</strong></div>
  271. </div>
  272. {run.message && <p className="ops-import-note">{run.message}</p>}
  273. {run.period && <p className="ops-import-note">账期:{run.period}</p>}
  274. {run.biz_date && <p className="ops-import-note">业务日:{run.biz_date}</p>}
  275. <p className="ops-import-note" role="status">{w29(detail.length)}</p>
  276. {detail.length === 0 ? (
  277. <p className="ops-import-note">{ROUND_DETAIL_EMPTY}</p>
  278. ) : (
  279. <div className="ops-list-shell job-round-table" role="region" aria-label="轮次对象明细,可滚动" tabIndex={0}>
  280. <table className="ops-table">
  281. <thead>
  282. <tr>
  283. <th>对象类型</th>
  284. <th>对象标识</th>
  285. <th>处置</th>
  286. <th>说明</th>
  287. </tr>
  288. </thead>
  289. <tbody>
  290. {detail.map((item, idx) => (
  291. <tr key={`${item.object_id ?? item.object_type ?? "row"}-${idx}`}>
  292. <td>{item.object_type ?? "—"}</td>
  293. <td className="ops-mono">{item.object_name ?? item.object_id ?? "—"}</td>
  294. <td>{item.disposition ?? "—"}</td>
  295. <td>{item.reason ?? "—"}</td>
  296. </tr>
  297. ))}
  298. </tbody>
  299. </table>
  300. </div>
  301. )}
  302. {violations.length > 0 && (
  303. <>
  304. <p className="ops-import-note">跳过/失败原因({violations.length} 条)</p>
  305. <ul className="ops-import-note">
  306. {violations.map((v, idx) => (
  307. <li key={`v-${idx}`}>
  308. <span className="ops-mono">{v.object_name ?? v.object_id ?? v.object_type ?? "—"}</span>
  309. {v.reason ? `:${v.reason}` : ""}
  310. </li>
  311. ))}
  312. </ul>
  313. </>
  314. )}
  315. <footer className="job-round-actions">
  316. <Button type="button" variant="outline" onClick={onClose}>
  317. 关闭
  318. </Button>
  319. </footer>
  320. </div>
  321. </OpsDialog>
  322. );
  323. }