-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathevents.ts
More file actions
327 lines (275 loc) · 9.91 KB
/
events.ts
File metadata and controls
327 lines (275 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// src/lib/events.ts
//
// Functions: parseLogLine, sanitizeLogDetail, classifyLogEvent, pinoLevelToSeverity,
// snapshotToEvent, backupToEvent, mergeAndSort, groupPollBatches
import { sanitizeNetworkError } from "@/lib/error-utils"
import { bytesToGiB, formatBytesNum, formatRatioDisplay } from "@/lib/formatters"
// ── Types ────────────────────────────────────────────────────────────────────
export const EVENT_CATEGORIES = ["polls", "clients", "auth", "settings", "backups"] as const
export type EventCategory = (typeof EVENT_CATEGORIES)[number]
export const EVENT_LEVELS = ["debug", "info", "warn", "error"] as const
export type EventLevel = (typeof EVENT_LEVELS)[number]
export interface SystemEvent {
id: string
timestamp: string // ISO 8601
category: EventCategory
level: EventLevel
title: string
detail: string | null
trackerId: number | null
trackerName: string | null
source: "log" | "db"
children?: SystemEvent[]
}
// ── Constants ────────────────────────────────────────────────────────────────
/** No-op: single-user self-hosted app, events tab is auth-gated. */
function redactIps(s: string): string {
return s
}
/** Pino numeric levels → our severity */
function pinoLevelToSeverity(level: number): EventLevel {
if (level >= 50) return "error" // error=50, fatal=60
if (level >= 40) return "warn" // warn=40
if (level >= 30) return "info" // info=30
return "debug" // debug=20, trace=10
}
// Auth event discriminators from existing structured log calls
const AUTH_EVENTS = new Set([
"login_success",
"login_failed",
"logout",
"totp_verified",
"totp_failed",
"session_expired",
"lockout_triggered",
])
// Backup event discriminators
const BACKUP_EVENTS = new Set([
"restore_starting",
"restore_completed",
"restore_failed",
"backup_created",
"backup_pruned",
])
// Settings/CRUD route prefixes
const SETTINGS_ROUTES = [
"PATCH /api/settings",
"PATCH /api/trackers/[id]",
"POST /api/trackers",
"DELETE /api/trackers",
"POST /api/clients",
"PATCH /api/clients",
"DELETE /api/clients",
]
// ── Classification ───────────────────────────────────────────────────────────
interface PinoLine {
level: number
time: number
msg?: string
event?: string
route?: string
trackerId?: number
trackerName?: string
clientId?: number
clientName?: string
ip?: string
action?: string
[key: string]: unknown
}
function classifyLogEvent(line: PinoLine): EventCategory {
// Auth events identified by structured `event` field
if (line.event && AUTH_EVENTS.has(line.event)) return "auth"
// Backup events
if (line.event && BACKUP_EVENTS.has(line.event)) return "backups"
// Settings/CRUD routes
if (line.route && SETTINGS_ROUTES.some((r) => line.route?.startsWith(r))) return "settings"
// Download client events (heartbeat, deep-poll, client connection)
if (line.clientId || (line.msg && /heartbeat|deep.poll|client.scheduler/i.test(line.msg))) {
return "clients"
}
// Tracker poll events
if (line.trackerId || (line.msg && /poll/i.test(line.msg))) return "polls"
// Default: settings (system startup, config, uncategorized errors)
return "settings"
}
// ── Sanitization ─────────────────────────────────────────────────────────────
function sanitizeLogDetail(line: PinoLine): string | null {
const parts: string[] = []
// Include tracker/client name so errors are identifiable
const entityName = line.trackerName ?? line.clientName
if (entityName && typeof entityName === "string") {
parts.push(entityName)
}
// Include action if present (e.g., "paused", "resumed")
if (line.action) parts.push(String(line.action))
// Include route context
if (line.route) parts.push(String(line.route))
// Include IP field — always redacted immediately
if (line.ip && typeof line.ip === "string") {
parts.push(`ip=[redacted]`)
}
// For warn+ lines, sanitize the message to strip raw error details
if (line.level >= 40 && line.msg) {
parts.push(sanitizeNetworkError(line.msg, redactIps(line.msg)))
}
// Redact any IP that leaked through from other fields
const raw = parts.length > 0 ? parts.join(" — ") : null
return raw ? redactIps(raw) : null
}
// ── Parser ───────────────────────────────────────────────────────────────────
export function parseLogLine(raw: string): SystemEvent | null {
if (!raw.trim()) return null
let line: PinoLine
try {
line = JSON.parse(raw) as PinoLine
} catch {
return null
}
// Must have at minimum: level, time, msg
if (typeof line.level !== "number" || typeof line.time !== "number") return null
if (!line.msg && !line.event) return null
const category = classifyLogEvent(line)
const severity = pinoLevelToSeverity(line.level)
return {
id: `log-${line.time}-${Math.random().toString(36).slice(2, 8)}`,
timestamp: new Date(line.time).toISOString(),
category,
level: severity,
title:
line.level >= 40
? sanitizeNetworkError(
line.msg ?? "Unknown event",
redactIps(line.msg ?? line.event ?? "Unknown event")
)
: redactIps(line.msg ?? line.event ?? "Unknown event"),
detail: sanitizeLogDetail(line),
trackerId: typeof line.trackerId === "number" ? line.trackerId : null,
trackerName: typeof line.trackerName === "string" ? line.trackerName : null,
source: "log",
}
}
// ── DB Event Converters ──────────────────────────────────────────────────────
interface SnapshotRow {
id: number
polledAt: string
uploadedBytes: string
downloadedBytes: string
ratio: number | null
trackerId: number
trackerName: string | null
}
export function snapshotToEvent(row: SnapshotRow): SystemEvent {
let upGib = "0.0"
let downGib = "0.0"
try {
upGib = bytesToGiB(row.uploadedBytes).toFixed(1)
downGib = bytesToGiB(row.downloadedBytes).toFixed(1)
} catch {
// Non-numeric byte strings default to 0
}
const ratioStr = row.ratio !== null ? formatRatioDisplay(row.ratio) : ""
return {
id: `snap-${row.id}`,
timestamp: row.polledAt,
category: "polls",
level: "info",
title: "Poll succeeded",
detail: [row.trackerName, `↑ ${upGib} GiB`, `↓ ${downGib} GiB`, ratioStr]
.filter(Boolean)
.join(" — "),
trackerId: row.trackerId,
trackerName: row.trackerName ?? null,
source: "db",
}
}
interface BackupRow {
id: number
createdAt: string
sizeBytes: number
encrypted: boolean
status: string
frequency: string | null
}
export function backupToEvent(row: BackupRow): SystemEvent {
const isError = row.status === "failed"
return {
id: `backup-${row.id}`,
timestamp: row.createdAt,
category: "backups",
level: isError ? "error" : "info",
title: `Backup ${row.status}`,
detail: [row.frequency, row.encrypted ? "encrypted" : null, formatBytesNum(row.sizeBytes)]
.filter(Boolean)
.join(" — "),
trackerId: null,
trackerName: null,
source: "db",
}
}
// ── Merge & Sort ─────────────────────────────────────────────────────────────
export function mergeAndSort(
dbEvents: SystemEvent[],
logEvents: SystemEvent[],
category: EventCategory | "all" = "all",
limit = 50,
offset = 0
): SystemEvent[] {
let combined = [...dbEvents, ...logEvents]
// Filter by category
if (category !== "all") {
combined = combined.filter((e) => e.category === category)
}
// Sort reverse chronological
combined.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime())
// Paginate
return combined.slice(offset, offset + limit)
}
// ── Batch Grouping ──────────────────────────────────────────────────────────
/**
* Collapses consecutive "Poll succeeded" events that share the same timestamp
* (from a single scheduler batch) into a single parent event with children.
* Non-poll events and lone polls pass through unchanged.
*/
export function groupPollBatches(events: SystemEvent[]): SystemEvent[] {
const result: SystemEvent[] = []
let i = 0
while (i < events.length) {
const event = events[i]
if (event.category !== "polls" || event.title !== "Poll succeeded") {
result.push(event)
i++
continue
}
// Collect consecutive poll-succeeded events with the same timestamp
const batch: SystemEvent[] = [event]
let j = i + 1
while (
j < events.length &&
events[j].category === "polls" &&
events[j].title === "Poll succeeded" &&
events[j].timestamp === event.timestamp
) {
batch.push(events[j])
j++
}
if (batch.length === 1) {
result.push(event)
} else {
const names = batch.map((e) => e.trackerName ?? "Unknown").join(", ")
result.push({
id: `batch-${event.timestamp}`,
timestamp: event.timestamp,
category: "polls",
level: "info",
title: `Poll succeeded for ${batch.length} trackers`,
detail: names,
trackerId: null,
trackerName: null,
source: "db",
children: batch,
})
}
i = j
}
return result
}