@hasna/logs 0.3.9 → 0.3.11

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/src/lib/ingest.ts CHANGED
@@ -37,7 +37,11 @@ export function ingestLog(db: Database, entry: LogEntry): LogRow {
37
37
  return row
38
38
  }
39
39
 
40
- export function ingestBatch(db: Database, entries: LogEntry[]): LogRow[] {
40
+ export function ingestBatch(db: Database, entries: LogEntry[], sharedTraceId?: string | null): LogRow[] {
41
+ // Apply shared trace_id to entries that don't have their own
42
+ if (sharedTraceId) {
43
+ entries = entries.map(e => e.trace_id ? e : { ...e, trace_id: sharedTraceId })
44
+ }
41
45
  const insert = db.prepare(`
42
46
  INSERT INTO logs (project_id, page_id, level, source, service, message, trace_id, session_id, agent, url, stack_trace, metadata)
43
47
  VALUES ($project_id, $page_id, $level, $source, $service, $message, $trace_id, $session_id, $agent, $url, $stack_trace, $metadata)
package/src/mcp/index.ts CHANGED
@@ -123,9 +123,15 @@ server.tool("log_push_batch", {
123
123
  project_id: z.string().optional(), service: z.string().optional(),
124
124
  trace_id: z.string().optional(), metadata: z.record(z.unknown()).optional(),
125
125
  })),
126
- }, ({ entries }) => {
127
- const rows = ingestBatch(db, entries.map(e => ({ ...e, project_id: rp(e.project_id) })))
128
- return { content: [{ type: "text", text: `Logged ${rows.length} entries` }] }
126
+ trace_id: z.string().optional().describe("Shared trace_id applied to all entries that don't have their own trace_id"),
127
+ project_id: z.string().optional().describe("Shared project_id applied to all entries (individual entry project_id takes precedence)"),
128
+ }, ({ entries, trace_id, project_id }) => {
129
+ const mapped = entries.map(e => ({
130
+ ...e,
131
+ project_id: rp(e.project_id ?? project_id),
132
+ }))
133
+ const rows = ingestBatch(db, mapped, trace_id)
134
+ return { content: [{ type: "text", text: `Logged ${rows.length} entries${trace_id ? ` (trace: ${trace_id})` : ''}` }] }
129
135
  })
130
136
 
131
137
  server.tool("log_search", {
@@ -300,5 +306,26 @@ server.tool("get_health", {}, () => ({
300
306
  content: [{ type: "text", text: JSON.stringify(getHealth(db)) }]
301
307
  }))
302
308
 
309
+ server.tool("log_stats", {
310
+ project_id: z.string().optional().describe("Project name or ID (scope stats to a project)"),
311
+ }, (args) => {
312
+ const projectId = rp(args.project_id)
313
+ const pFilter = projectId ? `WHERE project_id = ?` : ""
314
+ const pAnd = projectId ? `AND project_id = ?` : ""
315
+ const pParam = projectId ? [projectId] : []
316
+
317
+ const total = (db.query(`SELECT COUNT(*) as c FROM logs ${pFilter}`).get(...pParam) as { c: number }).c
318
+ const oldest = (db.query(`SELECT MIN(timestamp) as t FROM logs ${pFilter}`).get(...pParam) as { t: string | null }).t
319
+ const newest = (db.query(`SELECT MAX(timestamp) as t FROM logs ${pFilter}`).get(...pParam) as { t: string | null }).t
320
+ 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 }[]
321
+ 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 }[]
322
+ 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 }[]
323
+ const errors = (byLevel.find(r => r.level === "error")?.c ?? 0) + (byLevel.find(r => r.level === "fatal")?.c ?? 0)
324
+ const error_rate_pct = total > 0 ? parseFloat(((errors / total) * 100).toFixed(2)) : 0
325
+ return {
326
+ 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 }) }]
327
+ }
328
+ })
329
+
303
330
  const transport = new StdioServerTransport()
304
331
  await server.connect(transport)