- GET /dashboard: automation.library {entities, movies, series, seasons,
episodes, assets} and automation.slotSatisfiedRate; home media overview
shows the counts instead of placeholders.
- GET /engine/runs: `status` accepts comma-separated values; new
`triggeredBy` filter (comma-separated) evaluated server-side by scanning
batches and paging with the last inspected row's cursor (scan cap 2000).
- Task stream: source filter and the "active" tab (running,pending) are now
one paginated server query.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
94 lines
3.3 KiB
TypeScript
94 lines
3.3 KiB
TypeScript
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import type { CursorPage, EngineBusinessKind, EngineRun, EngineRunDetail, EngineStats } from "@umt/contracts";
|
|
import { apiData } from "~/lib/api.ts";
|
|
import { qk } from "~/lib/query-keys.ts";
|
|
import { errorCode } from "./parse.ts";
|
|
|
|
export type EngineRunFilters = {
|
|
/** 单值或逗号分隔多值(running,pending) */
|
|
status?: string;
|
|
/** 触发来源,单值或逗号分隔多值(user,api_key) */
|
|
triggeredBy?: string;
|
|
workflowName?: string;
|
|
businessKind?: EngineBusinessKind;
|
|
businessRefId?: string;
|
|
/** created_at 下界(ISO) */
|
|
from?: string;
|
|
};
|
|
|
|
/** 终态:不能再 cancel */
|
|
export const ENGINE_TERMINAL = new Set(["completed", "succeeded", "failed", "canceled"]);
|
|
|
|
export function useEngineRuns(filters: EngineRunFilters, enabled = true, refetchMs: number | false = false) {
|
|
return useInfiniteQuery({
|
|
enabled,
|
|
refetchInterval: refetchMs,
|
|
queryKey: qk.list("engine", filters),
|
|
initialPageParam: undefined as string | undefined,
|
|
queryFn: ({ pageParam, signal }) =>
|
|
apiData<CursorPage<EngineRun>>("/engine/runs", { params: { ...filters, cursor: pageParam, limit: 50 }, signal }),
|
|
getNextPageParam: (last) => last.nextCursor ?? undefined,
|
|
});
|
|
}
|
|
|
|
/** 只取第一页(首页区块、进行中合并用) */
|
|
export function useEngineRunsPage(
|
|
filters: EngineRunFilters,
|
|
limit = 20,
|
|
refetchMs: number | false = 30_000,
|
|
enabled = true,
|
|
) {
|
|
return useQuery({
|
|
enabled,
|
|
queryKey: qk.list("engine", { ...filters, limit, page: 1 }),
|
|
queryFn: ({ signal }) => apiData<CursorPage<EngineRun>>("/engine/runs", { params: { ...filters, limit }, signal }),
|
|
refetchInterval: refetchMs,
|
|
});
|
|
}
|
|
|
|
export function useEngineRun(id: string | undefined) {
|
|
return useQuery({
|
|
queryKey: qk.detail("engine", id ?? ""),
|
|
queryFn: ({ signal }) => apiData<EngineRunDetail>("/engine/runs/{id}", { pathParams: { id: id ?? "" }, signal }),
|
|
enabled: !!id,
|
|
// 不存在的 id(旧链接探测)不重试
|
|
retry: (count, err) => errorCode(err) !== "not_found" && count < 2,
|
|
// 非终态每 5 秒兜底刷新(SSE 事件驱动是主路径)
|
|
refetchInterval: (q) => (q.state.data && !ENGINE_TERMINAL.has(q.state.data.status) ? 5_000 : false),
|
|
});
|
|
}
|
|
|
|
export function useEngineStats() {
|
|
return useQuery({
|
|
queryKey: qk.engineStats,
|
|
queryFn: ({ signal }) => apiData<EngineStats>("/engine/stats", { signal }),
|
|
refetchInterval: 30_000,
|
|
});
|
|
}
|
|
|
|
export function useCancelEngineRun() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, reason }: { id: string; reason?: string }) =>
|
|
apiData<{ canceled: boolean }>("/engine/runs/{id}/cancel", {
|
|
method: "POST",
|
|
pathParams: { id },
|
|
body: { reason },
|
|
}),
|
|
onSettled: () => qc.invalidateQueries({ queryKey: qk.all("engine") }),
|
|
});
|
|
}
|
|
|
|
/** 重跑已结束的 run:服务端按原任务名与原始输入重新入队,返回新 runId */
|
|
export function useRetryEngineRun() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id }: { id: string }) =>
|
|
apiData<{ runId: string; retryOf: string; attempt: number }>("/engine/runs/{id}/retry", {
|
|
method: "POST",
|
|
pathParams: { id },
|
|
}),
|
|
onSettled: () => qc.invalidateQueries({ queryKey: qk.all("engine") }),
|
|
});
|
|
}
|