@nodestackapp/server 2.6.0 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/NodeStack.d.ts +4 -0
- package/dist/NodeStack.d.ts.map +1 -1
- package/dist/NodeStack.js +44 -6
- package/dist/NodeStack.js.map +1 -1
- package/dist/admin/AdminUIBundler.d.ts.map +1 -1
- package/dist/admin/AdminUIBundler.js +1 -0
- package/dist/admin/AdminUIBundler.js.map +1 -1
- package/dist/admin/ui/css/main.css +6 -3
- package/dist/admin/ui/index.html +2345 -55
- package/dist/admin/ui/js/api.js +8 -0
- package/dist/admin/ui/js/app.js +38 -7
- package/dist/admin/ui/js/apps.js +18 -0
- package/dist/admin/ui/js/command-palette.js +10 -0
- package/dist/admin/ui/js/cron.js +1706 -0
- package/dist/admin/ui/js/database.js +10 -0
- package/dist/admin/ui/js/flows-designer.js +94 -0
- package/dist/admin/ui/js/flows.js +43 -4
- package/dist/admin/ui/js/functions-workbench.js +18 -0
- package/dist/admin/ui/js/functions.js +6 -3
- package/dist/admin/ui/js/snapshots.js +338 -0
- package/dist/admin/ui/js/views.js +1 -1
- package/dist/apps/AppManager.d.ts.map +1 -1
- package/dist/apps/AppManager.js +7 -3
- package/dist/apps/AppManager.js.map +1 -1
- package/dist/apps/AppScope.d.ts +2 -0
- package/dist/apps/AppScope.d.ts.map +1 -1
- package/dist/apps/AppScope.js +22 -2
- package/dist/apps/AppScope.js.map +1 -1
- package/dist/auth/ApiKeyService.d.ts +1 -0
- package/dist/auth/ApiKeyService.d.ts.map +1 -1
- package/dist/auth/ApiKeyService.js +16 -1
- package/dist/auth/ApiKeyService.js.map +1 -1
- package/dist/cli/cli.js +1 -1
- package/dist/core/container/Tokens.d.ts +2 -0
- package/dist/core/container/Tokens.d.ts.map +1 -1
- package/dist/core/container/Tokens.js +2 -0
- package/dist/core/container/Tokens.js.map +1 -1
- package/dist/cron/CronParser.d.ts +83 -0
- package/dist/cron/CronParser.d.ts.map +1 -0
- package/dist/cron/CronParser.js +538 -0
- package/dist/cron/CronParser.js.map +1 -0
- package/dist/cron/CronService.d.ts +74 -0
- package/dist/cron/CronService.d.ts.map +1 -0
- package/dist/cron/CronService.js +1254 -0
- package/dist/cron/CronService.js.map +1 -0
- package/dist/cron/types.d.ts +98 -0
- package/dist/cron/types.d.ts.map +1 -0
- package/dist/cron/types.js +3 -0
- package/dist/cron/types.js.map +1 -0
- package/dist/database/DatabaseService.d.ts +3 -1
- package/dist/database/DatabaseService.d.ts.map +1 -1
- package/dist/database/DatabaseService.js +10 -3
- package/dist/database/DatabaseService.js.map +1 -1
- package/dist/database/config/DatabaseConfigService.d.ts +1 -0
- package/dist/database/config/DatabaseConfigService.d.ts.map +1 -1
- package/dist/database/config/DatabaseConfigService.js +4 -2
- package/dist/database/config/DatabaseConfigService.js.map +1 -1
- package/dist/database/drivers/DriverPackageLoader.d.ts +11 -0
- package/dist/database/drivers/DriverPackageLoader.d.ts.map +1 -0
- package/dist/database/drivers/DriverPackageLoader.js +112 -0
- package/dist/database/drivers/DriverPackageLoader.js.map +1 -0
- package/dist/database/drivers/MssqlDriver.d.ts.map +1 -1
- package/dist/database/drivers/MssqlDriver.js +5 -6
- package/dist/database/drivers/MssqlDriver.js.map +1 -1
- package/dist/database/drivers/PostgresDriver.d.ts.map +1 -1
- package/dist/database/drivers/PostgresDriver.js +4 -5
- package/dist/database/drivers/PostgresDriver.js.map +1 -1
- package/dist/database/migration/DatabaseMigrationService.d.ts +1 -0
- package/dist/database/migration/DatabaseMigrationService.d.ts.map +1 -1
- package/dist/database/migration/DatabaseMigrationService.js +51 -0
- package/dist/database/migration/DatabaseMigrationService.js.map +1 -1
- package/dist/database/migrations/SystemMigrations.d.ts.map +1 -1
- package/dist/database/migrations/SystemMigrations.js +138 -0
- package/dist/database/migrations/SystemMigrations.js.map +1 -1
- package/dist/flows/FlowService.d.ts +3 -0
- package/dist/flows/FlowService.d.ts.map +1 -1
- package/dist/flows/FlowService.js +106 -7
- package/dist/flows/FlowService.js.map +1 -1
- package/dist/flows/FlowValidator.d.ts +1 -1
- package/dist/flows/FlowValidator.d.ts.map +1 -1
- package/dist/flows/FlowValidator.js +13 -3
- package/dist/flows/FlowValidator.js.map +1 -1
- package/dist/flows/types.d.ts +6 -1
- package/dist/flows/types.d.ts.map +1 -1
- package/dist/functions/FunctionService.d.ts +5 -1
- package/dist/functions/FunctionService.d.ts.map +1 -1
- package/dist/functions/FunctionService.js +190 -7
- package/dist/functions/FunctionService.js.map +1 -1
- package/dist/functions/runtime/QuickJsWasmRuntime.d.ts.map +1 -1
- package/dist/functions/runtime/QuickJsWasmRuntime.js +183 -180
- package/dist/functions/runtime/QuickJsWasmRuntime.js.map +1 -1
- package/dist/functions/types.d.ts +15 -0
- package/dist/functions/types.d.ts.map +1 -1
- package/dist/http/HttpServer.d.ts +4 -1
- package/dist/http/HttpServer.d.ts.map +1 -1
- package/dist/http/HttpServer.js +34 -2
- package/dist/http/HttpServer.js.map +1 -1
- package/dist/http/controllers/CronController.d.ts +63 -0
- package/dist/http/controllers/CronController.d.ts.map +1 -0
- package/dist/http/controllers/CronController.js +92 -0
- package/dist/http/controllers/CronController.js.map +1 -0
- package/dist/http/controllers/DatabaseController.d.ts.map +1 -1
- package/dist/http/controllers/DatabaseController.js +53 -0
- package/dist/http/controllers/DatabaseController.js.map +1 -1
- package/dist/http/controllers/HealthController.js +2 -2
- package/dist/schema/OpenApiGenerator.js +3 -3
- package/dist/snapshots/SnapshotService.d.ts.map +1 -1
- package/dist/snapshots/SnapshotService.js +21 -9
- package/dist/snapshots/SnapshotService.js.map +1 -1
- package/dist/snapshots/drivers/LogicalSnapshotDriver.d.ts.map +1 -1
- package/dist/snapshots/drivers/LogicalSnapshotDriver.js +2 -0
- package/dist/snapshots/drivers/LogicalSnapshotDriver.js.map +1 -1
- package/dist/webhooks/WebhookService.js +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,1706 @@
|
|
|
1
|
+
// Native Cron & Scheduled Triggers Module for NodeStack Admin UI
|
|
2
|
+
|
|
3
|
+
const cronIcons = {
|
|
4
|
+
clock: (w = 12, h = 12) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: -1px; display: inline-block;"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg>`,
|
|
5
|
+
calendar: (w = 13, h = 13) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: -1px; display: inline-block;"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line></svg>`,
|
|
6
|
+
globe: (w = 11, h = 11) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: -1px; display: inline-block;"><circle cx="12" cy="12" r="10"></circle><line x1="2" y1="12" x2="22" y2="12"></line><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"></path></svg>`,
|
|
7
|
+
alert: (w = 11, h = 11) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: -1px; display: inline-block;"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"></path><line x1="12" y1="9" x2="12" y2="13"></line><line x1="12" y1="17" x2="12.01" y2="17"></line></svg>`,
|
|
8
|
+
refresh: (w = 10, h = 10) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: -1px; display: inline-block;"><polyline points="23 4 23 10 17 10"></polyline><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path></svg>`,
|
|
9
|
+
zap: (w = 10, h = 10) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: -1px; display: inline-block;"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"></polygon></svg>`,
|
|
10
|
+
hourglass: (w = 10, h = 10) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: -1px; display: inline-block;"><path d="M5 22h14M5 2h14M17 22v-4.172a2 2 0 0 0-.586-1.414L12 12l-4.414 4.414A2 2 0 0 0 7 17.828V22M7 2v4.172a2 2 0 0 0 .586 1.414L12 12l4.414-4.414A2 2 0 0 0 17 6.172V2"></path></svg>`,
|
|
11
|
+
database: (w = 10, h = 10) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: -1px; display: inline-block;"><ellipse cx="12" cy="5" rx="9" ry="3"></ellipse><path d="M21 12c0 1.66-4 3-9 3s-9-1.34-9-3"></path><path d="M3 5v14c0 1.66 4 3 9 3s9-1.34 9-3V5"></path></svg>`,
|
|
12
|
+
play: (w = 9, h = 9) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="currentColor" stroke="none" style="vertical-align: -1px; display: inline-block;"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>`,
|
|
13
|
+
externalLink: (w = 10, h = 10) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: -1px; display: inline-block;"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path><polyline points="15 3 21 3 21 9"></polyline><line x1="10" y1="14" x2="21" y2="3"></line></svg>`,
|
|
14
|
+
copy: (w = 12, h = 12) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: -1px; display: inline-block;"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>`,
|
|
15
|
+
chart: (w = 18, h = 18) => `<svg width="${w}" height="${h}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: inline-block;"><line x1="18" y1="20" x2="18" y2="10"></line><line x1="12" y1="20" x2="12" y2="4"></line><line x1="6" y1="20" x2="6" y2="14"></line></svg>`,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
let cronState = {
|
|
19
|
+
subTab: 'schedules', // 'schedules' | 'history'
|
|
20
|
+
schedules: [],
|
|
21
|
+
functionsList: [],
|
|
22
|
+
flowsList: [],
|
|
23
|
+
searchQuery: '',
|
|
24
|
+
statusFilter: 'all', // 'all' | 'enabled' | 'paused' | 'failed'
|
|
25
|
+
sortBy: 'nextRun', // 'nextRun' | 'name' | 'lastRun' | 'created'
|
|
26
|
+
stats: {
|
|
27
|
+
totalSchedules: 0,
|
|
28
|
+
activeSchedules: 0,
|
|
29
|
+
totalRuns: 0,
|
|
30
|
+
successfulRuns: 0,
|
|
31
|
+
failedRuns: 0,
|
|
32
|
+
skippedRuns: 0,
|
|
33
|
+
byTarget: { function: 0, flow: 0, snapshot: 0 },
|
|
34
|
+
},
|
|
35
|
+
runs: [],
|
|
36
|
+
runFilter: {
|
|
37
|
+
scheduleId: '',
|
|
38
|
+
targetType: '',
|
|
39
|
+
status: 'all',
|
|
40
|
+
page: 1,
|
|
41
|
+
perPage: 25,
|
|
42
|
+
total: 0,
|
|
43
|
+
totalPages: 1,
|
|
44
|
+
},
|
|
45
|
+
targetFilter: 'all', // 'all' | 'function' | 'flow' | 'snapshot'
|
|
46
|
+
activeScheduleForEdit: null,
|
|
47
|
+
activeRunForDetail: null,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
if (typeof window !== 'undefined') {
|
|
51
|
+
window.cronState = cronState;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getCronApiPrefix() {
|
|
55
|
+
const slug = typeof getActiveAppSlug === 'function' ? getActiveAppSlug() : 'default';
|
|
56
|
+
return slug && slug !== 'default' ? `/api/apps/${encodeURIComponent(slug)}/cron` : '/api/cron';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getFunctionsListApiPrefix() {
|
|
60
|
+
const slug = typeof getActiveAppSlug === 'function' ? getActiveAppSlug() : 'default';
|
|
61
|
+
return slug && slug !== 'default' ? `/api/apps/${encodeURIComponent(slug)}/functions` : '/api/functions';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getFlowsListApiPrefix() {
|
|
65
|
+
const slug = typeof getActiveAppSlug === 'function' ? getActiveAppSlug() : 'default';
|
|
66
|
+
return slug && slug !== 'default' ? `/api/apps/${encodeURIComponent(slug)}/flows` : '/api/flows';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getSupportedIanaTimezones() {
|
|
70
|
+
if (typeof Intl !== 'undefined' && typeof Intl.supportedValuesOf === 'function') {
|
|
71
|
+
try {
|
|
72
|
+
return Intl.supportedValuesOf('timeZone');
|
|
73
|
+
} catch {}
|
|
74
|
+
}
|
|
75
|
+
return ['UTC', 'America/New_York', 'America/Chicago', 'America/Denver', 'America/Los_Angeles', 'Europe/London', 'Europe/Paris', 'Asia/Tokyo', 'Asia/Kolkata', 'Australia/Sydney'];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function isSqliteDatabase() {
|
|
79
|
+
if (typeof state !== 'undefined' && state.demo && state.demo.supported === false) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
if (typeof databaseState !== 'undefined' && databaseState.status?.provider) {
|
|
83
|
+
return databaseState.status.provider.toLowerCase() === 'sqlite';
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function renderSchedulesView() {
|
|
89
|
+
const container = document.getElementById('page-view-schedules') || document.getElementById('main-body');
|
|
90
|
+
if (!container) return;
|
|
91
|
+
|
|
92
|
+
if (!isSqliteDatabase() && cronState.targetFilter === 'snapshot') {
|
|
93
|
+
cronState.targetFilter = 'all';
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
container.innerHTML = `
|
|
97
|
+
<div class="webhooks-container" id="schedules-manager-root">
|
|
98
|
+
<!-- Header -->
|
|
99
|
+
<div class="webhooks-header">
|
|
100
|
+
<div class="webhooks-title-area">
|
|
101
|
+
<h2>
|
|
102
|
+
<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
103
|
+
<circle cx="12" cy="12" r="10"></circle>
|
|
104
|
+
<polyline points="12 6 12 12 16 14"></polyline>
|
|
105
|
+
</svg>
|
|
106
|
+
Schedules Manager
|
|
107
|
+
</h2>
|
|
108
|
+
<p>Configure automated Cron triggers for Serverless Functions, Orchestrated Flows, and Backups with atomic distributed DB leases.</p>
|
|
109
|
+
</div>
|
|
110
|
+
<div style="display: flex; align-items: center; gap: 8px; flex-wrap: wrap;">
|
|
111
|
+
<button class="btn btn-secondary btn-sm" id="btn-refresh-schedules" onclick="refreshSchedulesView()" title="Refresh Schedules & Stats">
|
|
112
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M23 4v6h-6M1 20v-6h6"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></svg>
|
|
113
|
+
Refresh
|
|
114
|
+
</button>
|
|
115
|
+
<button class="btn btn-primary btn-sm" id="btn-new-schedule" onclick="openCreateScheduleDrawer()">
|
|
116
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
|
|
117
|
+
New Schedule
|
|
118
|
+
</button>
|
|
119
|
+
</div>
|
|
120
|
+
</div>
|
|
121
|
+
|
|
122
|
+
<!-- Stats Grid -->
|
|
123
|
+
<div class="webhooks-stats-grid" id="cron-stats-grid">
|
|
124
|
+
<div class="webhook-stat-card">
|
|
125
|
+
<span class="webhook-stat-label">Total Schedules</span>
|
|
126
|
+
<div class="webhook-stat-value" id="cron-stat-total">0</div>
|
|
127
|
+
</div>
|
|
128
|
+
<div class="webhook-stat-card">
|
|
129
|
+
<span class="webhook-stat-label">Active (Enabled)</span>
|
|
130
|
+
<div class="webhook-stat-value" style="color: var(--accent-success);" id="cron-stat-active">0</div>
|
|
131
|
+
</div>
|
|
132
|
+
<div class="webhook-stat-card">
|
|
133
|
+
<span class="webhook-stat-label">Succeeded Runs</span>
|
|
134
|
+
<div class="webhook-stat-value" style="color: var(--accent-primary);" id="cron-stat-succeeded">0</div>
|
|
135
|
+
</div>
|
|
136
|
+
<div class="webhook-stat-card">
|
|
137
|
+
<span class="webhook-stat-label">Failed Runs</span>
|
|
138
|
+
<div class="webhook-stat-value" style="color: var(--accent-danger);" id="cron-stat-failed">0</div>
|
|
139
|
+
</div>
|
|
140
|
+
<div class="webhook-stat-card">
|
|
141
|
+
<span class="webhook-stat-label">Skipped (Missed Window)</span>
|
|
142
|
+
<div class="webhook-stat-value" style="color: var(--accent-warning);" id="cron-stat-skipped">0</div>
|
|
143
|
+
</div>
|
|
144
|
+
</div>
|
|
145
|
+
|
|
146
|
+
<!-- Subtabs Bar -->
|
|
147
|
+
<div class="webhooks-tabs-bar" style="display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px;">
|
|
148
|
+
<div style="display: flex; align-items: center; gap: 6px;">
|
|
149
|
+
<button class="webhook-tab-btn ${cronState.subTab === 'schedules' ? 'active' : ''}" id="tab-btn-cron-schedules" onclick="switchCronTab('schedules')">
|
|
150
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg>
|
|
151
|
+
Active Schedules
|
|
152
|
+
</button>
|
|
153
|
+
<button class="webhook-tab-btn ${cronState.subTab === 'history' ? 'active' : ''}" id="tab-btn-cron-history" onclick="switchCronTab('history')">
|
|
154
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="8" y1="6" x2="21" y2="6"></line><line x1="8" y1="12" x2="21" y2="12"></line><line x1="8" y1="18" x2="21" y2="18"></line><line x1="3" y1="6" x2="3.01" y2="6"></line><line x1="3" y1="12" x2="3.01" y2="12"></line><line x1="3" y1="18" x2="3.01" y2="18"></line></svg>
|
|
155
|
+
Execution History
|
|
156
|
+
</button>
|
|
157
|
+
</div>
|
|
158
|
+
<div id="cron-tab-actions" style="display: flex; align-items: center; gap: 8px; flex-wrap: wrap;">
|
|
159
|
+
${cronState.subTab === 'schedules' ? `
|
|
160
|
+
<div style="display: flex; align-items: center; gap: 6px; flex-wrap: wrap;">
|
|
161
|
+
<input type="text" class="input input-sm" id="cron-search-input" placeholder="Search schedules..." value="${escapeHtml(cronState.searchQuery || '')}" oninput="handleCronSearchInput(this.value)" style="font-size: 11px; width: 140px; padding: 3px 8px;">
|
|
162
|
+
<select class="input input-sm" id="cron-target-filter" onchange="handleCronTargetFilter(this.value)" style="font-size: 11px; padding: 3px 6px;">
|
|
163
|
+
<option value="all" ${cronState.targetFilter === 'all' ? 'selected' : ''}>All Targets</option>
|
|
164
|
+
<option value="function" ${cronState.targetFilter === 'function' ? 'selected' : ''}>Functions</option>
|
|
165
|
+
<option value="flow" ${cronState.targetFilter === 'flow' ? 'selected' : ''}>Flows</option>
|
|
166
|
+
${isSqliteDatabase() ? `<option value="snapshot" ${cronState.targetFilter === 'snapshot' ? 'selected' : ''}>Snapshots</option>` : ''}
|
|
167
|
+
</select>
|
|
168
|
+
<select class="input input-sm" id="cron-status-filter" onchange="handleCronStatusFilter(this.value)" style="font-size: 11px; padding: 3px 6px;">
|
|
169
|
+
<option value="all" ${cronState.statusFilter === 'all' ? 'selected' : ''}>All States</option>
|
|
170
|
+
<option value="enabled" ${cronState.statusFilter === 'enabled' ? 'selected' : ''}>Active Only</option>
|
|
171
|
+
<option value="paused" ${cronState.statusFilter === 'paused' ? 'selected' : ''}>Paused Only</option>
|
|
172
|
+
<option value="failed" ${cronState.statusFilter === 'failed' ? 'selected' : ''}>Failing Only</option>
|
|
173
|
+
</select>
|
|
174
|
+
<select class="input input-sm" id="cron-sort-filter" onchange="handleCronSortChange(this.value)" style="font-size: 11px; padding: 3px 6px;">
|
|
175
|
+
<option value="nextRun" ${cronState.sortBy === 'nextRun' ? 'selected' : ''}>Sort: Next Run</option>
|
|
176
|
+
<option value="name" ${cronState.sortBy === 'name' ? 'selected' : ''}>Sort: Name</option>
|
|
177
|
+
<option value="lastRun" ${cronState.sortBy === 'lastRun' ? 'selected' : ''}>Sort: Last Run</option>
|
|
178
|
+
<option value="created" ${cronState.sortBy === 'created' ? 'selected' : ''}>Sort: Created</option>
|
|
179
|
+
</select>
|
|
180
|
+
</div>
|
|
181
|
+
` : `
|
|
182
|
+
<button class="btn btn-secondary btn-sm" id="btn-clear-cron-history" onclick="handleClearRunHistory()" style="font-size: 11px;">
|
|
183
|
+
${cronState.runFilter.scheduleId ? 'Clear Schedule History' : 'Clear All History'}
|
|
184
|
+
</button>
|
|
185
|
+
`}
|
|
186
|
+
</div>
|
|
187
|
+
</div>
|
|
188
|
+
|
|
189
|
+
<!-- Tab Content Area -->
|
|
190
|
+
<div id="cron-tab-content" style="margin-top: 16px;">
|
|
191
|
+
${cronState.subTab === 'schedules' ? renderSchedulesListHtml() : renderHistoryTableHtml()}
|
|
192
|
+
</div>
|
|
193
|
+
</div>
|
|
194
|
+
|
|
195
|
+
<!-- Datalist for IANA Timezones -->
|
|
196
|
+
<datalist id="iana-timezones-list">
|
|
197
|
+
${getSupportedIanaTimezones().map(tz => `<option value="${escapeHtml(tz)}"></option>`).join('')}
|
|
198
|
+
</datalist>
|
|
199
|
+
|
|
200
|
+
<!-- Create / Edit Schedule Modal Container -->
|
|
201
|
+
<div id="cron-schedule-modal-container"></div>
|
|
202
|
+
`;
|
|
203
|
+
|
|
204
|
+
loadCronData();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function switchCronTab(tab, updateUrl = true) {
|
|
208
|
+
cronState.subTab = tab;
|
|
209
|
+
if (updateUrl && typeof pushRoute === 'function') {
|
|
210
|
+
pushRoute(`schedules/${tab}`);
|
|
211
|
+
}
|
|
212
|
+
renderSchedulesView();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function handleCronTargetFilter(target) {
|
|
216
|
+
if (!isSqliteDatabase() && target === 'snapshot') {
|
|
217
|
+
target = 'all';
|
|
218
|
+
}
|
|
219
|
+
cronState.targetFilter = target;
|
|
220
|
+
refreshSchedulesListDom();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function handleCronSearchInput(query) {
|
|
224
|
+
cronState.searchQuery = query || '';
|
|
225
|
+
refreshSchedulesListDom();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function handleCronStatusFilter(status) {
|
|
229
|
+
cronState.statusFilter = status;
|
|
230
|
+
refreshSchedulesListDom();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function handleCronSortChange(sort) {
|
|
234
|
+
cronState.sortBy = sort;
|
|
235
|
+
refreshSchedulesListDom();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function refreshSchedulesListDom() {
|
|
239
|
+
const contentArea = document.getElementById('cron-tab-content');
|
|
240
|
+
if (contentArea && cronState.subTab === 'schedules') {
|
|
241
|
+
contentArea.innerHTML = renderSchedulesListHtml();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function viewScheduleHistory(scheduleId) {
|
|
246
|
+
cronState.runFilter.scheduleId = scheduleId;
|
|
247
|
+
cronState.runFilter.page = 1;
|
|
248
|
+
switchCronTab('history');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function navigateToCronTarget(targetType, targetId) {
|
|
252
|
+
if (targetType === 'function') {
|
|
253
|
+
if (typeof selectNav === 'function') {
|
|
254
|
+
selectNav('functions');
|
|
255
|
+
if (typeof openFunctionWorkbench === 'function' && targetId) {
|
|
256
|
+
setTimeout(() => openFunctionWorkbench(targetId), 120);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
} else if (targetType === 'flow') {
|
|
260
|
+
if (typeof selectNav === 'function') {
|
|
261
|
+
selectNav('flows');
|
|
262
|
+
if (typeof openFlowInDesigner === 'function' && targetId) {
|
|
263
|
+
setTimeout(() => openFlowInDesigner(targetId), 120);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
} else if (targetType === 'snapshot') {
|
|
267
|
+
if (typeof selectNav === 'function') {
|
|
268
|
+
selectNav('snapshots');
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async function loadCronData() {
|
|
274
|
+
try {
|
|
275
|
+
const apiPrefix = getCronApiPrefix();
|
|
276
|
+
const [stats, schedules] = await Promise.all([
|
|
277
|
+
api(`${apiPrefix}/stats`).catch(() => null),
|
|
278
|
+
api(`${apiPrefix}/schedules`).catch(() => []),
|
|
279
|
+
loadFunctionsAndFlows(),
|
|
280
|
+
(typeof fetchDatabaseStatus === 'function' && (typeof databaseState === 'undefined' || !databaseState.status)) ? fetchDatabaseStatus().catch(() => null) : Promise.resolve(null),
|
|
281
|
+
]);
|
|
282
|
+
|
|
283
|
+
if (stats) {
|
|
284
|
+
cronState.stats = stats;
|
|
285
|
+
updateStatsDom();
|
|
286
|
+
}
|
|
287
|
+
if (schedules) {
|
|
288
|
+
cronState.schedules = Array.isArray(schedules) ? schedules : (schedules.schedules || []);
|
|
289
|
+
const contentArea = document.getElementById('cron-tab-content');
|
|
290
|
+
if (contentArea && cronState.subTab === 'schedules') {
|
|
291
|
+
contentArea.innerHTML = renderSchedulesListHtml();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (cronState.subTab === 'history') {
|
|
296
|
+
await loadRunHistory();
|
|
297
|
+
}
|
|
298
|
+
} catch (err) {
|
|
299
|
+
console.warn('[Cron] Failed to load schedule data:', err);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function getTargetDisplayName(targetType, targetId) {
|
|
304
|
+
if (!targetId) return '';
|
|
305
|
+
if (targetType === 'function') {
|
|
306
|
+
const fn = (cronState.functionsList || []).find(f => f.id === targetId || f.name === targetId);
|
|
307
|
+
return fn ? fn.name : targetId;
|
|
308
|
+
}
|
|
309
|
+
if (targetType === 'flow') {
|
|
310
|
+
const fl = (cronState.flowsList || []).find(f => f.id === targetId || f.name === targetId);
|
|
311
|
+
return fl ? fl.name : targetId;
|
|
312
|
+
}
|
|
313
|
+
if (targetType === 'snapshot') {
|
|
314
|
+
return targetId === 'data.db' ? 'data.db (SQLite)' : targetId;
|
|
315
|
+
}
|
|
316
|
+
return targetId;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async function loadCronStats() {
|
|
320
|
+
try {
|
|
321
|
+
const apiPrefix = getCronApiPrefix();
|
|
322
|
+
const stats = await api(`${apiPrefix}/stats`).catch(() => null);
|
|
323
|
+
if (stats) {
|
|
324
|
+
cronState.stats = stats;
|
|
325
|
+
updateStatsDom();
|
|
326
|
+
}
|
|
327
|
+
} catch (_) {}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
async function loadFunctionsAndFlows() {
|
|
331
|
+
try {
|
|
332
|
+
const fnPrefix = getFunctionsListApiPrefix();
|
|
333
|
+
const flowPrefix = getFlowsListApiPrefix();
|
|
334
|
+
const [fnData, flowData] = await Promise.all([
|
|
335
|
+
api(fnPrefix).catch(() => []),
|
|
336
|
+
api(flowPrefix).catch(() => []),
|
|
337
|
+
]);
|
|
338
|
+
cronState.functionsList = Array.isArray(fnData) ? fnData : (fnData?.items || fnData?.functions || []);
|
|
339
|
+
cronState.flowsList = Array.isArray(flowData) ? flowData : (flowData?.items || flowData?.flows || []);
|
|
340
|
+
|
|
341
|
+
// Re-render schedules list or target picker if open
|
|
342
|
+
const contentArea = document.getElementById('cron-tab-content');
|
|
343
|
+
if (contentArea && cronState.subTab === 'schedules' && cronState.schedules.length > 0) {
|
|
344
|
+
contentArea.innerHTML = renderSchedulesListHtml();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// If modal target picker is open, refresh its options
|
|
348
|
+
const pickerArea = document.getElementById('sched-target-picker-area');
|
|
349
|
+
const targetTypeEl = document.getElementById('sched-input-target-type');
|
|
350
|
+
const targetIdEl = document.getElementById('sched-input-target-id');
|
|
351
|
+
if (pickerArea && targetTypeEl) {
|
|
352
|
+
pickerArea.innerHTML = renderModalTargetPickerHtml(targetTypeEl.value, targetIdEl?.value || '');
|
|
353
|
+
}
|
|
354
|
+
} catch (_) {}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function updateStatsDom() {
|
|
358
|
+
const s = cronState.stats;
|
|
359
|
+
const totalEl = document.getElementById('cron-stat-total');
|
|
360
|
+
const activeEl = document.getElementById('cron-stat-active');
|
|
361
|
+
const succEl = document.getElementById('cron-stat-succeeded');
|
|
362
|
+
const failEl = document.getElementById('cron-stat-failed');
|
|
363
|
+
const skipEl = document.getElementById('cron-stat-skipped');
|
|
364
|
+
|
|
365
|
+
if (totalEl) totalEl.textContent = s.totalSchedules || 0;
|
|
366
|
+
if (activeEl) activeEl.textContent = s.activeSchedules || 0;
|
|
367
|
+
if (succEl) succEl.textContent = s.successfulRuns || 0;
|
|
368
|
+
if (failEl) failEl.textContent = s.failedRuns || 0;
|
|
369
|
+
if (skipEl) skipEl.textContent = s.skippedRuns || 0;
|
|
370
|
+
|
|
371
|
+
const sidebarCountEl = document.getElementById('sidebar-schedules-count');
|
|
372
|
+
if (sidebarCountEl) {
|
|
373
|
+
sidebarCountEl.textContent = s.totalSchedules || 0;
|
|
374
|
+
sidebarCountEl.style.display = s.totalSchedules > 0 ? 'inline-block' : 'none';
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function refreshSchedulesView() {
|
|
379
|
+
loadCronData();
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function renderSchedulesListHtml() {
|
|
383
|
+
let list = (cronState.schedules || []).slice();
|
|
384
|
+
|
|
385
|
+
// Target Filter
|
|
386
|
+
if (cronState.targetFilter !== 'all') {
|
|
387
|
+
list = list.filter(s => s.targetType === cronState.targetFilter);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Status Filter
|
|
391
|
+
if (cronState.statusFilter === 'enabled') {
|
|
392
|
+
list = list.filter(s => Boolean(s.enabled));
|
|
393
|
+
} else if (cronState.statusFilter === 'paused') {
|
|
394
|
+
list = list.filter(s => !s.enabled);
|
|
395
|
+
} else if (cronState.statusFilter === 'failed') {
|
|
396
|
+
list = list.filter(s => s.lastStatus === 'failed');
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Search Filter
|
|
400
|
+
if (cronState.searchQuery && cronState.searchQuery.trim()) {
|
|
401
|
+
const q = cronState.searchQuery.trim().toLowerCase();
|
|
402
|
+
list = list.filter(s =>
|
|
403
|
+
(s.name && s.name.toLowerCase().includes(q)) ||
|
|
404
|
+
(s.cronExpression && s.cronExpression.toLowerCase().includes(q)) ||
|
|
405
|
+
(s.humanDescription && s.humanDescription.toLowerCase().includes(q)) ||
|
|
406
|
+
(s.targetId && s.targetId.toLowerCase().includes(q)) ||
|
|
407
|
+
(s.description && s.description.toLowerCase().includes(q))
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Sorting
|
|
412
|
+
list.sort((a, b) => {
|
|
413
|
+
if (cronState.sortBy === 'name') {
|
|
414
|
+
return a.name.localeCompare(b.name);
|
|
415
|
+
}
|
|
416
|
+
if (cronState.sortBy === 'created') {
|
|
417
|
+
return new Date(b.created).getTime() - new Date(a.created).getTime();
|
|
418
|
+
}
|
|
419
|
+
if (cronState.sortBy === 'lastRun') {
|
|
420
|
+
const tA = a.lastRunAt ? new Date(a.lastRunAt).getTime() : 0;
|
|
421
|
+
const tB = b.lastRunAt ? new Date(b.lastRunAt).getTime() : 0;
|
|
422
|
+
return tB - tA;
|
|
423
|
+
}
|
|
424
|
+
// Default 'nextRun': soonest nextRunAt first; disabled/null at the end
|
|
425
|
+
const tA = a.enabled && a.nextRunAt ? new Date(a.nextRunAt).getTime() : Infinity;
|
|
426
|
+
const tB = b.enabled && b.nextRunAt ? new Date(b.nextRunAt).getTime() : Infinity;
|
|
427
|
+
return tA - tB;
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
if (list.length === 0) {
|
|
431
|
+
if (cronState.schedules && cronState.schedules.length > 0) {
|
|
432
|
+
return `
|
|
433
|
+
<div style="padding: 40px 16px; text-align: center; background: var(--bg-card); border-radius: 8px; border: 1px dashed var(--border-subtle);">
|
|
434
|
+
<h4 style="margin: 0 0 6px 0; font-size: 14px;">No Matching Schedules</h4>
|
|
435
|
+
<p style="color: var(--text-muted); font-size: 12px; max-width: 420px; margin: 0 auto 14px auto;">
|
|
436
|
+
No schedules matched your search query or active state filters.
|
|
437
|
+
</p>
|
|
438
|
+
<button class="btn btn-secondary btn-sm" onclick="handleResetScheduleFilters()">
|
|
439
|
+
Reset Filters
|
|
440
|
+
</button>
|
|
441
|
+
</div>
|
|
442
|
+
`;
|
|
443
|
+
}
|
|
444
|
+
return `
|
|
445
|
+
<div style="padding: 48px 16px; text-align: center; background: var(--bg-card); border-radius: 8px; border: 1px dashed var(--border-subtle);">
|
|
446
|
+
<svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" style="margin-bottom: 12px; color: var(--text-muted); opacity: 0.7;">
|
|
447
|
+
<circle cx="12" cy="12" r="10"></circle>
|
|
448
|
+
<polyline points="12 6 12 12 16 14"></polyline>
|
|
449
|
+
</svg>
|
|
450
|
+
<h4 style="margin: 0 0 6px 0; font-size: 15px;">No Schedules Found</h4>
|
|
451
|
+
<p style="color: var(--text-muted); font-size: 13px; max-width: 440px; margin: 0 auto 16px auto;">
|
|
452
|
+
Automate periodic tasks like database health checks, daily metrics sync, scheduled flows, or automated backups.
|
|
453
|
+
</p>
|
|
454
|
+
<button class="btn btn-primary btn-sm" onclick="openCreateScheduleDrawer()">
|
|
455
|
+
+ Create First Schedule
|
|
456
|
+
</button>
|
|
457
|
+
</div>
|
|
458
|
+
`;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return `
|
|
462
|
+
<div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)); gap: 14px;">
|
|
463
|
+
${list.map(s => renderScheduleCardHtml(s)).join('')}
|
|
464
|
+
</div>
|
|
465
|
+
`;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function handleResetScheduleFilters() {
|
|
469
|
+
cronState.searchQuery = '';
|
|
470
|
+
cronState.targetFilter = 'all';
|
|
471
|
+
cronState.statusFilter = 'all';
|
|
472
|
+
cronState.sortBy = 'nextRun';
|
|
473
|
+
const searchInput = document.getElementById('cron-search-input');
|
|
474
|
+
if (searchInput) searchInput.value = '';
|
|
475
|
+
const targetFilterEl = document.getElementById('cron-target-filter');
|
|
476
|
+
if (targetFilterEl) targetFilterEl.value = 'all';
|
|
477
|
+
const statusFilterEl = document.getElementById('cron-status-filter');
|
|
478
|
+
if (statusFilterEl) statusFilterEl.value = 'all';
|
|
479
|
+
const sortFilterEl = document.getElementById('cron-sort-filter');
|
|
480
|
+
if (sortFilterEl) sortFilterEl.value = 'nextRun';
|
|
481
|
+
refreshSchedulesListDom();
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function renderScheduleCardHtml(s) {
|
|
485
|
+
const isEnabled = Boolean(s.enabled);
|
|
486
|
+
const isUnsupportedSnapshot = s.targetType === 'snapshot' && !isSqliteDatabase();
|
|
487
|
+
const dbProviderName = (typeof databaseState !== 'undefined' && databaseState.status?.provider ? databaseState.status.provider : 'remote').toUpperCase();
|
|
488
|
+
const targetBadgeColor = s.targetType === 'function' ? 'var(--accent-primary)' : (s.targetType === 'flow' ? '#8b5cf6' : '#10b981');
|
|
489
|
+
const lastStatusColor = s.lastStatus === 'succeeded' ? 'var(--accent-success)' : (s.lastStatus === 'failed' ? 'var(--accent-danger)' : (s.lastStatus === 'skipped' ? 'var(--accent-warning)' : 'var(--text-muted)'));
|
|
490
|
+
|
|
491
|
+
// Target status checks (disabled or missing targets)
|
|
492
|
+
let isTargetDisabled = false;
|
|
493
|
+
let isTargetMissing = false;
|
|
494
|
+
if (s.targetType === 'function' && (cronState.functionsList || []).length > 0) {
|
|
495
|
+
const fn = cronState.functionsList.find(f => f.id === s.targetId || f.name === s.targetId);
|
|
496
|
+
if (!fn) isTargetMissing = true;
|
|
497
|
+
else if (fn.enabled === false) isTargetDisabled = true;
|
|
498
|
+
} else if (s.targetType === 'flow' && (cronState.flowsList || []).length > 0) {
|
|
499
|
+
const fl = cronState.flowsList.find(f => f.id === s.targetId || f.name === s.targetId);
|
|
500
|
+
if (!fl) isTargetMissing = true;
|
|
501
|
+
else if (fl.enabled === false) isTargetDisabled = true;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
const nextRunStr = s.nextRunAt ? formatDualTime(s.nextRunAt, s.timezone) : 'Disabled / Never';
|
|
505
|
+
const lastRunStr = s.lastRunAt ? formatRelativeTime(s.lastRunAt) : 'Never executed';
|
|
506
|
+
|
|
507
|
+
return `
|
|
508
|
+
<div style="background: var(--bg-card); border: 1px solid ${isUnsupportedSnapshot ? 'rgba(245,158,11,0.4)' : (isTargetMissing ? 'rgba(239,68,68,0.3)' : 'var(--border-subtle)')}; border-radius: 8px; padding: 16px; display: flex; flex-direction: column; justify-content: space-between; position: relative; transition: border-color 0.15s ease;">
|
|
509
|
+
<div>
|
|
510
|
+
<div style="display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; margin-bottom: 6px;">
|
|
511
|
+
<div>
|
|
512
|
+
<div style="display: flex; align-items: center; gap: 6px; flex-wrap: wrap;">
|
|
513
|
+
<span style="font-size: 14px; font-weight: 600; color: var(--text-heading);">${escapeHtml(s.name)}</span>
|
|
514
|
+
${s.isSystemBound ? `<span class="badge" style="font-size: 10px; background: rgba(59,130,246,0.12); color: var(--accent-primary);">1:1 Bound</span>` : ''}
|
|
515
|
+
${isUnsupportedSnapshot ? `<span class="badge" style="font-size: 10px; background: rgba(245,158,11,0.15); color: var(--accent-warning);">${cronIcons.alert(10, 10)} Unsupported on ${dbProviderName}</span>` : ''}
|
|
516
|
+
${isTargetDisabled ? `<span class="badge" style="font-size: 10px; background: rgba(245,158,11,0.15); color: var(--accent-warning);" title="Target is currently disabled">${cronIcons.alert(10, 10)} Target Disabled</span>` : ''}
|
|
517
|
+
${isTargetMissing ? `<span class="badge" style="font-size: 10px; background: rgba(239,68,68,0.15); color: var(--accent-danger);" title="Target ID not found in system">${cronIcons.alert(10, 10)} Target Missing</span>` : ''}
|
|
518
|
+
</div>
|
|
519
|
+
${s.description ? `<p style="font-size: 12px; color: var(--text-muted); margin: 3px 0 0 0;">${escapeHtml(s.description)}</p>` : ''}
|
|
520
|
+
</div>
|
|
521
|
+
<label class="switch" style="transform: scale(0.8); transform-origin: right center;">
|
|
522
|
+
<input type="checkbox" ${isEnabled ? 'checked' : ''} ${isUnsupportedSnapshot ? 'disabled title="Snapshots cannot be enabled on ' + dbProviderName + '"' : 'onchange="handleToggleScheduleEnabled(\'' + s.id + '\', this.checked)" title="Enable / Disable Schedule"'}>
|
|
523
|
+
<span class="slider round"></span>
|
|
524
|
+
</label>
|
|
525
|
+
</div>
|
|
526
|
+
|
|
527
|
+
<!-- Human-Readable Cadence Display -->
|
|
528
|
+
<div style="font-size: 12px; font-weight: 500; color: var(--text-heading); margin: 6px 0 8px 0; display: flex; align-items: center; gap: 6px;">
|
|
529
|
+
${cronIcons.calendar(13, 13)}
|
|
530
|
+
<span>${escapeHtml(s.humanDescription || s.cronExpression)}</span>
|
|
531
|
+
</div>
|
|
532
|
+
|
|
533
|
+
<div style="display: flex; align-items: center; gap: 8px; margin: 8px 0 10px 0; font-size: 11px; flex-wrap: wrap;">
|
|
534
|
+
<span style="display: inline-flex; align-items: center; gap: 4px; padding: 2px 7px; border-radius: 4px; background: var(--bg-card-hover); border: 1px solid var(--border-subtle); cursor: pointer; transition: background 0.15s ease;"
|
|
535
|
+
onclick="navigateToCronTarget('${escapeHtml(s.targetType)}', '${escapeHtml(s.targetId)}')"
|
|
536
|
+
title="Click to view ${escapeHtml(s.targetType)}">
|
|
537
|
+
<span style="width: 6px; height: 6px; border-radius: 50%; background: ${targetBadgeColor};"></span>
|
|
538
|
+
<strong>${escapeHtml(s.targetType.toUpperCase())}:</strong> ${escapeHtml(getTargetDisplayName(s.targetType, s.targetId))} ${cronIcons.externalLink(10, 10)}
|
|
539
|
+
</span>
|
|
540
|
+
<span style="display: inline-flex; align-items: center; gap: 4px; padding: 2px 7px; border-radius: 4px; background: var(--bg-card-hover); border: 1px solid var(--border-subtle); font-family: monospace;" title="Cron expression: ${escapeHtml(s.cronExpression)}">
|
|
541
|
+
${cronIcons.clock(11, 11)} ${escapeHtml(s.cronExpression)}
|
|
542
|
+
</span>
|
|
543
|
+
<span style="display: inline-flex; align-items: center; gap: 4px; color: var(--text-muted);">
|
|
544
|
+
${cronIcons.globe(11, 11)} ${escapeHtml(s.timezone || 'UTC')}
|
|
545
|
+
</span>
|
|
546
|
+
</div>
|
|
547
|
+
|
|
548
|
+
${(s.maxRetries > 0 || s.autoPauseAfterFailures > 0 || s.consecutiveFailures > 0 || s.expiresAt || (s.targetType === 'snapshot' && s.keepLast)) ? `
|
|
549
|
+
<div style="display: flex; align-items: center; gap: 6px; margin: 0 0 10px 0; font-size: 10px; flex-wrap: wrap;">
|
|
550
|
+
${s.maxRetries > 0 ? `<span class="badge" style="background: rgba(99,102,241,0.12); color: #6366f1; display: inline-flex; align-items: center; gap: 3px;" title="Retries up to ${s.maxRetries} times with exponential backoff on failure">${cronIcons.refresh(10, 10)} ${s.maxRetries}x Retries</span>` : ''}
|
|
551
|
+
${s.autoPauseAfterFailures > 0 ? `<span class="badge" style="background: rgba(234,88,12,0.12); color: #ea580c; display: inline-flex; align-items: center; gap: 3px;" title="Circuit breaker: auto-pauses after ${s.autoPauseAfterFailures} consecutive failures">${cronIcons.zap(10, 10)} Auto-pause (${s.autoPauseAfterFailures}x)</span>` : ''}
|
|
552
|
+
${s.consecutiveFailures > 0 ? `<span class="badge" style="background: rgba(239,68,68,0.15); color: var(--accent-danger); display: inline-flex; align-items: center; gap: 3px;" title="${s.consecutiveFailures} consecutive failure(s)">${cronIcons.alert(10, 10)} ${s.consecutiveFailures} Failure${s.consecutiveFailures > 1 ? 's' : ''}</span>` : ''}
|
|
553
|
+
${s.expiresAt ? (() => {
|
|
554
|
+
const isPast = new Date(s.expiresAt).getTime() <= Date.now();
|
|
555
|
+
return `<span class="badge" style="background: ${isPast ? 'rgba(239,68,68,0.15)' : 'rgba(107,114,128,0.15)'}; color: ${isPast ? 'var(--accent-danger)' : 'var(--text-muted)'}; display: inline-flex; align-items: center; gap: 3px;" title="Auto-expires at ${escapeHtml(s.expiresAt)}">${cronIcons.hourglass(10, 10)} ${isPast ? `Expired (${formatRelativeTime(s.expiresAt)})` : `Expires: ${formatRelativeTime(s.expiresAt)}`}</span>`;
|
|
556
|
+
})() : ''}
|
|
557
|
+
${s.targetType === 'snapshot' && s.keepLast ? `<span class="badge" style="background: rgba(16,185,129,0.12); color: #10b981; display: inline-flex; align-items: center; gap: 3px;" title="Retains last ${s.keepLast} snapshots created by this schedule">${cronIcons.database(10, 10)} Retain ${s.keepLast}</span>` : ''}
|
|
558
|
+
</div>
|
|
559
|
+
` : ''}
|
|
560
|
+
|
|
561
|
+
${isUnsupportedSnapshot ? `
|
|
562
|
+
<div style="background: rgba(245,158,11,0.08); border: 1px solid rgba(245,158,11,0.25); border-radius: 6px; padding: 7px 10px; font-size: 11px; color: var(--accent-warning); margin-bottom: 10px; display: flex; align-items: center; gap: 6px;">
|
|
563
|
+
${cronIcons.alert(14, 14)}
|
|
564
|
+
<span>In-app snapshots are exclusive to embedded SQLite applications and are paused on <strong>${dbProviderName}</strong>.</span>
|
|
565
|
+
</div>
|
|
566
|
+
` : ''}
|
|
567
|
+
|
|
568
|
+
<div style="background: var(--bg-page); border-radius: 6px; padding: 8px 10px; font-size: 11px; margin-bottom: 12px; border: 1px solid var(--border-subtle);">
|
|
569
|
+
<div style="display: flex; justify-content: space-between; margin-bottom: 4px;">
|
|
570
|
+
<span style="color: var(--text-muted);">Next Run:</span>
|
|
571
|
+
<span style="font-weight: 500; color: ${isEnabled && !isUnsupportedSnapshot ? 'var(--text-heading)' : 'var(--text-muted)'};">${isUnsupportedSnapshot ? 'Paused (Requires SQLite)' : nextRunStr}</span>
|
|
572
|
+
</div>
|
|
573
|
+
<div style="display: flex; justify-content: space-between;">
|
|
574
|
+
<span style="color: var(--text-muted);">Last Run:</span>
|
|
575
|
+
<span style="color: ${lastStatusColor}; font-weight: 500;">
|
|
576
|
+
${s.lastStatus ? `● ${escapeHtml(s.lastStatus)} (${lastRunStr})` : lastRunStr}
|
|
577
|
+
</span>
|
|
578
|
+
</div>
|
|
579
|
+
${s.lastError ? `
|
|
580
|
+
<div style="color: var(--accent-danger); margin-top: 4px; font-size: 10px; word-break: break-all; display: flex; align-items: flex-start; gap: 4px;">
|
|
581
|
+
${cronIcons.alert(11, 11)} <span>${escapeHtml(s.lastError)}</span>
|
|
582
|
+
</div>
|
|
583
|
+
` : ''}
|
|
584
|
+
</div>
|
|
585
|
+
</div>
|
|
586
|
+
|
|
587
|
+
<div style="display: flex; align-items: center; justify-content: space-between; gap: 6px; border-top: 1px solid var(--border-subtle); padding-top: 10px;">
|
|
588
|
+
<span style="font-size: 11px; color: var(--text-muted);">
|
|
589
|
+
Policy: <code style="font-size: 10px;">${escapeHtml(s.missedRunPolicy || 'skip')}</code>
|
|
590
|
+
</span>
|
|
591
|
+
<div style="display: flex; gap: 6px;">
|
|
592
|
+
<button class="btn btn-secondary btn-sm" onclick="viewScheduleHistory('${s.id}')" title="View execution history logs for this schedule" style="font-size: 11px; padding: 2px 8px;">
|
|
593
|
+
History
|
|
594
|
+
</button>
|
|
595
|
+
<button class="btn btn-secondary btn-sm" ${isUnsupportedSnapshot ? 'disabled' : ''} onclick="handleTriggerSchedule('${s.id}')" title="${isUnsupportedSnapshot ? 'Snapshots are unsupported on ' + dbProviderName : 'Trigger immediately on-demand'}" style="font-size: 11px; padding: 2px 8px; display: inline-flex; align-items: center; gap: 4px;">
|
|
596
|
+
${cronIcons.play(9, 9)} Run Now
|
|
597
|
+
</button>
|
|
598
|
+
</button>
|
|
599
|
+
<button class="btn btn-secondary btn-sm" onclick="openEditScheduleDrawer('${s.id}')" title="Edit Schedule Configuration" style="font-size: 11px; padding: 2px 8px;">
|
|
600
|
+
Edit
|
|
601
|
+
</button>
|
|
602
|
+
<button class="btn btn-danger btn-sm" onclick="handleDeleteSchedule('${s.id}')" title="Delete Schedule" style="font-size: 11px; padding: 2px 8px;">
|
|
603
|
+
Delete
|
|
604
|
+
</button>
|
|
605
|
+
</div>
|
|
606
|
+
</div>
|
|
607
|
+
</div>
|
|
608
|
+
`;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
async function handleToggleScheduleEnabled(id, enabled) {
|
|
612
|
+
try {
|
|
613
|
+
const apiPrefix = getCronApiPrefix();
|
|
614
|
+
await api(`${apiPrefix}/schedules/${encodeURIComponent(id)}`, {
|
|
615
|
+
method: 'PATCH',
|
|
616
|
+
body: { enabled },
|
|
617
|
+
});
|
|
618
|
+
toast(enabled ? 'Schedule enabled' : 'Schedule disabled', 'success');
|
|
619
|
+
await loadCronData();
|
|
620
|
+
} catch (err) {
|
|
621
|
+
toast(err.message || 'Failed to toggle schedule state', 'error');
|
|
622
|
+
await loadCronData();
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
async function handleTriggerSchedule(id) {
|
|
627
|
+
try {
|
|
628
|
+
toast('Triggering scheduled run...', 'info');
|
|
629
|
+
const apiPrefix = getCronApiPrefix();
|
|
630
|
+
const runLog = await api(`${apiPrefix}/schedules/${encodeURIComponent(id)}/trigger`, {
|
|
631
|
+
method: 'POST',
|
|
632
|
+
});
|
|
633
|
+
if (runLog && runLog.status === 'succeeded') {
|
|
634
|
+
toast(`Schedule executed successfully in ${runLog.durationMs}ms`, 'success');
|
|
635
|
+
} else {
|
|
636
|
+
toast(`Schedule run ended with status '${runLog?.status || 'failed'}': ${runLog?.error || 'Unknown error'}`, 'error');
|
|
637
|
+
}
|
|
638
|
+
await loadCronData();
|
|
639
|
+
} catch (err) {
|
|
640
|
+
toast(err.message || 'Trigger failed', 'error');
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
async function handleDeleteSchedule(id) {
|
|
645
|
+
const schedule = cronState.schedules.find(s => s.id === id);
|
|
646
|
+
const name = schedule ? schedule.name : id;
|
|
647
|
+
if (!confirm(`Are you sure you want to delete schedule "${name}"? This will cancel future runs and delete execution history for this schedule.`)) {
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
try {
|
|
652
|
+
const apiPrefix = getCronApiPrefix();
|
|
653
|
+
await api(`${apiPrefix}/schedules/${encodeURIComponent(id)}`, {
|
|
654
|
+
method: 'DELETE',
|
|
655
|
+
});
|
|
656
|
+
toast(`Schedule "${name}" deleted`, 'success');
|
|
657
|
+
await loadCronData();
|
|
658
|
+
} catch (err) {
|
|
659
|
+
toast(err.message || 'Delete failed', 'error');
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// ==========================================
|
|
664
|
+
// Execution History Tab
|
|
665
|
+
// ==========================================
|
|
666
|
+
|
|
667
|
+
async function loadRunHistory() {
|
|
668
|
+
try {
|
|
669
|
+
const apiPrefix = getCronApiPrefix();
|
|
670
|
+
const filter = cronState.runFilter;
|
|
671
|
+
const params = new URLSearchParams();
|
|
672
|
+
if (filter.scheduleId) params.set('scheduleId', filter.scheduleId);
|
|
673
|
+
if (filter.targetType) params.set('targetType', filter.targetType);
|
|
674
|
+
if (filter.status && filter.status !== 'all') params.set('status', filter.status);
|
|
675
|
+
params.set('page', String(filter.page));
|
|
676
|
+
params.set('perPage', String(filter.perPage));
|
|
677
|
+
|
|
678
|
+
const data = await api(`${apiPrefix}/runs?${params.toString()}`);
|
|
679
|
+
if (data) {
|
|
680
|
+
cronState.runs = data.items || [];
|
|
681
|
+
cronState.runFilter.total = data.total || 0;
|
|
682
|
+
cronState.runFilter.totalPages = Math.ceil((data.total || 0) / filter.perPage) || 1;
|
|
683
|
+
const contentArea = document.getElementById('cron-tab-content');
|
|
684
|
+
if (contentArea && cronState.subTab === 'history') {
|
|
685
|
+
contentArea.innerHTML = renderHistoryTableHtml();
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
} catch (err) {
|
|
689
|
+
console.warn('[Cron] Failed to load runs history:', err);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function renderHistoryTableHtml() {
|
|
694
|
+
const runs = cronState.runs || [];
|
|
695
|
+
const filter = cronState.runFilter;
|
|
696
|
+
|
|
697
|
+
return `
|
|
698
|
+
<div style="background: var(--bg-card); border: 1px solid var(--border-subtle); border-radius: 8px; padding: 14px;">
|
|
699
|
+
<!-- History Filters Bar -->
|
|
700
|
+
<div style="display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; flex-wrap: wrap;">
|
|
701
|
+
<div style="display: flex; align-items: center; gap: 8px; flex-wrap: wrap;">
|
|
702
|
+
<select class="input input-sm" onchange="handleHistoryStatusFilter(this.value)" style="font-size: 11px;">
|
|
703
|
+
<option value="all" ${filter.status === 'all' ? 'selected' : ''}>All Statuses</option>
|
|
704
|
+
<option value="succeeded" ${filter.status === 'succeeded' ? 'selected' : ''}>Succeeded</option>
|
|
705
|
+
<option value="failed" ${filter.status === 'failed' ? 'selected' : ''}>Failed</option>
|
|
706
|
+
<option value="skipped" ${filter.status === 'skipped' ? 'selected' : ''}>Skipped</option>
|
|
707
|
+
<option value="running" ${filter.status === 'running' ? 'selected' : ''}>Running</option>
|
|
708
|
+
</select>
|
|
709
|
+
<select class="input input-sm" onchange="handleHistoryScheduleFilter(this.value)" style="font-size: 11px;">
|
|
710
|
+
<option value="">All Schedules</option>
|
|
711
|
+
${cronState.schedules.map(s => `<option value="${s.id}" ${filter.scheduleId === s.id ? 'selected' : ''}>${escapeHtml(s.name)}</option>`).join('')}
|
|
712
|
+
</select>
|
|
713
|
+
</div>
|
|
714
|
+
<div style="font-size: 11px; color: var(--text-muted);">
|
|
715
|
+
Total: ${filter.total} runs
|
|
716
|
+
</div>
|
|
717
|
+
</div>
|
|
718
|
+
|
|
719
|
+
<!-- History Table -->
|
|
720
|
+
${runs.length === 0 ? `
|
|
721
|
+
<div style="padding: 32px 16px; text-align: center; color: var(--text-muted); font-size: 13px;">
|
|
722
|
+
No execution runs recorded yet.
|
|
723
|
+
</div>
|
|
724
|
+
` : `
|
|
725
|
+
<div style="overflow-x: auto;">
|
|
726
|
+
<table class="table" style="width: 100%; font-size: 12px;">
|
|
727
|
+
<thead>
|
|
728
|
+
<tr>
|
|
729
|
+
<th style="text-align: left;">Timestamp</th>
|
|
730
|
+
<th style="text-align: left;">Schedule</th>
|
|
731
|
+
<th style="text-align: left;">Target</th>
|
|
732
|
+
<th style="text-align: left;">Status</th>
|
|
733
|
+
<th style="text-align: right;">Duration</th>
|
|
734
|
+
<th style="text-align: left;">Details</th>
|
|
735
|
+
<th style="text-align: right;">Action</th>
|
|
736
|
+
</tr>
|
|
737
|
+
</thead>
|
|
738
|
+
<tbody>
|
|
739
|
+
${runs.map(r => renderRunRowHtml(r)).join('')}
|
|
740
|
+
</tbody>
|
|
741
|
+
</table>
|
|
742
|
+
</div>
|
|
743
|
+
|
|
744
|
+
<!-- Pagination -->
|
|
745
|
+
<div style="display: flex; align-items: center; justify-content: space-between; margin-top: 12px; font-size: 11px; color: var(--text-muted);">
|
|
746
|
+
<span>Page ${filter.page} of ${filter.totalPages}</span>
|
|
747
|
+
<div style="display: flex; gap: 4px;">
|
|
748
|
+
<button class="btn btn-secondary btn-sm" ${filter.page <= 1 ? 'disabled' : ''} onclick="handleHistoryPageChange(${filter.page - 1})">Prev</button>
|
|
749
|
+
<button class="btn btn-secondary btn-sm" ${filter.page >= filter.totalPages ? 'disabled' : ''} onclick="handleHistoryPageChange(${filter.page + 1})">Next</button>
|
|
750
|
+
</div>
|
|
751
|
+
</div>
|
|
752
|
+
`}
|
|
753
|
+
</div>
|
|
754
|
+
`;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
function renderRunRowHtml(r) {
|
|
758
|
+
const statusBadge = r.status === 'succeeded'
|
|
759
|
+
? '<span class="badge" style="background: rgba(16,185,129,0.15); color: var(--accent-success);">Succeeded</span>'
|
|
760
|
+
: (r.status === 'failed'
|
|
761
|
+
? '<span class="badge" style="background: rgba(239,68,68,0.15); color: var(--accent-danger);">Failed</span>'
|
|
762
|
+
: (r.status === 'skipped'
|
|
763
|
+
? '<span class="badge" style="background: rgba(245,158,11,0.15); color: var(--accent-warning);">Skipped</span>'
|
|
764
|
+
: '<span class="badge" style="background: rgba(59,130,246,0.15); color: var(--accent-primary);">Running</span>'));
|
|
765
|
+
|
|
766
|
+
return `
|
|
767
|
+
<tr style="border-bottom: 1px solid var(--border-subtle); cursor: pointer;" onclick="openRunDetailDrawer('${r.id}')" title="Click to view run details">
|
|
768
|
+
<td style="white-space: nowrap; font-family: monospace;">${formatDateTime(r.startedAt)}</td>
|
|
769
|
+
<td>
|
|
770
|
+
<strong>${escapeHtml(r.scheduleName)}</strong>
|
|
771
|
+
${r.isCatchUp ? '<span class="badge" style="font-size: 9px; margin-left: 4px; background: rgba(139,92,246,0.15); color: #8b5cf6;">Catch-Up</span>' : ''}
|
|
772
|
+
${r.attempt && r.attempt > 1 ? `<span class="badge" style="font-size: 9px; margin-left: 4px; background: rgba(99,102,241,0.15); color: #6366f1;" title="Execution completed on attempt #${r.attempt}">Retry #${r.attempt}</span>` : ''}
|
|
773
|
+
</td>
|
|
774
|
+
<td>
|
|
775
|
+
<span style="font-size: 11px; padding: 1px 5px; border-radius: 3px; background: var(--bg-card-hover);" title="${r.targetId ? `Target ID: ${escapeHtml(r.targetId)}` : ''}">
|
|
776
|
+
${escapeHtml(r.targetType)}: <strong>${escapeHtml(getTargetDisplayName(r.targetType, r.targetId))}</strong>
|
|
777
|
+
</span>
|
|
778
|
+
</td>
|
|
779
|
+
<td>${statusBadge}</td>
|
|
780
|
+
<td style="text-align: right; font-family: monospace;">${r.durationMs || 0}ms</td>
|
|
781
|
+
<td style="max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
|
|
782
|
+
${r.error ? `<span style="color: var(--accent-danger);" title="${escapeHtml(r.error)}">${cronIcons.alert(11, 11)} ${escapeHtml(r.error)}</span>` : (r.output ? `<code style="font-size: 10px;" title="${escapeHtml(r.output)}">${escapeHtml(r.output)}</code>` : '<span style="color: var(--text-muted);">-</span>')}
|
|
783
|
+
</td>
|
|
784
|
+
<td style="text-align: right; white-space: nowrap;">
|
|
785
|
+
<button class="btn btn-secondary btn-sm" onclick="event.stopPropagation(); openRunDetailDrawer('${r.id}')" style="font-size: 10px; padding: 1px 7px;" title="Inspect output and error details">
|
|
786
|
+
Inspect
|
|
787
|
+
</button>
|
|
788
|
+
</td>
|
|
789
|
+
</tr>
|
|
790
|
+
`;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
function handleHistoryStatusFilter(status) {
|
|
794
|
+
cronState.runFilter.status = status;
|
|
795
|
+
cronState.runFilter.page = 1;
|
|
796
|
+
loadRunHistory();
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
function handleHistoryScheduleFilter(scheduleId) {
|
|
800
|
+
cronState.runFilter.scheduleId = scheduleId;
|
|
801
|
+
cronState.runFilter.page = 1;
|
|
802
|
+
const btn = document.getElementById('btn-clear-cron-history');
|
|
803
|
+
if (btn) {
|
|
804
|
+
btn.textContent = scheduleId ? 'Clear Schedule History' : 'Clear All History';
|
|
805
|
+
}
|
|
806
|
+
loadRunHistory();
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
function handleHistoryPageChange(newPage) {
|
|
810
|
+
cronState.runFilter.page = Math.max(1, newPage);
|
|
811
|
+
loadRunHistory();
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
async function handleClearRunHistory() {
|
|
815
|
+
const scheduleId = cronState.runFilter.scheduleId;
|
|
816
|
+
const targetSched = scheduleId ? (cronState.schedules || []).find(s => s.id === scheduleId) : null;
|
|
817
|
+
const schedName = targetSched ? targetSched.name : scheduleId;
|
|
818
|
+
|
|
819
|
+
const confirmMsg = targetSched
|
|
820
|
+
? `Are you sure you want to clear execution history for schedule "${schedName}"?`
|
|
821
|
+
: (scheduleId
|
|
822
|
+
? 'Are you sure you want to clear execution history for the selected schedule?'
|
|
823
|
+
: 'Are you sure you want to clear all execution run history logs across ALL schedules?');
|
|
824
|
+
|
|
825
|
+
if (!confirm(confirmMsg)) {
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
try {
|
|
829
|
+
const apiPrefix = getCronApiPrefix();
|
|
830
|
+
const url = scheduleId
|
|
831
|
+
? `${apiPrefix}/runs?scheduleId=${encodeURIComponent(scheduleId)}`
|
|
832
|
+
: `${apiPrefix}/runs`;
|
|
833
|
+
await api(url, {
|
|
834
|
+
method: 'DELETE',
|
|
835
|
+
});
|
|
836
|
+
toast(targetSched ? `Run history for "${schedName}" cleared` : 'Run history cleared', 'success');
|
|
837
|
+
await loadCronData();
|
|
838
|
+
} catch (err) {
|
|
839
|
+
toast(err.message || 'Failed to clear run history', 'error');
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// ==========================================
|
|
844
|
+
// Slide-Over Drawer: Execution Run Detail Inspector
|
|
845
|
+
// ==========================================
|
|
846
|
+
|
|
847
|
+
function openRunDetailDrawer(runId) {
|
|
848
|
+
const run = (cronState.runs || []).find(r => r.id === runId);
|
|
849
|
+
if (!run) return;
|
|
850
|
+
cronState.activeRunForDetail = run;
|
|
851
|
+
renderRunDetailDrawer();
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
function renderRunDetailDrawer() {
|
|
855
|
+
const r = cronState.activeRunForDetail;
|
|
856
|
+
if (!r) return;
|
|
857
|
+
|
|
858
|
+
let root = document.getElementById('drawer-root');
|
|
859
|
+
if (!root) {
|
|
860
|
+
root = document.createElement('div');
|
|
861
|
+
root.id = 'drawer-root';
|
|
862
|
+
document.body.appendChild(root);
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
let formattedOutput = '';
|
|
866
|
+
if (r.output) {
|
|
867
|
+
try {
|
|
868
|
+
formattedOutput = JSON.stringify(JSON.parse(r.output), null, 2);
|
|
869
|
+
} catch {
|
|
870
|
+
formattedOutput = r.output;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// Calculate jitter/latency between scheduledAt and startedAt
|
|
875
|
+
let latencyStr = '';
|
|
876
|
+
if (r.scheduledAt && r.startedAt) {
|
|
877
|
+
const diff = new Date(r.startedAt).getTime() - new Date(r.scheduledAt).getTime();
|
|
878
|
+
if (r.isCatchUp) {
|
|
879
|
+
latencyStr = `Catch-up execution (${Math.round(diff / 1000)}s delayed)`;
|
|
880
|
+
} else if (Math.abs(diff) < 1000) {
|
|
881
|
+
latencyStr = `${diff}ms jitter (on-time)`;
|
|
882
|
+
} else {
|
|
883
|
+
latencyStr = `${Math.round(diff / 1000)}s dispatch delay`;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
const statusBadge = r.status === 'succeeded'
|
|
888
|
+
? '<span class="badge" style="background: rgba(16,185,129,0.15); color: var(--accent-success); font-size: 11px;">Succeeded</span>'
|
|
889
|
+
: (r.status === 'failed'
|
|
890
|
+
? '<span class="badge" style="background: rgba(239,68,68,0.15); color: var(--accent-danger); font-size: 11px;">Failed</span>'
|
|
891
|
+
: (r.status === 'skipped'
|
|
892
|
+
? '<span class="badge" style="background: rgba(245,158,11,0.15); color: var(--accent-warning); font-size: 11px;">Skipped</span>'
|
|
893
|
+
: '<span class="badge" style="background: rgba(59,130,246,0.15); color: var(--accent-primary); font-size: 11px;">Running</span>'));
|
|
894
|
+
|
|
895
|
+
root.innerHTML = `
|
|
896
|
+
<div id="run-detail-backdrop" class="drawer-backdrop" onclick="closeRunDetailDrawer()" aria-hidden="true"></div>
|
|
897
|
+
<aside id="run-detail-panel" class="schedule-drawer" role="dialog" aria-modal="true" aria-labelledby="run-detail-title" tabindex="-1">
|
|
898
|
+
<div class="drawer-header">
|
|
899
|
+
<div class="drawer-title-area">
|
|
900
|
+
<div style="width: 34px; height: 34px; border-radius: 8px; background: rgba(59,130,246,0.12); color: var(--accent-primary); display: flex; align-items: center; justify-content: center; font-size: 17px; flex-shrink: 0;">
|
|
901
|
+
${cronIcons.chart(18, 18)}
|
|
902
|
+
</div>
|
|
903
|
+
<div>
|
|
904
|
+
<div style="display: flex; align-items: center; gap: 8px;">
|
|
905
|
+
<h3 id="run-detail-title" class="drawer-title" style="margin: 0; font-size: 15px;">Run Details</h3>
|
|
906
|
+
${statusBadge}
|
|
907
|
+
</div>
|
|
908
|
+
<span style="font-size: 11px; color: var(--text-muted);">${escapeHtml(r.scheduleName)} • ${formatDateTime(r.startedAt)}</span>
|
|
909
|
+
</div>
|
|
910
|
+
</div>
|
|
911
|
+
<button type="button" class="btn btn-secondary btn-sm" onclick="closeRunDetailDrawer()" style="border: none; font-size: 18px; line-height: 1; cursor: pointer;" title="Close (Esc)">✕</button>
|
|
912
|
+
</div>
|
|
913
|
+
|
|
914
|
+
<div class="drawer-body" style="flex: 1; overflow-y: auto; padding: 1.25rem 1.5rem; display: flex; flex-direction: column; gap: 1rem;">
|
|
915
|
+
<!-- Metadata Summary Card -->
|
|
916
|
+
<div class="drawer-card">
|
|
917
|
+
<span class="drawer-section-title">Execution Summary</span>
|
|
918
|
+
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; font-size: 12px;">
|
|
919
|
+
<div>
|
|
920
|
+
<span style="color: var(--text-muted); display: block; font-size: 11px;">Schedule</span>
|
|
921
|
+
<strong style="color: var(--text-heading);">${escapeHtml(r.scheduleName)}</strong>
|
|
922
|
+
</div>
|
|
923
|
+
<div>
|
|
924
|
+
<span style="color: var(--text-muted); display: block; font-size: 11px;">Target</span>
|
|
925
|
+
<strong>${escapeHtml(r.targetType.toUpperCase())}:</strong> ${escapeHtml(getTargetDisplayName(r.targetType, r.targetId))}
|
|
926
|
+
</div>
|
|
927
|
+
<div>
|
|
928
|
+
<span style="color: var(--text-muted); display: block; font-size: 11px;">Started At</span>
|
|
929
|
+
<span style="font-family: monospace;">${formatDateTime(r.startedAt)}</span>
|
|
930
|
+
</div>
|
|
931
|
+
<div>
|
|
932
|
+
<span style="color: var(--text-muted); display: block; font-size: 11px;">Duration</span>
|
|
933
|
+
<span style="font-family: monospace; font-weight: 600;">${r.durationMs || 0}ms</span>
|
|
934
|
+
</div>
|
|
935
|
+
<div>
|
|
936
|
+
<span style="color: var(--text-muted); display: block; font-size: 11px;">Attempt Count</span>
|
|
937
|
+
<span style="font-family: monospace; font-weight: 600;">${r.attempt || 1}</span>
|
|
938
|
+
</div>
|
|
939
|
+
<div>
|
|
940
|
+
<span style="color: var(--text-muted); display: block; font-size: 11px;">Scheduled At</span>
|
|
941
|
+
<span style="font-family: monospace;">${formatDateTime(r.scheduledAt)}</span>
|
|
942
|
+
</div>
|
|
943
|
+
<div>
|
|
944
|
+
<span style="color: var(--text-muted); display: block; font-size: 11px;">Dispatch Accuracy</span>
|
|
945
|
+
<span style="color: ${r.isCatchUp ? 'var(--accent-warning)' : 'var(--accent-success)'}; font-size: 11px; font-weight: 500;">${escapeHtml(latencyStr || 'On-time')}</span>
|
|
946
|
+
</div>
|
|
947
|
+
${r.executionRefId ? `
|
|
948
|
+
<div style="grid-column: span 2;">
|
|
949
|
+
<span style="color: var(--text-muted); display: block; font-size: 11px;">Execution Reference ID</span>
|
|
950
|
+
<code style="font-size: 11px; word-break: break-all;">${escapeHtml(r.executionRefId)}</code>
|
|
951
|
+
</div>
|
|
952
|
+
` : ''}
|
|
953
|
+
</div>
|
|
954
|
+
</div>
|
|
955
|
+
|
|
956
|
+
<!-- Error Card (if failed or error) -->
|
|
957
|
+
${r.error ? `
|
|
958
|
+
<div class="drawer-card" style="border-left: 3px solid var(--accent-danger);">
|
|
959
|
+
<span class="drawer-section-title" style="color: var(--accent-danger); margin-bottom: 6px;">Error Details</span>
|
|
960
|
+
<div style="background: rgba(239,68,68,0.06); border: 1px solid rgba(239,68,68,0.2); border-radius: 6px; padding: 10px; font-family: monospace; font-size: 11px; color: var(--accent-danger); white-space: pre-wrap; word-break: break-word;">
|
|
961
|
+
${escapeHtml(r.error)}
|
|
962
|
+
</div>
|
|
963
|
+
</div>
|
|
964
|
+
` : ''}
|
|
965
|
+
|
|
966
|
+
<!-- Output Card (if output) -->
|
|
967
|
+
${formattedOutput ? `
|
|
968
|
+
<div class="drawer-card" style="flex: 1; display: flex; flex-direction: column;">
|
|
969
|
+
<div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px;">
|
|
970
|
+
<span class="drawer-section-title" style="margin-bottom: 0;">Output Data</span>
|
|
971
|
+
<button class="btn btn-secondary btn-sm" onclick="copyRunOutputToClipboard()" style="font-size: 10px; padding: 2px 7px; display: inline-flex; align-items: center; gap: 4px;">
|
|
972
|
+
${cronIcons.copy(11, 11)} Copy Output
|
|
973
|
+
</button>
|
|
974
|
+
</div>
|
|
975
|
+
<pre id="run-output-pre" style="background: var(--bg-page); border: 1px solid var(--border-subtle); border-radius: 6px; padding: 10px; font-size: 11px; font-family: monospace; overflow: auto; max-height: 280px; margin: 0; white-space: pre-wrap; word-break: break-word;">${escapeHtml(formattedOutput)}</pre>
|
|
976
|
+
</div>
|
|
977
|
+
` : `
|
|
978
|
+
<div class="drawer-card">
|
|
979
|
+
<span class="drawer-section-title">Output Data</span>
|
|
980
|
+
<span style="color: var(--text-muted); font-size: 12px;">No output payload recorded for this execution.</span>
|
|
981
|
+
</div>
|
|
982
|
+
`}
|
|
983
|
+
</div>
|
|
984
|
+
|
|
985
|
+
<div class="drawer-footer" style="padding: 1rem 1.5rem; border-top: 1px solid var(--border-subtle); display: flex; justify-content: space-between; align-items: center; background: var(--bg-card); flex-shrink: 0;">
|
|
986
|
+
<code style="font-size: 10px; color: var(--text-muted);">Run ID: ${escapeHtml(r.id)}</code>
|
|
987
|
+
<button type="button" class="btn btn-secondary btn-sm" onclick="closeRunDetailDrawer()">Close</button>
|
|
988
|
+
</div>
|
|
989
|
+
</aside>
|
|
990
|
+
`;
|
|
991
|
+
|
|
992
|
+
// Trigger animation
|
|
993
|
+
const triggerDrawerOpen = () => {
|
|
994
|
+
const backdrop = document.getElementById('run-detail-backdrop');
|
|
995
|
+
const panel = document.getElementById('run-detail-panel');
|
|
996
|
+
if (backdrop) backdrop.classList.add('open');
|
|
997
|
+
if (panel) panel.classList.add('open');
|
|
998
|
+
};
|
|
999
|
+
if (typeof requestAnimationFrame === 'function') {
|
|
1000
|
+
requestAnimationFrame(triggerDrawerOpen);
|
|
1001
|
+
} else {
|
|
1002
|
+
triggerDrawerOpen();
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
window.removeEventListener('keydown', handleRunDetailDrawerKeyDown);
|
|
1006
|
+
window.addEventListener('keydown', handleRunDetailDrawerKeyDown);
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
function closeRunDetailDrawer() {
|
|
1010
|
+
const backdrop = document.getElementById('run-detail-backdrop');
|
|
1011
|
+
const panel = document.getElementById('run-detail-panel');
|
|
1012
|
+
if (backdrop) backdrop.classList.remove('open');
|
|
1013
|
+
if (panel) panel.classList.remove('open');
|
|
1014
|
+
|
|
1015
|
+
window.removeEventListener('keydown', handleRunDetailDrawerKeyDown);
|
|
1016
|
+
cronState.activeRunForDetail = null;
|
|
1017
|
+
|
|
1018
|
+
setTimeout(() => {
|
|
1019
|
+
const root = document.getElementById('drawer-root');
|
|
1020
|
+
if (root && root.querySelector('#run-detail-panel')) {
|
|
1021
|
+
root.innerHTML = '';
|
|
1022
|
+
}
|
|
1023
|
+
}, 280);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
function handleRunDetailDrawerKeyDown(e) {
|
|
1027
|
+
if (e.key === 'Escape' && document.getElementById('run-detail-backdrop')) {
|
|
1028
|
+
closeRunDetailDrawer();
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
function copyRunOutputToClipboard() {
|
|
1033
|
+
const pre = document.getElementById('run-output-pre');
|
|
1034
|
+
if (!pre) return;
|
|
1035
|
+
if (navigator && navigator.clipboard && navigator.clipboard.writeText) {
|
|
1036
|
+
navigator.clipboard.writeText(pre.textContent || '').then(() => {
|
|
1037
|
+
toast('Output copied to clipboard', 'success');
|
|
1038
|
+
}).catch(() => {
|
|
1039
|
+
toast('Failed to copy', 'error');
|
|
1040
|
+
});
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
// ==========================================
|
|
1045
|
+
// Slide-Over Drawer: Create / Edit Schedule & Live Preview
|
|
1046
|
+
// ==========================================
|
|
1047
|
+
|
|
1048
|
+
function openCreateScheduleDrawer(targetType = 'function', targetId = '') {
|
|
1049
|
+
if (!isSqliteDatabase() && targetType === 'snapshot') {
|
|
1050
|
+
targetType = 'function';
|
|
1051
|
+
targetId = '';
|
|
1052
|
+
}
|
|
1053
|
+
cronState.activeScheduleForEdit = null;
|
|
1054
|
+
cronState.createTargetType = targetType;
|
|
1055
|
+
cronState.createTargetId = targetId;
|
|
1056
|
+
renderScheduleDrawer();
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
function openEditScheduleDrawer(id) {
|
|
1060
|
+
const schedule = cronState.schedules.find(s => s.id === id);
|
|
1061
|
+
if (!schedule) return;
|
|
1062
|
+
cronState.activeScheduleForEdit = schedule;
|
|
1063
|
+
cronState.createTargetType = null;
|
|
1064
|
+
cronState.createTargetId = null;
|
|
1065
|
+
renderScheduleDrawer();
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
function closeScheduleDrawer() {
|
|
1069
|
+
const backdrop = document.getElementById('schedule-drawer-backdrop');
|
|
1070
|
+
const panel = document.getElementById('schedule-drawer-panel');
|
|
1071
|
+
if (backdrop) backdrop.classList.remove('open');
|
|
1072
|
+
if (panel) panel.classList.remove('open');
|
|
1073
|
+
|
|
1074
|
+
window.removeEventListener('keydown', handleScheduleDrawerKeyDown);
|
|
1075
|
+
cronState.activeScheduleForEdit = null;
|
|
1076
|
+
cronState.createTargetType = null;
|
|
1077
|
+
cronState.createTargetId = null;
|
|
1078
|
+
|
|
1079
|
+
setTimeout(() => {
|
|
1080
|
+
const root = document.getElementById('drawer-root');
|
|
1081
|
+
if (root && root.querySelector('#schedule-drawer-panel')) {
|
|
1082
|
+
root.innerHTML = '';
|
|
1083
|
+
}
|
|
1084
|
+
const modalRoot = document.getElementById('modal-root');
|
|
1085
|
+
if (modalRoot && modalRoot.querySelector('#schedule-drawer-panel')) {
|
|
1086
|
+
modalRoot.innerHTML = '';
|
|
1087
|
+
}
|
|
1088
|
+
}, 280);
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
function handleScheduleDrawerKeyDown(e) {
|
|
1092
|
+
if (e.key === 'Escape' && document.getElementById('schedule-drawer-backdrop')) {
|
|
1093
|
+
closeScheduleDrawer();
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
// Aliases for compatibility
|
|
1098
|
+
const openCreateScheduleModal = openCreateScheduleDrawer;
|
|
1099
|
+
const openEditScheduleModal = openEditScheduleDrawer;
|
|
1100
|
+
const closeScheduleModal = closeScheduleDrawer;
|
|
1101
|
+
|
|
1102
|
+
function renderScheduleDrawer() {
|
|
1103
|
+
let root = document.getElementById('drawer-root');
|
|
1104
|
+
if (!root) {
|
|
1105
|
+
root = document.createElement('div');
|
|
1106
|
+
root.id = 'drawer-root';
|
|
1107
|
+
document.body.appendChild(root);
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
const isEdit = Boolean(cronState.activeScheduleForEdit);
|
|
1111
|
+
const isSqlite = isSqliteDatabase();
|
|
1112
|
+
const dbProviderName = (typeof databaseState !== 'undefined' && databaseState.status?.provider ? databaseState.status.provider : 'remote').toUpperCase();
|
|
1113
|
+
let defaultTargetType = cronState.createTargetType || 'function';
|
|
1114
|
+
if (!isSqlite && defaultTargetType === 'snapshot') {
|
|
1115
|
+
defaultTargetType = 'function';
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
const s = cronState.activeScheduleForEdit || {
|
|
1119
|
+
name: '',
|
|
1120
|
+
targetType: defaultTargetType,
|
|
1121
|
+
targetId: cronState.createTargetId || '',
|
|
1122
|
+
cronExpression: '0 9 * * 1-5',
|
|
1123
|
+
timezone: 'UTC',
|
|
1124
|
+
missedRunPolicy: defaultTargetType === 'snapshot' ? 'catch_up_once' : 'skip',
|
|
1125
|
+
payload: null,
|
|
1126
|
+
enabled: true,
|
|
1127
|
+
};
|
|
1128
|
+
if (!isEdit && !isSqlite && s.targetType === 'snapshot') {
|
|
1129
|
+
s.targetType = 'function';
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
const payloadStr = s.payload !== undefined && s.payload !== null
|
|
1133
|
+
? (typeof s.payload === 'string' ? s.payload : JSON.stringify(s.payload, null, 2))
|
|
1134
|
+
: '';
|
|
1135
|
+
|
|
1136
|
+
root.innerHTML = `
|
|
1137
|
+
<div id="schedule-drawer-backdrop" class="drawer-backdrop" onclick="closeScheduleDrawer()" aria-hidden="true"></div>
|
|
1138
|
+
<aside id="schedule-drawer-panel" class="schedule-drawer" role="dialog" aria-modal="true" aria-labelledby="schedule-drawer-title" tabindex="-1">
|
|
1139
|
+
<!-- Drawer Header -->
|
|
1140
|
+
<div class="drawer-header">
|
|
1141
|
+
<div class="drawer-title-area">
|
|
1142
|
+
<div style="width: 34px; height: 34px; border-radius: 8px; background: rgba(59,130,246,0.12); color: var(--accent-primary); display: flex; align-items: center; justify-content: center; font-size: 17px; flex-shrink: 0;">
|
|
1143
|
+
${cronIcons.clock(18, 18)}
|
|
1144
|
+
</div>
|
|
1145
|
+
<div>
|
|
1146
|
+
<h3 id="schedule-drawer-title" class="drawer-title" style="margin: 0; font-size: 15px;">${isEdit ? 'Edit Schedule' : 'Create New Schedule'}</h3>
|
|
1147
|
+
<span style="font-size: 11px; color: var(--text-muted);">${isEdit ? 'Modify scheduled trigger parameters and policies' : (isSqlite ? 'Configure an automated cron schedule for Functions, Flows, or Snapshots' : 'Configure an automated cron schedule for Functions or Flows')}</span>
|
|
1148
|
+
</div>
|
|
1149
|
+
</div>
|
|
1150
|
+
<button type="button" class="btn btn-secondary btn-sm" onclick="closeScheduleDrawer()" style="border: none; font-size: 18px; line-height: 1; cursor: pointer;" title="Close Drawer (Esc)">✕</button>
|
|
1151
|
+
</div>
|
|
1152
|
+
|
|
1153
|
+
<!-- Drawer Body (Form) -->
|
|
1154
|
+
<form id="cron-schedule-form" onsubmit="handleSaveSchedule(event)" style="display: flex; flex-direction: column; flex: 1; overflow: hidden; margin: 0;">
|
|
1155
|
+
<div class="drawer-body" style="flex: 1; overflow-y: auto; padding: 1.25rem 1.5rem; display: flex; flex-direction: column; gap: 1.25rem;">
|
|
1156
|
+
<!-- Schedule Name & Enabled -->
|
|
1157
|
+
<div class="drawer-card">
|
|
1158
|
+
<div style="display: flex; gap: 12px; align-items: flex-end;">
|
|
1159
|
+
<div style="flex: 1;">
|
|
1160
|
+
<label for="sched-input-name" style="display: block; font-size: 12px; font-weight: 600; margin-bottom: 4px;">Schedule Name *</label>
|
|
1161
|
+
<input type="text" id="sched-input-name" class="input" required value="${escapeHtml(s.name)}" placeholder="e.g. Daily Metrics Aggregation" style="width: 100%;">
|
|
1162
|
+
</div>
|
|
1163
|
+
<div style="display: flex; align-items: center; gap: 8px; margin-bottom: 6px;">
|
|
1164
|
+
<label class="switch">
|
|
1165
|
+
<input type="checkbox" id="sched-input-enabled" ${s.enabled ? 'checked' : ''}>
|
|
1166
|
+
<span class="slider round"></span>
|
|
1167
|
+
</label>
|
|
1168
|
+
<span style="font-size: 12px; font-weight: 500;">Enabled</span>
|
|
1169
|
+
</div>
|
|
1170
|
+
</div>
|
|
1171
|
+
</div>
|
|
1172
|
+
|
|
1173
|
+
<!-- Target Selection -->
|
|
1174
|
+
<div class="drawer-card">
|
|
1175
|
+
<span class="drawer-section-title">Trigger Target</span>
|
|
1176
|
+
<div style="display: grid; grid-template-columns: 140px 1fr; gap: 10px;">
|
|
1177
|
+
<div>
|
|
1178
|
+
<label for="sched-input-target-type" style="display: block; font-size: 12px; font-weight: 600; margin-bottom: 4px;">Target Type</label>
|
|
1179
|
+
<select id="sched-input-target-type" class="input" onchange="handleModalTargetTypeChange(this.value)" style="width: 100%;">
|
|
1180
|
+
<option value="function" ${s.targetType === 'function' ? 'selected' : ''}>Function</option>
|
|
1181
|
+
<option value="flow" ${s.targetType === 'flow' ? 'selected' : ''}>Flow</option>
|
|
1182
|
+
${isSqlite ? `<option value="snapshot" ${s.targetType === 'snapshot' ? 'selected' : ''}>Snapshot</option>` : (s.targetType === 'snapshot' ? `<option value="snapshot" selected>Snapshot (Unsupported on ${dbProviderName})</option>` : '')}
|
|
1183
|
+
</select>
|
|
1184
|
+
</div>
|
|
1185
|
+
<div>
|
|
1186
|
+
<label for="sched-input-target-id" style="display: block; font-size: 12px; font-weight: 600; margin-bottom: 4px;">Target Selection *</label>
|
|
1187
|
+
<div id="sched-target-picker-area">
|
|
1188
|
+
${renderModalTargetPickerHtml(s.targetType, s.targetId)}
|
|
1189
|
+
</div>
|
|
1190
|
+
</div>
|
|
1191
|
+
</div>
|
|
1192
|
+
</div>
|
|
1193
|
+
|
|
1194
|
+
<!-- Schedule Cadence & Expression -->
|
|
1195
|
+
<div class="drawer-card">
|
|
1196
|
+
<div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px;">
|
|
1197
|
+
<span class="drawer-section-title" style="margin-bottom: 0;">Cron Cadence</span>
|
|
1198
|
+
<select class="input input-sm" onchange="handleModalPresetChange(this.value)" style="font-size: 11px; padding: 2px 6px;">
|
|
1199
|
+
<option value="">Preset Templates...</option>
|
|
1200
|
+
<option value="* * * * *">Every minute (* * * * *)</option>
|
|
1201
|
+
<option value="*/15 * * * *">Every 15 minutes (*/15 * * * *)</option>
|
|
1202
|
+
<option value="@hourly">Hourly (@hourly)</option>
|
|
1203
|
+
<option value="@daily">Daily at midnight (@daily)</option>
|
|
1204
|
+
<option value="0 9 * * *">Daily at 9:00 AM (0 9 * * *)</option>
|
|
1205
|
+
<option value="0 9 * * 1-5">Weekdays at 9:00 AM (0 9 * * 1-5)</option>
|
|
1206
|
+
<option value="@weekly">Weekly on Sunday (@weekly)</option>
|
|
1207
|
+
<option value="@monthly">Monthly on 1st day (@monthly)</option>
|
|
1208
|
+
<option value="0 0 L * *">Monthly on last day (0 0 L * *)</option>
|
|
1209
|
+
<option value="@yearly">Yearly on Jan 1st (@yearly)</option>
|
|
1210
|
+
</select>
|
|
1211
|
+
</div>
|
|
1212
|
+
|
|
1213
|
+
<div style="display: flex; gap: 8px;">
|
|
1214
|
+
<input type="text" id="sched-input-expr" class="input" required value="${escapeHtml(s.cronExpression)}" placeholder="e.g. 0 9 * * 1-5" oninput="handleCronExpressionInput()" style="flex: 1; font-family: monospace; font-size: 13px;" autocomplete="off">
|
|
1215
|
+
<input type="text" id="sched-input-tz" class="input" required value="${escapeHtml(s.timezone || 'UTC')}" list="iana-timezones-list" placeholder="Timezone" oninput="handleCronExpressionInput()" style="width: 170px; font-size: 12px;">
|
|
1216
|
+
</div>
|
|
1217
|
+
<span style="font-size: 11px; color: var(--text-muted); margin-top: 3px; display: block;">
|
|
1218
|
+
Standard 5-field cron or macro (<code>@hourly</code>, <code>@daily</code>, <code>0 0 L * *</code>).
|
|
1219
|
+
</span>
|
|
1220
|
+
|
|
1221
|
+
<!-- Live Next Runs Preview Card -->
|
|
1222
|
+
<div id="sched-preview-card" style="background: var(--bg-page); border: 1px solid var(--border-subtle); border-radius: 6px; padding: 10px 12px; margin-top: 8px;">
|
|
1223
|
+
<div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px;">
|
|
1224
|
+
<span style="font-size: 11px; font-weight: 600; color: var(--text-heading);">Upcoming Executions Preview</span>
|
|
1225
|
+
<span id="sched-preview-human" style="font-size: 11px; color: var(--accent-primary);">Loading...</span>
|
|
1226
|
+
</div>
|
|
1227
|
+
<ul id="sched-preview-list" style="margin: 0; padding-left: 18px; font-size: 11px; font-family: monospace; color: var(--text-muted);">
|
|
1228
|
+
<li>Calculating...</li>
|
|
1229
|
+
</ul>
|
|
1230
|
+
</div>
|
|
1231
|
+
</div>
|
|
1232
|
+
|
|
1233
|
+
<!-- Missed-Run Policy -->
|
|
1234
|
+
<div class="drawer-card">
|
|
1235
|
+
<span class="drawer-section-title">Missed-Run Policy (Misfire Grace >60s)</span>
|
|
1236
|
+
<div style="display: flex; flex-direction: column; gap: 8px; font-size: 12px;">
|
|
1237
|
+
<label style="display: flex; align-items: flex-start; gap: 8px; cursor: pointer;">
|
|
1238
|
+
<input type="radio" name="missedRunPolicy" value="skip" ${s.missedRunPolicy === 'skip' ? 'checked' : ''} style="margin-top: 2px;">
|
|
1239
|
+
<div>
|
|
1240
|
+
<strong>skip:</strong> Discard missed slots and jump next execution forward (default for Functions & Flows)
|
|
1241
|
+
</div>
|
|
1242
|
+
</label>
|
|
1243
|
+
<label style="display: flex; align-items: flex-start; gap: 8px; cursor: pointer;">
|
|
1244
|
+
<input type="radio" name="missedRunPolicy" value="catch_up_once" ${s.missedRunPolicy === 'catch_up_once' ? 'checked' : ''} style="margin-top: 2px;">
|
|
1245
|
+
<div>
|
|
1246
|
+
<strong>catch_up_once:</strong> Coalesce all missed slots into 1 immediate execution (default for Snapshots)
|
|
1247
|
+
</div>
|
|
1248
|
+
</label>
|
|
1249
|
+
<label style="display: flex; align-items: flex-start; gap: 8px; cursor: pointer;">
|
|
1250
|
+
<input type="radio" name="missedRunPolicy" value="catch_up_all" ${s.missedRunPolicy === 'catch_up_all' ? 'checked' : ''} style="margin-top: 2px;">
|
|
1251
|
+
<div>
|
|
1252
|
+
<strong>catch_up_all:</strong> Replay all missed slots sequentially (capped at safety limit of 10)
|
|
1253
|
+
</div>
|
|
1254
|
+
</label>
|
|
1255
|
+
</div>
|
|
1256
|
+
</div>
|
|
1257
|
+
|
|
1258
|
+
<!-- Reliability & Lifecycle Controls -->
|
|
1259
|
+
<div class="drawer-card">
|
|
1260
|
+
<div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px;">
|
|
1261
|
+
<span class="drawer-section-title" style="margin-bottom: 0;">Reliability & Lifecycle Controls</span>
|
|
1262
|
+
<span style="font-size: 11px; color: var(--text-muted);">Retries, circuit breaker, expiration</span>
|
|
1263
|
+
</div>
|
|
1264
|
+
|
|
1265
|
+
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 10px;">
|
|
1266
|
+
<div>
|
|
1267
|
+
<label for="sched-input-retries" style="display: block; font-size: 11px; font-weight: 600; margin-bottom: 3px;">Max Retries (Backoff)</label>
|
|
1268
|
+
<select id="sched-input-retries" class="input" style="width: 100%; font-size: 12px;">
|
|
1269
|
+
<option value="0" ${Number(s.maxRetries || 0) === 0 ? 'selected' : ''}>No retries (0)</option>
|
|
1270
|
+
<option value="1" ${Number(s.maxRetries) === 1 ? 'selected' : ''}>1 retry with backoff</option>
|
|
1271
|
+
<option value="2" ${Number(s.maxRetries) === 2 ? 'selected' : ''}>2 retries with backoff</option>
|
|
1272
|
+
<option value="3" ${Number(s.maxRetries) === 3 ? 'selected' : ''}>3 retries with backoff</option>
|
|
1273
|
+
<option value="5" ${Number(s.maxRetries) === 5 ? 'selected' : ''}>5 retries with backoff</option>
|
|
1274
|
+
</select>
|
|
1275
|
+
</div>
|
|
1276
|
+
|
|
1277
|
+
<div>
|
|
1278
|
+
<label for="sched-input-autopause" style="display: block; font-size: 11px; font-weight: 600; margin-bottom: 3px;">Circuit Breaker (Auto-Pause)</label>
|
|
1279
|
+
<select id="sched-input-autopause" class="input" style="width: 100%; font-size: 12px;">
|
|
1280
|
+
<option value="0" ${Number(s.autoPauseAfterFailures || 0) === 0 ? 'selected' : ''}>Never auto-pause (Disabled)</option>
|
|
1281
|
+
<option value="3" ${Number(s.autoPauseAfterFailures) === 3 ? 'selected' : ''}>Pause after 3 failures</option>
|
|
1282
|
+
<option value="5" ${Number(s.autoPauseAfterFailures) === 5 ? 'selected' : ''}>Pause after 5 failures</option>
|
|
1283
|
+
<option value="10" ${Number(s.autoPauseAfterFailures) === 10 ? 'selected' : ''}>Pause after 10 failures</option>
|
|
1284
|
+
</select>
|
|
1285
|
+
</div>
|
|
1286
|
+
</div>
|
|
1287
|
+
|
|
1288
|
+
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 12px;">
|
|
1289
|
+
<div>
|
|
1290
|
+
<label for="sched-input-expires" style="display: block; font-size: 11px; font-weight: 600; margin-bottom: 3px;">Auto-Expire Date (Optional)</label>
|
|
1291
|
+
<input type="datetime-local" id="sched-input-expires" class="input" value="${s.expiresAt ? formatForDateTimeLocal(s.expiresAt) : ''}" style="width: 100%; font-size: 12px;">
|
|
1292
|
+
<span style="font-size: 10px; color: var(--text-muted); display: block; margin-top: 2px;">Schedule pauses after this timestamp</span>
|
|
1293
|
+
</div>
|
|
1294
|
+
|
|
1295
|
+
<div id="sched-retention-container" style="${s.targetType === 'snapshot' ? '' : 'display: none;'}">
|
|
1296
|
+
<label for="sched-input-keeplast" style="display: block; font-size: 11px; font-weight: 600; margin-bottom: 3px;">Snapshot Retention (keepLast)</label>
|
|
1297
|
+
<input type="number" id="sched-input-keeplast" class="input" min="1" max="1000" placeholder="e.g. 7" value="${s.keepLast ? s.keepLast : ''}" style="width: 100%; font-size: 12px;">
|
|
1298
|
+
<span style="font-size: 10px; color: var(--text-muted); display: block; margin-top: 2px;">Retain last N snapshots created by this schedule</span>
|
|
1299
|
+
</div>
|
|
1300
|
+
</div>
|
|
1301
|
+
</div>
|
|
1302
|
+
|
|
1303
|
+
<!-- Input Payload (JSON) -->
|
|
1304
|
+
<div id="sched-payload-container" class="drawer-card" style="${s.targetType === 'snapshot' ? 'display: none;' : ''}">
|
|
1305
|
+
<label for="sched-input-payload" style="display: block; font-size: 12px; font-weight: 600; margin-bottom: 4px;">Payload / Context Data (JSON)</label>
|
|
1306
|
+
<textarea id="sched-input-payload" class="input" rows="3" placeholder='{"dryRun": false}' style="width: 100%; font-family: monospace; font-size: 12px;">${escapeHtml(payloadStr)}</textarea>
|
|
1307
|
+
<span style="font-size: 11px; color: var(--text-muted); margin-top: 3px; display: block;">
|
|
1308
|
+
Passed to function as <code>context.request.body</code> or flow as initial input payload.
|
|
1309
|
+
</span>
|
|
1310
|
+
</div>
|
|
1311
|
+
</div>
|
|
1312
|
+
|
|
1313
|
+
<!-- Drawer Footer -->
|
|
1314
|
+
<div class="drawer-footer" style="padding: 1rem 1.5rem; border-top: 1px solid var(--border-subtle); display: flex; justify-content: flex-end; align-items: center; gap: 8px; background: var(--bg-card); flex-shrink: 0;">
|
|
1315
|
+
<button type="button" class="btn btn-secondary btn-sm" onclick="closeScheduleDrawer()">Cancel</button>
|
|
1316
|
+
<button type="submit" class="btn btn-primary btn-sm" id="btn-save-schedule">${isEdit ? 'Save Changes' : 'Create Schedule'}</button>
|
|
1317
|
+
</div>
|
|
1318
|
+
</form>
|
|
1319
|
+
</aside>
|
|
1320
|
+
`;
|
|
1321
|
+
|
|
1322
|
+
// Trigger smooth slide-in animation
|
|
1323
|
+
const triggerDrawerOpen = () => {
|
|
1324
|
+
const backdrop = document.getElementById('schedule-drawer-backdrop');
|
|
1325
|
+
const panel = document.getElementById('schedule-drawer-panel');
|
|
1326
|
+
if (backdrop) backdrop.classList.add('open');
|
|
1327
|
+
if (panel) {
|
|
1328
|
+
panel.classList.add('open');
|
|
1329
|
+
const firstInput = panel.querySelector('input:not([type="hidden"]), select, textarea');
|
|
1330
|
+
if (firstInput && typeof firstInput.focus === 'function') {
|
|
1331
|
+
try { firstInput.focus(); } catch (_) {}
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
};
|
|
1335
|
+
if (typeof requestAnimationFrame === 'function') {
|
|
1336
|
+
requestAnimationFrame(triggerDrawerOpen);
|
|
1337
|
+
} else {
|
|
1338
|
+
triggerDrawerOpen();
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
window.removeEventListener('keydown', handleScheduleDrawerKeyDown);
|
|
1342
|
+
window.addEventListener('keydown', handleScheduleDrawerKeyDown);
|
|
1343
|
+
|
|
1344
|
+
// Fetch initial preview
|
|
1345
|
+
handleCronExpressionInput();
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
const renderScheduleModal = renderScheduleDrawer;
|
|
1349
|
+
|
|
1350
|
+
function renderModalTargetPickerHtml(targetType, selectedId) {
|
|
1351
|
+
if (targetType === 'function') {
|
|
1352
|
+
const list = cronState.functionsList || [];
|
|
1353
|
+
if (list.length === 0) {
|
|
1354
|
+
return `<input type="text" id="sched-input-target-id" class="input" required value="${escapeHtml(selectedId || '')}" placeholder="Function ID or name" style="width: 100%;">`;
|
|
1355
|
+
}
|
|
1356
|
+
const nameCounts = {};
|
|
1357
|
+
for (const f of list) {
|
|
1358
|
+
nameCounts[f.name] = (nameCounts[f.name] || 0) + 1;
|
|
1359
|
+
}
|
|
1360
|
+
return `
|
|
1361
|
+
<select id="sched-input-target-id" class="input" required style="width: 100%;">
|
|
1362
|
+
<option value="">Select Function Target...</option>
|
|
1363
|
+
${list.map(fn => {
|
|
1364
|
+
const isDup = (nameCounts[fn.name] || 0) > 1;
|
|
1365
|
+
const label = fn.name + (isDup ? ` (${fn.id.slice(0, 8)})` : '') + (fn.enabled === false ? ' [Disabled]' : '');
|
|
1366
|
+
return `<option value="${escapeHtml(fn.id)}" ${selectedId === fn.id || selectedId === fn.name ? 'selected' : ''}>${escapeHtml(label)}</option>`;
|
|
1367
|
+
}).join('')}
|
|
1368
|
+
</select>
|
|
1369
|
+
`;
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
if (targetType === 'flow') {
|
|
1373
|
+
const list = cronState.flowsList || [];
|
|
1374
|
+
if (list.length === 0) {
|
|
1375
|
+
return `<input type="text" id="sched-input-target-id" class="input" required value="${escapeHtml(selectedId || '')}" placeholder="Flow ID or name" style="width: 100%;">`;
|
|
1376
|
+
}
|
|
1377
|
+
const nameCounts = {};
|
|
1378
|
+
for (const fl of list) {
|
|
1379
|
+
nameCounts[fl.name] = (nameCounts[fl.name] || 0) + 1;
|
|
1380
|
+
}
|
|
1381
|
+
return `
|
|
1382
|
+
<select id="sched-input-target-id" class="input" required style="width: 100%;">
|
|
1383
|
+
<option value="">Select Flow Target...</option>
|
|
1384
|
+
${list.map(fl => {
|
|
1385
|
+
const isDup = (nameCounts[fl.name] || 0) > 1;
|
|
1386
|
+
const label = fl.name + (isDup ? ` (${fl.id.slice(0, 8)})` : '') + (fl.enabled === false ? ' [Disabled]' : '');
|
|
1387
|
+
return `<option value="${escapeHtml(fl.id)}" ${selectedId === fl.id || selectedId === fl.name ? 'selected' : ''}>${escapeHtml(label)}</option>`;
|
|
1388
|
+
}).join('')}
|
|
1389
|
+
</select>
|
|
1390
|
+
`;
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
if (targetType === 'snapshot') {
|
|
1394
|
+
if (!isSqliteDatabase()) {
|
|
1395
|
+
return `
|
|
1396
|
+
<input type="hidden" id="sched-input-target-id" value="${escapeHtml(selectedId || 'data.db')}">
|
|
1397
|
+
<div class="alert alert-warning" style="font-size: 11px; padding: 8px 10px; margin: 0; border-radius: 4px; background: rgba(245,158,11,0.12); color: var(--accent-warning); border: 1px solid rgba(245,158,11,0.25);">
|
|
1398
|
+
Automated snapshots are exclusive to embedded SQLite databases and are paused.
|
|
1399
|
+
</div>
|
|
1400
|
+
`;
|
|
1401
|
+
}
|
|
1402
|
+
return `
|
|
1403
|
+
<input type="text" id="sched-input-target-id" class="input" value="${escapeHtml(selectedId || 'data.db')}" readonly style="width: 100%; background: var(--bg-card-hover);" title="Automated snapshot targets local SQLite database">
|
|
1404
|
+
<span style="font-size: 10px; color: var(--text-muted); margin-top: 2px; display: block;">
|
|
1405
|
+
Creates rolling baseline snapshots with oldest-unprotected eviction.
|
|
1406
|
+
</span>
|
|
1407
|
+
`;
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
return `<input type="text" id="sched-input-target-id" class="input" required value="${escapeHtml(selectedId || '')}" style="width: 100%;">`;
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
function handleModalTargetTypeChange(type) {
|
|
1414
|
+
if (!isSqliteDatabase() && type === 'snapshot') {
|
|
1415
|
+
type = 'function';
|
|
1416
|
+
const typeEl = document.getElementById('sched-input-target-type');
|
|
1417
|
+
if (typeEl) typeEl.value = 'function';
|
|
1418
|
+
}
|
|
1419
|
+
const pickerArea = document.getElementById('sched-target-picker-area');
|
|
1420
|
+
const payloadContainer = document.getElementById('sched-payload-container');
|
|
1421
|
+
const retentionContainer = document.getElementById('sched-retention-container');
|
|
1422
|
+
if (pickerArea) {
|
|
1423
|
+
pickerArea.innerHTML = renderModalTargetPickerHtml(type, '');
|
|
1424
|
+
}
|
|
1425
|
+
if (payloadContainer) {
|
|
1426
|
+
payloadContainer.style.display = type === 'snapshot' ? 'none' : 'block';
|
|
1427
|
+
}
|
|
1428
|
+
if (retentionContainer) {
|
|
1429
|
+
retentionContainer.style.display = type === 'snapshot' ? 'block' : 'none';
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
// When creating a new schedule, update the selected default policy for the target type
|
|
1433
|
+
if (!cronState.activeScheduleForEdit) {
|
|
1434
|
+
const defaultPolicy = type === 'snapshot' ? 'catch_up_once' : 'skip';
|
|
1435
|
+
const radio = document.querySelector(`input[name="missedRunPolicy"][value="${defaultPolicy}"]`);
|
|
1436
|
+
if (radio) {
|
|
1437
|
+
radio.checked = true;
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
function handleModalPresetChange(preset) {
|
|
1443
|
+
if (!preset) return;
|
|
1444
|
+
const exprInput = document.getElementById('sched-input-expr');
|
|
1445
|
+
if (exprInput) {
|
|
1446
|
+
exprInput.value = preset;
|
|
1447
|
+
handleCronExpressionInput();
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
let cronPreviewDebounceTimer = null;
|
|
1452
|
+
function handleCronExpressionInput() {
|
|
1453
|
+
if (cronPreviewDebounceTimer) {
|
|
1454
|
+
clearTimeout(cronPreviewDebounceTimer);
|
|
1455
|
+
}
|
|
1456
|
+
cronPreviewDebounceTimer = setTimeout(async () => {
|
|
1457
|
+
const exprInput = document.getElementById('sched-input-expr');
|
|
1458
|
+
const tzInput = document.getElementById('sched-input-tz');
|
|
1459
|
+
const humanEl = document.getElementById('sched-preview-human');
|
|
1460
|
+
const listEl = document.getElementById('sched-preview-list');
|
|
1461
|
+
|
|
1462
|
+
if (!exprInput || !humanEl || !listEl) return;
|
|
1463
|
+
|
|
1464
|
+
const cronExpression = exprInput.value.trim();
|
|
1465
|
+
const timezone = tzInput ? tzInput.value.trim() : 'UTC';
|
|
1466
|
+
|
|
1467
|
+
if (!cronExpression) {
|
|
1468
|
+
humanEl.textContent = 'Enter expression';
|
|
1469
|
+
listEl.innerHTML = '<li>Awaiting 5-field expression...</li>';
|
|
1470
|
+
return;
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
try {
|
|
1474
|
+
const apiPrefix = getCronApiPrefix();
|
|
1475
|
+
const data = await api(`${apiPrefix}/preview`, {
|
|
1476
|
+
method: 'POST',
|
|
1477
|
+
body: { cronExpression, timezone, count: 5 },
|
|
1478
|
+
});
|
|
1479
|
+
if (data && data.valid) {
|
|
1480
|
+
humanEl.textContent = data.description || 'Valid Expression';
|
|
1481
|
+
humanEl.style.color = 'var(--accent-primary)';
|
|
1482
|
+
listEl.innerHTML = (data.nextRuns || []).map(r => `
|
|
1483
|
+
<li>${escapeHtml(r.zonedFormatted)} <span style="color: var(--text-muted); font-size: 10px;">(${escapeHtml(r.isoUtc)})</span></li>
|
|
1484
|
+
`).join('');
|
|
1485
|
+
} else {
|
|
1486
|
+
humanEl.textContent = data?.error || 'Invalid Expression';
|
|
1487
|
+
humanEl.style.color = 'var(--accent-danger)';
|
|
1488
|
+
listEl.innerHTML = `<li style="color: var(--accent-danger);">${escapeHtml(data?.error || 'Syntax error')}</li>`;
|
|
1489
|
+
}
|
|
1490
|
+
} catch {
|
|
1491
|
+
humanEl.textContent = 'Preview unavailable';
|
|
1492
|
+
}
|
|
1493
|
+
}, 250);
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
async function handleSaveSchedule(e) {
|
|
1497
|
+
e.preventDefault();
|
|
1498
|
+
const nameEl = document.getElementById('sched-input-name');
|
|
1499
|
+
const enabledEl = document.getElementById('sched-input-enabled');
|
|
1500
|
+
const targetTypeEl = document.getElementById('sched-input-target-type');
|
|
1501
|
+
const targetIdEl = document.getElementById('sched-input-target-id');
|
|
1502
|
+
const exprEl = document.getElementById('sched-input-expr');
|
|
1503
|
+
const tzEl = document.getElementById('sched-input-tz');
|
|
1504
|
+
const payloadEl = document.getElementById('sched-input-payload');
|
|
1505
|
+
const policyEl = document.querySelector('input[name="missedRunPolicy"]:checked');
|
|
1506
|
+
|
|
1507
|
+
if (!nameEl || !targetTypeEl || !targetIdEl || !exprEl) return;
|
|
1508
|
+
|
|
1509
|
+
const retriesEl = document.getElementById('sched-input-retries');
|
|
1510
|
+
const autoPauseEl = document.getElementById('sched-input-autopause');
|
|
1511
|
+
const expiresEl = document.getElementById('sched-input-expires');
|
|
1512
|
+
const keepLastEl = document.getElementById('sched-input-keeplast');
|
|
1513
|
+
|
|
1514
|
+
const maxRetries = retriesEl ? parseInt(retriesEl.value, 10) : 0;
|
|
1515
|
+
const autoPauseAfterFailures = autoPauseEl ? parseInt(autoPauseEl.value, 10) : 0;
|
|
1516
|
+
const expiresAt = expiresEl && expiresEl.value ? new Date(expiresEl.value).toISOString() : null;
|
|
1517
|
+
const keepLast = (targetTypeEl.value === 'snapshot' && keepLastEl && keepLastEl.value) ? parseInt(keepLastEl.value, 10) : null;
|
|
1518
|
+
|
|
1519
|
+
let parsedPayload = undefined;
|
|
1520
|
+
if (payloadEl && payloadEl.value.trim()) {
|
|
1521
|
+
try {
|
|
1522
|
+
parsedPayload = JSON.parse(payloadEl.value.trim());
|
|
1523
|
+
} catch (err) {
|
|
1524
|
+
toast('Invalid JSON payload: ' + err.message, 'error');
|
|
1525
|
+
return;
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
if (targetTypeEl.value === 'snapshot' && !isSqliteDatabase() && enabledEl && enabledEl.checked) {
|
|
1530
|
+
toast('Snapshots cannot be enabled on non-SQLite databases and will remain paused.', 'warning');
|
|
1531
|
+
enabledEl.checked = false;
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
const dto = {
|
|
1535
|
+
name: nameEl.value.trim(),
|
|
1536
|
+
targetType: targetTypeEl.value,
|
|
1537
|
+
targetId: targetIdEl.value.trim(),
|
|
1538
|
+
cronExpression: exprEl.value.trim(),
|
|
1539
|
+
timezone: tzEl ? tzEl.value.trim() : 'UTC',
|
|
1540
|
+
missedRunPolicy: policyEl ? policyEl.value : 'skip',
|
|
1541
|
+
payload: parsedPayload,
|
|
1542
|
+
enabled: (!isSqliteDatabase() && targetTypeEl.value === 'snapshot') ? false : (enabledEl ? enabledEl.checked : true),
|
|
1543
|
+
maxRetries,
|
|
1544
|
+
autoPauseAfterFailures,
|
|
1545
|
+
expiresAt,
|
|
1546
|
+
keepLast,
|
|
1547
|
+
};
|
|
1548
|
+
|
|
1549
|
+
try {
|
|
1550
|
+
const isEdit = Boolean(cronState.activeScheduleForEdit);
|
|
1551
|
+
const apiPrefix = getCronApiPrefix();
|
|
1552
|
+
const url = isEdit
|
|
1553
|
+
? `${apiPrefix}/schedules/${encodeURIComponent(cronState.activeScheduleForEdit.id)}`
|
|
1554
|
+
: `${apiPrefix}/schedules`;
|
|
1555
|
+
const method = isEdit ? 'PATCH' : 'POST';
|
|
1556
|
+
|
|
1557
|
+
await api(url, {
|
|
1558
|
+
method,
|
|
1559
|
+
body: dto,
|
|
1560
|
+
});
|
|
1561
|
+
|
|
1562
|
+
closeScheduleModal();
|
|
1563
|
+
toast(isEdit ? 'Schedule updated' : 'Schedule created', 'success');
|
|
1564
|
+
await loadCronData();
|
|
1565
|
+
} catch (err) {
|
|
1566
|
+
toast('Error: ' + err.message, 'error');
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
// ==========================================
|
|
1571
|
+
// Helpers
|
|
1572
|
+
// ==========================================
|
|
1573
|
+
|
|
1574
|
+
function formatDualTime(isoString, timezone) {
|
|
1575
|
+
try {
|
|
1576
|
+
const d = new Date(isoString);
|
|
1577
|
+
if (isNaN(d.getTime())) return isoString;
|
|
1578
|
+
return d.toLocaleString('en-US', {
|
|
1579
|
+
timeZone: timezone || 'UTC',
|
|
1580
|
+
month: 'short',
|
|
1581
|
+
day: 'numeric',
|
|
1582
|
+
hour: '2-digit',
|
|
1583
|
+
minute: '2-digit',
|
|
1584
|
+
hour12: false,
|
|
1585
|
+
});
|
|
1586
|
+
} catch {
|
|
1587
|
+
return isoString;
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
function formatDateTime(isoString) {
|
|
1592
|
+
try {
|
|
1593
|
+
const d = new Date(isoString);
|
|
1594
|
+
if (isNaN(d.getTime())) return isoString;
|
|
1595
|
+
return d.toLocaleString('en-US', {
|
|
1596
|
+
month: 'short',
|
|
1597
|
+
day: 'numeric',
|
|
1598
|
+
hour: '2-digit',
|
|
1599
|
+
minute: '2-digit',
|
|
1600
|
+
second: '2-digit',
|
|
1601
|
+
hour12: false,
|
|
1602
|
+
});
|
|
1603
|
+
} catch {
|
|
1604
|
+
return isoString;
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
function formatRelativeTime(isoString) {
|
|
1609
|
+
try {
|
|
1610
|
+
const targetMs = new Date(isoString).getTime();
|
|
1611
|
+
if (isNaN(targetMs)) return isoString;
|
|
1612
|
+
const now = Date.now();
|
|
1613
|
+
const isFuture = targetMs > now;
|
|
1614
|
+
const diffSec = Math.floor(Math.abs(now - targetMs) / 1000);
|
|
1615
|
+
|
|
1616
|
+
if (diffSec < 60) {
|
|
1617
|
+
if (diffSec < 5 && !isFuture) return 'just now';
|
|
1618
|
+
return isFuture ? `in ${diffSec}s` : `${diffSec}s ago`;
|
|
1619
|
+
}
|
|
1620
|
+
const min = Math.floor(diffSec / 60);
|
|
1621
|
+
if (min < 60) {
|
|
1622
|
+
return isFuture ? `in ${min}m` : `${min}m ago`;
|
|
1623
|
+
}
|
|
1624
|
+
const hr = Math.floor(min / 60);
|
|
1625
|
+
if (hr < 24) {
|
|
1626
|
+
return isFuture ? `in ${hr}h` : `${hr}h ago`;
|
|
1627
|
+
}
|
|
1628
|
+
const days = Math.floor(hr / 24);
|
|
1629
|
+
if (days < 30) {
|
|
1630
|
+
return isFuture ? `in ${days}d` : `${days}d ago`;
|
|
1631
|
+
}
|
|
1632
|
+
const months = Math.floor(days / 30);
|
|
1633
|
+
if (months < 12) {
|
|
1634
|
+
return isFuture ? `in ${months}mo` : `${months}mo ago`;
|
|
1635
|
+
}
|
|
1636
|
+
const years = Math.floor(days / 365);
|
|
1637
|
+
return isFuture ? `in ${years}y` : `${years}y ago`;
|
|
1638
|
+
} catch {
|
|
1639
|
+
return isoString;
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
function formatForDateTimeLocal(isoString) {
|
|
1644
|
+
if (!isoString) return '';
|
|
1645
|
+
try {
|
|
1646
|
+
const d = new Date(isoString);
|
|
1647
|
+
if (isNaN(d.getTime())) return '';
|
|
1648
|
+
const pad = n => String(n).padStart(2, '0');
|
|
1649
|
+
const YYYY = d.getFullYear();
|
|
1650
|
+
const MM = pad(d.getMonth() + 1);
|
|
1651
|
+
const DD = pad(d.getDate());
|
|
1652
|
+
const hh = pad(d.getHours());
|
|
1653
|
+
const mm = pad(d.getMinutes());
|
|
1654
|
+
return `${YYYY}-${MM}-${DD}T${hh}:${mm}`;
|
|
1655
|
+
} catch {
|
|
1656
|
+
return '';
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
function showToast(msg, type = 'info') {
|
|
1661
|
+
if (typeof toast === 'function') {
|
|
1662
|
+
toast(msg, type);
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
if (typeof window !== 'undefined') {
|
|
1667
|
+
window.cronState = cronState;
|
|
1668
|
+
window.renderSchedulesView = renderSchedulesView;
|
|
1669
|
+
window.refreshSchedulesView = refreshSchedulesView;
|
|
1670
|
+
window.switchCronTab = switchCronTab;
|
|
1671
|
+
window.handleCronTargetFilter = handleCronTargetFilter;
|
|
1672
|
+
window.handleCronSearchInput = handleCronSearchInput;
|
|
1673
|
+
window.handleCronStatusFilter = handleCronStatusFilter;
|
|
1674
|
+
window.handleCronSortChange = handleCronSortChange;
|
|
1675
|
+
window.handleResetScheduleFilters = handleResetScheduleFilters;
|
|
1676
|
+
window.viewScheduleHistory = viewScheduleHistory;
|
|
1677
|
+
window.navigateToCronTarget = navigateToCronTarget;
|
|
1678
|
+
window.openRunDetailDrawer = openRunDetailDrawer;
|
|
1679
|
+
window.renderRunDetailDrawer = renderRunDetailDrawer;
|
|
1680
|
+
window.closeRunDetailDrawer = closeRunDetailDrawer;
|
|
1681
|
+
window.copyRunOutputToClipboard = copyRunOutputToClipboard;
|
|
1682
|
+
window.openCreateScheduleDrawer = openCreateScheduleDrawer;
|
|
1683
|
+
window.openEditScheduleDrawer = openEditScheduleDrawer;
|
|
1684
|
+
window.closeScheduleDrawer = closeScheduleDrawer;
|
|
1685
|
+
window.renderScheduleDrawer = renderScheduleDrawer;
|
|
1686
|
+
window.openCreateScheduleModal = openCreateScheduleDrawer;
|
|
1687
|
+
window.openEditScheduleModal = openEditScheduleDrawer;
|
|
1688
|
+
window.closeScheduleModal = closeScheduleDrawer;
|
|
1689
|
+
window.renderScheduleModal = renderScheduleDrawer;
|
|
1690
|
+
window.handleModalTargetTypeChange = handleModalTargetTypeChange;
|
|
1691
|
+
window.handleModalPresetChange = handleModalPresetChange;
|
|
1692
|
+
window.handleToggleScheduleEnabled = handleToggleScheduleEnabled;
|
|
1693
|
+
window.handleTriggerSchedule = handleTriggerSchedule;
|
|
1694
|
+
window.handleDeleteSchedule = handleDeleteSchedule;
|
|
1695
|
+
window.handleClearRunHistory = handleClearRunHistory;
|
|
1696
|
+
window.handleCronExpressionInput = handleCronExpressionInput;
|
|
1697
|
+
window.handleSaveSchedule = handleSaveSchedule;
|
|
1698
|
+
window.formatForDateTimeLocal = formatForDateTimeLocal;
|
|
1699
|
+
window.loadCronData = loadCronData;
|
|
1700
|
+
window.loadCronStats = loadCronStats;
|
|
1701
|
+
window.showToast = showToast;
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
if (typeof state !== 'undefined' && state.token && typeof loadCronStats === 'function') {
|
|
1705
|
+
loadCronStats();
|
|
1706
|
+
}
|