feat(shipping-notice): 增加 RPA Worker 心跳上报与状态查询功能

This commit is contained in:
hugo committed 2026-07-12 12:52:01 +08:00
1 parent c3f05ba96b
commit 5a147c4d85
49 files changed
+4032 -1500

No files matched your search

@@ -18,7 +18,10 @@ const admin = reactive(adminApi) as SchedulesAdminContextProvided;
provide(SCHEDULES_ADMIN_KEY, admin);
/** 弹窗 v-if 须绑定原始 ref,避免 reactive 属性赋值破坏 ref 链接 */
provide(SCHEDULE_CREATE_MODAL_OPEN_KEY, adminApi.showScheduleCreateModal);
onMounted(() => { void admin.loadAll(); });
onMounted(async () => {
await admin.loadAll();
await admin.applyFromRouteQuery();
});
</script>
<template>
@@ -0,0 +1,23 @@
/**
* features/admin/modules/schedules/components/ScheduleWorkbenchPanel.spec.ts
* 用途:断言执行记录页不默认展示原始库存下载;仅企微深链 raw_zip=1 时突出下载入口。
*/
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { describe, expect, it } from 'vitest';
describe('ScheduleWorkbenchPanel run table actions', () => {
it('does not render raw inventory zip download in the run table column', () => {
const panelPath = resolve(__dirname, 'ScheduleWorkbenchPanel.vue');
const content = readFileSync(panelPath, 'utf8');
expect(content).not.toMatch(/downloadScheduleInventoryRawZip\(r\.id\)/);
});
it('only shows raw inventory download when wecom deep link prompt is active', () => {
const panelPath = resolve(__dirname, 'ScheduleWorkbenchPanel.vue');
const content = readFileSync(panelPath, 'utf8');
expect(content).toMatch(/wecomRawZipPromptRunId/);
expect(content).not.toMatch(/v-if="admin\.expandedScheduleRun\.inventory_raw_zip_available"/);
expect(content).toMatch(/admin\.wecomRawZipPromptRunId === admin\.expandedScheduleRun\.id/);
});
});
@@ -130,6 +130,35 @@ const admin = useSchedulesAdminContext();
<h3 class="text-lg font-semibold text-primary">{{ $t('admin.schedule.recordsTitle', { name: admin.selectedSchedule.name }) }}</h3>
<p class="mt-1 text-sm text-muted">{{ $t('admin.schedule.recordsSubtitle') }}</p>
</div>
<div
v-if="admin.isInventoryRpaSchedule"
class="inline-flex items-center gap-2 rounded-lg border border-default px-3 py-2 text-xs"
>
<span class="text-muted">{{ $t('admin.schedule.rpaWorker.title') }}</span>
<span
v-if="admin.rpaWorkerStatusLoading"
class="rounded-full bg-surface-soft px-2 py-0.5 text-secondary"
>
{{ $t('admin.schedule.rpaWorker.loading') }}
</span>
<span
v-else
class="rounded-full px-2 py-0.5 font-medium"
:class="admin.rpaWorkerStatus?.ready ? 'bg-emerald-100 text-emerald-700' : 'bg-rose-100 text-rose-700'"
>
{{ admin.rpaWorkerStatus?.ready ? $t('admin.schedule.rpaWorker.online') : $t('admin.schedule.rpaWorker.offline') }}
</span>
<span class="text-muted">
{{ $t('admin.schedule.rpaWorker.lastHeartbeat', { time: admin.rpaWorkerLastHeartbeatAt }) }}
</span>
<span
v-if="!admin.rpaWorkerStatusLoading && admin.rpaWorkerStatus && !admin.rpaWorkerStatus.ready && admin.rpaWorkerStatus.detail_message"
class="max-w-[360px] truncate text-amber-700"
:title="admin.rpaWorkerStatus.detail_message"
>
{{ admin.rpaWorkerStatus.detail_message }}
</span>
</div>
</div>
<div>
@@ -232,7 +261,7 @@ const admin = useSchedulesAdminContext();
<span v-else class="text-xs text-muted">-</span>
</td>
<td class="max-w-0 overflow-hidden px-4 py-2.5">
<div class="flex min-w-0 items-center overflow-hidden">
<div class="flex min-w-0 flex-wrap items-center gap-1 overflow-hidden">
<a
v-if="r.total_count > 0"
href="#"
@@ -240,9 +269,9 @@ const admin = useSchedulesAdminContext();
class="inline-flex shrink-0 items-center gap-1 whitespace-nowrap rounded-lg border border-default px-2 py-1 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft"
>
<Download class="h-3 w-3 shrink-0" />
{{ $t('admin.schedule.download') }}
{{ $t('admin.schedule.downloadDetail') }}
</a>
<span v-if="r.total_count === 0" class="text-muted">-</span>
<span v-else class="text-muted">-</span>
</div>
</td>
<td class="max-w-0 overflow-hidden px-4 py-2.5">
@@ -267,6 +296,20 @@ const admin = useSchedulesAdminContext();
>
<td colspan="11" class="w-full max-w-none px-4 py-3 bg-surface-soft/60">
<div class="min-w-0 w-full max-w-full space-y-3">
<div
v-if="admin.wecomRawZipPromptRunId === admin.expandedScheduleRun.id && admin.expandedScheduleRun.inventory_raw_zip_available"
class="rounded-xl border border-blue-200 bg-blue-50/80 p-4"
>
<p class="text-sm font-medium text-blue-900">{{ $t('admin.schedule.rawInventoryZipWecomHint') }}</p>
<button
type="button"
class="mt-3 inline-flex items-center gap-2 rounded-xl border border-blue-300 bg-surface px-4 py-2 text-sm font-semibold text-blue-700 shadow-sm transition-colors hover:bg-blue-50"
@click="admin.downloadScheduleInventoryRawZip(admin.expandedScheduleRun.id)"
>
<Download class="h-4 w-4 shrink-0" />
{{ $t('admin.schedule.downloadInventoryRawZipPackage') }}
</button>
</div>
<div
v-if="admin.expandedScheduleRun.kb_sync_status"
class="rounded-xl border border-emerald-200 bg-emerald-50/50 p-3 text-xs text-secondary"
@@ -319,7 +362,7 @@ const admin = useSchedulesAdminContext();
<div v-else class="text-xs text-muted">{{ $t('admin.schedule.difySnapshot.notEnabledRun') }}</div>
</div>
<div class="space-y-2">
<div class="flex items-center justify-between mb-2">
<div class="flex flex-wrap items-center justify-between gap-2 mb-2">
<h5 class="text-xs font-bold text-secondary uppercase">{{ $t('admin.schedule.runLogs') }}</h5>
<span class="text-xs text-muted">{{ $t('admin.schedule.stepsCount', { count: admin.expandedScheduleRun.step_logs?.length || 0 }) }}</span>
</div>
@@ -3,7 +3,7 @@
* 定时任务管理页共享 ref 与删除确认弹窗状态。
*/
import { ref } from 'vue';
import type { PlatformJobOption, PlatformTaskDefinition, ScheduleRun, ScheduleTask, TestScheduleRunResult } from '@/features/admin/api/schedule.api';
import type { PlatformJobOption, PlatformTaskDefinition, RpaWorkerHeartbeatSummary, ScheduleRun, ScheduleTask, TestScheduleRunResult } from '@/features/admin/api/schedule.api';
import type { AdminAgentOption, AdminUserRow } from '@/features/admin/types/admin.types';
import type { KbGroup } from '@/features/kb/api/kb.api';
import {
@@ -55,6 +55,8 @@ export function createScheduleAdminState() {
const schedulesLoaded = ref(false);
void schedulesLoaded;
const selectedScheduleRuns = ref<ScheduleRun[]>([]);
const rpaWorkerStatus = ref<RpaWorkerHeartbeatSummary | null>(null);
const rpaWorkerStatusLoading = ref(false);
const scheduleLoading = ref(false);
const scheduleRunsLoading = ref(false);
const selectedScheduleId = ref<number | null>(null);
@@ -84,6 +86,8 @@ export function createScheduleAdminState() {
const scheduleConfigCollapsed = ref(true);
const expandedScheduleRunId = ref<number | null>(null);
/** 企微深链 raw_zip=1 进入时待手动下载的执行记录 ID;普通列表浏览不展示原始库存下载入口。 */
const wecomRawZipPromptRunId = ref<number | null>(null);
const showTestRunDialog = ref(false);
const testRunSchedule = ref<ScheduleTask | null>(null);
@@ -123,6 +127,8 @@ export function createScheduleAdminState() {
schedules,
schedulesLoaded,
selectedScheduleRuns,
rpaWorkerStatus,
rpaWorkerStatusLoading,
scheduleLoading,
scheduleRunsLoading,
selectedScheduleId,
@@ -138,6 +144,7 @@ export function createScheduleAdminState() {
scheduleCreateForm,
scheduleConfigCollapsed,
expandedScheduleRunId,
wecomRawZipPromptRunId,
showTestRunDialog,
testRunSchedule,
testRunWhere,
@@ -0,0 +1,60 @@
/**
* features/admin/modules/schedules/composables/schedule-admin/useScheduleAdminDeepLink.ts
* 用途:解析企微深链 query,定位排程执行记录并标记原始库存手动下载入口。
* 依赖:vue-router、schedule-admin.state、useScheduleAdminRuns。
* 逻辑:SchedulesPage onMounted 在 loadAll 后调用;消费 task_id/run_id/raw_zip,不自动下载。
*/
import { useRoute, useRouter } from 'vue-router';
import type { ScheduleAdminState } from './schedule-admin.state';
import type { ScheduleAdminRuns } from './useScheduleAdminRuns';
function parsePositiveInt(raw: unknown): number | null {
const value = Number(raw);
if (!Number.isFinite(value) || value <= 0) return null;
return Math.trunc(value);
}
/**
* 根据路由 query 打开工作台执行记录;raw_zip=1 时仅展开 run 并突出手动下载按钮。
* 核心逻辑:不自动触发下载,避免企微内置浏览器拦截异步下载。
*/
export function useScheduleAdminDeepLink(
state: ScheduleAdminState,
runs: Pick<ScheduleAdminRuns, 'viewScheduleRuns'>,
) {
const route = useRoute();
const router = useRouter();
const applyFromRouteQuery = async (): Promise<void> => {
const taskId = parsePositiveInt(route.query.task_id);
if (!taskId) return;
const runId = parsePositiveInt(route.query.run_id);
const rawZipPrompt = String(route.query.raw_zip || '') === '1';
state.scheduleViewMode.value = 'workbench';
state.selectedScheduleId.value = taskId;
state.wecomRawZipPromptRunId.value = null;
await runs.viewScheduleRuns(taskId);
if (runId) {
state.expandedScheduleRunId.value = runId;
if (rawZipPrompt) {
const target = state.selectedScheduleRuns.value.find((item) => item.id === runId);
if (target?.inventory_raw_zip_available) {
state.wecomRawZipPromptRunId.value = runId;
}
}
}
const nextQuery = { ...route.query };
delete nextQuery.task_id;
delete nextQuery.run_id;
delete nextQuery.raw_zip;
await router.replace({ path: route.path, query: nextQuery });
};
return { applyFromRouteQuery };
}
export type ScheduleAdminDeepLink = ReturnType<typeof useScheduleAdminDeepLink>;
@@ -4,7 +4,7 @@
*/
import { computed, watch, type Ref } from 'vue';
import type { ComposerTranslation } from 'vue-i18n';
import { listAdminScheduleRuns } from '@/features/admin/api/schedule.api';
import { listAdminScheduleRuns, adminRunInventoryRawZipUrl, getAdminRpaWorkersStatus } from '@/features/admin/api/schedule.api';
import { useMessage } from '@/shared/composables/useMessage';
import { getAuthHeaders } from '@/shared/utils/auth';
import { formatDateTime } from '../../utils/schedule-format.utils';
@@ -25,6 +25,8 @@ export function useScheduleAdminRuns(
selectedScheduleRuns,
scheduleRunsLoading,
selectedScheduleId,
rpaWorkerStatus,
rpaWorkerStatusLoading,
scheduleRunStatusFilter,
scheduleRunStartedAfter,
scheduleRunStartedBefore,
@@ -146,6 +148,35 @@ export function useScheduleAdminRuns(
return raw ? formatDateTime(raw) : '-';
});
const isInventoryRpaSchedule = computed(
() => selectedSchedule.value?.platform_api_path === 'shipping-inventory-rpa',
);
const rpaWorkerLastHeartbeatAt = computed(() => {
const workers = rpaWorkerStatus.value?.workers || [];
const latest = workers
.map((item) => item.received_at || item.timestamp)
.filter(Boolean)
.sort()
.pop();
return latest ? formatDateTime(String(latest)) : '-';
});
const refreshRpaWorkerStatus = async () => {
if (!isInventoryRpaSchedule.value) {
rpaWorkerStatus.value = null;
return;
}
rpaWorkerStatusLoading.value = true;
try {
rpaWorkerStatus.value = await getAdminRpaWorkersStatus();
} catch {
rpaWorkerStatus.value = null;
} finally {
rpaWorkerStatusLoading.value = false;
}
};
const todayStart = () => {
const d = new Date();
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}T00:00:00`;
@@ -177,6 +208,7 @@ export function useScheduleAdminRuns(
params.started_before = new Date(scheduleRunStartedBefore.value).toISOString();
}
selectedScheduleRuns.value = await listAdminScheduleRuns(taskId, params);
await refreshRpaWorkerStatus();
} finally {
scheduleRunsLoading.value = false;
}
@@ -200,6 +232,24 @@ export function useScheduleAdminRuns(
URL.revokeObjectURL(url);
};
const downloadScheduleInventoryRawZip = async (runId: number) => {
const res = await fetch(adminRunInventoryRawZipUrl(runId), { headers: getAuthHeaders() });
if (!res.ok) {
msg.error(t('admin.schedule.msg.downloadInventoryRawZipFailed'));
return;
}
const blob = await res.blob();
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
const disposition = res.headers.get('content-disposition');
a.download = disposition?.match(/filename\*?=(?:UTF-8'')?["']?([^;"'\n]+)/)?.[1] || 'inventory-rpa-raw.zip';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
const getScheduleRunFailedCountTitle = (run: (typeof selectedScheduleRuns.value)[number]) =>
buildScheduleRunFailedCountTitle(run, t('admin.schedule.excelExportFailedHint'));
@@ -213,9 +263,13 @@ export function useScheduleAdminRuns(
stepStatusMap,
selectedScheduleRunMetrics,
selectedScheduleLatestRunAt,
isInventoryRpaSchedule,
rpaWorkerLastHeartbeatAt,
refreshRpaWorkerStatus,
resetScheduleRunFilters,
viewScheduleRuns,
downloadScheduleExcel,
downloadScheduleInventoryRawZip,
startScheduleColResize,
formatDateTime,
isScheduleRunExcelExportFailed,
@@ -9,6 +9,7 @@ import { useScheduleAdminList } from './schedule-admin/useScheduleAdminList';
import { useScheduleAdminLookups } from './schedule-admin/useScheduleAdminLookups';
import { useScheduleAdminRuns } from './schedule-admin/useScheduleAdminRuns';
import { useScheduleAdminTestRun } from './schedule-admin/useScheduleAdminTestRun';
import { useScheduleAdminDeepLink } from './schedule-admin/useScheduleAdminDeepLink';
export function useSchedulesAdmin() {
const { t, locale } = useI18n();
@@ -16,6 +17,7 @@ export function useSchedulesAdmin() {
const lookups = useScheduleAdminLookups(state);
const runs = useScheduleAdminRuns(state, t, locale);
const list = useScheduleAdminList(state, runs, t);
const deepLink = useScheduleAdminDeepLink(state, runs);
const form = useScheduleAdminForm(state, lookups, list, runs, t);
const testRun = useScheduleAdminTestRun(state, list, runs, t);
@@ -24,6 +26,7 @@ export function useSchedulesAdmin() {
...lookups,
...runs,
...list,
...deepLink,
...form,
...testRun,
};