@hasna/logs 0.3.19 → 0.3.21

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.
@@ -1,39 +0,0 @@
1
- // @bun
2
- import {
3
- __require
4
- } from "./index-re3ntm60.js";
5
-
6
- // src/lib/health.ts
7
- var startTime = Date.now();
8
- function getHealth(db) {
9
- const projects = db.prepare("SELECT COUNT(*) as c FROM projects").get().c;
10
- const total_logs = db.prepare("SELECT COUNT(*) as c FROM logs").get().c;
11
- const scheduler_jobs = db.prepare("SELECT COUNT(*) as c FROM scan_jobs WHERE enabled = 1").get().c;
12
- const open_issues = db.prepare("SELECT COUNT(*) as c FROM issues WHERE status = 'open'").get().c;
13
- const levelRows = db.prepare("SELECT level, COUNT(*) as c FROM logs GROUP BY level").all();
14
- const logs_by_level = Object.fromEntries(levelRows.map((r) => [r.level, r.c]));
15
- const oldest = db.prepare("SELECT MIN(timestamp) as t FROM logs").get();
16
- const newest = db.prepare("SELECT MAX(timestamp) as t FROM logs").get();
17
- let db_size_bytes = null;
18
- try {
19
- const dbPath = process.env.HASNA_LOGS_DB_PATH ?? process.env.LOGS_DB_PATH;
20
- if (dbPath) {
21
- const { statSync } = __require("fs");
22
- db_size_bytes = statSync(dbPath).size;
23
- }
24
- } catch {}
25
- return {
26
- status: "ok",
27
- uptime_seconds: Math.floor((Date.now() - startTime) / 1000),
28
- db_size_bytes,
29
- projects,
30
- total_logs,
31
- logs_by_level,
32
- oldest_log: oldest.t,
33
- newest_log: newest.t,
34
- scheduler_jobs,
35
- open_issues
36
- };
37
- }
38
-
39
- export { getHealth };
@@ -1,51 +0,0 @@
1
- // @bun
2
- import {
3
- parseTime
4
- } from "./index-997bkzr2.js";
5
-
6
- // src/lib/count.ts
7
- function countLogs(db, opts) {
8
- const conditions = [];
9
- const params = {};
10
- if (opts.project_id) {
11
- conditions.push("project_id = $p");
12
- params.$p = opts.project_id;
13
- }
14
- if (opts.service) {
15
- conditions.push("service = $service");
16
- params.$service = opts.service;
17
- }
18
- if (opts.level) {
19
- conditions.push("level = $level");
20
- params.$level = opts.level;
21
- }
22
- const since = parseTime(opts.since);
23
- const until = parseTime(opts.until);
24
- if (since) {
25
- conditions.push("timestamp >= $since");
26
- params.$since = since;
27
- }
28
- if (until) {
29
- conditions.push("timestamp <= $until");
30
- params.$until = until;
31
- }
32
- const where = conditions.length ? `WHERE ${conditions.join(" AND ")}` : "";
33
- const byLevel = db.prepare(`SELECT level, COUNT(*) as c FROM logs ${where} GROUP BY level`).all(params);
34
- const by_level = Object.fromEntries(byLevel.map((r) => [r.level, r.c]));
35
- const total = byLevel.reduce((s, r) => s + r.c, 0);
36
- let by_service;
37
- if (opts.group_by === "service") {
38
- const bySvc = db.prepare(`SELECT COALESCE(service, '-') as service, COUNT(*) as c FROM logs ${where} GROUP BY service ORDER BY c DESC`).all(params);
39
- by_service = Object.fromEntries(bySvc.map((r) => [r.service, r.c]));
40
- }
41
- return {
42
- total,
43
- errors: by_level["error"] ?? 0,
44
- warns: by_level["warn"] ?? 0,
45
- fatals: by_level["fatal"] ?? 0,
46
- by_level,
47
- by_service
48
- };
49
- }
50
-
51
- export { countLogs };
@@ -1,70 +0,0 @@
1
- // @bun
2
- // src/lib/export.ts
3
- function* iterLogs(db, opts) {
4
- const conditions = [];
5
- const params = {};
6
- if (opts.project_id) {
7
- conditions.push("project_id = $p");
8
- params.$p = opts.project_id;
9
- }
10
- if (opts.since) {
11
- conditions.push("timestamp >= $since");
12
- params.$since = opts.since;
13
- }
14
- if (opts.until) {
15
- conditions.push("timestamp <= $until");
16
- params.$until = opts.until;
17
- }
18
- if (opts.level) {
19
- conditions.push("level = $level");
20
- params.$level = opts.level;
21
- }
22
- if (opts.service) {
23
- conditions.push("service = $service");
24
- params.$service = opts.service;
25
- }
26
- const where = conditions.length ? `WHERE ${conditions.join(" AND ")}` : "";
27
- const limit = opts.limit ?? 1e5;
28
- let offset = 0;
29
- while (offset < limit) {
30
- const batch = db.prepare(`SELECT * FROM logs ${where} ORDER BY timestamp ASC LIMIT 1000 OFFSET $offset`).all({ ...params, $offset: offset });
31
- if (!batch.length)
32
- break;
33
- yield* batch;
34
- offset += batch.length;
35
- if (batch.length < 1000)
36
- break;
37
- }
38
- }
39
- function exportToJson(db, opts, writeLine) {
40
- writeLine("[");
41
- let count = 0;
42
- for (const row of iterLogs(db, opts)) {
43
- writeLine((count > 0 ? "," : "") + JSON.stringify(row));
44
- count++;
45
- }
46
- writeLine("]");
47
- return count;
48
- }
49
- var CSV_HEADER = `id,timestamp,level,service,message,trace_id,url
50
- `;
51
- function exportToCsv(db, opts, writeLine) {
52
- writeLine(CSV_HEADER);
53
- let count = 0;
54
- for (const row of iterLogs(db, opts)) {
55
- const fields = [row.id, row.timestamp, row.level, row.service ?? "", escapeCSV(row.message), row.trace_id ?? "", row.url ?? ""];
56
- writeLine(fields.join(",") + `
57
- `);
58
- count++;
59
- }
60
- return count;
61
- }
62
- function escapeCSV(s) {
63
- if (s.includes(",") || s.includes('"') || s.includes(`
64
- `)) {
65
- return `"${s.replace(/"/g, '""')}"`;
66
- }
67
- return s;
68
- }
69
-
70
- export { exportToJson, exportToCsv };