@hasna/uptime 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,32 @@
1
+ import type { CheckResult, CreateMonitorInput, Incident, ListResultsOptions, Monitor, UpdateMonitorInput, UptimeSummary } from "./types.js";
2
+ export interface UptimeStoreOptions {
3
+ dbPath?: string;
4
+ }
5
+ export declare class UptimeStore {
6
+ readonly dbPath: string;
7
+ private readonly db;
8
+ constructor(options?: UptimeStoreOptions);
9
+ close(): void;
10
+ migrate(): void;
11
+ createMonitor(input: CreateMonitorInput): Monitor;
12
+ listMonitors(options?: {
13
+ includeDisabled?: boolean;
14
+ }): Monitor[];
15
+ getMonitor(idOrName: string): Monitor | null;
16
+ updateMonitor(idOrName: string, input: UpdateMonitorInput): Monitor;
17
+ deleteMonitor(idOrName: string): boolean;
18
+ recordCheckResult(input: Omit<CheckResult, "id" | "checkedAt"> & {
19
+ checkedAt?: string;
20
+ }): CheckResult;
21
+ listResults(options?: ListResultsOptions): CheckResult[];
22
+ listIncidents(options?: {
23
+ status?: "open" | "closed";
24
+ monitorId?: string;
25
+ limit?: number;
26
+ }): Incident[];
27
+ getOpenIncident(monitorId: string): Incident | null;
28
+ summary(): UptimeSummary;
29
+ private monitorSummary;
30
+ private reconcileIncidentInTransaction;
31
+ }
32
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EACV,WAAW,EACX,kBAAkB,EAClB,QAAQ,EACR,kBAAkB,EAClB,OAAO,EAGP,kBAAkB,EAClB,aAAa,EACd,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AA0DD,qBAAa,WAAW;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAW;gBAElB,OAAO,GAAE,kBAAuB;IAW5C,KAAK,IAAI,IAAI;IAIb,OAAO,IAAI,IAAI;IAkDf,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO;IAkDjD,YAAY,CAAC,OAAO,GAAE;QAAE,eAAe,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,OAAO,EAAE;IAOpE,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI;IAO5C,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,kBAAkB,GAAG,OAAO;IAiCnE,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAOxC,iBAAiB,CAAC,KAAK,EAAE,IAAI,CAAC,WAAW,EAAE,IAAI,GAAG,WAAW,CAAC,GAAG;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,WAAW;IAwCrG,WAAW,CAAC,OAAO,GAAE,kBAAuB,GAAG,WAAW,EAAE;IAU5D,aAAa,CAAC,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,MAAM,GAAG,QAAQ,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAO,GAAG,QAAQ,EAAE;IAmB3G,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI;IAOnD,OAAO,IAAI,aAAa;IAkBxB,OAAO,CAAC,cAAc;IAyBtB,OAAO,CAAC,8BAA8B;CA2BvC"}
package/dist/store.js ADDED
@@ -0,0 +1,431 @@
1
+ // @bun
2
+ // src/paths.ts
3
+ import { mkdirSync } from "fs";
4
+ import { homedir } from "os";
5
+ import { join } from "path";
6
+ function uptimeHome() {
7
+ return process.env.HASNA_UPTIME_HOME || join(homedir(), ".hasna", "uptime");
8
+ }
9
+ function uptimeDbPath() {
10
+ return process.env.HASNA_UPTIME_DB || join(uptimeHome(), "uptime.db");
11
+ }
12
+ function ensureUptimeHome() {
13
+ const home = uptimeHome();
14
+ mkdirSync(home, { recursive: true });
15
+ return home;
16
+ }
17
+
18
+ // src/store.ts
19
+ import { mkdirSync as mkdirSync2 } from "fs";
20
+ import { dirname } from "path";
21
+ import { randomUUID } from "crypto";
22
+ import { Database } from "bun:sqlite";
23
+
24
+ // src/limits.ts
25
+ var MIN_INTERVAL_SECONDS = 1;
26
+ var MAX_INTERVAL_SECONDS = 86400;
27
+ var MIN_TIMEOUT_MS = 1;
28
+ var MAX_TIMEOUT_MS = 60000;
29
+ var MIN_RETRY_COUNT = 0;
30
+ var MAX_RETRY_COUNT = 10;
31
+ var MAX_RESULT_LIMIT = 1000;
32
+
33
+ // src/store.ts
34
+ class UptimeStore {
35
+ dbPath;
36
+ db;
37
+ constructor(options = {}) {
38
+ this.dbPath = options.dbPath ?? uptimeDbPath();
39
+ if (this.dbPath !== ":memory:") {
40
+ mkdirSync2(dirname(this.dbPath), { recursive: true });
41
+ }
42
+ this.db = new Database(this.dbPath, { create: true });
43
+ this.db.run("PRAGMA journal_mode = WAL");
44
+ this.db.run("PRAGMA foreign_keys = ON");
45
+ this.migrate();
46
+ }
47
+ close() {
48
+ this.db.close();
49
+ }
50
+ migrate() {
51
+ this.db.run(`
52
+ CREATE TABLE IF NOT EXISTS monitors (
53
+ id TEXT PRIMARY KEY,
54
+ name TEXT NOT NULL UNIQUE,
55
+ kind TEXT NOT NULL CHECK (kind IN ('http', 'tcp')),
56
+ url TEXT,
57
+ host TEXT,
58
+ port INTEGER,
59
+ method TEXT NOT NULL DEFAULT 'GET',
60
+ expected_status INTEGER,
61
+ interval_seconds INTEGER NOT NULL DEFAULT 60,
62
+ timeout_ms INTEGER NOT NULL DEFAULT 5000,
63
+ retry_count INTEGER NOT NULL DEFAULT 0,
64
+ enabled INTEGER NOT NULL DEFAULT 1,
65
+ status TEXT NOT NULL DEFAULT 'unknown',
66
+ last_checked_at TEXT,
67
+ created_at TEXT NOT NULL,
68
+ updated_at TEXT NOT NULL
69
+ )
70
+ `);
71
+ this.db.run(`
72
+ CREATE TABLE IF NOT EXISTS check_results (
73
+ id TEXT PRIMARY KEY,
74
+ monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
75
+ checked_at TEXT NOT NULL,
76
+ status TEXT NOT NULL CHECK (status IN ('up', 'down')),
77
+ latency_ms REAL,
78
+ status_code INTEGER,
79
+ error TEXT,
80
+ attempt_count INTEGER NOT NULL DEFAULT 1
81
+ )
82
+ `);
83
+ this.db.run(`
84
+ CREATE TABLE IF NOT EXISTS incidents (
85
+ id TEXT PRIMARY KEY,
86
+ monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
87
+ status TEXT NOT NULL CHECK (status IN ('open', 'closed')),
88
+ opened_at TEXT NOT NULL,
89
+ closed_at TEXT,
90
+ last_failure_at TEXT NOT NULL,
91
+ failure_count INTEGER NOT NULL DEFAULT 1,
92
+ recovery_check_id TEXT,
93
+ reason TEXT
94
+ )
95
+ `);
96
+ this.db.run("CREATE INDEX IF NOT EXISTS idx_results_monitor_time ON check_results(monitor_id, checked_at DESC)");
97
+ this.db.run("CREATE INDEX IF NOT EXISTS idx_incidents_monitor_status ON incidents(monitor_id, status)");
98
+ }
99
+ createMonitor(input) {
100
+ const normalized = normalizeCreateMonitor(input);
101
+ const now = new Date().toISOString();
102
+ const monitor = {
103
+ id: newId("mon"),
104
+ name: normalized.name,
105
+ kind: normalized.kind,
106
+ url: normalized.url ?? null,
107
+ host: normalized.host ?? null,
108
+ port: normalized.port ?? null,
109
+ method: normalized.method ?? "GET",
110
+ expectedStatus: normalized.expectedStatus ?? null,
111
+ intervalSeconds: normalized.intervalSeconds ?? 60,
112
+ timeoutMs: normalized.timeoutMs ?? 5000,
113
+ retryCount: normalized.retryCount ?? 0,
114
+ enabled: normalized.enabled ?? true,
115
+ status: normalized.enabled === false ? "paused" : "unknown",
116
+ lastCheckedAt: null,
117
+ createdAt: now,
118
+ updatedAt: now
119
+ };
120
+ this.db.query(`INSERT INTO monitors (
121
+ id, name, kind, url, host, port, method, expected_status,
122
+ interval_seconds, timeout_ms, retry_count, enabled, status,
123
+ last_checked_at, created_at, updated_at
124
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(monitor.id, monitor.name, monitor.kind, monitor.url, monitor.host, monitor.port, monitor.method, monitor.expectedStatus, monitor.intervalSeconds, monitor.timeoutMs, monitor.retryCount, monitor.enabled ? 1 : 0, monitor.status, monitor.lastCheckedAt, monitor.createdAt, monitor.updatedAt);
125
+ return monitor;
126
+ }
127
+ listMonitors(options = {}) {
128
+ const rows = options.includeDisabled ? this.db.query("SELECT * FROM monitors ORDER BY name ASC").all() : this.db.query("SELECT * FROM monitors WHERE enabled = 1 ORDER BY name ASC").all();
129
+ return rows.map(monitorFromRow);
130
+ }
131
+ getMonitor(idOrName) {
132
+ const row = this.db.query("SELECT * FROM monitors WHERE id = ? OR name = ?").get(idOrName, idOrName);
133
+ return row ? monitorFromRow(row) : null;
134
+ }
135
+ updateMonitor(idOrName, input) {
136
+ const current = this.getMonitor(idOrName);
137
+ if (!current)
138
+ throw new Error(`Monitor not found: ${idOrName}`);
139
+ const updatedAt = new Date().toISOString();
140
+ const next = normalizeUpdateMonitor(current, input, updatedAt);
141
+ this.db.query(`UPDATE monitors SET
142
+ name = ?, kind = ?, url = ?, host = ?, port = ?, method = ?,
143
+ expected_status = ?, interval_seconds = ?, timeout_ms = ?,
144
+ retry_count = ?, enabled = ?, status = ?, last_checked_at = ?, updated_at = ?
145
+ WHERE id = ?`).run(next.name, next.kind, next.url, next.host, next.port, next.method, next.expectedStatus, next.intervalSeconds, next.timeoutMs, next.retryCount, next.enabled ? 1 : 0, next.status, next.lastCheckedAt, updatedAt, current.id);
146
+ return this.getMonitor(current.id);
147
+ }
148
+ deleteMonitor(idOrName) {
149
+ const current = this.getMonitor(idOrName);
150
+ if (!current)
151
+ return false;
152
+ this.db.query("DELETE FROM monitors WHERE id = ?").run(current.id);
153
+ return true;
154
+ }
155
+ recordCheckResult(input) {
156
+ const monitor = this.getMonitor(input.monitorId);
157
+ if (!monitor)
158
+ throw new Error(`Monitor not found: ${input.monitorId}`);
159
+ const checkedAt = input.checkedAt ?? new Date().toISOString();
160
+ const result = {
161
+ id: newId("chk"),
162
+ monitorId: monitor.id,
163
+ checkedAt,
164
+ status: input.status,
165
+ latencyMs: input.latencyMs,
166
+ statusCode: input.statusCode,
167
+ error: input.error,
168
+ attemptCount: Math.max(1, input.attemptCount)
169
+ };
170
+ const tx = this.db.transaction(() => {
171
+ this.db.query(`INSERT INTO check_results (
172
+ id, monitor_id, checked_at, status, latency_ms, status_code, error, attempt_count
173
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(result.id, result.monitorId, result.checkedAt, result.status, result.latencyMs, result.statusCode, result.error, result.attemptCount);
174
+ this.db.query("UPDATE monitors SET status = ?, last_checked_at = ?, updated_at = ? WHERE id = ?").run(result.status, result.checkedAt, result.checkedAt, result.monitorId);
175
+ this.reconcileIncidentInTransaction(result);
176
+ });
177
+ tx();
178
+ return result;
179
+ }
180
+ listResults(options = {}) {
181
+ const limit = clampLimit(options.limit ?? 50);
182
+ const rows = options.monitorId ? this.db.query("SELECT * FROM check_results WHERE monitor_id = ? ORDER BY checked_at DESC LIMIT ?").all(options.monitorId, limit) : this.db.query("SELECT * FROM check_results ORDER BY checked_at DESC LIMIT ?").all(limit);
183
+ return rows.map(checkResultFromRow);
184
+ }
185
+ listIncidents(options = {}) {
186
+ const clauses = [];
187
+ const args = [];
188
+ if (options.status) {
189
+ clauses.push("status = ?");
190
+ args.push(options.status);
191
+ }
192
+ if (options.monitorId) {
193
+ clauses.push("monitor_id = ?");
194
+ args.push(options.monitorId);
195
+ }
196
+ const where = clauses.length ? `WHERE ${clauses.join(" AND ")}` : "";
197
+ args.push(clampLimit(options.limit ?? 50));
198
+ const rows = this.db.query(`SELECT * FROM incidents ${where} ORDER BY opened_at DESC LIMIT ?`).all(...args);
199
+ return rows.map(incidentFromRow);
200
+ }
201
+ getOpenIncident(monitorId) {
202
+ const row = this.db.query("SELECT * FROM incidents WHERE monitor_id = ? AND status = 'open' ORDER BY opened_at DESC LIMIT 1").get(monitorId);
203
+ return row ? incidentFromRow(row) : null;
204
+ }
205
+ summary() {
206
+ const monitors = this.listMonitors({ includeDisabled: true });
207
+ const summaries = monitors.map((monitor) => this.monitorSummary(monitor));
208
+ return {
209
+ generatedAt: new Date().toISOString(),
210
+ monitors: summaries,
211
+ totals: {
212
+ monitors: monitors.length,
213
+ enabled: monitors.filter((m) => m.enabled).length,
214
+ up: monitors.filter((m) => m.status === "up").length,
215
+ down: monitors.filter((m) => m.status === "down").length,
216
+ paused: monitors.filter((m) => !m.enabled || m.status === "paused").length,
217
+ unknown: monitors.filter((m) => m.status === "unknown").length,
218
+ openIncidents: this.listIncidents({ status: "open", limit: 1000 }).length
219
+ }
220
+ };
221
+ }
222
+ monitorSummary(monitor) {
223
+ const row = this.db.query(`SELECT
224
+ COUNT(*) as total,
225
+ SUM(CASE WHEN status = 'up' THEN 1 ELSE 0 END) as up_count,
226
+ SUM(CASE WHEN status = 'down' THEN 1 ELSE 0 END) as down_count,
227
+ AVG(CASE WHEN status = 'up' THEN latency_ms ELSE NULL END) as avg_latency
228
+ FROM check_results WHERE monitor_id = ?`).get(monitor.id);
229
+ const total = Number(row.total ?? 0);
230
+ const up = Number(row.up_count ?? 0);
231
+ const down = Number(row.down_count ?? 0);
232
+ return {
233
+ monitor,
234
+ totalChecks: total,
235
+ upChecks: up,
236
+ downChecks: down,
237
+ uptimePercent: total > 0 ? round(up / total * 100, 4) : null,
238
+ averageLatencyMs: row.avg_latency == null ? null : round(row.avg_latency, 2),
239
+ openIncident: this.getOpenIncident(monitor.id)
240
+ };
241
+ }
242
+ reconcileIncidentInTransaction(result) {
243
+ const open = this.db.query("SELECT * FROM incidents WHERE monitor_id = ? AND status = 'open' ORDER BY opened_at DESC LIMIT 1").get(result.monitorId);
244
+ if (result.status === "down") {
245
+ if (open) {
246
+ this.db.query("UPDATE incidents SET last_failure_at = ?, failure_count = failure_count + 1, reason = COALESCE(?, reason) WHERE id = ?").run(result.checkedAt, result.error, open.id);
247
+ } else {
248
+ this.db.query(`INSERT INTO incidents (
249
+ id, monitor_id, status, opened_at, closed_at, last_failure_at,
250
+ failure_count, recovery_check_id, reason
251
+ ) VALUES (?, ?, 'open', ?, NULL, ?, 1, NULL, ?)`).run(newId("inc"), result.monitorId, result.checkedAt, result.checkedAt, result.error);
252
+ }
253
+ return;
254
+ }
255
+ if (open) {
256
+ this.db.query("UPDATE incidents SET status = 'closed', closed_at = ?, recovery_check_id = ? WHERE id = ?").run(result.checkedAt, result.id, open.id);
257
+ }
258
+ }
259
+ }
260
+ function normalizeCreateMonitor(input) {
261
+ const name = input.name?.trim();
262
+ if (!name)
263
+ throw new Error("Monitor name is required");
264
+ const method = normalizeMethod(input.method ?? "GET");
265
+ const expectedStatus = normalizeExpectedStatus(input.expectedStatus);
266
+ if (input.kind === "http") {
267
+ const url = normalizeHttpUrl(input.url);
268
+ return {
269
+ name,
270
+ kind: input.kind,
271
+ url,
272
+ method,
273
+ expectedStatus,
274
+ intervalSeconds: boundedInteger(input.intervalSeconds ?? 60, "intervalSeconds", MIN_INTERVAL_SECONDS, MAX_INTERVAL_SECONDS),
275
+ timeoutMs: boundedInteger(input.timeoutMs ?? 5000, "timeoutMs", MIN_TIMEOUT_MS, MAX_TIMEOUT_MS),
276
+ retryCount: boundedInteger(input.retryCount ?? 0, "retryCount", MIN_RETRY_COUNT, MAX_RETRY_COUNT),
277
+ enabled: input.enabled ?? true
278
+ };
279
+ } else if (input.kind === "tcp") {
280
+ const host = input.host?.trim();
281
+ if (!host)
282
+ throw new Error("TCP monitors require host");
283
+ if (!Number.isInteger(input.port) || input.port <= 0 || input.port > 65535) {
284
+ throw new Error("TCP monitors require a port from 1 to 65535");
285
+ }
286
+ return {
287
+ name,
288
+ kind: input.kind,
289
+ host,
290
+ port: input.port,
291
+ method,
292
+ expectedStatus: null,
293
+ intervalSeconds: boundedInteger(input.intervalSeconds ?? 60, "intervalSeconds", MIN_INTERVAL_SECONDS, MAX_INTERVAL_SECONDS),
294
+ timeoutMs: boundedInteger(input.timeoutMs ?? 5000, "timeoutMs", MIN_TIMEOUT_MS, MAX_TIMEOUT_MS),
295
+ retryCount: boundedInteger(input.retryCount ?? 0, "retryCount", MIN_RETRY_COUNT, MAX_RETRY_COUNT),
296
+ enabled: input.enabled ?? true
297
+ };
298
+ } else {
299
+ throw new Error("Monitor kind must be http or tcp");
300
+ }
301
+ }
302
+ function normalizeUpdateMonitor(current, input, updatedAt) {
303
+ const merged = {
304
+ ...current,
305
+ ...input,
306
+ expectedStatus: input.expectedStatus === undefined ? current.expectedStatus : input.expectedStatus,
307
+ updatedAt
308
+ };
309
+ const normalized = normalizeCreateMonitor({
310
+ name: merged.name,
311
+ kind: merged.kind,
312
+ url: merged.url ?? undefined,
313
+ host: merged.host ?? undefined,
314
+ port: merged.port ?? undefined,
315
+ method: merged.method,
316
+ expectedStatus: merged.expectedStatus,
317
+ intervalSeconds: merged.intervalSeconds,
318
+ timeoutMs: merged.timeoutMs,
319
+ retryCount: merged.retryCount,
320
+ enabled: merged.enabled
321
+ });
322
+ const checkDefinitionChanged = normalized.kind !== current.kind || (normalized.url ?? null) !== current.url || (normalized.host ?? null) !== current.host || (normalized.port ?? null) !== current.port || normalized.method !== current.method || normalized.expectedStatus !== current.expectedStatus;
323
+ const status = normalized.enabled ? checkDefinitionChanged || !current.enabled ? "unknown" : current.status : "paused";
324
+ return {
325
+ ...current,
326
+ name: normalized.name,
327
+ kind: normalized.kind,
328
+ url: normalized.url ?? null,
329
+ host: normalized.host ?? null,
330
+ port: normalized.port ?? null,
331
+ method: normalized.method,
332
+ expectedStatus: normalized.expectedStatus,
333
+ intervalSeconds: normalized.intervalSeconds,
334
+ timeoutMs: normalized.timeoutMs,
335
+ retryCount: normalized.retryCount,
336
+ enabled: normalized.enabled,
337
+ status,
338
+ lastCheckedAt: checkDefinitionChanged ? null : current.lastCheckedAt,
339
+ updatedAt
340
+ };
341
+ }
342
+ function normalizeHttpUrl(value) {
343
+ const raw = value?.trim();
344
+ if (!raw)
345
+ throw new Error("HTTP monitors require url");
346
+ const parsed = new URL(raw);
347
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
348
+ throw new Error("HTTP monitor url must use http or https");
349
+ }
350
+ return parsed.toString();
351
+ }
352
+ function normalizeMethod(value) {
353
+ const method = value.trim().toUpperCase();
354
+ if (!/^[A-Z]+$/.test(method))
355
+ throw new Error("HTTP method must contain only letters");
356
+ return method;
357
+ }
358
+ function normalizeExpectedStatus(value) {
359
+ if (value == null)
360
+ return null;
361
+ if (!Number.isInteger(value) || value < 100 || value > 599) {
362
+ throw new Error("expectedStatus must be an HTTP status from 100 to 599");
363
+ }
364
+ return value;
365
+ }
366
+ function monitorFromRow(row) {
367
+ return {
368
+ id: row.id,
369
+ name: row.name,
370
+ kind: row.kind,
371
+ url: row.url,
372
+ host: row.host,
373
+ port: row.port,
374
+ method: row.method,
375
+ expectedStatus: row.expected_status,
376
+ intervalSeconds: row.interval_seconds,
377
+ timeoutMs: row.timeout_ms,
378
+ retryCount: row.retry_count,
379
+ enabled: Boolean(row.enabled),
380
+ status: row.status,
381
+ lastCheckedAt: row.last_checked_at,
382
+ createdAt: row.created_at,
383
+ updatedAt: row.updated_at
384
+ };
385
+ }
386
+ function checkResultFromRow(row) {
387
+ return {
388
+ id: row.id,
389
+ monitorId: row.monitor_id,
390
+ checkedAt: row.checked_at,
391
+ status: row.status,
392
+ latencyMs: row.latency_ms,
393
+ statusCode: row.status_code,
394
+ error: row.error,
395
+ attemptCount: row.attempt_count
396
+ };
397
+ }
398
+ function incidentFromRow(row) {
399
+ return {
400
+ id: row.id,
401
+ monitorId: row.monitor_id,
402
+ status: row.status,
403
+ openedAt: row.opened_at,
404
+ closedAt: row.closed_at,
405
+ lastFailureAt: row.last_failure_at,
406
+ failureCount: row.failure_count,
407
+ recoveryCheckId: row.recovery_check_id,
408
+ reason: row.reason
409
+ };
410
+ }
411
+ function newId(prefix) {
412
+ return `${prefix}_${randomUUID().replace(/-/g, "").slice(0, 18)}`;
413
+ }
414
+ function boundedInteger(value, label, min, max) {
415
+ if (!Number.isInteger(value) || value < min || value > max) {
416
+ throw new Error(`${label} must be an integer from ${min} to ${max}`);
417
+ }
418
+ return value;
419
+ }
420
+ function clampLimit(value) {
421
+ if (!Number.isFinite(value))
422
+ return 50;
423
+ return Math.max(1, Math.min(Math.floor(value), MAX_RESULT_LIMIT));
424
+ }
425
+ function round(value, places) {
426
+ const factor = 10 ** places;
427
+ return Math.round(value * factor) / factor;
428
+ }
429
+ export {
430
+ UptimeStore
431
+ };
@@ -0,0 +1,95 @@
1
+ export type MonitorKind = "http" | "tcp";
2
+ export type MonitorStatus = "unknown" | "up" | "down" | "paused";
3
+ export type CheckStatus = "up" | "down";
4
+ export type IncidentStatus = "open" | "closed";
5
+ export interface Monitor {
6
+ id: string;
7
+ name: string;
8
+ kind: MonitorKind;
9
+ url: string | null;
10
+ host: string | null;
11
+ port: number | null;
12
+ method: string;
13
+ expectedStatus: number | null;
14
+ intervalSeconds: number;
15
+ timeoutMs: number;
16
+ retryCount: number;
17
+ enabled: boolean;
18
+ status: MonitorStatus;
19
+ lastCheckedAt: string | null;
20
+ createdAt: string;
21
+ updatedAt: string;
22
+ }
23
+ export interface CreateMonitorInput {
24
+ name: string;
25
+ kind: MonitorKind;
26
+ url?: string;
27
+ host?: string;
28
+ port?: number;
29
+ method?: string;
30
+ expectedStatus?: number | null;
31
+ intervalSeconds?: number;
32
+ timeoutMs?: number;
33
+ retryCount?: number;
34
+ enabled?: boolean;
35
+ }
36
+ export type UpdateMonitorInput = Partial<Omit<CreateMonitorInput, "kind">> & {
37
+ kind?: MonitorKind;
38
+ };
39
+ export interface CheckResult {
40
+ id: string;
41
+ monitorId: string;
42
+ checkedAt: string;
43
+ status: CheckStatus;
44
+ latencyMs: number | null;
45
+ statusCode: number | null;
46
+ error: string | null;
47
+ attemptCount: number;
48
+ }
49
+ export interface CheckAttemptResult {
50
+ status: CheckStatus;
51
+ latencyMs: number | null;
52
+ statusCode?: number | null;
53
+ error?: string | null;
54
+ }
55
+ export interface Incident {
56
+ id: string;
57
+ monitorId: string;
58
+ status: IncidentStatus;
59
+ openedAt: string;
60
+ closedAt: string | null;
61
+ lastFailureAt: string;
62
+ failureCount: number;
63
+ recoveryCheckId: string | null;
64
+ reason: string | null;
65
+ }
66
+ export interface MonitorSummary {
67
+ monitor: Monitor;
68
+ totalChecks: number;
69
+ upChecks: number;
70
+ downChecks: number;
71
+ uptimePercent: number | null;
72
+ averageLatencyMs: number | null;
73
+ openIncident: Incident | null;
74
+ }
75
+ export interface UptimeSummary {
76
+ generatedAt: string;
77
+ monitors: MonitorSummary[];
78
+ totals: {
79
+ monitors: number;
80
+ enabled: number;
81
+ up: number;
82
+ down: number;
83
+ paused: number;
84
+ unknown: number;
85
+ openIncidents: number;
86
+ };
87
+ }
88
+ export interface ListResultsOptions {
89
+ monitorId?: string;
90
+ limit?: number;
91
+ }
92
+ export interface SchedulerHandle {
93
+ stop: () => void;
94
+ }
95
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,KAAK,CAAC;AACzC,MAAM,MAAM,aAAa,GAAG,SAAS,GAAG,IAAI,GAAG,MAAM,GAAG,QAAQ,CAAC;AACjE,MAAM,MAAM,WAAW,GAAG,IAAI,GAAG,MAAM,CAAC;AACxC,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,QAAQ,CAAC;AAE/C,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,WAAW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,EAAE,MAAM,CAAC;KACvB,CAAC;CACH;AAED,MAAM,WAAW,kBAAkB;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,IAAI,CAAC;CAClB"}
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ // @bun
@@ -0,0 +1,2 @@
1
+ export declare function packageVersion(): string;
2
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAIA,wBAAgB,cAAc,IAAI,MAAM,CAcvC"}
@@ -0,0 +1,21 @@
1
+ // @bun
2
+ // src/version.ts
3
+ import { readFileSync } from "fs";
4
+ import { dirname, join } from "path";
5
+ import { fileURLToPath } from "url";
6
+ function packageVersion() {
7
+ const here = dirname(fileURLToPath(import.meta.url));
8
+ const candidates = [
9
+ join(here, "..", "package.json"),
10
+ join(here, "..", "..", "package.json")
11
+ ];
12
+ for (const candidate of candidates) {
13
+ try {
14
+ return JSON.parse(readFileSync(candidate, "utf8")).version ?? "0.0.0";
15
+ } catch {}
16
+ }
17
+ return "0.0.0";
18
+ }
19
+ export {
20
+ packageVersion
21
+ };
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@hasna/uptime",
3
+ "version": "0.1.0",
4
+ "description": "Local-first uptime and downtime monitoring service with CLI, MCP, SDK, SQLite persistence, and a dashboard.",
5
+ "license": "Apache-2.0",
6
+ "type": "module",
7
+ "homepage": "https://github.com/hasna/uptime#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/hasna/uptime.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/hasna/uptime/issues"
14
+ },
15
+ "keywords": [
16
+ "uptime",
17
+ "monitoring",
18
+ "pingdom",
19
+ "mcp",
20
+ "hasna"
21
+ ],
22
+ "files": [
23
+ "dist",
24
+ "README.md",
25
+ "CHANGELOG.md",
26
+ "LICENSE",
27
+ "NOTICE",
28
+ "THIRD_PARTY_NOTICES.md",
29
+ "SECURITY.md",
30
+ "CONTRIBUTING.md",
31
+ "CODE_OF_CONDUCT.md"
32
+ ],
33
+ "bin": {
34
+ "uptime": "dist/cli/index.js",
35
+ "uptime-mcp": "dist/mcp/index.js"
36
+ },
37
+ "exports": {
38
+ ".": {
39
+ "types": "./dist/index.d.ts",
40
+ "import": "./dist/index.js"
41
+ },
42
+ "./sdk": {
43
+ "types": "./dist/index.d.ts",
44
+ "import": "./dist/index.js"
45
+ },
46
+ "./api": {
47
+ "types": "./dist/api.d.ts",
48
+ "import": "./dist/api.js"
49
+ },
50
+ "./storage": {
51
+ "types": "./dist/store.d.ts",
52
+ "import": "./dist/store.js"
53
+ }
54
+ },
55
+ "scripts": {
56
+ "build": "rm -rf dist && bun build src/cli/index.ts --outdir dist/cli --target bun --external @modelcontextprotocol/sdk && bun build src/mcp/index.ts --outdir dist/mcp --target bun --external @modelcontextprotocol/sdk && bun build src/index.ts src/api.ts src/service.ts src/store.ts src/checks.ts src/types.ts src/paths.ts src/dashboard.ts src/version.ts --root src --outdir dist --target bun && tsc -p tsconfig.build.json --emitDeclarationOnly --outDir dist && chmod +x dist/cli/index.js dist/mcp/index.js",
57
+ "typecheck": "tsc --noEmit",
58
+ "test": "bun test ./tests",
59
+ "dev:cli": "bun run src/cli/index.ts",
60
+ "dev:mcp": "bun run src/mcp/index.ts",
61
+ "dev:server": "bun run src/cli/index.ts serve",
62
+ "prepublishOnly": "bun run build && bun run typecheck && bun test",
63
+ "postinstall": "mkdir -p $HOME/.hasna/uptime 2>/dev/null || true"
64
+ },
65
+ "dependencies": {
66
+ "@modelcontextprotocol/sdk": "^1.29.0",
67
+ "chalk": "^5.6.2",
68
+ "commander": "^13.1.0",
69
+ "zod": "^4.3.6"
70
+ },
71
+ "devDependencies": {
72
+ "@types/bun": "^1.3.14",
73
+ "@types/node": "^20.14.0",
74
+ "typescript": "^5.9.3"
75
+ }
76
+ }