@hasna/logs 0.3.9 → 0.3.10
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/dist/cli/index.js +43 -0
- package/dist/mcp/index.js +19 -0
- package/package.json +1 -1
- package/src/cli/index.ts +57 -0
- package/src/mcp/index.ts +21 -0
package/dist/cli/index.js
CHANGED
|
@@ -2430,6 +2430,49 @@ Exported ${count} log(s)
|
|
|
2430
2430
|
`);
|
|
2431
2431
|
}
|
|
2432
2432
|
});
|
|
2433
|
+
program2.command("stats").description("Volume overview: count, DB size, timeline, top services, error rate").option("--project <name|id>", "Scope to a project").action((opts) => {
|
|
2434
|
+
const db = getDb();
|
|
2435
|
+
const projectId = resolveProject(opts.project);
|
|
2436
|
+
const pFilter = projectId ? `WHERE project_id = '${projectId.replace(/'/g, "''")}'` : "";
|
|
2437
|
+
const pAnd = projectId ? `AND project_id = '${projectId.replace(/'/g, "''")}'` : "";
|
|
2438
|
+
const total = db.query(`SELECT COUNT(*) as c FROM logs ${pFilter}`).get().c;
|
|
2439
|
+
const oldest = db.query(`SELECT MIN(timestamp) as t FROM logs ${pFilter}`).get().t;
|
|
2440
|
+
const newest = db.query(`SELECT MAX(timestamp) as t FROM logs ${pFilter}`).get().t;
|
|
2441
|
+
const byLevel = db.query(`SELECT level, COUNT(*) as c FROM logs ${pFilter} GROUP BY level ORDER BY c DESC`).all();
|
|
2442
|
+
const topServices = db.query(`SELECT COALESCE(service, '-') as service, COUNT(*) as c FROM logs ${pFilter} GROUP BY service ORDER BY c DESC LIMIT 5`).all();
|
|
2443
|
+
const days = db.query(`SELECT strftime('%Y-%m-%d', timestamp) as day, COUNT(*) as c FROM logs WHERE timestamp >= datetime('now', '-7 days') ${pAnd} GROUP BY day ORDER BY day`).all();
|
|
2444
|
+
const errors = byLevel.find((r) => r.level === "error")?.c ?? 0;
|
|
2445
|
+
const fatals = byLevel.find((r) => r.level === "fatal")?.c ?? 0;
|
|
2446
|
+
const errorRate = total > 0 ? ((errors + fatals) / total * 100).toFixed(2) : "0.00";
|
|
2447
|
+
console.log(`
|
|
2448
|
+
${C.bold}Log Volume Stats${C.reset}${projectId ? ` [${opts.project}]` : ""}`);
|
|
2449
|
+
console.log(` Total: ${total.toLocaleString()}`);
|
|
2450
|
+
console.log(` Oldest: ${oldest?.slice(0, 19) ?? "-"}`);
|
|
2451
|
+
console.log(` Newest: ${newest?.slice(0, 19) ?? "-"}`);
|
|
2452
|
+
console.log(` Error rate: ${errorRate}% (${errors} errors, ${fatals} fatals)`);
|
|
2453
|
+
if (byLevel.length) {
|
|
2454
|
+
console.log(`
|
|
2455
|
+
${C.bold}By Level:${C.reset}`);
|
|
2456
|
+
for (const r of byLevel)
|
|
2457
|
+
console.log(` ${colorLevel(r.level)} ${r.c.toLocaleString()}`);
|
|
2458
|
+
}
|
|
2459
|
+
if (topServices.length) {
|
|
2460
|
+
console.log(`
|
|
2461
|
+
${C.bold}Top Services:${C.reset}`);
|
|
2462
|
+
for (const r of topServices)
|
|
2463
|
+
console.log(` ${C.cyan}${pad(r.service, 20)}${C.reset} ${r.c.toLocaleString()}`);
|
|
2464
|
+
}
|
|
2465
|
+
if (days.length) {
|
|
2466
|
+
const maxC = Math.max(...days.map((d) => d.c));
|
|
2467
|
+
console.log(`
|
|
2468
|
+
${C.bold}Last 7 Days:${C.reset}`);
|
|
2469
|
+
for (const d of days) {
|
|
2470
|
+
const bar = "\u2588".repeat(Math.max(1, Math.round(d.c / maxC * 20)));
|
|
2471
|
+
console.log(` ${d.day} ${C.cyan}${bar}${C.reset} ${d.c.toLocaleString()}`);
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
console.log("");
|
|
2475
|
+
});
|
|
2433
2476
|
program2.command("health").description("Show server health and DB stats").action(async () => {
|
|
2434
2477
|
const { getHealth } = await import("../health-9792c1rc.js");
|
|
2435
2478
|
const h = getHealth(getDb());
|
package/dist/mcp/index.js
CHANGED
|
@@ -28770,5 +28770,24 @@ server.tool("delete_alert_rule", { id: exports_external.string() }, ({ id }) =>
|
|
|
28770
28770
|
server.tool("get_health", {}, () => ({
|
|
28771
28771
|
content: [{ type: "text", text: JSON.stringify(getHealth(db)) }]
|
|
28772
28772
|
}));
|
|
28773
|
+
server.tool("log_stats", {
|
|
28774
|
+
project_id: exports_external.string().optional().describe("Project name or ID (scope stats to a project)")
|
|
28775
|
+
}, (args) => {
|
|
28776
|
+
const projectId = rp(args.project_id);
|
|
28777
|
+
const pFilter = projectId ? `WHERE project_id = ?` : "";
|
|
28778
|
+
const pAnd = projectId ? `AND project_id = ?` : "";
|
|
28779
|
+
const pParam = projectId ? [projectId] : [];
|
|
28780
|
+
const total = db.query(`SELECT COUNT(*) as c FROM logs ${pFilter}`).get(...pParam).c;
|
|
28781
|
+
const oldest = db.query(`SELECT MIN(timestamp) as t FROM logs ${pFilter}`).get(...pParam).t;
|
|
28782
|
+
const newest = db.query(`SELECT MAX(timestamp) as t FROM logs ${pFilter}`).get(...pParam).t;
|
|
28783
|
+
const byLevel = db.query(`SELECT level, COUNT(*) as c FROM logs ${pFilter} GROUP BY level ORDER BY c DESC`).all(...pParam);
|
|
28784
|
+
const topServices = db.query(`SELECT COALESCE(service, '-') as service, COUNT(*) as c FROM logs ${pFilter} GROUP BY service ORDER BY c DESC LIMIT 5`).all(...pParam);
|
|
28785
|
+
const days = db.query(`SELECT strftime('%Y-%m-%d', timestamp) as day, COUNT(*) as c FROM logs WHERE timestamp >= datetime('now', '-7 days') ${pAnd} GROUP BY day ORDER BY day`).all(...pParam);
|
|
28786
|
+
const errors3 = (byLevel.find((r) => r.level === "error")?.c ?? 0) + (byLevel.find((r) => r.level === "fatal")?.c ?? 0);
|
|
28787
|
+
const error_rate_pct = total > 0 ? parseFloat((errors3 / total * 100).toFixed(2)) : 0;
|
|
28788
|
+
return {
|
|
28789
|
+
content: [{ type: "text", text: JSON.stringify({ total, oldest, newest, by_level: Object.fromEntries(byLevel.map((r) => [r.level, r.c])), top_services: topServices, last_7_days: days, error_rate_pct }) }]
|
|
28790
|
+
};
|
|
28791
|
+
});
|
|
28773
28792
|
var transport = new StdioServerTransport;
|
|
28774
28793
|
await server.connect(transport);
|
package/package.json
CHANGED
package/src/cli/index.ts
CHANGED
|
@@ -346,6 +346,63 @@ program.command("export")
|
|
|
346
346
|
}
|
|
347
347
|
})
|
|
348
348
|
|
|
349
|
+
// ── logs stats ────────────────────────────────────────────
|
|
350
|
+
program.command("stats")
|
|
351
|
+
.description("Volume overview: count, DB size, timeline, top services, error rate")
|
|
352
|
+
.option("--project <name|id>", "Scope to a project")
|
|
353
|
+
.action((opts) => {
|
|
354
|
+
const db = getDb()
|
|
355
|
+
const projectId = resolveProject(opts.project)
|
|
356
|
+
const pFilter = projectId ? `WHERE project_id = '${projectId.replace(/'/g, "''")}'` : ""
|
|
357
|
+
const pAnd = projectId ? `AND project_id = '${projectId.replace(/'/g, "''")}'` : ""
|
|
358
|
+
|
|
359
|
+
const total = (db.query(`SELECT COUNT(*) as c FROM logs ${pFilter}`).get() as { c: number }).c
|
|
360
|
+
const oldest = (db.query(`SELECT MIN(timestamp) as t FROM logs ${pFilter}`).get() as { t: string | null }).t
|
|
361
|
+
const newest = (db.query(`SELECT MAX(timestamp) as t FROM logs ${pFilter}`).get() as { t: string | null }).t
|
|
362
|
+
|
|
363
|
+
const byLevel = db.query(`SELECT level, COUNT(*) as c FROM logs ${pFilter} GROUP BY level ORDER BY c DESC`)
|
|
364
|
+
.all() as { level: string; c: number }[]
|
|
365
|
+
|
|
366
|
+
const topServices = db.query(
|
|
367
|
+
`SELECT COALESCE(service, '-') as service, COUNT(*) as c FROM logs ${pFilter} GROUP BY service ORDER BY c DESC LIMIT 5`
|
|
368
|
+
).all() as { service: string; c: number }[]
|
|
369
|
+
|
|
370
|
+
// Last 7 days histogram
|
|
371
|
+
const days = db.query(
|
|
372
|
+
`SELECT strftime('%Y-%m-%d', timestamp) as day, COUNT(*) as c FROM logs WHERE timestamp >= datetime('now', '-7 days') ${pAnd} GROUP BY day ORDER BY day`
|
|
373
|
+
).all() as { day: string; c: number }[]
|
|
374
|
+
|
|
375
|
+
const errors = byLevel.find(r => r.level === "error")?.c ?? 0
|
|
376
|
+
const fatals = byLevel.find(r => r.level === "fatal")?.c ?? 0
|
|
377
|
+
const errorRate = total > 0 ? (((errors + fatals) / total) * 100).toFixed(2) : "0.00"
|
|
378
|
+
|
|
379
|
+
console.log(`\n${C.bold}Log Volume Stats${C.reset}${projectId ? ` [${opts.project}]` : ""}`)
|
|
380
|
+
console.log(` Total: ${total.toLocaleString()}`)
|
|
381
|
+
console.log(` Oldest: ${oldest?.slice(0, 19) ?? "-"}`)
|
|
382
|
+
console.log(` Newest: ${newest?.slice(0, 19) ?? "-"}`)
|
|
383
|
+
console.log(` Error rate: ${errorRate}% (${errors} errors, ${fatals} fatals)`)
|
|
384
|
+
|
|
385
|
+
if (byLevel.length) {
|
|
386
|
+
console.log(`\n${C.bold}By Level:${C.reset}`)
|
|
387
|
+
for (const r of byLevel) console.log(` ${colorLevel(r.level)} ${r.c.toLocaleString()}`)
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (topServices.length) {
|
|
391
|
+
console.log(`\n${C.bold}Top Services:${C.reset}`)
|
|
392
|
+
for (const r of topServices) console.log(` ${C.cyan}${pad(r.service, 20)}${C.reset} ${r.c.toLocaleString()}`)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (days.length) {
|
|
396
|
+
const maxC = Math.max(...days.map(d => d.c))
|
|
397
|
+
console.log(`\n${C.bold}Last 7 Days:${C.reset}`)
|
|
398
|
+
for (const d of days) {
|
|
399
|
+
const bar = "█".repeat(Math.max(1, Math.round((d.c / maxC) * 20)))
|
|
400
|
+
console.log(` ${d.day} ${C.cyan}${bar}${C.reset} ${d.c.toLocaleString()}`)
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
console.log("")
|
|
404
|
+
})
|
|
405
|
+
|
|
349
406
|
// ── logs health ───────────────────────────────────────────
|
|
350
407
|
program.command("health")
|
|
351
408
|
.description("Show server health and DB stats")
|
package/src/mcp/index.ts
CHANGED
|
@@ -300,5 +300,26 @@ server.tool("get_health", {}, () => ({
|
|
|
300
300
|
content: [{ type: "text", text: JSON.stringify(getHealth(db)) }]
|
|
301
301
|
}))
|
|
302
302
|
|
|
303
|
+
server.tool("log_stats", {
|
|
304
|
+
project_id: z.string().optional().describe("Project name or ID (scope stats to a project)"),
|
|
305
|
+
}, (args) => {
|
|
306
|
+
const projectId = rp(args.project_id)
|
|
307
|
+
const pFilter = projectId ? `WHERE project_id = ?` : ""
|
|
308
|
+
const pAnd = projectId ? `AND project_id = ?` : ""
|
|
309
|
+
const pParam = projectId ? [projectId] : []
|
|
310
|
+
|
|
311
|
+
const total = (db.query(`SELECT COUNT(*) as c FROM logs ${pFilter}`).get(...pParam) as { c: number }).c
|
|
312
|
+
const oldest = (db.query(`SELECT MIN(timestamp) as t FROM logs ${pFilter}`).get(...pParam) as { t: string | null }).t
|
|
313
|
+
const newest = (db.query(`SELECT MAX(timestamp) as t FROM logs ${pFilter}`).get(...pParam) as { t: string | null }).t
|
|
314
|
+
const byLevel = db.query(`SELECT level, COUNT(*) as c FROM logs ${pFilter} GROUP BY level ORDER BY c DESC`).all(...pParam) as { level: string; c: number }[]
|
|
315
|
+
const topServices = db.query(`SELECT COALESCE(service, '-') as service, COUNT(*) as c FROM logs ${pFilter} GROUP BY service ORDER BY c DESC LIMIT 5`).all(...pParam) as { service: string; c: number }[]
|
|
316
|
+
const days = db.query(`SELECT strftime('%Y-%m-%d', timestamp) as day, COUNT(*) as c FROM logs WHERE timestamp >= datetime('now', '-7 days') ${pAnd} GROUP BY day ORDER BY day`).all(...pParam) as { day: string; c: number }[]
|
|
317
|
+
const errors = (byLevel.find(r => r.level === "error")?.c ?? 0) + (byLevel.find(r => r.level === "fatal")?.c ?? 0)
|
|
318
|
+
const error_rate_pct = total > 0 ? parseFloat(((errors / total) * 100).toFixed(2)) : 0
|
|
319
|
+
return {
|
|
320
|
+
content: [{ type: "text" as const, text: JSON.stringify({ total, oldest, newest, by_level: Object.fromEntries(byLevel.map(r => [r.level, r.c])), top_services: topServices, last_7_days: days, error_rate_pct }) }]
|
|
321
|
+
}
|
|
322
|
+
})
|
|
323
|
+
|
|
303
324
|
const transport = new StdioServerTransport()
|
|
304
325
|
await server.connect(transport)
|