feat(schedule):cron支持图形,RoHS检核增加进度显示
This commit is contained in:
1 parent
6d0e78be81
commit
f5aac9c893
46 files changed
+1987
-221
No files matched your search
@@ -151,7 +151,11 @@ const {
|
||||
{{ $t('admin.schedule.lastRun') }}:{{ formatDateTime(s.last_run_at) }}
|
||||
</div>
|
||||
<div class="mb-3 text-sm text-muted">
|
||||
{{ $t('admin.schedule.nextRun') }}:{{ formatDateTime(s.next_run_at) }}
|
||||
{{ $t('admin.schedule.nextRun') }}:{{
|
||||
!s.enabled
|
||||
? $t('admin.schedule.nextRunDisabled')
|
||||
: (s.next_run_at ? formatDateTime(s.next_run_at) : $t('admin.schedule.nextRunPending'))
|
||||
}}
|
||||
</div>
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<button
|
||||
|
||||
@@ -11,7 +11,7 @@ const props = defineProps<{
|
||||
}>();
|
||||
|
||||
const d = props.designer;
|
||||
const { state, cronExpr, isValid, nextRunAt, weekdayOptions } = d;
|
||||
const { state, timezone, cronExpr, cronRisky, isValid, nextRunAt, weekdayOptions } = d;
|
||||
|
||||
const handleSwitchVisual = () => {
|
||||
if (state.value.editorMode === 'visual') return;
|
||||
@@ -30,6 +30,7 @@ const handleSwitchAdvanced = () => {
|
||||
<div class="min-w-0">
|
||||
<h4 class="text-base font-bold text-primary">{{ $t('admin.schedule.form.cronTitle') }}</h4>
|
||||
<p class="mt-1 text-sm leading-relaxed text-muted">{{ $t('admin.schedule.form.cronHint') }}</p>
|
||||
<p class="mt-1 text-xs text-muted">{{ $t('admin.schedule.form.cronTimezoneHint', { tz: timezone }) }}</p>
|
||||
</div>
|
||||
<div
|
||||
class="segment-control segment-control--cols-2 segment-control--sm w-full max-w-xs"
|
||||
@@ -84,6 +85,13 @@ const handleSwitchAdvanced = () => {
|
||||
<button type="button" class="rounded-lg border border-default bg-surface px-3 py-1.5 text-xs font-medium text-secondary transition hover:border-blue-300 hover:text-primary" @click="d.setPreset('weekly')">
|
||||
{{ $t('admin.schedule.form.weekly') }}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-lg border border-default bg-surface px-3 py-1.5 text-xs font-medium text-secondary transition hover:border-blue-300 hover:text-primary"
|
||||
@click="d.applyAdvancedCron('0 12,17 * * *')"
|
||||
>
|
||||
{{ $t('admin.schedule.form.twiceDaily') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -231,6 +239,13 @@ const handleSwitchAdvanced = () => {
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<p
|
||||
v-if="cronRisky"
|
||||
class="mb-3 rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-xs text-amber-800"
|
||||
>
|
||||
{{ $t('admin.schedule.form.cronRiskyHint') }}
|
||||
</p>
|
||||
|
||||
<div
|
||||
class="mt-4 flex flex-col gap-1.5 rounded-xl border border-dashed px-4 py-3 text-xs sm:flex-row sm:flex-wrap sm:items-center sm:gap-x-4"
|
||||
:class="isValid ? 'border-default bg-surface-soft text-secondary' : 'border-red-200 bg-red-50 text-red-700'"
|
||||
|
||||
+5
@@ -9,6 +9,7 @@ import {
|
||||
defaultCronDesignerState,
|
||||
parseCronExpr,
|
||||
toCronField,
|
||||
normalizeCronExpr,
|
||||
validateCronDesignerState,
|
||||
} from './scheduleCronDesigner.utils';
|
||||
|
||||
@@ -91,4 +92,8 @@ describe('scheduleCronDesigner.utils', () => {
|
||||
state.months = [];
|
||||
expect(validateCronDesignerState(state)).toBe(false);
|
||||
});
|
||||
|
||||
it('normalizes risky cron to fixed minute zero', () => {
|
||||
expect(normalizeCronExpr('* 12,17 * * *')).toBe('0 12,17 * * *');
|
||||
});
|
||||
});
|
||||
+20
-2
@@ -101,6 +101,24 @@ export function parseCronFieldToArray(field: string, min: number, max: number):
|
||||
return Array.from(result).sort((a, b) => a - b);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修正常见 Cron 笔误:分钟为 * 且小时已限定(如 * 12,17 * * *)时改为整点 0 分。
|
||||
*/
|
||||
export function normalizeCronExpr(expr: string): string {
|
||||
const parts = String(expr || '').trim().split(/\s+/);
|
||||
if (parts.length === 5 && parts[0] === '*' && parts[1] && parts[1] !== '*') {
|
||||
parts[0] = '0';
|
||||
return parts.join(' ');
|
||||
}
|
||||
return String(expr || '').trim();
|
||||
}
|
||||
|
||||
/** 分钟为 * 且小时非 *:会在指定小时内每分钟执行。 */
|
||||
export function isRiskyCronExpr(expr: string): boolean {
|
||||
const parts = String(expr || '').trim().split(/\s+/);
|
||||
return parts.length === 5 && parts[0] === '*' && !!parts[1] && parts[1] !== '*';
|
||||
}
|
||||
|
||||
export function buildCronExpr(state: ScheduleCronDesignerState): string {
|
||||
const dom = toCronField(state.daysOfMonth, 1, 31);
|
||||
const month = toCronField(state.months, 1, 12);
|
||||
@@ -237,8 +255,8 @@ function isCalendarValid(values: number[] | null): boolean {
|
||||
|
||||
export function validateCronDesignerState(state: ScheduleCronDesignerState): boolean {
|
||||
if (state.editorMode === 'advanced') {
|
||||
const text = state.advancedCronExpr.trim();
|
||||
return text.length > 0 && text.split(/\s+/).length === 5;
|
||||
const text = normalizeCronExpr(state.advancedCronExpr);
|
||||
return text.length > 0 && text.split(/\s+/).length === 5 && !isRiskyCronExpr(state.advancedCronExpr);
|
||||
}
|
||||
|
||||
if (!isCalendarValid(state.months) || !isCalendarValid(state.weekdays) || !isCalendarValid(state.daysOfMonth)) {
|
||||
|
||||
+31
-2
@@ -5,11 +5,14 @@
|
||||
import { computed, ref } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { CronExpressionParser } from 'cron-parser';
|
||||
import { DEFAULT_SCHEDULE_TIMEZONE } from '../constants';
|
||||
import {
|
||||
applyPreset,
|
||||
buildCronExpr,
|
||||
computeNextRunAt,
|
||||
defaultCronDesignerState,
|
||||
isRiskyCronExpr,
|
||||
normalizeCronExpr,
|
||||
parseCronExpr,
|
||||
validateCronDesignerState,
|
||||
type CronPreset,
|
||||
@@ -19,23 +22,36 @@ import {
|
||||
export function useScheduleCronDesigner() {
|
||||
const { t } = useI18n();
|
||||
const state = ref<ScheduleCronDesignerState>(defaultCronDesignerState());
|
||||
const timezone = ref(DEFAULT_SCHEDULE_TIMEZONE);
|
||||
|
||||
const cronExpr = computed(() => {
|
||||
if (state.value.editorMode === 'advanced') {
|
||||
return state.value.advancedCronExpr.trim();
|
||||
return normalizeCronExpr(state.value.advancedCronExpr);
|
||||
}
|
||||
return buildCronExpr(state.value);
|
||||
});
|
||||
|
||||
const cronRisky = computed(() =>
|
||||
state.value.editorMode === 'advanced' && isRiskyCronExpr(state.value.advancedCronExpr),
|
||||
);
|
||||
|
||||
const isValid = computed(() => validateCronDesignerState(state.value));
|
||||
|
||||
const nextRunAt = computed(() =>
|
||||
computeNextRunAt(cronExpr.value, (expr) => {
|
||||
const normalized = normalizeCronForParser(expr);
|
||||
return CronExpressionParser.parse(normalized);
|
||||
return CronExpressionParser.parse(normalized, { tz: timezone.value || DEFAULT_SCHEDULE_TIMEZONE });
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* 更新 Cron 预览计算时区。
|
||||
* @param value 排程保存的 IANA 时区;为空时回退默认业务时区。
|
||||
*/
|
||||
const setTimezone = (value: string | null | undefined) => {
|
||||
timezone.value = String(value || '').trim() || DEFAULT_SCHEDULE_TIMEZONE;
|
||||
};
|
||||
|
||||
const loadFromExpr = (expr: string | null | undefined) => {
|
||||
const result = parseCronExpr(expr);
|
||||
if (result.ok && result.state) {
|
||||
@@ -82,6 +98,15 @@ export function useScheduleCronDesigner() {
|
||||
return true;
|
||||
};
|
||||
|
||||
const applyAdvancedCron = (expr: string) => {
|
||||
state.value = {
|
||||
...defaultCronDesignerState(),
|
||||
editorMode: 'advanced',
|
||||
advancedCronExpr: normalizeCronExpr(expr),
|
||||
parseFailed: false,
|
||||
};
|
||||
};
|
||||
|
||||
const setPreset = (preset: CronPreset, options?: { everyMinutes?: number; weeklyDay?: number }) => {
|
||||
state.value = applyPreset(preset, state.value, options);
|
||||
state.value.editorMode = 'visual';
|
||||
@@ -145,13 +170,17 @@ export function useScheduleCronDesigner() {
|
||||
|
||||
return {
|
||||
state,
|
||||
timezone,
|
||||
cronExpr,
|
||||
cronRisky,
|
||||
isValid,
|
||||
nextRunAt,
|
||||
setTimezone,
|
||||
loadFromExpr,
|
||||
reset,
|
||||
switchToAdvanced,
|
||||
switchToVisual,
|
||||
applyAdvancedCron,
|
||||
setPreset,
|
||||
toggleMonth,
|
||||
toggleWeekday,
|
||||
|
||||
@@ -26,6 +26,7 @@ import { useMessage } from '@/shared/composables/useMessage';
|
||||
import { getAuthHeaders } from '@/shared/utils/auth';
|
||||
import type { AdminAgentOption, AdminUserRow } from '@/features/admin/types/admin.types';
|
||||
import { useScheduleCronDesigner } from './useScheduleCronDesigner';
|
||||
import { DEFAULT_SCHEDULE_TIMEZONE } from '../constants';
|
||||
|
||||
export function useSchedulesAdmin() {
|
||||
const { t, locale } = useI18n();
|
||||
@@ -186,6 +187,18 @@ const scheduleCronDesigner = useScheduleCronDesigner();
|
||||
const scheduleCronPreview = scheduleCronDesigner.cronExpr;
|
||||
const scheduleCronValid = scheduleCronDesigner.isValid;
|
||||
|
||||
/**
|
||||
* 读取浏览器 IANA 时区,作为新建排程的 Cron 当地时间口径。
|
||||
* @returns 浏览器时区;读取失败时回退默认业务时区。
|
||||
*/
|
||||
const detectBrowserTimezone = (): string => {
|
||||
try {
|
||||
return Intl.DateTimeFormat().resolvedOptions().timeZone || DEFAULT_SCHEDULE_TIMEZONE;
|
||||
} catch {
|
||||
return DEFAULT_SCHEDULE_TIMEZONE;
|
||||
}
|
||||
};
|
||||
|
||||
const resetScheduleCreateForm = () => {
|
||||
scheduleEditingId.value = null;
|
||||
scheduleCreateForm.value = {
|
||||
@@ -228,6 +241,7 @@ const resetScheduleCreateForm = () => {
|
||||
dify_system_prompt: '',
|
||||
};
|
||||
scheduleTestResult.value = null;
|
||||
scheduleCronDesigner.setTimezone(detectBrowserTimezone());
|
||||
scheduleNotifyUserQuery.value = '';
|
||||
scheduleNotifyDropdownOpen.value = false;
|
||||
scheduleOwnerQuery.value = '';
|
||||
@@ -262,6 +276,7 @@ const openScheduleEditModal = async (task: ScheduleTask) => {
|
||||
enabled: !!task.enabled,
|
||||
data_source_type: (task.data_source_type || 'database') as 'database' | 'external_api',
|
||||
};
|
||||
scheduleCronDesigner.setTimezone(task.schedule_timezone || detectBrowserTimezone());
|
||||
|
||||
try {
|
||||
const result = await getAdminScheduleTaskConfig(task.id);
|
||||
@@ -724,6 +739,7 @@ const createSchedule = async () => {
|
||||
schedule_type: 'platform_app',
|
||||
owner_user_id: scheduleCreateForm.value.owner_user_id,
|
||||
cron_expr: scheduleCronPreview.value,
|
||||
schedule_timezone: scheduleCronDesigner.timezone.value || detectBrowserTimezone(),
|
||||
wait_seconds: Math.max(0, Number(scheduleCreateForm.value.wait_seconds || 0)),
|
||||
dify_agent_id: scheduleCreateForm.value.dify_agent_id || undefined,
|
||||
notify_user_ids_json: scheduleCreateForm.value.notify_enabled ? scheduleCreateForm.value.notify_user_ids_json : [],
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
/** 与后端 SCHEDULE_TIMEZONE 默认值保持一致 */
|
||||
export const DEFAULT_SCHEDULE_TIMEZONE = 'Asia/Shanghai';
|
||||
Reference in new issue
Block a user