Răsfoiți Sursa

优化定时任务执行记录表格和快捷筛选

sy 2 ore în urmă
părinte
comite
0bff576c61

+ 77 - 49
web-scaffold/src/pages/JobRunsDialog.tsx

@@ -5,14 +5,14 @@
  * `page` 0 基翻页、留存期总数 N 与命中数 M 分列下发;标注行返回空流水说明(⛔ 显 0)。
  * 关键字当前只检索轮次字段(平台 R-1 `object_keyword` 未交付)⇒ `W22` 常驻。
  */
-import { useMemo, useState } from "react";
-import { Search, X } from "lucide-react";
-import { OpsDialog, OpsStatus } from "../components/OperationsKit";
+import { useMemo, useState, type ReactNode } from "react";
+import { Check, Search, X } from "lucide-react";
+import { OpsDialog, OpsStatus, OpsTableColumns } from "../components/OperationsKit";
 import { Button } from "../components/ui/button";
 import { FilterSelect } from "../components/ui/filter-select";
 import { Input } from "../components/ui/input";
 import {
-  fmtDateTimeParts,
+  fmtDateTime,
   fmtDuration,
   last7Days,
   useJobRuns,
@@ -62,6 +62,9 @@ export function JobRunsDialog({ preset, rows, onClose }: { preset: JobRunsPreset
   const state = useJobRuns(applied);
 
   const jobOptions = useMemo(() => rows.filter((row) => !row.annotation), [rows]);
+  const recentDates = last7DaysFilter();
+  const recentSelected = applied.dateFrom === recentDates.dateFrom && applied.dateTo === recentDates.dateTo;
+  const abnormalSelected = applied.result === "异常";
 
   const apply = (next: RunsFilter) => {
     setFilter(next);
@@ -121,10 +124,24 @@ export function JobRunsDialog({ preset, rows, onClose }: { preset: JobRunsPreset
           </Button>
         </div>
         <div className="ops-toolbar">
-          <Button size="sm" type="button" variant="outline" onClick={() => apply({ ...EMPTY_RUNS_FILTER, ...last7DaysFilter() })}>
+          <Button
+            size="sm"
+            type="button"
+            variant={recentSelected ? "default" : "outline"}
+            aria-pressed={recentSelected}
+            onClick={() => apply({ ...filter, ...(recentSelected ? { dateFrom: "", dateTo: "" } : recentDates) })}
+          >
+            {recentSelected && <Check size={14} aria-hidden="true" />}
             {W21_LAST7}
           </Button>
-          <Button size="sm" type="button" variant="outline" onClick={() => apply({ ...EMPTY_RUNS_FILTER, result: "异常" })}>
+          <Button
+            size="sm"
+            type="button"
+            variant={abnormalSelected ? "default" : "outline"}
+            aria-pressed={abnormalSelected}
+            onClick={() => apply({ ...filter, result: abnormalSelected ? "" : "异常" })}
+          >
+            {abnormalSelected && <Check size={14} aria-hidden="true" />}
             {W21_ABNORMAL}
           </Button>
         </div>
@@ -142,25 +159,21 @@ export function JobRunsDialog({ preset, rows, onClose }: { preset: JobRunsPreset
           <div className="ops-empty">{RUNS_EMPTY}</div>
         ) : (
           <div className="ops-list-shell">
-            <div className="job-runs-table" role="region" aria-label="执行流水,可横向滚动" tabIndex={0}>
-              <table className="ops-table">
-                <thead>
-                  <tr>
-                    <th>执行编号</th>
-                    <th>任务</th>
-                    <th>触发方式</th>
-                    <th>开始时间</th>
-                    <th>耗时</th>
-                    <th>扫描/命中</th>
-                    <th>成功</th>
-                    <th>跳过</th>
-                    <th>失败</th>
-                    <th>结果/处置</th>
-                    <th>执行人</th>
-                    <th>操作</th>
+            <div className="job-runs-table">
+              <table className="ops-table" role="table" aria-label="执行流水">
+                <OpsTableColumns widths={[15, 16, 11, 17, 17, 13, 11]} />
+                <thead role="rowgroup">
+                  <tr role="row">
+                    <th scope="col">执行编号</th>
+                    <th scope="col">任务</th>
+                    <th scope="col">触发 / 执行人</th>
+                    <th scope="col">执行时间</th>
+                    <th scope="col">对象处理<small>扫描 / 命中 / 成功 / 跳过 / 失败</small></th>
+                    <th scope="col">结果 / 处置</th>
+                    <th scope="col">操作</th>
                   </tr>
                 </thead>
-                <tbody>
+                <tbody role="rowgroup">
                   {state.runs.map((run) => (
                     <RunRow key={run.run_id} run={run} rows={rows} onDetail={() => setDetail(run)} onGoto={(page) => { onClose(); navigate(page); }} />
                   ))}
@@ -187,36 +200,51 @@ function last7DaysFilter(): Pick<RunsFilter, "dateFrom" | "dateTo"> {
   return { dateFrom: from, dateTo: to };
 }
 
+function RunCell({ label, children }: { label: string; children: ReactNode }) {
+  return (
+    <td role="cell">
+      <span className="job-run-cell-label" aria-hidden="true">{label}</span>
+      {children}
+    </td>
+  );
+}
+
+function RunText({ children }: { children: string }) {
+  return <span className="job-run-text" title={children}>{children}</span>;
+}
+
 function RunRow({ run, rows, onDetail, onGoto }: { run: JobRunRow; rows: JobListRow[]; onDetail: () => void; onGoto: (page: string) => void }) {
   const meta = rows.find((row) => row.key === run.job_key);
   const gotoPage = run.goto_page;
-  const started = fmtDateTimeParts(run.started_at);
-  const finished = fmtDateTimeParts(run.finished_at);
   const executor = run.trigger === "人工" ? run.actor_user_id ?? "—" : "系统";
+  const duration = fmtDuration(run.started_at, run.finished_at);
+  const counts = [run.scanned, run.matched, run.succeeded, run.skipped, run.failed].join(" / ");
   return (
-    <tr>
-      <td className="ops-mono">{run.run_id}</td>
-      <td>
-        {run.job_name ?? meta?.displayName ?? run.job_key}
-        <small className="ops-mono">{run.job_key}</small>
-      </td>
-      <td>{run.trigger || "—"}</td>
-      <td>
-        {started.date}
-        <small>{started.time}</small>
-        {finished.date && <small className="ops-import-note">完成 {finished.date} {finished.time}</small>}
-      </td>
-      <td>{fmtDuration(run.started_at, run.finished_at)}</td>
-      <td className="ops-mono">{run.scanned} / {run.matched}</td>
-      <td className="ops-mono">{run.succeeded}</td>
-      <td className="ops-mono">{run.skipped}</td>
-      <td className="ops-mono">{run.failed}</td>
-      <td>
-        <OpsStatus tone={run.status === "成功" ? "success" : run.status === "失败" ? "danger" : "warning"}>{run.status}</OpsStatus>
-        {run.disposition_code !== "none" && <small>{run.disposition}</small>}
-      </td>
-      <td>{executor}</td>
-      <td>
+    <tr role="row">
+      <RunCell label="执行编号"><code className="ops-mono"><RunText>{run.run_id}</RunText></code></RunCell>
+      <RunCell label="任务">
+        <RunText>{run.job_name ?? meta?.displayName ?? run.job_key}</RunText>
+        <small className="ops-mono"><RunText>{run.job_key}</RunText></small>
+      </RunCell>
+      <RunCell label="触发 / 执行人">
+        <RunText>{run.trigger || "—"}</RunText>
+        <small><RunText>{executor}</RunText></small>
+      </RunCell>
+      <RunCell label="执行时间">
+        <RunText>{fmtDateTime(run.started_at)}</RunText>
+        {run.finished_at && <small><RunText>{`完成 ${fmtDateTime(run.finished_at)}`}</RunText></small>}
+        <small><RunText>{`耗时 ${duration}`}</RunText></small>
+      </RunCell>
+      <RunCell label="对象处理(扫描 / 命中 / 成功 / 跳过 / 失败)">
+        <code className="ops-mono"><RunText>{counts}</RunText></code>
+      </RunCell>
+      <RunCell label="结果 / 处置">
+        <OpsStatus tone={run.status === "成功" ? "success" : run.status === "失败" ? "danger" : "warning"}>
+          <RunText>{run.status}</RunText>
+        </OpsStatus>
+        {run.disposition_code !== "none" && run.disposition && <small><RunText>{run.disposition}</RunText></small>}
+      </RunCell>
+      <RunCell label="操作">
         <div className="ops-table-actions">
           <button className="ops-table-action" type="button" onClick={onDetail}>
             {W23_DETAIL}
@@ -227,7 +255,7 @@ function RunRow({ run, rows, onDetail, onGoto }: { run: JobRunRow; rows: JobList
             </button>
           )}
         </div>
-      </td>
+      </RunCell>
     </tr>
   );
 }

+ 58 - 0
web-scaffold/src/pages/__tests__/job-runs-dialog.test.tsx

@@ -0,0 +1,58 @@
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { MemoryRouter } from "react-router";
+import { expect, it, vi } from "vitest";
+import type { RunsFilter } from "../../lib/scheduled-jobs";
+import { JobRunsDialog } from "../JobRunsDialog";
+
+const { useJobRuns } = vi.hoisted(() => ({ useJobRuns: vi.fn() }));
+vi.mock("../../lib/scheduled-jobs", async (load) => ({
+  ...await load<typeof import("../../lib/scheduled-jobs")>(),
+  last7Days: () => ({ from: "2026-11-17", to: "2026-11-23" }),
+  useJobRuns,
+}));
+
+it("近 7 天与只看异常可以独立选中、取消,并保留当前任务", () => {
+  useJobRuns.mockReturnValue({ retentionTotal: 0, runs: [], loading: false });
+  const prototype = HTMLDialogElement.prototype;
+  const { showModal, close } = prototype;
+  Object.assign(prototype, {
+    showModal(this: HTMLDialogElement) { this.open = true; },
+    close(this: HTMLDialogElement) { this.open = false; },
+  });
+
+  try {
+    render(
+      <MemoryRouter>
+        <JobRunsDialog preset={{ jobKey: "RECON", jobName: "自动生成对账清单" }} rows={[]} onClose={() => {}} />
+      </MemoryRouter>,
+    );
+    const recent = screen.getByRole("button", { name: "近 7 天", pressed: false });
+    const abnormal = screen.getByRole("button", { name: "只看异常", pressed: false });
+
+    fireEvent.click(recent);
+    fireEvent.click(abnormal);
+    expect(recent.getAttribute("aria-pressed")).toBe("true");
+    expect(abnormal.getAttribute("aria-pressed")).toBe("true");
+    expect(useJobRuns.mock.lastCall?.[0] as RunsFilter).toMatchObject({
+      jobKey: "RECON", result: "异常", dateFrom: "2026-11-17", dateTo: "2026-11-23",
+    });
+
+    fireEvent.click(abnormal);
+    expect(abnormal.getAttribute("aria-pressed")).toBe("false");
+    expect(useJobRuns.mock.lastCall?.[0] as RunsFilter).toMatchObject({ jobKey: "RECON", result: "", dateFrom: "2026-11-17" });
+
+    fireEvent.click(abnormal);
+    fireEvent.click(recent);
+    expect(recent.getAttribute("aria-pressed")).toBe("false");
+    expect(abnormal.getAttribute("aria-pressed")).toBe("true");
+    expect(useJobRuns.mock.lastCall?.[0] as RunsFilter).toMatchObject({ jobKey: "RECON", result: "异常", dateFrom: "", dateTo: "" });
+
+    fireEvent.click(screen.getByRole("button", { name: "重置筛选" }));
+    expect(recent.getAttribute("aria-pressed")).toBe("false");
+    expect(abnormal.getAttribute("aria-pressed")).toBe("false");
+    expect(useJobRuns.mock.lastCall?.[0] as RunsFilter).toMatchObject({ dateFrom: "", dateTo: "" });
+  } finally {
+    cleanup();
+    Object.assign(prototype, { showModal, close });
+  }
+});

+ 30 - 1
web-scaffold/src/styles/scheduled-jobs.css

@@ -211,7 +211,36 @@
 .job-runs-dialog { width: min(1200px, calc(100vw - 24px)); }
 .job-round-dialog { width: min(680px, calc(100vw - 24px)); }
 .job-runs-body { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; padding: 16px clamp(14px, 3vw, 24px) 22px; }
-.job-runs-table { overflow-x: auto; }
+.job-runs-table { container-type: inline-size; }
+.job-runs-table .ops-table { min-width: 0; table-layout: fixed; }
+.job-runs-table .ops-table :is(th, td) { min-width: 0; padding-inline: 9px; }
+.job-runs-table .ops-table :is(th, td):where(:first-child) { padding-inline-start: 14px; }
+.job-runs-table .ops-table :is(th, td):where(:last-child) { padding-inline-end: 14px; }
+.job-runs-table .ops-table th { white-space: normal; }
+.job-runs-table th small, .job-runs-table td small { display: block; margin-top: 3px; font-size: var(--type-meta); line-height: 1.4; }
+.job-runs-table td code { display: block; font-size: var(--type-meta); }
+.job-run-text { display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
+.job-run-cell-label { display: none; }
+.job-runs-table .ops-status { max-width: 100%; }
+.job-runs-table .ops-status i { flex: none; }
+.job-runs-table .ops-table-actions { display: flex; gap: 0 3px; }
+.job-runs-table .ops-table-action { padding-inline: 4px; font-size: var(--type-meta); }
+
+@container (max-width: 650px) {
+  .job-runs-table :is(table, tbody) { display: block; }
+  .job-runs-table colgroup { display: none; }
+  .job-runs-table thead { position: absolute; width: 1px; height: 1px; clip-path: inset(50%); overflow: hidden; }
+  .job-runs-table tbody tr {
+    display: grid;
+    grid-template-columns: repeat(2, minmax(0, 1fr));
+    gap: 11px 14px;
+    padding: 14px;
+  }
+  .job-runs-table tbody tr + tr { border-top: 1px solid var(--border-subtle); }
+  .job-runs-table .ops-table td { display: block; padding: 0; border: 0; white-space: normal; }
+  .job-runs-table tbody td:last-child { grid-column: 1 / -1; }
+  .job-run-cell-label { display: block; margin-bottom: 4px; color: var(--muted-foreground); font-size: var(--type-meta); line-height: 1.45; }
+}
 .job-runs-body > .ops-toolbar { padding: 0; }
 .job-runs-body .scheduled-jobs-filters input[type="date"] { width: auto; min-width: 0; }
 .job-round-table { max-height: 260px; overflow: auto; }