任务列表:归档折叠 + 游标分页 + 关键词搜索;设置页版本号精简
This commit is contained in:
@@ -52,9 +52,41 @@ router.post('/', wrap((req, res) => {
|
||||
res.json(session);
|
||||
}));
|
||||
|
||||
// 列表查询参数(2.0):
|
||||
// status=active|done|all 归档分组:active=排队/活跃/保持,done=已完成/已终止
|
||||
// q=关键词 标题或消息内容 LIKE 搜索
|
||||
// before + before_id 游标分页(上一页末行的 updated_at,id),避免 offset 翻页在
|
||||
// updated_at 变动时重复/漏行
|
||||
// with_total=1 返回 {rows,total}(归档折叠区需要总数);默认仍返回纯数组
|
||||
router.get('/', wrap((req, res) => {
|
||||
const db = engine.getDb();
|
||||
const rows = db.prepare('SELECT * FROM sessions ORDER BY updated_at DESC LIMIT 50').all();
|
||||
const status = String(req.query.status || 'all');
|
||||
const q = String(req.query.q || '').trim();
|
||||
let limit = parseInt(req.query.limit, 10);
|
||||
if (!Number.isFinite(limit) || limit < 1) limit = 50;
|
||||
if (limit > 200) limit = 200;
|
||||
|
||||
const where = [];
|
||||
const args = [];
|
||||
if (status === 'active') where.push("status IN ('queued','active','held')");
|
||||
else if (status === 'done') where.push("status IN ('completed','cancelled')");
|
||||
if (q) {
|
||||
where.push('(title LIKE ? OR EXISTS(SELECT 1 FROM messages m WHERE m.session_id = sessions.id AND m.content LIKE ?))');
|
||||
args.push('%' + q + '%', '%' + q + '%');
|
||||
}
|
||||
const before = req.query.before;
|
||||
const beforeId = req.query.before_id;
|
||||
if (before != null && before !== '' && beforeId) {
|
||||
where.push('(updated_at < ? OR (updated_at = ? AND id < ?))');
|
||||
args.push(String(before), String(before), String(beforeId));
|
||||
}
|
||||
const whereSql = where.length ? ' WHERE ' + where.join(' AND ') : '';
|
||||
|
||||
const stmt = db.prepare(
|
||||
'SELECT * FROM sessions' + whereSql + ' ORDER BY updated_at DESC, id DESC LIMIT ?'
|
||||
);
|
||||
const rows = stmt.all.apply(stmt, args.concat([limit]));
|
||||
|
||||
const qpos = db.prepare(
|
||||
'SELECT session_id, ROW_NUMBER() OVER (ORDER BY queued_at, id) AS pos FROM wait_queue'
|
||||
).all();
|
||||
@@ -63,6 +95,11 @@ router.get('/', wrap((req, res) => {
|
||||
for (const row of rows) {
|
||||
row.queue_pos = row.status === 'queued' ? (posMap[row.id] || null) : null;
|
||||
}
|
||||
if (req.query.with_total === '1') {
|
||||
const cnt = db.prepare('SELECT COUNT(*) AS n FROM sessions' + whereSql);
|
||||
const total = cnt.all.apply(cnt, args)[0].n;
|
||||
return res.json({ rows: rows, total: total });
|
||||
}
|
||||
res.json(rows);
|
||||
}));
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@ const router = express.Router();
|
||||
const STARTED_AT = Date.now();
|
||||
let VERSION = '2.0.0';
|
||||
try { VERSION = require('../../package.json').version || VERSION; } catch (_) {}
|
||||
// 附带 git 提交号(post-commit 钩子自动生成),免去手动改版本号
|
||||
if (versionInfo.commit) VERSION += '+' + versionInfo.commit;
|
||||
// git 提交号单独返回(提交时 post-commit 钩子自动生成),前端只显示简洁版本号
|
||||
const VERSION_COMMIT = versionInfo.commit || '';
|
||||
|
||||
function sendErr(res, err) {
|
||||
const code = err && err.status_code || 500;
|
||||
@@ -31,6 +31,7 @@ router.get('/status', wrap((req, res) => {
|
||||
res.json({
|
||||
ok: true,
|
||||
version: VERSION,
|
||||
version_commit: VERSION_COMMIT,
|
||||
version_date: versionInfo.date,
|
||||
pid: process.pid,
|
||||
node: process.version,
|
||||
|
||||
Reference in New Issue
Block a user