@hasna/logs 0.3.8 → 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 +51 -0
- package/package.json +1 -1
- package/src/cli/index.ts +57 -0
- package/src/mcp/index.ts +46 -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
|
@@ -41,6 +41,10 @@ import {
|
|
|
41
41
|
import {
|
|
42
42
|
parseTime
|
|
43
43
|
} from "../index-997bkzr2.js";
|
|
44
|
+
import {
|
|
45
|
+
exportToCsv,
|
|
46
|
+
exportToJson
|
|
47
|
+
} from "../index-eh9bkbpa.js";
|
|
44
48
|
import {
|
|
45
49
|
__commonJS,
|
|
46
50
|
__export,
|
|
@@ -28654,6 +28658,34 @@ server.tool("log_context_from_id", {
|
|
|
28654
28658
|
}, ({ log_id, brief }) => ({
|
|
28655
28659
|
content: [{ type: "text", text: JSON.stringify(applyBrief(getLogContextFromId(db, log_id), brief !== false)) }]
|
|
28656
28660
|
}));
|
|
28661
|
+
server.tool("log_export", {
|
|
28662
|
+
project_id: exports_external.string().optional().describe("Project name or ID"),
|
|
28663
|
+
format: exports_external.enum(["json", "csv"]).optional().default("json").describe("Output format"),
|
|
28664
|
+
since: exports_external.string().optional().describe("Since time (1h, 24h, 7d, ISO)"),
|
|
28665
|
+
until: exports_external.string().optional(),
|
|
28666
|
+
level: exports_external.array(exports_external.string()).optional().describe("Filter by levels"),
|
|
28667
|
+
service: exports_external.string().optional(),
|
|
28668
|
+
limit: exports_external.number().optional().default(1e5)
|
|
28669
|
+
}, (args) => {
|
|
28670
|
+
const chunks = [];
|
|
28671
|
+
const write = (s) => {
|
|
28672
|
+
chunks.push(s);
|
|
28673
|
+
return true;
|
|
28674
|
+
};
|
|
28675
|
+
const options = {
|
|
28676
|
+
project_id: rp(args.project_id),
|
|
28677
|
+
level: args.level,
|
|
28678
|
+
service: args.service,
|
|
28679
|
+
since: args.since,
|
|
28680
|
+
until: args.until,
|
|
28681
|
+
limit: args.limit ?? 1e5
|
|
28682
|
+
};
|
|
28683
|
+
if (args.format === "csv")
|
|
28684
|
+
exportToCsv(db, options, write);
|
|
28685
|
+
else
|
|
28686
|
+
exportToJson(db, options, write);
|
|
28687
|
+
return { content: [{ type: "text", text: chunks.join("") }] };
|
|
28688
|
+
});
|
|
28657
28689
|
server.tool("log_diagnose", {
|
|
28658
28690
|
project_id: exports_external.string(),
|
|
28659
28691
|
since: exports_external.string().optional(),
|
|
@@ -28738,5 +28770,24 @@ server.tool("delete_alert_rule", { id: exports_external.string() }, ({ id }) =>
|
|
|
28738
28770
|
server.tool("get_health", {}, () => ({
|
|
28739
28771
|
content: [{ type: "text", text: JSON.stringify(getHealth(db)) }]
|
|
28740
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
|
+
});
|
|
28741
28792
|
var transport = new StdioServerTransport;
|
|
28742
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
|
@@ -13,6 +13,7 @@ import { getLatestSnapshot, getPerfTrend, scoreLabel } from "../lib/perf.ts"
|
|
|
13
13
|
import { createAlertRule, deleteAlertRule, listAlertRules } from "../lib/alerts.ts"
|
|
14
14
|
import { listIssues, updateIssueStatus } from "../lib/issues.ts"
|
|
15
15
|
import { diagnose } from "../lib/diagnose.ts"
|
|
16
|
+
import { exportToJson, exportToCsv } from "../lib/export.ts"
|
|
16
17
|
import { compare } from "../lib/compare.ts"
|
|
17
18
|
import { getHealth } from "../lib/health.ts"
|
|
18
19
|
import { getSessionContext } from "../lib/session-context.ts"
|
|
@@ -189,6 +190,30 @@ server.tool("log_context_from_id", {
|
|
|
189
190
|
content: [{ type: "text", text: JSON.stringify(applyBrief(getLogContextFromId(db, log_id), brief !== false)) }]
|
|
190
191
|
}))
|
|
191
192
|
|
|
193
|
+
server.tool("log_export", {
|
|
194
|
+
project_id: z.string().optional().describe("Project name or ID"),
|
|
195
|
+
format: z.enum(["json", "csv"]).optional().default("json").describe("Output format"),
|
|
196
|
+
since: z.string().optional().describe("Since time (1h, 24h, 7d, ISO)"),
|
|
197
|
+
until: z.string().optional(),
|
|
198
|
+
level: z.array(z.string()).optional().describe("Filter by levels"),
|
|
199
|
+
service: z.string().optional(),
|
|
200
|
+
limit: z.number().optional().default(100000),
|
|
201
|
+
}, (args) => {
|
|
202
|
+
const chunks: string[] = []
|
|
203
|
+
const write = (s: string) => { chunks.push(s); return true }
|
|
204
|
+
const options = {
|
|
205
|
+
project_id: rp(args.project_id),
|
|
206
|
+
level: args.level as never,
|
|
207
|
+
service: args.service,
|
|
208
|
+
since: args.since,
|
|
209
|
+
until: args.until,
|
|
210
|
+
limit: args.limit ?? 100000,
|
|
211
|
+
}
|
|
212
|
+
if (args.format === "csv") exportToCsv(db, options, write)
|
|
213
|
+
else exportToJson(db, options, write)
|
|
214
|
+
return { content: [{ type: "text" as const, text: chunks.join("") }] }
|
|
215
|
+
})
|
|
216
|
+
|
|
192
217
|
server.tool("log_diagnose", {
|
|
193
218
|
project_id: z.string(),
|
|
194
219
|
since: z.string().optional(),
|
|
@@ -275,5 +300,26 @@ server.tool("get_health", {}, () => ({
|
|
|
275
300
|
content: [{ type: "text", text: JSON.stringify(getHealth(db)) }]
|
|
276
301
|
}))
|
|
277
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
|
+
|
|
278
324
|
const transport = new StdioServerTransport()
|
|
279
325
|
await server.connect(transport)
|