@uniquedivine/tg-sai 0.0.4 → 0.1.0

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.
@@ -0,0 +1,698 @@
1
+ /**
2
+ * `tg-sai events`: read-only views over the telemetry database. Output is a
3
+ * TOON table by default and the same projection as JSON with `--json`.
4
+ */
5
+ import type { Database } from "bun:sqlite"
6
+ import type { Command } from "commander"
7
+ import { existsSync, statSync } from "node:fs"
8
+
9
+ import type { ProfileStoreEnv } from "../profiles"
10
+ import {
11
+ LOG_LEVELS,
12
+ telemetryDbPath,
13
+ type EventRow,
14
+ type LogLevel,
15
+ } from "./db"
16
+ import {
17
+ countEvents,
18
+ eventStats,
19
+ exportEvents,
20
+ floodReport,
21
+ logEvents,
22
+ openTelemetryReadOnly,
23
+ parseCountBy,
24
+ recentRuns,
25
+ showTimeline,
26
+ tailEvents,
27
+ uncertainCalls,
28
+ type CountLayer,
29
+ } from "./queries"
30
+
31
+ // ---------------------------------------------------------------------------
32
+ // Durations and option parsing
33
+
34
+ const DURATION_UNITS_MS: Readonly<Record<string, number>> = {
35
+ ms: 1,
36
+ s: 1000,
37
+ m: 60_000,
38
+ h: 3_600_000,
39
+ d: 86_400_000,
40
+ w: 604_800_000,
41
+ }
42
+
43
+ /**
44
+ * Parses `30m`, `24h`, `7d`, `1.5h`, `90s`, `2w`, or `500ms` into
45
+ * milliseconds. Throws on anything else, including zero or negative values.
46
+ */
47
+ export function parseDuration(value: string): number {
48
+ const match = /^(\d+(?:\.\d+)?)(ms|s|m|h|d|w)$/.exec(value.trim())
49
+ if (!match) {
50
+ throw new Error(
51
+ `Invalid duration "${value}"; use a number and unit, e.g. 30m, 24h, 7d`,
52
+ )
53
+ }
54
+ const amount = Number(match[1])
55
+ const unit = DURATION_UNITS_MS[match[2] ?? ""] ?? 0
56
+ const ms = Math.round(amount * unit)
57
+ if (!(ms > 0)) throw new Error(`Duration must be positive: "${value}"`)
58
+ return ms
59
+ }
60
+
61
+ /** `now - duration`, the inclusive lower bound for `--since`. */
62
+ export function sinceCutoff(value: string, nowMs: number): number {
63
+ return nowMs - parseDuration(value)
64
+ }
65
+
66
+ export function parsePositiveInt(value: string, flag: string): number {
67
+ const n = Number(value)
68
+ if (!Number.isSafeInteger(n) || n <= 0) {
69
+ throw new Error(`${flag} must be a positive integer, got "${value}"`)
70
+ }
71
+ return n
72
+ }
73
+
74
+ export function parseAccountId(value: string): number {
75
+ const n = Number(value)
76
+ if (!/^\d+$/.test(value.trim()) || !Number.isSafeInteger(n)) {
77
+ throw new Error(`--account must be a numeric Telegram user id: "${value}"`)
78
+ }
79
+ return n
80
+ }
81
+
82
+ export function parseLevel(value: string): LogLevel {
83
+ const level = value.trim().toLowerCase()
84
+ if ((LOG_LEVELS as readonly string[]).includes(level)) {
85
+ return level as LogLevel
86
+ }
87
+ throw new Error(
88
+ `--level must be one of ${LOG_LEVELS.join(", ")}, got "${value}"`,
89
+ )
90
+ }
91
+
92
+ export function parseLayer(value: string): CountLayer {
93
+ if (value === "attempt" || value === "call") return value
94
+ throw new Error(`--layer must be attempt or call, got "${value}"`)
95
+ }
96
+
97
+ // ---------------------------------------------------------------------------
98
+ // TOON encoding
99
+
100
+ export type Cell = string | number | boolean | null | undefined | object
101
+
102
+ const NUMERIC_LIKE = /^-?(?:\d+(?:\.\d+)?(?:e[+-]?\d+)?|0\d+)$/i
103
+ const NEEDS_QUOTE = /[:"\\[\]{},\n\r\t]/
104
+
105
+ function quoteToon(value: string): string {
106
+ const escaped = value
107
+ .replace(/\\/g, "\\\\")
108
+ .replace(/"/g, '\\"')
109
+ .replace(/\n/g, "\\n")
110
+ .replace(/\r/g, "\\r")
111
+ .replace(/\t/g, "\\t")
112
+ return `"${escaped}"`
113
+ }
114
+
115
+ /** Encodes one TOON primitive, quoting strings that would be ambiguous. */
116
+ export function toonValue(value: Cell): string {
117
+ if (value === null || value === undefined) return "null"
118
+ if (typeof value === "number") {
119
+ return Number.isFinite(value) ? String(value) : "null"
120
+ }
121
+ if (typeof value === "boolean") return String(value)
122
+ if (typeof value === "object") return quoteToon(JSON.stringify(value))
123
+ const needsQuote =
124
+ value === "" ||
125
+ value !== value.trim() ||
126
+ value === "true" ||
127
+ value === "false" ||
128
+ value === "null" ||
129
+ value.startsWith("-") ||
130
+ NUMERIC_LIKE.test(value) ||
131
+ NEEDS_QUOTE.test(value)
132
+ return needsQuote ? quoteToon(value) : value
133
+ }
134
+
135
+ /**
136
+ * Encodes rows as a TOON tabular array: `name[N]{a,b}:` then one indented,
137
+ * comma-separated line per row. An empty table is `name[0]:`.
138
+ */
139
+ export function toonTable(
140
+ name: string,
141
+ fields: readonly string[],
142
+ rows: readonly Record<string, Cell>[],
143
+ ): string {
144
+ if (rows.length === 0) return `${name}[0]:`
145
+ const header = `${name}[${rows.length}]{${fields.join(",")}}:`
146
+ const lines = rows.map(
147
+ (row) => " " + fields.map((f) => toonValue(row[f])).join(","),
148
+ )
149
+ return [header, ...lines].join("\n")
150
+ }
151
+
152
+ /** Encodes `key: value` lines for a flat object. */
153
+ export function toonObject(entries: Record<string, Cell>): string {
154
+ return Object.entries(entries)
155
+ .map(([key, value]) => `${key}: ${toonValue(value)}`)
156
+ .join("\n")
157
+ }
158
+
159
+ // ---------------------------------------------------------------------------
160
+ // Projections shared by TOON and JSON output
161
+
162
+ export function isoTime(ms: number | null | undefined): string | null {
163
+ if (ms === null || ms === undefined) return null
164
+ return new Date(ms).toISOString()
165
+ }
166
+
167
+ function parseJsonColumn(value: string | null): unknown {
168
+ if (value === null) return null
169
+ try {
170
+ return JSON.parse(value)
171
+ } catch {
172
+ return value
173
+ }
174
+ }
175
+
176
+ export const TAIL_FIELDS = [
177
+ "ts",
178
+ "event",
179
+ "outcome",
180
+ "account_id",
181
+ "method",
182
+ "attempt",
183
+ "duration_ms",
184
+ "error_code",
185
+ "err",
186
+ "wait_s",
187
+ "request_id",
188
+ ] as const
189
+
190
+ export function tailProjection(row: EventRow): Record<string, Cell> {
191
+ return {
192
+ ts: isoTime(row.ts_ms),
193
+ event: row.event,
194
+ outcome: row.outcome,
195
+ account_id: row.account_id,
196
+ method: row.method,
197
+ attempt: row.attempt,
198
+ duration_ms: row.duration_ms,
199
+ error_code: row.error_code,
200
+ err: row.err,
201
+ wait_s: row.wait_s,
202
+ request_id: row.request_id,
203
+ }
204
+ }
205
+
206
+ export const SHOW_FIELDS = [
207
+ "ts",
208
+ "event",
209
+ "outcome",
210
+ "method",
211
+ "attempt",
212
+ "duration_ms",
213
+ "error_code",
214
+ "err",
215
+ "wait_s",
216
+ "request_id",
217
+ "job_id",
218
+ "level",
219
+ "msg",
220
+ "req",
221
+ "res",
222
+ "attrs",
223
+ ] as const
224
+
225
+ export function showProjection(row: EventRow): Record<string, Cell> {
226
+ return {
227
+ ts: isoTime(row.ts_ms),
228
+ event: row.event,
229
+ outcome: row.outcome,
230
+ level: row.level ?? null,
231
+ msg: row.msg ?? null,
232
+ method: row.method,
233
+ attempt: row.attempt,
234
+ duration_ms: row.duration_ms,
235
+ error_code: row.error_code,
236
+ err: row.err,
237
+ wait_s: row.wait_s,
238
+ request_id: row.request_id,
239
+ job_id: row.job_id,
240
+ req: parseJsonColumn(row.req) as Cell,
241
+ res: parseJsonColumn(row.res) as Cell,
242
+ attrs: parseJsonColumn(row.attrs ?? null) as Cell,
243
+ }
244
+ }
245
+
246
+ export const LOG_FIELDS = [
247
+ "ts",
248
+ "level",
249
+ "event",
250
+ "subsystem",
251
+ "msg",
252
+ "attrs",
253
+ "account_id",
254
+ "job_id",
255
+ "run_id",
256
+ ] as const
257
+
258
+ export function logProjection(row: EventRow): Record<string, Cell> {
259
+ return {
260
+ ts: isoTime(row.ts_ms),
261
+ level: row.level,
262
+ event: row.event,
263
+ subsystem: row.subsystem,
264
+ msg: row.msg,
265
+ attrs: parseJsonColumn(row.attrs) as Cell,
266
+ account_id: row.account_id,
267
+ job_id: row.job_id,
268
+ run_id: row.run_id,
269
+ }
270
+ }
271
+
272
+ export const UNCERTAIN_FIELDS = [
273
+ "ts",
274
+ "age_s",
275
+ "account_id",
276
+ "method",
277
+ "command",
278
+ "run_id",
279
+ "request_id",
280
+ ] as const
281
+
282
+ export const RUN_FIELDS = [
283
+ "started",
284
+ "run_id",
285
+ "command",
286
+ "subsystem",
287
+ "outcome",
288
+ "error_code",
289
+ "err",
290
+ "duration_ms",
291
+ "calls",
292
+ "errors",
293
+ "attempts",
294
+ "ticks",
295
+ ] as const
296
+
297
+ export const FLOOD_FIELDS = [
298
+ "event",
299
+ "account_id",
300
+ "method",
301
+ "wait_s",
302
+ "count",
303
+ "latest",
304
+ ] as const
305
+
306
+ export const ACTIVE_FLOOD_FIELDS = [
307
+ "account_id",
308
+ "method",
309
+ "wait_s",
310
+ "started",
311
+ "ends",
312
+ "remaining_s",
313
+ "request_id",
314
+ ] as const
315
+
316
+ // ---------------------------------------------------------------------------
317
+ // Command wiring
318
+
319
+ export interface EventsCliContext {
320
+ env?: ProfileStoreEnv
321
+ /** Overrides `telemetryDbPath(env)`. */
322
+ dbPath?: string
323
+ now?: () => number
324
+ stdout?: (text: string) => void
325
+ stderr?: (text: string) => void
326
+ }
327
+
328
+ interface ResolvedContext {
329
+ path: string
330
+ now: () => number
331
+ out: (text: string) => void
332
+ err: (text: string) => void
333
+ }
334
+
335
+ function resolveContext(ctx: EventsCliContext): ResolvedContext {
336
+ return {
337
+ path: ctx.dbPath ?? telemetryDbPath(ctx.env ?? process.env),
338
+ now: ctx.now ?? Date.now,
339
+ out: ctx.stdout ?? ((text) => process.stdout.write(`${text}\n`)),
340
+ err: ctx.stderr ?? ((text) => process.stderr.write(`${text}\n`)),
341
+ }
342
+ }
343
+
344
+ /**
345
+ * Opens the database read-only and runs `fn`. A missing database prints a
346
+ * note to stderr and returns without error: nothing has been recorded yet.
347
+ */
348
+ export function withReadOnlyDb(
349
+ ctx: ResolvedContext,
350
+ fn: (db: Database) => void,
351
+ ): void {
352
+ if (!existsSync(ctx.path)) {
353
+ ctx.err(
354
+ `No telemetry database at ${ctx.path}; nothing has been recorded yet.`,
355
+ )
356
+ return
357
+ }
358
+ const db = openTelemetryReadOnly(ctx.path)
359
+ try {
360
+ fn(db)
361
+ } finally {
362
+ db.close()
363
+ }
364
+ }
365
+
366
+ function emit(
367
+ ctx: ResolvedContext,
368
+ json: boolean | undefined,
369
+ name: string,
370
+ fields: readonly string[],
371
+ rows: Record<string, Cell>[],
372
+ ): void {
373
+ if (json) ctx.out(JSON.stringify(rows, null, 2))
374
+ else ctx.out(toonTable(name, fields, rows))
375
+ }
376
+
377
+ interface TailCliOptions {
378
+ limit: string
379
+ account?: string
380
+ method?: string
381
+ errors?: boolean
382
+ run?: string
383
+ since?: string
384
+ json?: boolean
385
+ }
386
+
387
+ interface LogsCliOptions {
388
+ since: string
389
+ level?: string
390
+ subsystem?: string
391
+ run?: string
392
+ job?: string
393
+ limit: string
394
+ json?: boolean
395
+ }
396
+
397
+ interface SinceCliOptions {
398
+ since: string
399
+ account?: string
400
+ json?: boolean
401
+ }
402
+
403
+ interface CountsCliOptions extends SinceCliOptions {
404
+ by: string
405
+ layer: string
406
+ }
407
+
408
+ /**
409
+ * Registers `tg-sai events` and its subcommands on `program`. `context` is
410
+ * for tests; production uses the environment, clock, and stdio.
411
+ */
412
+ export function registerEventsCommand(
413
+ program: Command,
414
+ context: EventsCliContext = {},
415
+ ): Command {
416
+ const ctx = resolveContext(context)
417
+ const events = program
418
+ .command("events")
419
+ .description(
420
+ "Read the local telemetry database: Telegram calls and app logs " +
421
+ "(read-only)",
422
+ )
423
+ .addHelpText(
424
+ "after",
425
+ `
426
+ Examples:
427
+ tg-sai events tail --account 8274304175 --errors
428
+ tg-sai events logs --since 24h --level warn --subsystem worker
429
+ tg-sai events show <request_id|run_id|job_id>
430
+ tg-sai events counts --since 24h --by method,outcome
431
+ tg-sai events floods --since 7d
432
+ tg-sai events export --since 7d > events.jsonl
433
+
434
+ Output is TOON by default; add --json for JSON. Durations: 30m, 24h, 7d.
435
+ `,
436
+ )
437
+ .action(() => {
438
+ events.help({ error: false })
439
+ })
440
+
441
+ events
442
+ .command("tail")
443
+ .description("Newest calls, attempts, and flood waits, one per line")
444
+ .option("--limit <n>", "Maximum rows", "50")
445
+ .option("--account <id>", "Only this Telegram account id")
446
+ .option("--method <name>", "Only this TL method, e.g. messages.getDialogs")
447
+ .option("--errors", "Only failed rows and flood waits")
448
+ .option("--run <run_id>", "Only rows from this run")
449
+ .option("--since <duration>", "Only rows newer than this, e.g. 24h")
450
+ .option("--json", "Print JSON")
451
+ .action((options: TailCliOptions) => {
452
+ const limit = parsePositiveInt(options.limit, "--limit")
453
+ const filter = {
454
+ limit,
455
+ accountId:
456
+ options.account === undefined
457
+ ? undefined
458
+ : parseAccountId(options.account),
459
+ method: options.method,
460
+ runId: options.run,
461
+ errorsOnly: options.errors,
462
+ sinceMs:
463
+ options.since === undefined
464
+ ? undefined
465
+ : sinceCutoff(options.since, ctx.now()),
466
+ }
467
+ withReadOnlyDb(ctx, (db) => {
468
+ const rows = tailEvents(db, filter).map(tailProjection)
469
+ emit(ctx, options.json, "events", TAIL_FIELDS, rows)
470
+ })
471
+ })
472
+
473
+ events
474
+ .command("logs")
475
+ .description("Newest app log rows (rows with a level), one per line")
476
+ .option("--since <duration>", "Window, e.g. 24h", "24h")
477
+ .option(
478
+ "--level <level>",
479
+ "Minimum level: debug, info, warn, or error",
480
+ )
481
+ .option("--subsystem <name>", "Only this subsystem: worker, health, cli")
482
+ .option("--run <run_id>", "Only rows from this run")
483
+ .option("--job <job_id>", "Only rows for this job (setup request id)")
484
+ .option("--limit <n>", "Maximum rows", "100")
485
+ .option("--json", "Print JSON")
486
+ .action((options: LogsCliOptions) => {
487
+ const query = {
488
+ limit: parsePositiveInt(options.limit, "--limit"),
489
+ sinceMs: sinceCutoff(options.since, ctx.now()),
490
+ minLevel:
491
+ options.level === undefined ? undefined : parseLevel(options.level),
492
+ subsystem: options.subsystem,
493
+ runId: options.run,
494
+ jobId: options.job,
495
+ }
496
+ withReadOnlyDb(ctx, (db) => {
497
+ const rows = logEvents(db, query).map(logProjection)
498
+ emit(ctx, options.json, "logs", LOG_FIELDS, rows)
499
+ })
500
+ })
501
+
502
+ events
503
+ .command("show <id>")
504
+ .description("Timeline of every row sharing a request, run, or job id")
505
+ .option("--json", "Print JSON")
506
+ .action((id: string, options: { json?: boolean }) => {
507
+ withReadOnlyDb(ctx, (db) => {
508
+ const rows = showTimeline(db, id).map(showProjection)
509
+ if (rows.length === 0) ctx.err(`No telemetry rows match ${id}`)
510
+ emit(ctx, options.json, "timeline", SHOW_FIELDS, rows)
511
+ })
512
+ })
513
+
514
+ events
515
+ .command("counts")
516
+ .description("Row counts grouped by fields")
517
+ .option("--since <duration>", "Window, e.g. 24h", "24h")
518
+ .option("--by <fields>", "Comma-separated group fields", "method,outcome")
519
+ .option(
520
+ "--layer <layer>",
521
+ "attempt (requests sent to Telegram) or call (finished calls)",
522
+ "attempt",
523
+ )
524
+ .option("--account <id>", "Only this Telegram account id")
525
+ .option("--json", "Print JSON")
526
+ .action((options: CountsCliOptions) => {
527
+ const by = parseCountBy(options.by)
528
+ const layer = parseLayer(options.layer)
529
+ const sinceMs = sinceCutoff(options.since, ctx.now())
530
+ const accountId =
531
+ options.account === undefined
532
+ ? undefined
533
+ : parseAccountId(options.account)
534
+ withReadOnlyDb(ctx, (db) => {
535
+ const rows = countEvents(db, { sinceMs, accountId, by, layer })
536
+ emit(ctx, options.json, "counts", [...by, "count"], rows)
537
+ })
538
+ })
539
+
540
+ events
541
+ .command("floods")
542
+ .description("Flood waits by method and wait, plus waits still active")
543
+ .option("--since <duration>", "Window, e.g. 7d", "7d")
544
+ .option("--account <id>", "Only this Telegram account id")
545
+ .option("--json", "Print JSON")
546
+ .action((options: SinceCliOptions) => {
547
+ const nowMs = ctx.now()
548
+ const sinceMs = sinceCutoff(options.since, nowMs)
549
+ const accountId =
550
+ options.account === undefined
551
+ ? undefined
552
+ : parseAccountId(options.account)
553
+ withReadOnlyDb(ctx, (db) => {
554
+ const report = floodReport(db, { sinceMs, accountId, nowMs })
555
+ const floods = report.floods.map((row) => ({
556
+ event: row.event,
557
+ account_id: row.account_id,
558
+ method: row.method,
559
+ wait_s: row.wait_s,
560
+ count: row.count,
561
+ latest: isoTime(row.latest_ms),
562
+ }))
563
+ const active = report.active.map((row) => ({
564
+ account_id: row.account_id,
565
+ method: row.method,
566
+ wait_s: row.wait_s,
567
+ started: isoTime(row.started_ms),
568
+ ends: isoTime(row.ends_ms),
569
+ remaining_s: Math.ceil((row.ends_ms - nowMs) / 1000),
570
+ request_id: row.request_id,
571
+ }))
572
+ if (options.json) {
573
+ ctx.out(JSON.stringify({ floods, active }, null, 2))
574
+ return
575
+ }
576
+ ctx.out(toonTable("floods", FLOOD_FIELDS, floods))
577
+ ctx.out(toonTable("active", ACTIVE_FLOOD_FIELDS, active))
578
+ })
579
+ })
580
+
581
+ events
582
+ .command("uncertain")
583
+ .description("Calls that started and never recorded a completion")
584
+ .option("--since <duration>", "Window, e.g. 7d", "7d")
585
+ .option("--json", "Print JSON")
586
+ .action((options: { since: string; json?: boolean }) => {
587
+ const nowMs = ctx.now()
588
+ const sinceMs = sinceCutoff(options.since, nowMs)
589
+ withReadOnlyDb(ctx, (db) => {
590
+ const rows = uncertainCalls(db, { sinceMs }).map((row) => ({
591
+ ts: isoTime(row.ts_ms),
592
+ age_s: Math.floor((nowMs - row.ts_ms) / 1000),
593
+ account_id: row.account_id,
594
+ method: row.method,
595
+ command: row.command,
596
+ run_id: row.run_id,
597
+ request_id: row.request_id,
598
+ }))
599
+ emit(ctx, options.json, "uncertain", UNCERTAIN_FIELDS, rows)
600
+ })
601
+ })
602
+
603
+ events
604
+ .command("runs")
605
+ .description("Recent runs with outcome, duration, and error count")
606
+ .option("--limit <n>", "Maximum runs", "20")
607
+ .option("--json", "Print JSON")
608
+ .action((options: { limit: string; json?: boolean }) => {
609
+ const limit = parsePositiveInt(options.limit, "--limit")
610
+ withReadOnlyDb(ctx, (db) => {
611
+ const rows = recentRuns(db, limit).map((row) => ({
612
+ started: isoTime(row.started_ms),
613
+ run_id: row.run_id,
614
+ command: row.command,
615
+ subsystem: row.subsystem,
616
+ outcome: row.outcome ?? "unfinished",
617
+ error_code: row.error_code,
618
+ err: row.err,
619
+ duration_ms: row.duration_ms,
620
+ calls: row.calls,
621
+ errors: row.errors,
622
+ attempts: row.attempts,
623
+ ticks: row.ticks,
624
+ }))
625
+ emit(ctx, options.json, "runs", RUN_FIELDS, rows)
626
+ })
627
+ })
628
+
629
+ events
630
+ .command("stats")
631
+ .description("Database path, size, row count, and rows per event kind")
632
+ .option("--json", "Print JSON")
633
+ .action((options: { json?: boolean }) => {
634
+ withReadOnlyDb(ctx, (db) => {
635
+ const stats = eventStats(db)
636
+ const summary = {
637
+ path: ctx.path,
638
+ size_bytes: fileSize(ctx.path),
639
+ wal_bytes: fileSize(`${ctx.path}-wal`),
640
+ rows: stats.rows,
641
+ log_rows: stats.log_rows,
642
+ oldest: isoTime(stats.oldest_ms),
643
+ newest: isoTime(stats.newest_ms),
644
+ }
645
+ if (options.json) {
646
+ ctx.out(
647
+ JSON.stringify(
648
+ { ...summary, kinds: stats.kinds, levels: stats.levels },
649
+ null,
650
+ 2,
651
+ ),
652
+ )
653
+ return
654
+ }
655
+ ctx.out(toonObject(summary))
656
+ const kinds = stats.kinds.map((k) => ({ event: k.event, rows: k.rows }))
657
+ ctx.out(toonTable("kinds", ["event", "rows"], kinds))
658
+ const levels = stats.levels.map((l) => ({ level: l.level, rows: l.rows }))
659
+ ctx.out(toonTable("levels", ["level", "rows"], levels))
660
+ })
661
+ })
662
+
663
+ events
664
+ .command("export")
665
+ .description("Every row as JSONL on stdout, oldest first")
666
+ .option("--since <duration>", "Window, e.g. 7d", "7d")
667
+ .action((options: { since: string }) => {
668
+ const sinceMs = sinceCutoff(options.since, ctx.now())
669
+ withReadOnlyDb(ctx, (db) => {
670
+ for (const row of exportEvents(db, { sinceMs })) {
671
+ ctx.out(JSON.stringify(exportProjection(row)))
672
+ }
673
+ })
674
+ })
675
+
676
+ return events
677
+ }
678
+
679
+ /** Full row with `req`/`res` parsed back to JSON values. */
680
+ export function exportProjection(row: EventRow): Record<string, unknown> {
681
+ return {
682
+ ...row,
683
+ req: parseJsonColumn(row.req),
684
+ res: parseJsonColumn(row.res),
685
+ ...(row.attrs === undefined
686
+ ? {}
687
+ : { attrs: parseJsonColumn(row.attrs) }),
688
+ }
689
+ }
690
+
691
+ /** File size in bytes, or 0 when the file does not exist. */
692
+ export function fileSize(path: string): number {
693
+ try {
694
+ return statSync(path).size
695
+ } catch {
696
+ return 0
697
+ }
698
+ }