feat(records): add source history and Tabler record ledgers
This commit is contained in:
1 parent
1421edd6d4
commit
20bb68615f
27 files changed
+291
-91
No files matched your search
@@ -13,6 +13,8 @@ function createAssets(){
|
||||
'/mail.js':{path:path.join(webRoot,'modules/mail/view.js'),type:'text/javascript'},
|
||||
'/mail-history.js':{path:path.join(webRoot,'modules/mail/history.js'),type:'text/javascript'},
|
||||
'/orders.js':{path:path.join(webRoot,'modules/bpm/arrivals.js'),type:'text/javascript'},
|
||||
'/record-ui.js':{path:path.join(webRoot,'modules/records/shared.js'),type:'text/javascript'},
|
||||
'/pdf-records.js':{path:path.join(webRoot,'modules/records/pdf.js'),type:'text/javascript'},
|
||||
'/storage.js':{path:path.join(webRoot,'modules/storage/view.js'),type:'text/javascript'},
|
||||
'/storage-preview.html':{path:path.join(webRoot,'modules/storage/preview.html'),type:'text/html',preview:true},
|
||||
'/storage-preview.js':{path:path.join(webRoot,'modules/storage/preview.js'),type:'text/javascript',preview:true},
|
||||
|
||||
@@ -38,7 +38,7 @@ function createDashboard({store,outputDir,workspace,bpmSessions,bpmMonitor,mail,
|
||||
if(route==='/api/health' && request.method==='GET'){if(!health)fail(503,'健康状态服务未启动。');return reply(200,health.snapshot(user));}
|
||||
if(await bpmApi({workspace,bpmSessions,bpmLifecycle,user,sessionId:token,route,method:request.method,body,request,response,reply,trustedProxy}))return;
|
||||
if(await mailApi({mail,workspace,user,route,method:request.method,body,request,response,reply,trustedProxy}))return;
|
||||
if(await bpmScanApi({scanner,workspace,bpmMonitor,user,sessionId:token,url,route,method:request.method,body,reply}))return;
|
||||
if(await bpmScanApi({scanner,workspace,bpmMonitor,orderRules,user,sessionId:token,url,route,method:request.method,body,reply}))return;
|
||||
if(await storageApi({storage,workspace,user,url,route,method:request.method,body,request,response,reply,trustedProxy}))return;
|
||||
if(await notificationApi({notifications,workspace,user,route,method:request.method,body,reply}))return;
|
||||
if(await orderExecutionApi({execution:orderExecution,orderRules,dispatcher:orderDispatcher,workspace,user,sessionId:token,url,route,method:request.method,body,reply}))return;
|
||||
|
||||
@@ -64,7 +64,7 @@ class BpmArrivalMonitor{
|
||||
const runId=this.scanner.store.begin(id,principal.user.username,'todo');
|
||||
const promise=Promise.resolve().then(async()=>{
|
||||
try{let rows;try{rows=await this.provider.scan(connection,principal);}catch(error){if(error.status!==401)throw error;const result=await this.manager.ensure(connection,principal);if(result.status!=='connected')throw error;rows=await this.provider.scan(connection,principal);}
|
||||
if(this.stopped||this.workspace.getConnection(id).status==='disabled')fail('连接已暂停,本次对账未提交。',409);return {...this.scanner.store.complete(runId,id,rows.map(todoArrival)),runId};
|
||||
const current=this.workspace.getConnection(id);if(this.stopped||current.status==='disabled')fail('连接已暂停,本次对账未提交。',409);return {...this.scanner.store.complete(runId,id,rows.map(todoArrival),current),runId};
|
||||
}catch(error){this.scanner.store.fail(runId,error.status===401?'需要恢复BPM登录。':error.status===409?'连接忙碌或已暂停。':'待办对账失败,请稍后重试。');throw error;}
|
||||
}).finally(()=>this.todoPending.delete(id));this.todoPending.set(id,promise);return {runId,promise};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
const fail=message=>{throw Object.assign(new Error(message),{status:400});};
|
||||
function queryLedger({store,workspace,processNames},input={}){
|
||||
const filters={scope:'current',business:'orders',kind:'all',q:'',connectionId:'',from:'',to:'',page:1,pageSize:25,...input};
|
||||
if(!['current','archived','all'].includes(filters.scope)||!['orders','all'].includes(filters.business)||!['all','todo','inform'].includes(filters.kind))fail('记录筛选范围无效。');
|
||||
for(const key of ['q','connectionId'])if(typeof filters[key]!=='string'||filters[key].length>200)fail('记录查询条件过长。');
|
||||
const date=value=>!value||typeof value==='string'&&/^\d{4}-\d{2}-\d{2}$/.test(value)&&Number.isFinite(Date.parse(value+'T00:00:00Z'))&&new Date(value+'T00:00:00Z').toISOString().slice(0,10)===value;
|
||||
if(!date(filters.from)||!date(filters.to)||filters.from&&filters.to&&filters.from>filters.to)fail('请选择有效的起止日期。');
|
||||
if(!Number.isSafeInteger(filters.page)||filters.page<1||filters.page>1000000||!Number.isInteger(filters.pageSize)||filters.pageSize<1||filters.pageSize>100)fail('记录分页无效。');
|
||||
const catalog=workspace.recordSources(),sources=new Map(catalog.map(c=>[c.id,{...c,missing:false}]));
|
||||
for(const id of store.sourceIds())if(!sources.has(id))sources.set(id,{id,name:'历史连接 · '+id.slice(0,8),owner:'',identity:'',status:'archived',enabled:false,archivedAt:null,missing:true});
|
||||
const page=store.ledger(filters,catalog.filter(c=>c.status==='current').map(c=>c.id),[...new Set(processNames)]);
|
||||
return {...page,items:page.items.map(row=>{const current=sources.get(row.connectionId),first=row.origin;const origin={...current};if(first)for(const key of ['name','owner','identity'])if(typeof first[key]==='string')origin[key]=first[key].slice(0,200);return {...row,kind:row.source.startsWith('todo:')?'todo':'inform',origin};}),sources:[...sources.values()]};
|
||||
}
|
||||
module.exports={queryLedger};
|
||||
@@ -1,9 +1,15 @@
|
||||
'use strict';
|
||||
async function bpmScanApi({scanner,workspace,bpmMonitor,user,sessionId,url,route,method,body,reply}){
|
||||
async function bpmScanApi({scanner,workspace,bpmMonitor,orderRules,user,sessionId,url,route,method,body,reply}){
|
||||
if(!route.startsWith('/api/bpm-scan')&&route!=='/api/arrivals')return false;
|
||||
const fail=(message,status=400)=>{throw Object.assign(new Error(message),{status});};
|
||||
if(!scanner)fail('BPM 扫描暂不可用。',503);
|
||||
if(route==='/api/arrivals'&&method==='GET'){
|
||||
if(url.searchParams.get('view')==='ledger'){
|
||||
const filters={};for(const key of ['scope','business','kind','q','connectionId','from','to','page','pageSize'])if(url.searchParams.has(key))filters[key]=['page','pageSize'].includes(key)?Number(url.searchParams.get(key)):url.searchParams.get(key);
|
||||
const configured=(orderRules?.all('rules')||[]).flatMap(r=>[...(r.draft?.processNames||[]),...(r.published?.processNames||[]),...(r.draft?.scopes||[]).map(s=>s.processName),...(r.published?.scopes||[]).map(s=>s.processName)]);
|
||||
const processNames=[...require('../order-rules/catalog.cjs').seedRules().flatMap(r=>r.processNames),...configured];
|
||||
reply(200,require('./records-query.cjs').queryLedger({store:scanner.store,workspace,processNames},filters));return true;
|
||||
}
|
||||
const filters={connectionId:url.searchParams.get('connectionId')||'',q:url.searchParams.get('q')||'',before:url.searchParams.get('before')||'',days:Number(url.searchParams.get('days')||7),kind:url.searchParams.get('kind')||'inform'};
|
||||
reply(200,scanner.store.list(filters));return true;
|
||||
}
|
||||
|
||||
@@ -21,15 +21,29 @@ class BpmScanStore{
|
||||
config(){return JSON.parse(this.db.prepare('SELECT value FROM config WHERE id=1').get().value);}
|
||||
saveConfig(input){if(!input||Object.keys(input).some(k=>!['enabled','intervalMinutes'].includes(k))||typeof input.enabled!=='boolean'||!Number.isInteger(input.intervalMinutes)||input.intervalMinutes<1||input.intervalMinutes>1440)throw invalid('扫描间隔需为 1–1440 分钟。');this.db.prepare('UPDATE config SET value=? WHERE id=1').run(JSON.stringify(input));return this.config();}
|
||||
begin(connectionId,actor,kind='inform'){if(!['inform','todo'].includes(kind))throw invalid('到达类型无效。');const id=crypto.randomUUID();this.db.prepare('INSERT INTO runs(id,connection_id,actor,started_at,status,kind) VALUES(?,?,?,?,?,?)').run(id,connectionId,actor,new Date().toISOString(),'running',kind);return id;}
|
||||
complete(id,connectionId,rows){return this.transaction(()=>{
|
||||
complete(id,connectionId,rows,connection){return this.transaction(()=>{
|
||||
let added=0;const now=new Date().toISOString();
|
||||
const insert=this.db.prepare('INSERT OR IGNORE INTO arrivals VALUES(?,?,?,?,?,?,?)');const update=this.db.prepare('UPDATE arrivals SET day=?,value=?,last_seen=? WHERE connection_id=? AND source=? AND task_id=?');
|
||||
for(const row of rows){if(!row.arrivalDay||!row.taskId||!row.source)throw invalid('流程记录无效。');const value=JSON.stringify(row),key=row.recordKey||row.taskId;added+=Number(insert.run(connectionId,row.source,key,row.arrivalDay,value,now,now).changes);update.run(row.arrivalDay,value,now,connectionId,row.source,key);}
|
||||
const prior=this.db.prepare('SELECT value FROM arrivals WHERE connection_id=? AND source=? AND task_id=?');
|
||||
for(const row of rows){if(!row.arrivalDay||!row.taskId||!row.source)throw invalid('流程记录无效。');const key=row.recordKey||row.taskId,old=prior.get(connectionId,row.source,key),snapshot=old?JSON.parse(old.value).origin:null,saved={...row};delete saved.origin;if(snapshot||connection)saved.origin=snapshot||{id:connectionId,name:connection.name,owner:connection.owner,identity:connection.identity||''};const value=JSON.stringify(saved);added+=Number(insert.run(connectionId,row.source,key,row.arrivalDay,value,now,now).changes);update.run(row.arrivalDay,value,now,connectionId,row.source,key);}
|
||||
this.db.prepare('UPDATE runs SET status=?,finished_at=?,seen=?,added=?,message=? WHERE id=? AND status=?').run('success',now,rows.length,added,'',id,'running');this.prune();return {seen:rows.length,added};
|
||||
});}
|
||||
fail(id,message){this.db.prepare('UPDATE runs SET status=?,finished_at=?,message=? WHERE id=?').run('error',new Date().toISOString(),message,id);this.prune();}
|
||||
prune(){this.db.exec("DELETE FROM runs WHERE status!='running' AND id NOT IN(SELECT id FROM runs ORDER BY started_at DESC,rowid DESC LIMIT 200)");}
|
||||
status(){return {config:this.config(),total:this.db.prepare("SELECT COUNT(*) n FROM arrivals WHERE source NOT LIKE 'todo:%'").get().n,todoTotal:this.db.prepare("SELECT COUNT(*) n FROM arrivals WHERE source LIKE 'todo:%'").get().n,runs:this.db.prepare('SELECT id,connection_id AS connectionId,actor,started_at AS startedAt,finished_at AS finishedAt,status,seen,added,message,kind FROM runs ORDER BY started_at DESC,rowid DESC LIMIT 30').all()};}
|
||||
sourceIds(){return this.db.prepare('SELECT DISTINCT connection_id AS id FROM arrivals').all().map(row=>row.id);}
|
||||
ledger({scope,business,kind,q,connectionId,from,to,page,pageSize},currentIds,processNames){
|
||||
const conditions=[],args=[];
|
||||
if(scope!=='all'){conditions.push('connection_id '+(scope==='archived'?'NOT IN':'IN')+' (SELECT value FROM json_each(?))');args.push(JSON.stringify(currentIds));}
|
||||
if(business==='orders'){conditions.push("json_extract(value,'$.processName') IN (SELECT value FROM json_each(?))");args.push(JSON.stringify(processNames));}
|
||||
if(kind!=='all')conditions.push(kind==='todo'?"source LIKE 'todo:%'":"source NOT LIKE 'todo:%'");
|
||||
if(connectionId){conditions.push('connection_id=?');args.push(connectionId);}
|
||||
if(q){conditions.push("value LIKE ? ESCAPE '\\'");args.push('%'+q.replace(/[\\%_]/g,'\\$&')+'%');}
|
||||
if(from){conditions.push('day>=?');args.push(from);}if(to){conditions.push('day<=?');args.push(to);}
|
||||
const where=conditions.length?' WHERE '+conditions.join(' AND '):'',total=this.db.prepare('SELECT COUNT(*) n FROM arrivals'+where).get(...args).n;
|
||||
const items=this.db.prepare("SELECT connection_id AS connectionId,source,task_id AS storageKey,value,first_seen AS firstSeenAt,last_seen AS lastSeenAt FROM arrivals"+where+" ORDER BY json_extract(value,'$.arrivedAt') DESC,connection_id,source,task_id LIMIT ? OFFSET ?").all(...args,pageSize,(page-1)*pageSize).map(({value,storageKey,...rest})=>({...JSON.parse(value),...rest,recordId:JSON.stringify([rest.connectionId,rest.source,storageKey])}));
|
||||
return {items,total,page,pageSize};
|
||||
}
|
||||
list({connectionId='',q='',before='',days=7,kind='inform'}={}){
|
||||
if(!['inform','todo'].includes(kind))throw invalid('到达类型无效。');
|
||||
if(typeof q!=='string'||q.length>200||typeof connectionId!=='string'||connectionId.length>200||typeof before!=='string'||(before&&!/^\d{4}-\d{2}-\d{2}$/.test(before))||!Number.isInteger(days)||days<1||days>31)throw invalid('查询条件无效。');
|
||||
|
||||
@@ -12,7 +12,7 @@ class BpmScanner{
|
||||
if(this.pending.has(id))throw fail('此连接正在扫描,请稍后查看结果。',409);
|
||||
const runId=this.store.begin(id,principal.user.username);
|
||||
const operation=Promise.resolve().then(async()=>{
|
||||
try{const rows=await this.manager.scanArrivals(connection,principal);if(this.workspace.getConnection(id).status==='disabled')throw fail('此 BPM 连接已停用。',409);return {...this.store.complete(runId,id,rows),runId};}
|
||||
try{const rows=await this.manager.scanArrivals(connection,principal);const current=this.workspace.getConnection(id);if(current.status==='disabled')throw fail('此 BPM 连接已停用。',409);return {...this.store.complete(runId,id,rows,current),runId};}
|
||||
catch(cause){this.store.fail(runId,cause.status?cause.message:'BPM 扫描失败,请稍后重试。');throw cause;}
|
||||
}).finally(()=>this.pending.delete(id));this.pending.set(id,operation);return {promise:operation,runId};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const fs=require('node:fs');const path=require('node:path');
|
||||
const {nextSlots}=require('../../shared/schedule.cjs');
|
||||
const publicJob=({path:ignored,...job})=>({...job,downloadable:job.status==='success'});
|
||||
const {queryJobs,publicJobs,jobFile}=require('./job-records.cjs');
|
||||
async function dashboardApi({store,outputDir,workspace,mail,scanner,storage,orderRules,user,route,request,response,body,reply,fail}){
|
||||
const handled=(...args)=>{reply(...args);return true;};
|
||||
if(route==='/api/overview' && request.method==='GET') {
|
||||
@@ -11,13 +11,17 @@ const handled=(...args)=>{reply(...args);return true;};
|
||||
const rules=orderRules?.snapshot(),published=rules?.rules?.filter(r=>r.status==='enabled')||[];
|
||||
const capabilities=rules?.capabilities||{},ready=['liveTodo','pdfExport','automaticArchive'].every(key=>capabilities[key]===true);
|
||||
const automation={enabled:rules?.settings?.enabled===true,ready,reason:rules?.settings?.enabled!==true?'分类自动发信已暂停;请先完成类别字段、目录、邮箱与预览核验。':ready?'分类执行链路已完成实际接入检查。':'分类执行链路仍有未通过的实际检查,请查看订单执行中的阻塞原因。'};
|
||||
return handled(200,{stats:{total:jobs.length,today:todayJobs.length,completed:jobs.filter(job=>job.status==='success').length,failed:jobs.filter(job=>['error','interrupted'].includes(job.status)).length,running:jobs.filter(job=>job.status==='running').length},scan,automation,connections:[{name:'PDF 导出',ready:true,detail:'浏览器扩展导出与任务记录已接入'},{name:'Excel 分类规则',ready:published.length>0,detail:`15类规则,已发布 ${published.length} 类;实际字段按流程版本核验`},{name:'BPM 待办执行',ready:capabilities.liveTodo===true,detail:capabilities.liveTodo===true?'真实待办接口检查通过;各类别仍需字段核验':'请在订单通知管理的订单执行中检查实时待办,字段映射在业务设置维护'},{name:'到达历史扫描',ready:!!scan?.runs.some(r=>r.status==='success'&&(r.kind||'inform')==='inform'),detail:scan?`${scan.config.enabled?'已开启':'已暂停'},每 ${scan.config.intervalMinutes} 分钟扫描 · 已收录 ${scan.total} 条流程`:'历史扫描服务未启动'},{name:'存储服务器',ready:!!storage?.store.servers().some(s=>s.enabled&&s.state.readable&&s.state.writable),detail:storage?`已配置 ${storage.store.servers().length} 个 SMB 存储服务器`:'存储服务未启动'},{name:'邮件渠道',ready:mailChannels.some(c=>!c.readOnly&&c.state.smtp),detail:`已配置 ${mailChannels.length} 个邮箱`},{name:'邮件模板',ready:mailTemplates.some(t=>t.status==='active'),detail:`${mailTemplates.filter(t=>t.status==='active').length} 个已生效,${mailTemplates.filter(t=>t.status==='pending').length} 个待确认;按类别显式关联`}],nextSlots:nextSlots(settings.schedule,new Date(),2,workspace?workspace.snapshot(store.users()).calendar.overrides:[]),schedule:settings.schedule,jobs:jobs.slice(0,6).map(publicJob),audit:store.audit().slice(0,5)});
|
||||
return handled(200,{stats:{total:jobs.length,today:todayJobs.length,completed:jobs.filter(job=>job.status==='success').length,failed:jobs.filter(job=>['error','interrupted'].includes(job.status)).length,running:jobs.filter(job=>job.status==='running').length},scan,automation,connections:[{name:'PDF 导出',ready:true,detail:'浏览器扩展导出与任务记录已接入'},{name:'Excel 分类规则',ready:published.length>0,detail:`15类规则,已发布 ${published.length} 类;实际字段按流程版本核验`},{name:'BPM 待办执行',ready:capabilities.liveTodo===true,detail:capabilities.liveTodo===true?'真实待办接口检查通过;各类别仍需字段核验':'请在订单通知管理的订单执行中检查实时待办,字段映射在业务设置维护'},{name:'到达历史扫描',ready:!!scan?.runs.some(r=>r.status==='success'&&(r.kind||'inform')==='inform'),detail:scan?`${scan.config.enabled?'已开启':'已暂停'},每 ${scan.config.intervalMinutes} 分钟扫描 · 已收录 ${scan.total} 条流程`:'历史扫描服务未启动'},{name:'存储服务器',ready:!!storage?.store.servers().some(s=>s.enabled&&s.state.readable&&s.state.writable),detail:storage?`已配置 ${storage.store.servers().length} 个 SMB 存储服务器`:'存储服务未启动'},{name:'邮件渠道',ready:mailChannels.some(c=>!c.readOnly&&c.state.smtp),detail:`已配置 ${mailChannels.length} 个邮箱`},{name:'邮件模板',ready:mailTemplates.some(t=>t.status==='active'),detail:`${mailTemplates.filter(t=>t.status==='active').length} 个已生效,${mailTemplates.filter(t=>t.status==='pending').length} 个待确认;按类别显式关联`}],nextSlots:nextSlots(settings.schedule,new Date(),2,workspace?workspace.snapshot(store.users()).calendar.overrides:[]),schedule:settings.schedule,jobs:await publicJobs(jobs.slice(0,6),outputDir,storage?.store),audit:store.audit().slice(0,5)});
|
||||
}
|
||||
if(route==='/api/settings') {
|
||||
if(request.method==='GET')return handled(200,store.settings());
|
||||
if(request.method==='POST'){if(user.role!=='admin')fail(403,'仅管理员可以修改配置。');if(Object.hasOwn(body,'groups')||Object.hasOwn(body,'templates'))fail(410,'旧群组与通用模板写入已停用,请在分类规则维护收件人,在邮件模板维护类别关联。');return handled(200,store.saveSettings({...body,schedule:{...store.settings().schedule,...body.schedule}},body.version,user.username));}
|
||||
}
|
||||
if(route==='/api/jobs' && request.method==='GET')return handled(200,{jobs:store.jobs().map(publicJob)});
|
||||
if(route==='/api/jobs' && request.method==='GET'){
|
||||
const params=new URL(request.url,'http://localhost').searchParams;
|
||||
if(params.get('view')==='ledger'){const filters={};for(const key of ['q','status','page','pageSize'])if(params.has(key))filters[key]=['page','pageSize'].includes(key)?Number(params.get(key)):params.get(key);return handled(200,await queryJobs({jobs:store.jobs(),outputDir,archiveStore:storage?.store},filters));}
|
||||
return handled(200,{jobs:await publicJobs(store.jobs(),outputDir,storage?.store)});
|
||||
}
|
||||
if(route==='/api/audit' && request.method==='GET')return handled(200,{audit:store.audit()});
|
||||
if(route==='/api/users') {
|
||||
if(user.role!=='admin')fail(403,'仅管理员可以管理账号。');
|
||||
@@ -27,11 +31,7 @@ const handled=(...args)=>{reply(...args);return true;};
|
||||
const download=/^\/api\/jobs\/([\w-]+)\/file$/.exec(route);
|
||||
if(download && request.method==='GET') {
|
||||
const job=store.jobs().find(job=>job.jobId===download[1]);
|
||||
if(!job || job.status!=='success' || !job.path)fail(404,'文件不可用。');
|
||||
let realRoot,realFile;
|
||||
try{[realRoot,realFile]=await Promise.all([fs.promises.realpath(outputDir),fs.promises.realpath(job.path)]);}catch{fail(404,'文件不存在。');}
|
||||
const relative=path.relative(realRoot,realFile);
|
||||
if(!relative || relative.startsWith('..'+path.sep) || relative==='..' || path.isAbsolute(relative) || path.extname(realFile).toLowerCase()!=='.pdf')fail(404,'文件不可用。');
|
||||
const {file:realFile}=await jobFile(job,outputDir);
|
||||
const bytes=await fs.promises.readFile(realFile);
|
||||
response.writeHead(200,{'Content-Type':'application/pdf','Content-Disposition':`attachment; filename*=UTF-8''${encodeURIComponent(path.basename(realFile))}`});response.end(bytes);return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
const fs=require('node:fs/promises'),path=require('node:path');
|
||||
const fail=(message,status=400,code)=>Object.assign(new Error(message),{status,code});
|
||||
async function jobFile(job,outputDir){
|
||||
if(job?.status!=='success'||typeof job.path!=='string')throw fail('文件尚不可下载。',404,'unavailable');
|
||||
let root,file;try{[root,file]=await Promise.all([fs.realpath(outputDir),fs.realpath(job.path)]);}catch{throw fail('本地文件不存在。',404,'missing');}
|
||||
const relative=path.relative(root,file);if(!relative||relative==='..'||relative.startsWith('..'+path.sep)||path.isAbsolute(relative)||path.extname(file).toLowerCase()!=='.pdf')throw fail('文件不在允许的输出目录。',404,'unavailable');
|
||||
let handle;try{handle=await fs.open(file,'r');const stat=await handle.stat();if(!stat.isFile())throw fail('文件不可用。',404,'unavailable');const signature=Buffer.alloc(5);await handle.read(signature,0,5,0);if(signature.toString()!=='%PDF-')throw fail('文件不是有效的PDF输出。',404,'unavailable');return {file,filename:path.basename(file),bytes:stat.size};}catch(e){if(e.status)throw e;throw fail('文件暂不可读取。',404,'unavailable');}finally{await handle?.close();}
|
||||
}
|
||||
async function publicJobs(jobs,outputDir,archiveStore){
|
||||
const receipts=archiveStore?.archivesForJobs?.(jobs.map(j=>j.jobId))||[],result=new Array(jobs.length);let next=0;
|
||||
async function worker(){while(next<jobs.length){const index=next++,{path:ignored,...job}=jobs[index];let fileStatus=job.status==='running'?'pending':'unavailable',fileBytes=null;if(job.status==='success')try{const file=await jobFile(jobs[index],outputDir);fileStatus='available';fileBytes=file.bytes;}catch(e){fileStatus=e.code==='missing'?'missing':'unavailable';}
|
||||
result[index]={...job,downloadable:fileStatus==='available',fileStatus,fileBytes,fileCheckedAt:new Date().toISOString(),archiveKnown:!!archiveStore?.archivesForJobs,archiveReceipts:receipts.filter(r=>r.jobId===job.jobId).map(({id,status,serverName,relativePath,finishedAt})=>({id,status,serverName:serverName||'',relativePath:relativePath||'',finishedAt:finishedAt||null}))};
|
||||
}}await Promise.all(Array.from({length:Math.min(8,jobs.length)},worker));return result;
|
||||
}
|
||||
async function queryJobs({jobs,outputDir,archiveStore},input={}){
|
||||
const {q='',status='',page=1,pageSize=25}=input;
|
||||
if(typeof q!=='string'||q.length>200||!['','success','running','error','interrupted'].includes(status)||!Number.isSafeInteger(page)||page<1||page>1000000||!Number.isInteger(pageSize)||pageSize<1||pageSize>100)throw fail('导出记录查询条件无效。');
|
||||
const needle=q.trim().toLowerCase(),filtered=jobs.filter(j=>(!status||j.status===status)&&(!needle||[j.metadata?.order,j.metadata?.project,j.metadata?.model,j.metadata?.serial,j.filename].join(' ').toLowerCase().includes(needle))).sort((a,b)=>String(b.updatedAt||'').localeCompare(String(a.updatedAt||''))||String(a.jobId).localeCompare(String(b.jobId)));
|
||||
return {jobs:await publicJobs(filtered.slice((page-1)*pageSize,page*pageSize),outputDir,archiveStore),total:filtered.length,page,pageSize};
|
||||
}
|
||||
module.exports={queryJobs,publicJobs,jobFile};
|
||||
@@ -77,6 +77,7 @@ class StorageStore{
|
||||
beginArchive(serverId,jobId,actor,directory){const prior=this.db.prepare('SELECT id,value FROM archives WHERE server_id=? AND job_id=?').get(serverId,jobId),id=prior?.id||crypto.randomUUID();const value={...(prior?JSON.parse(prior.value):{}),actor,directory,startedAt:now(),finishedAt:null,error:null};this.db.prepare('INSERT INTO archives VALUES(?,?,?,?,?) ON CONFLICT(server_id,job_id) DO UPDATE SET status=excluded.status,value=excluded.value').run(id,serverId,jobId,'running',JSON.stringify(value));return {id,serverId,jobId,status:'running',...value};}
|
||||
finishArchive(id,status,patch){const row=this.db.prepare('SELECT value FROM archives WHERE id=?').get(id);const value={...JSON.parse(row.value),...patch,finishedAt:now()};const saved=this.db.prepare('UPDATE archives SET status=?,value=? WHERE id=? RETURNING id,server_id AS serverId,job_id AS jobId,status').get(status,JSON.stringify(value),id);return {...value,...saved};}
|
||||
archives(){return this.db.prepare("SELECT id,server_id AS serverId,job_id AS jobId,status,value FROM archives ORDER BY COALESCE(json_extract(value,'$.finishedAt'),json_extract(value,'$.startedAt')) DESC,rowid DESC LIMIT 100").all().map(({value,...r})=>({...JSON.parse(value),...r}));}
|
||||
archivesForJobs(ids){return this.db.prepare("SELECT id,server_id AS serverId,job_id AS jobId,status,value FROM archives WHERE job_id IN (SELECT value FROM json_each(?)) ORDER BY COALESCE(json_extract(value,'$.finishedAt'),json_extract(value,'$.startedAt')) DESC,rowid DESC").all(JSON.stringify(ids)).map(({value,...r})=>({...JSON.parse(value),...r}));}
|
||||
close(){if(this.db){this.db.close();this.db=null;}this.vault.destroy();}
|
||||
}
|
||||
module.exports={StorageStore};
|
||||
@@ -15,6 +15,7 @@ class WorkspaceStore {
|
||||
CREATE TABLE IF NOT EXISTS metadata(key TEXT PRIMARY KEY,value TEXT NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS exceptions(id TEXT PRIMARY KEY,value TEXT NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS connections(id TEXT PRIMARY KEY,value TEXT NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS connection_archives(id TEXT PRIMARY KEY,value TEXT NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS audit(seq INTEGER PRIMARY KEY AUTOINCREMENT,value TEXT NOT NULL);`);
|
||||
const insert=this.db.prepare('INSERT OR IGNORE INTO metadata(key,value) VALUES(?,?)');
|
||||
insert.run('calendar',JSON.stringify({version:1,overrides:[]}));insert.run('supervisor',JSON.stringify({version:1,defaultOwner:'',notifyChannel:'none',maxAttempts:0}));
|
||||
@@ -132,7 +133,10 @@ class WorkspaceStore {
|
||||
});
|
||||
}
|
||||
deleteConnection(id,actor){
|
||||
authorize(actor,['admin']);return this.transaction(()=>{const item=this.getConnection(id);if(item.status!=='disabled')fail('请先停用连接再删除。',409);this.db.prepare('DELETE FROM connections WHERE id=?').run(id);this.record(actor,'删除连接',item.name);return {id,deleted:true};});
|
||||
authorize(actor,['admin']);return this.transaction(()=>{const item=this.getConnection(id);if(item.status!=='disabled')fail('请先停用连接再删除。',409);const archive={id,name:item.name,owner:item.owner,identity:item.identity||'',archivedAt:new Date().toISOString()};this.db.prepare('INSERT INTO connection_archives(id,value) VALUES(?,?)').run(id,JSON.stringify(archive));this.db.prepare('DELETE FROM connections WHERE id=?').run(id);this.record(actor,'删除连接',item.name);return {id,deleted:true};});
|
||||
}
|
||||
recordSources(){
|
||||
return this.transaction(()=>[...this.db.prepare('SELECT value FROM connections').all().map(({value})=>{const c=JSON.parse(value);return {id:c.id,name:c.name,owner:c.owner,identity:c.identity||'',status:'current',enabled:c.status!=='disabled',archivedAt:null};}),...this.db.prepare('SELECT value FROM connection_archives').all().map(({value})=>({...JSON.parse(value),status:'archived',enabled:false}))]);
|
||||
}
|
||||
saveSupervisor(input,actor,members){
|
||||
authorize(actor,['admin']);object(input,['version','defaultOwner','notifyChannel','maxAttempts']);version(input.version);const defaultOwner=owner(input.defaultOwner,members);
|
||||
|
||||
Reference in new issue
Block a user