| 1273 | }; |
| 1274 | |
| 1275 | const describeJobCompletion = (detail) => { |
| 1276 | const job = (detail && typeof detail === 'object' && detail.job && typeof detail.job === 'object') ? detail.job : null; |
| 1277 | const payload = (job && job.payload && typeof job.payload === 'object') ? job.payload : null; |
| 1278 | const result = (payload && payload.aiResult && typeof payload.aiResult === 'object') ? payload.aiResult : null; |
| 1279 | const status = String(job?.status || '').toLowerCase(); |
| 1280 | if (!job) return ''; |
| 1281 | |
| 1282 | if (status === 'succeeded') { |
| 1283 | const processed = Number(result?.processedCount || 0); |
| 1284 | const filtered = Number(result?.filteredOutCount || 0); |
| 1285 | const failed = Number(result?.failedCount || 0); |
| 1286 | const saved = (result?.savedOutputFile && typeof result.savedOutputFile === 'object') ? result.savedOutputFile : null; |
| 1287 | if (saved && String(saved.fileName || '').trim() !== '') { |
| 1288 | return `Job #${job.id} finished. Created ${saved.fileName} in ${saved.folder || 'root'} (processed ${processed}, filtered ${filtered}, failed ${failed}).`; |
| 1289 | } |
| 1290 | return `Job #${job.id} finished (processed ${processed}, filtered ${filtered}, failed ${failed}).`; |
| 1291 | } |
| 1292 | if (status === 'failed' || status === 'dead' || status === 'canceled') { |
| 1293 | const error = String(result?.error || '').trim(); |
| 1294 | return error !== '' |
| 1295 | ? `Job #${job.id} ${status}. ${error}` |
| 1296 | : `Job #${job.id} ${status}.`; |
| 1297 | } |
| 1298 | return ''; |
| 1299 | }; |
| 1300 | |
| 1301 | const watchJob = (jobId, options = {}) => { |
| 1302 | const numericJobId = Number(jobId || 0); |