@hasna/logs 0.3.0 → 0.3.1

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,88 @@
1
+ // @bun
2
+ // src/lib/parse-time.ts
3
+ function parseTime(val) {
4
+ if (!val)
5
+ return;
6
+ const m = val.match(/^(\d+(?:\.\d+)?)(m|h|d|w)$/);
7
+ if (!m)
8
+ return val;
9
+ const n = parseFloat(m[1]);
10
+ const unit = m[2];
11
+ const ms = n * { m: 60, h: 3600, d: 86400, w: 604800 }[unit] * 1000;
12
+ return new Date(Date.now() - ms).toISOString();
13
+ }
14
+
15
+ // src/lib/query.ts
16
+ function searchLogs(db, q) {
17
+ const conditions = [];
18
+ const params = {};
19
+ if (q.project_id) {
20
+ conditions.push("l.project_id = $project_id");
21
+ params.$project_id = q.project_id;
22
+ }
23
+ if (q.page_id) {
24
+ conditions.push("l.page_id = $page_id");
25
+ params.$page_id = q.page_id;
26
+ }
27
+ if (q.service) {
28
+ conditions.push("l.service = $service");
29
+ params.$service = q.service;
30
+ }
31
+ if (q.trace_id) {
32
+ conditions.push("l.trace_id = $trace_id");
33
+ params.$trace_id = q.trace_id;
34
+ }
35
+ if (q.since) {
36
+ conditions.push("l.timestamp >= $since");
37
+ params.$since = parseTime(q.since) ?? q.since;
38
+ }
39
+ if (q.until) {
40
+ conditions.push("l.timestamp <= $until");
41
+ params.$until = parseTime(q.until) ?? q.until;
42
+ }
43
+ if (q.level) {
44
+ const levels = Array.isArray(q.level) ? q.level : [q.level];
45
+ const placeholders = levels.map((_, i) => `$level${i}`).join(",");
46
+ levels.forEach((lv, i) => {
47
+ params[`$level${i}`] = lv;
48
+ });
49
+ conditions.push(`l.level IN (${placeholders})`);
50
+ }
51
+ const limit = q.limit ?? 100;
52
+ const offset = q.offset ?? 0;
53
+ params.$limit = limit;
54
+ params.$offset = offset;
55
+ if (q.text) {
56
+ params.$text = q.text;
57
+ const where2 = conditions.length ? `WHERE ${conditions.join(" AND ")} AND` : "WHERE";
58
+ const sql2 = `
59
+ SELECT l.* FROM logs l
60
+ ${where2} l.rowid IN (SELECT rowid FROM logs_fts WHERE logs_fts MATCH $text)
61
+ ORDER BY l.timestamp DESC
62
+ LIMIT $limit OFFSET $offset
63
+ `;
64
+ return db.prepare(sql2).all(params);
65
+ }
66
+ const where = conditions.length ? `WHERE ${conditions.join(" AND ")}` : "";
67
+ const sql = `SELECT * FROM logs l ${where} ORDER BY l.timestamp DESC LIMIT $limit OFFSET $offset`;
68
+ return db.prepare(sql).all(params);
69
+ }
70
+ function tailLogs(db, projectId, n = 50) {
71
+ if (projectId) {
72
+ return db.prepare("SELECT * FROM logs WHERE project_id = $p ORDER BY timestamp DESC LIMIT $n").all({ $p: projectId, $n: n });
73
+ }
74
+ return db.prepare("SELECT * FROM logs ORDER BY timestamp DESC LIMIT $n").all({ $n: n });
75
+ }
76
+ function getLogContext(db, traceId) {
77
+ return db.prepare("SELECT * FROM logs WHERE trace_id = $t ORDER BY timestamp ASC").all({ $t: traceId });
78
+ }
79
+ function getLogContextFromId(db, logId) {
80
+ const log = db.prepare("SELECT * FROM logs WHERE id = $id").get({ $id: logId });
81
+ if (!log)
82
+ return [];
83
+ if (log.trace_id)
84
+ return getLogContext(db, log.trace_id);
85
+ return [log];
86
+ }
87
+
88
+ export { parseTime, searchLogs, tailLogs, getLogContext, getLogContextFromId };