@sleep2agi/commhub-server 0.5.0-preview.12 → 0.5.0-preview.13
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/package.json +1 -1
- package/src/index.ts +19 -7
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -277,7 +277,11 @@ Bun.serve({
|
|
|
277
277
|
if (url.pathname === "/api/status") {
|
|
278
278
|
const cutoff = new Date(Date.now() - 10 * 60 * 1000).toISOString().replace("T", " ").slice(0, 19);
|
|
279
279
|
db.run("UPDATE sessions SET status = 'offline' WHERE updated_at < ?1 AND status != 'offline'", [cutoff]);
|
|
280
|
-
const
|
|
280
|
+
const netFilter = url.searchParams.get("network_id");
|
|
281
|
+
const sql = netFilter
|
|
282
|
+
? "SELECT * FROM sessions WHERE network_id = ?1 ORDER BY updated_at DESC"
|
|
283
|
+
: "SELECT * FROM sessions ORDER BY updated_at DESC";
|
|
284
|
+
const sessions = netFilter ? db.query(sql).all(netFilter) : db.query(sql).all();
|
|
281
285
|
return withCors(req, Response.json({ ok: true, sessions }));
|
|
282
286
|
}
|
|
283
287
|
|
|
@@ -401,15 +405,18 @@ Bun.serve({
|
|
|
401
405
|
|
|
402
406
|
// ── REST: stats summary ──
|
|
403
407
|
if (url.pathname === "/api/stats") {
|
|
404
|
-
const
|
|
405
|
-
const
|
|
406
|
-
const
|
|
407
|
-
const
|
|
408
|
+
const n = url.searchParams.get("network_id");
|
|
409
|
+
const nw = n ? ` WHERE network_id = '${n}'` : "";
|
|
410
|
+
const taskStats = db.query<any, []>(`SELECT status, COUNT(*) as count FROM tasks${nw} GROUP BY status`).all();
|
|
411
|
+
const sessionStats = db.query<any, []>(`SELECT status, COUNT(*) as count FROM sessions${nw} GROUP BY status`).all();
|
|
412
|
+
const totalTasks = db.query<{ cnt: number }, []>(`SELECT COUNT(*) as cnt FROM tasks${nw}`).get();
|
|
413
|
+
const totalNodes = db.query<{ cnt: number }, []>(`SELECT COUNT(*) as cnt FROM nodes${nw}`).get();
|
|
408
414
|
const recentTasks = db.query<any, []>(
|
|
409
|
-
|
|
415
|
+
`SELECT task_id, from_name, to_name, status, created_at FROM tasks${nw} ORDER BY created_at DESC LIMIT 5`
|
|
410
416
|
).all();
|
|
411
417
|
return withCors(req, Response.json({
|
|
412
418
|
ok: true,
|
|
419
|
+
network_id: n || null,
|
|
413
420
|
tasks: { total: totalTasks?.cnt || 0, by_status: taskStats },
|
|
414
421
|
sessions: { by_status: sessionStats },
|
|
415
422
|
nodes: { total: totalNodes?.cnt || 0 },
|
|
@@ -455,8 +462,10 @@ Bun.serve({
|
|
|
455
462
|
if (url.pathname === "/api/nodes") {
|
|
456
463
|
const nodeId = url.searchParams.get("node_id");
|
|
457
464
|
const alias = url.searchParams.get("alias");
|
|
465
|
+
const netFilter = url.searchParams.get("network_id");
|
|
458
466
|
let sql = "SELECT * FROM nodes WHERE 1=1";
|
|
459
467
|
const params: any[] = [];
|
|
468
|
+
if (netFilter) { sql += ` AND network_id = ?${params.length + 1}`; params.push(netFilter); }
|
|
460
469
|
if (nodeId) { sql += ` AND node_id = ?${params.length + 1}`; params.push(nodeId); }
|
|
461
470
|
if (alias) { sql += ` AND alias = ?${params.length + 1}`; params.push(alias); }
|
|
462
471
|
sql += " ORDER BY updated_at DESC";
|
|
@@ -470,10 +479,12 @@ Bun.serve({
|
|
|
470
479
|
const status = url.searchParams.get("status");
|
|
471
480
|
const toName = url.searchParams.get("to_name");
|
|
472
481
|
const fromName = url.searchParams.get("from_name");
|
|
482
|
+
const netFilter = url.searchParams.get("network_id");
|
|
473
483
|
const limit = Math.min(Number(url.searchParams.get("limit")) || 50, 200);
|
|
474
484
|
|
|
475
485
|
let sql = "SELECT * FROM tasks WHERE 1=1";
|
|
476
486
|
const params: any[] = [];
|
|
487
|
+
if (netFilter) { sql += ` AND network_id = ?${params.length + 1}`; params.push(netFilter); }
|
|
477
488
|
if (taskId) { sql += ` AND task_id = ?${params.length + 1}`; params.push(taskId); }
|
|
478
489
|
if (status) { sql += ` AND status = ?${params.length + 1}`; params.push(status); }
|
|
479
490
|
if (toName) { sql += ` AND to_name = ?${params.length + 1}`; params.push(toName); }
|
|
@@ -482,7 +493,8 @@ Bun.serve({
|
|
|
482
493
|
params.push(limit);
|
|
483
494
|
|
|
484
495
|
const rows = db.query(sql).all(...params);
|
|
485
|
-
const
|
|
496
|
+
const statsFilter = netFilter ? ` WHERE network_id = '${netFilter}'` : "";
|
|
497
|
+
const stats = db.query<any, []>(`SELECT status, COUNT(*) as count FROM tasks${statsFilter} GROUP BY status`).all();
|
|
486
498
|
return withCors(req, Response.json({ ok: true, tasks: rows, count: rows.length, stats }));
|
|
487
499
|
}
|
|
488
500
|
|