@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.
- package/CHANGELOG.md +21 -0
- package/CODE_OF_CONDUCT.md +36 -0
- package/CONTRIBUTING.md +24 -0
- package/LICENSE +191 -0
- package/NOTICE +8 -0
- package/README.md +123 -0
- package/SECURITY.md +13 -0
- package/THIRD_PARTY_NOTICES.md +113 -0
- package/dist/api.d.ts +15 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +1081 -0
- package/dist/checks.d.ts +10 -0
- package/dist/checks.d.ts.map +1 -0
- package/dist/checks.js +72 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +3945 -0
- package/dist/dashboard.d.ts +2 -0
- package/dist/dashboard.d.ts.map +1 -0
- package/dist/dashboard.js +353 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1090 -0
- package/dist/limits.d.ts +9 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/mcp/index.d.ts +8 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +15053 -0
- package/dist/paths.d.ts +4 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +21 -0
- package/dist/service.d.ts +36 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +611 -0
- package/dist/store.d.ts +32 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +431 -0
- package/dist/types.d.ts +95 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +21 -0
- package/package.json +76 -0
package/dist/paths.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAIA,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AAED,wBAAgB,YAAY,IAAI,MAAM,CAErC;AAED,wBAAgB,gBAAgB,IAAI,MAAM,CAIzC"}
|
package/dist/paths.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
export {
|
|
18
|
+
uptimeHome,
|
|
19
|
+
uptimeDbPath,
|
|
20
|
+
ensureUptimeHome
|
|
21
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { UptimeStore, type UptimeStoreOptions } from "./store.js";
|
|
2
|
+
import type { CheckAttemptResult, CheckResult, CreateMonitorInput, Incident, ListResultsOptions, Monitor, SchedulerHandle, UpdateMonitorInput, UptimeSummary } from "./types.js";
|
|
3
|
+
export interface UptimeServiceOptions extends UptimeStoreOptions {
|
|
4
|
+
store?: UptimeStore;
|
|
5
|
+
checkRunner?: (monitor: Monitor) => Promise<CheckAttemptResult>;
|
|
6
|
+
}
|
|
7
|
+
export declare class UptimeService {
|
|
8
|
+
readonly store: UptimeStore;
|
|
9
|
+
private readonly checkRunner;
|
|
10
|
+
private readonly inFlightChecks;
|
|
11
|
+
constructor(options?: UptimeServiceOptions);
|
|
12
|
+
close(): void;
|
|
13
|
+
createMonitor(input: CreateMonitorInput): Monitor;
|
|
14
|
+
updateMonitor(idOrName: string, input: UpdateMonitorInput): Monitor;
|
|
15
|
+
deleteMonitor(idOrName: string): boolean;
|
|
16
|
+
listMonitors(options?: {
|
|
17
|
+
includeDisabled?: boolean;
|
|
18
|
+
}): Monitor[];
|
|
19
|
+
getMonitor(idOrName: string): Monitor | null;
|
|
20
|
+
listResults(options?: ListResultsOptions): CheckResult[];
|
|
21
|
+
listIncidents(options?: {
|
|
22
|
+
status?: "open" | "closed";
|
|
23
|
+
monitorId?: string;
|
|
24
|
+
limit?: number;
|
|
25
|
+
}): Incident[];
|
|
26
|
+
summary(): UptimeSummary;
|
|
27
|
+
checkMonitor(idOrName: string): Promise<CheckResult>;
|
|
28
|
+
checkAll(): Promise<CheckResult[]>;
|
|
29
|
+
startScheduler(options?: {
|
|
30
|
+
tickMs?: number;
|
|
31
|
+
}): SchedulerHandle;
|
|
32
|
+
runDueChecks(now?: Date): Promise<CheckResult[]>;
|
|
33
|
+
private isDue;
|
|
34
|
+
}
|
|
35
|
+
export declare function createUptimeClient(options?: UptimeServiceOptions): UptimeService;
|
|
36
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAClE,OAAO,KAAK,EACV,kBAAkB,EAClB,WAAW,EACX,kBAAkB,EAClB,QAAQ,EACR,kBAAkB,EAClB,OAAO,EACP,eAAe,EACf,kBAAkB,EAClB,aAAa,EACd,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,oBAAqB,SAAQ,kBAAkB;IAC9D,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,kBAAkB,CAAC,CAAC;CACjE;AAED,qBAAa,aAAa;IACxB,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoD;IAChF,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAqB;gBAExC,OAAO,GAAE,oBAAyB;IAK9C,KAAK,IAAI,IAAI;IAIb,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO;IAIjD,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,kBAAkB,GAAG,OAAO;IAInE,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAIxC,YAAY,CAAC,OAAO,GAAE;QAAE,eAAe,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,OAAO,EAAE;IAIpE,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI;IAI5C,WAAW,CAAC,OAAO,GAAE,kBAAuB,GAAG,WAAW,EAAE;IAI5D,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;IAI3G,OAAO,IAAI,aAAa;IAIlB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IA4BpD,QAAQ,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IASxC,cAAc,CAAC,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAA;KAAO,GAAG,eAAe;IAY5D,YAAY,CAAC,GAAG,GAAE,IAAiB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAWlE,OAAO,CAAC,KAAK;CAOd;AAED,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,oBAAyB,GAAG,aAAa,CAEpF"}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/checks.ts
|
|
3
|
+
import net from "net";
|
|
4
|
+
async function runMonitorCheck(monitor, options = {}) {
|
|
5
|
+
if (!monitor.enabled) {
|
|
6
|
+
return { status: "down", latencyMs: null, error: "monitor is disabled" };
|
|
7
|
+
}
|
|
8
|
+
if (monitor.kind === "http")
|
|
9
|
+
return runHttpCheck(monitor, options.fetch ?? fetch);
|
|
10
|
+
return runTcpCheck(monitor);
|
|
11
|
+
}
|
|
12
|
+
async function runHttpCheck(monitor, fetchImpl = fetch) {
|
|
13
|
+
if (!monitor.url)
|
|
14
|
+
return { status: "down", latencyMs: null, error: "missing url" };
|
|
15
|
+
const controller = new AbortController;
|
|
16
|
+
const timeout = setTimeout(() => controller.abort(), monitor.timeoutMs);
|
|
17
|
+
const started = performance.now();
|
|
18
|
+
try {
|
|
19
|
+
const response = await fetchImpl(monitor.url, {
|
|
20
|
+
method: monitor.method || "GET",
|
|
21
|
+
redirect: "manual",
|
|
22
|
+
signal: controller.signal
|
|
23
|
+
});
|
|
24
|
+
const latencyMs = Math.round((performance.now() - started) * 100) / 100;
|
|
25
|
+
const ok = monitor.expectedStatus == null ? response.status >= 200 && response.status < 400 : response.status === monitor.expectedStatus;
|
|
26
|
+
return {
|
|
27
|
+
status: ok ? "up" : "down",
|
|
28
|
+
latencyMs,
|
|
29
|
+
statusCode: response.status,
|
|
30
|
+
error: ok ? null : `unexpected status ${response.status}`
|
|
31
|
+
};
|
|
32
|
+
} catch (error) {
|
|
33
|
+
return {
|
|
34
|
+
status: "down",
|
|
35
|
+
latencyMs: Math.round((performance.now() - started) * 100) / 100,
|
|
36
|
+
statusCode: null,
|
|
37
|
+
error: error instanceof Error ? error.message : String(error)
|
|
38
|
+
};
|
|
39
|
+
} finally {
|
|
40
|
+
clearTimeout(timeout);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async function runTcpCheck(monitor) {
|
|
44
|
+
if (!monitor.host || !monitor.port)
|
|
45
|
+
return { status: "down", latencyMs: null, error: "missing host or port" };
|
|
46
|
+
const started = performance.now();
|
|
47
|
+
return new Promise((resolve) => {
|
|
48
|
+
const socket = net.createConnection({ host: monitor.host, port: monitor.port, timeout: monitor.timeoutMs });
|
|
49
|
+
let settled = false;
|
|
50
|
+
const finish = (result) => {
|
|
51
|
+
if (settled)
|
|
52
|
+
return;
|
|
53
|
+
settled = true;
|
|
54
|
+
socket.destroy();
|
|
55
|
+
resolve(result);
|
|
56
|
+
};
|
|
57
|
+
socket.once("connect", () => {
|
|
58
|
+
finish({ status: "up", latencyMs: Math.round((performance.now() - started) * 100) / 100, statusCode: null, error: null });
|
|
59
|
+
});
|
|
60
|
+
socket.once("timeout", () => {
|
|
61
|
+
finish({ status: "down", latencyMs: Math.round((performance.now() - started) * 100) / 100, statusCode: null, error: "tcp timeout" });
|
|
62
|
+
});
|
|
63
|
+
socket.once("error", (error) => {
|
|
64
|
+
finish({ status: "down", latencyMs: Math.round((performance.now() - started) * 100) / 100, statusCode: null, error: error.message });
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/paths.ts
|
|
70
|
+
import { mkdirSync } from "fs";
|
|
71
|
+
import { homedir } from "os";
|
|
72
|
+
import { join } from "path";
|
|
73
|
+
function uptimeHome() {
|
|
74
|
+
return process.env.HASNA_UPTIME_HOME || join(homedir(), ".hasna", "uptime");
|
|
75
|
+
}
|
|
76
|
+
function uptimeDbPath() {
|
|
77
|
+
return process.env.HASNA_UPTIME_DB || join(uptimeHome(), "uptime.db");
|
|
78
|
+
}
|
|
79
|
+
function ensureUptimeHome() {
|
|
80
|
+
const home = uptimeHome();
|
|
81
|
+
mkdirSync(home, { recursive: true });
|
|
82
|
+
return home;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/store.ts
|
|
86
|
+
import { mkdirSync as mkdirSync2 } from "fs";
|
|
87
|
+
import { dirname } from "path";
|
|
88
|
+
import { randomUUID } from "crypto";
|
|
89
|
+
import { Database } from "bun:sqlite";
|
|
90
|
+
|
|
91
|
+
// src/limits.ts
|
|
92
|
+
var MIN_INTERVAL_SECONDS = 1;
|
|
93
|
+
var MAX_INTERVAL_SECONDS = 86400;
|
|
94
|
+
var MIN_TIMEOUT_MS = 1;
|
|
95
|
+
var MAX_TIMEOUT_MS = 60000;
|
|
96
|
+
var MIN_RETRY_COUNT = 0;
|
|
97
|
+
var MAX_RETRY_COUNT = 10;
|
|
98
|
+
var MAX_RESULT_LIMIT = 1000;
|
|
99
|
+
|
|
100
|
+
// src/store.ts
|
|
101
|
+
class UptimeStore {
|
|
102
|
+
dbPath;
|
|
103
|
+
db;
|
|
104
|
+
constructor(options = {}) {
|
|
105
|
+
this.dbPath = options.dbPath ?? uptimeDbPath();
|
|
106
|
+
if (this.dbPath !== ":memory:") {
|
|
107
|
+
mkdirSync2(dirname(this.dbPath), { recursive: true });
|
|
108
|
+
}
|
|
109
|
+
this.db = new Database(this.dbPath, { create: true });
|
|
110
|
+
this.db.run("PRAGMA journal_mode = WAL");
|
|
111
|
+
this.db.run("PRAGMA foreign_keys = ON");
|
|
112
|
+
this.migrate();
|
|
113
|
+
}
|
|
114
|
+
close() {
|
|
115
|
+
this.db.close();
|
|
116
|
+
}
|
|
117
|
+
migrate() {
|
|
118
|
+
this.db.run(`
|
|
119
|
+
CREATE TABLE IF NOT EXISTS monitors (
|
|
120
|
+
id TEXT PRIMARY KEY,
|
|
121
|
+
name TEXT NOT NULL UNIQUE,
|
|
122
|
+
kind TEXT NOT NULL CHECK (kind IN ('http', 'tcp')),
|
|
123
|
+
url TEXT,
|
|
124
|
+
host TEXT,
|
|
125
|
+
port INTEGER,
|
|
126
|
+
method TEXT NOT NULL DEFAULT 'GET',
|
|
127
|
+
expected_status INTEGER,
|
|
128
|
+
interval_seconds INTEGER NOT NULL DEFAULT 60,
|
|
129
|
+
timeout_ms INTEGER NOT NULL DEFAULT 5000,
|
|
130
|
+
retry_count INTEGER NOT NULL DEFAULT 0,
|
|
131
|
+
enabled INTEGER NOT NULL DEFAULT 1,
|
|
132
|
+
status TEXT NOT NULL DEFAULT 'unknown',
|
|
133
|
+
last_checked_at TEXT,
|
|
134
|
+
created_at TEXT NOT NULL,
|
|
135
|
+
updated_at TEXT NOT NULL
|
|
136
|
+
)
|
|
137
|
+
`);
|
|
138
|
+
this.db.run(`
|
|
139
|
+
CREATE TABLE IF NOT EXISTS check_results (
|
|
140
|
+
id TEXT PRIMARY KEY,
|
|
141
|
+
monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
|
|
142
|
+
checked_at TEXT NOT NULL,
|
|
143
|
+
status TEXT NOT NULL CHECK (status IN ('up', 'down')),
|
|
144
|
+
latency_ms REAL,
|
|
145
|
+
status_code INTEGER,
|
|
146
|
+
error TEXT,
|
|
147
|
+
attempt_count INTEGER NOT NULL DEFAULT 1
|
|
148
|
+
)
|
|
149
|
+
`);
|
|
150
|
+
this.db.run(`
|
|
151
|
+
CREATE TABLE IF NOT EXISTS incidents (
|
|
152
|
+
id TEXT PRIMARY KEY,
|
|
153
|
+
monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
|
|
154
|
+
status TEXT NOT NULL CHECK (status IN ('open', 'closed')),
|
|
155
|
+
opened_at TEXT NOT NULL,
|
|
156
|
+
closed_at TEXT,
|
|
157
|
+
last_failure_at TEXT NOT NULL,
|
|
158
|
+
failure_count INTEGER NOT NULL DEFAULT 1,
|
|
159
|
+
recovery_check_id TEXT,
|
|
160
|
+
reason TEXT
|
|
161
|
+
)
|
|
162
|
+
`);
|
|
163
|
+
this.db.run("CREATE INDEX IF NOT EXISTS idx_results_monitor_time ON check_results(monitor_id, checked_at DESC)");
|
|
164
|
+
this.db.run("CREATE INDEX IF NOT EXISTS idx_incidents_monitor_status ON incidents(monitor_id, status)");
|
|
165
|
+
}
|
|
166
|
+
createMonitor(input) {
|
|
167
|
+
const normalized = normalizeCreateMonitor(input);
|
|
168
|
+
const now = new Date().toISOString();
|
|
169
|
+
const monitor = {
|
|
170
|
+
id: newId("mon"),
|
|
171
|
+
name: normalized.name,
|
|
172
|
+
kind: normalized.kind,
|
|
173
|
+
url: normalized.url ?? null,
|
|
174
|
+
host: normalized.host ?? null,
|
|
175
|
+
port: normalized.port ?? null,
|
|
176
|
+
method: normalized.method ?? "GET",
|
|
177
|
+
expectedStatus: normalized.expectedStatus ?? null,
|
|
178
|
+
intervalSeconds: normalized.intervalSeconds ?? 60,
|
|
179
|
+
timeoutMs: normalized.timeoutMs ?? 5000,
|
|
180
|
+
retryCount: normalized.retryCount ?? 0,
|
|
181
|
+
enabled: normalized.enabled ?? true,
|
|
182
|
+
status: normalized.enabled === false ? "paused" : "unknown",
|
|
183
|
+
lastCheckedAt: null,
|
|
184
|
+
createdAt: now,
|
|
185
|
+
updatedAt: now
|
|
186
|
+
};
|
|
187
|
+
this.db.query(`INSERT INTO monitors (
|
|
188
|
+
id, name, kind, url, host, port, method, expected_status,
|
|
189
|
+
interval_seconds, timeout_ms, retry_count, enabled, status,
|
|
190
|
+
last_checked_at, created_at, updated_at
|
|
191
|
+
) 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);
|
|
192
|
+
return monitor;
|
|
193
|
+
}
|
|
194
|
+
listMonitors(options = {}) {
|
|
195
|
+
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();
|
|
196
|
+
return rows.map(monitorFromRow);
|
|
197
|
+
}
|
|
198
|
+
getMonitor(idOrName) {
|
|
199
|
+
const row = this.db.query("SELECT * FROM monitors WHERE id = ? OR name = ?").get(idOrName, idOrName);
|
|
200
|
+
return row ? monitorFromRow(row) : null;
|
|
201
|
+
}
|
|
202
|
+
updateMonitor(idOrName, input) {
|
|
203
|
+
const current = this.getMonitor(idOrName);
|
|
204
|
+
if (!current)
|
|
205
|
+
throw new Error(`Monitor not found: ${idOrName}`);
|
|
206
|
+
const updatedAt = new Date().toISOString();
|
|
207
|
+
const next = normalizeUpdateMonitor(current, input, updatedAt);
|
|
208
|
+
this.db.query(`UPDATE monitors SET
|
|
209
|
+
name = ?, kind = ?, url = ?, host = ?, port = ?, method = ?,
|
|
210
|
+
expected_status = ?, interval_seconds = ?, timeout_ms = ?,
|
|
211
|
+
retry_count = ?, enabled = ?, status = ?, last_checked_at = ?, updated_at = ?
|
|
212
|
+
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);
|
|
213
|
+
return this.getMonitor(current.id);
|
|
214
|
+
}
|
|
215
|
+
deleteMonitor(idOrName) {
|
|
216
|
+
const current = this.getMonitor(idOrName);
|
|
217
|
+
if (!current)
|
|
218
|
+
return false;
|
|
219
|
+
this.db.query("DELETE FROM monitors WHERE id = ?").run(current.id);
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
recordCheckResult(input) {
|
|
223
|
+
const monitor = this.getMonitor(input.monitorId);
|
|
224
|
+
if (!monitor)
|
|
225
|
+
throw new Error(`Monitor not found: ${input.monitorId}`);
|
|
226
|
+
const checkedAt = input.checkedAt ?? new Date().toISOString();
|
|
227
|
+
const result = {
|
|
228
|
+
id: newId("chk"),
|
|
229
|
+
monitorId: monitor.id,
|
|
230
|
+
checkedAt,
|
|
231
|
+
status: input.status,
|
|
232
|
+
latencyMs: input.latencyMs,
|
|
233
|
+
statusCode: input.statusCode,
|
|
234
|
+
error: input.error,
|
|
235
|
+
attemptCount: Math.max(1, input.attemptCount)
|
|
236
|
+
};
|
|
237
|
+
const tx = this.db.transaction(() => {
|
|
238
|
+
this.db.query(`INSERT INTO check_results (
|
|
239
|
+
id, monitor_id, checked_at, status, latency_ms, status_code, error, attempt_count
|
|
240
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(result.id, result.monitorId, result.checkedAt, result.status, result.latencyMs, result.statusCode, result.error, result.attemptCount);
|
|
241
|
+
this.db.query("UPDATE monitors SET status = ?, last_checked_at = ?, updated_at = ? WHERE id = ?").run(result.status, result.checkedAt, result.checkedAt, result.monitorId);
|
|
242
|
+
this.reconcileIncidentInTransaction(result);
|
|
243
|
+
});
|
|
244
|
+
tx();
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
listResults(options = {}) {
|
|
248
|
+
const limit = clampLimit(options.limit ?? 50);
|
|
249
|
+
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);
|
|
250
|
+
return rows.map(checkResultFromRow);
|
|
251
|
+
}
|
|
252
|
+
listIncidents(options = {}) {
|
|
253
|
+
const clauses = [];
|
|
254
|
+
const args = [];
|
|
255
|
+
if (options.status) {
|
|
256
|
+
clauses.push("status = ?");
|
|
257
|
+
args.push(options.status);
|
|
258
|
+
}
|
|
259
|
+
if (options.monitorId) {
|
|
260
|
+
clauses.push("monitor_id = ?");
|
|
261
|
+
args.push(options.monitorId);
|
|
262
|
+
}
|
|
263
|
+
const where = clauses.length ? `WHERE ${clauses.join(" AND ")}` : "";
|
|
264
|
+
args.push(clampLimit(options.limit ?? 50));
|
|
265
|
+
const rows = this.db.query(`SELECT * FROM incidents ${where} ORDER BY opened_at DESC LIMIT ?`).all(...args);
|
|
266
|
+
return rows.map(incidentFromRow);
|
|
267
|
+
}
|
|
268
|
+
getOpenIncident(monitorId) {
|
|
269
|
+
const row = this.db.query("SELECT * FROM incidents WHERE monitor_id = ? AND status = 'open' ORDER BY opened_at DESC LIMIT 1").get(monitorId);
|
|
270
|
+
return row ? incidentFromRow(row) : null;
|
|
271
|
+
}
|
|
272
|
+
summary() {
|
|
273
|
+
const monitors = this.listMonitors({ includeDisabled: true });
|
|
274
|
+
const summaries = monitors.map((monitor) => this.monitorSummary(monitor));
|
|
275
|
+
return {
|
|
276
|
+
generatedAt: new Date().toISOString(),
|
|
277
|
+
monitors: summaries,
|
|
278
|
+
totals: {
|
|
279
|
+
monitors: monitors.length,
|
|
280
|
+
enabled: monitors.filter((m) => m.enabled).length,
|
|
281
|
+
up: monitors.filter((m) => m.status === "up").length,
|
|
282
|
+
down: monitors.filter((m) => m.status === "down").length,
|
|
283
|
+
paused: monitors.filter((m) => !m.enabled || m.status === "paused").length,
|
|
284
|
+
unknown: monitors.filter((m) => m.status === "unknown").length,
|
|
285
|
+
openIncidents: this.listIncidents({ status: "open", limit: 1000 }).length
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
monitorSummary(monitor) {
|
|
290
|
+
const row = this.db.query(`SELECT
|
|
291
|
+
COUNT(*) as total,
|
|
292
|
+
SUM(CASE WHEN status = 'up' THEN 1 ELSE 0 END) as up_count,
|
|
293
|
+
SUM(CASE WHEN status = 'down' THEN 1 ELSE 0 END) as down_count,
|
|
294
|
+
AVG(CASE WHEN status = 'up' THEN latency_ms ELSE NULL END) as avg_latency
|
|
295
|
+
FROM check_results WHERE monitor_id = ?`).get(monitor.id);
|
|
296
|
+
const total = Number(row.total ?? 0);
|
|
297
|
+
const up = Number(row.up_count ?? 0);
|
|
298
|
+
const down = Number(row.down_count ?? 0);
|
|
299
|
+
return {
|
|
300
|
+
monitor,
|
|
301
|
+
totalChecks: total,
|
|
302
|
+
upChecks: up,
|
|
303
|
+
downChecks: down,
|
|
304
|
+
uptimePercent: total > 0 ? round(up / total * 100, 4) : null,
|
|
305
|
+
averageLatencyMs: row.avg_latency == null ? null : round(row.avg_latency, 2),
|
|
306
|
+
openIncident: this.getOpenIncident(monitor.id)
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
reconcileIncidentInTransaction(result) {
|
|
310
|
+
const open = this.db.query("SELECT * FROM incidents WHERE monitor_id = ? AND status = 'open' ORDER BY opened_at DESC LIMIT 1").get(result.monitorId);
|
|
311
|
+
if (result.status === "down") {
|
|
312
|
+
if (open) {
|
|
313
|
+
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);
|
|
314
|
+
} else {
|
|
315
|
+
this.db.query(`INSERT INTO incidents (
|
|
316
|
+
id, monitor_id, status, opened_at, closed_at, last_failure_at,
|
|
317
|
+
failure_count, recovery_check_id, reason
|
|
318
|
+
) VALUES (?, ?, 'open', ?, NULL, ?, 1, NULL, ?)`).run(newId("inc"), result.monitorId, result.checkedAt, result.checkedAt, result.error);
|
|
319
|
+
}
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (open) {
|
|
323
|
+
this.db.query("UPDATE incidents SET status = 'closed', closed_at = ?, recovery_check_id = ? WHERE id = ?").run(result.checkedAt, result.id, open.id);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
function normalizeCreateMonitor(input) {
|
|
328
|
+
const name = input.name?.trim();
|
|
329
|
+
if (!name)
|
|
330
|
+
throw new Error("Monitor name is required");
|
|
331
|
+
const method = normalizeMethod(input.method ?? "GET");
|
|
332
|
+
const expectedStatus = normalizeExpectedStatus(input.expectedStatus);
|
|
333
|
+
if (input.kind === "http") {
|
|
334
|
+
const url = normalizeHttpUrl(input.url);
|
|
335
|
+
return {
|
|
336
|
+
name,
|
|
337
|
+
kind: input.kind,
|
|
338
|
+
url,
|
|
339
|
+
method,
|
|
340
|
+
expectedStatus,
|
|
341
|
+
intervalSeconds: boundedInteger(input.intervalSeconds ?? 60, "intervalSeconds", MIN_INTERVAL_SECONDS, MAX_INTERVAL_SECONDS),
|
|
342
|
+
timeoutMs: boundedInteger(input.timeoutMs ?? 5000, "timeoutMs", MIN_TIMEOUT_MS, MAX_TIMEOUT_MS),
|
|
343
|
+
retryCount: boundedInteger(input.retryCount ?? 0, "retryCount", MIN_RETRY_COUNT, MAX_RETRY_COUNT),
|
|
344
|
+
enabled: input.enabled ?? true
|
|
345
|
+
};
|
|
346
|
+
} else if (input.kind === "tcp") {
|
|
347
|
+
const host = input.host?.trim();
|
|
348
|
+
if (!host)
|
|
349
|
+
throw new Error("TCP monitors require host");
|
|
350
|
+
if (!Number.isInteger(input.port) || input.port <= 0 || input.port > 65535) {
|
|
351
|
+
throw new Error("TCP monitors require a port from 1 to 65535");
|
|
352
|
+
}
|
|
353
|
+
return {
|
|
354
|
+
name,
|
|
355
|
+
kind: input.kind,
|
|
356
|
+
host,
|
|
357
|
+
port: input.port,
|
|
358
|
+
method,
|
|
359
|
+
expectedStatus: null,
|
|
360
|
+
intervalSeconds: boundedInteger(input.intervalSeconds ?? 60, "intervalSeconds", MIN_INTERVAL_SECONDS, MAX_INTERVAL_SECONDS),
|
|
361
|
+
timeoutMs: boundedInteger(input.timeoutMs ?? 5000, "timeoutMs", MIN_TIMEOUT_MS, MAX_TIMEOUT_MS),
|
|
362
|
+
retryCount: boundedInteger(input.retryCount ?? 0, "retryCount", MIN_RETRY_COUNT, MAX_RETRY_COUNT),
|
|
363
|
+
enabled: input.enabled ?? true
|
|
364
|
+
};
|
|
365
|
+
} else {
|
|
366
|
+
throw new Error("Monitor kind must be http or tcp");
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
function normalizeUpdateMonitor(current, input, updatedAt) {
|
|
370
|
+
const merged = {
|
|
371
|
+
...current,
|
|
372
|
+
...input,
|
|
373
|
+
expectedStatus: input.expectedStatus === undefined ? current.expectedStatus : input.expectedStatus,
|
|
374
|
+
updatedAt
|
|
375
|
+
};
|
|
376
|
+
const normalized = normalizeCreateMonitor({
|
|
377
|
+
name: merged.name,
|
|
378
|
+
kind: merged.kind,
|
|
379
|
+
url: merged.url ?? undefined,
|
|
380
|
+
host: merged.host ?? undefined,
|
|
381
|
+
port: merged.port ?? undefined,
|
|
382
|
+
method: merged.method,
|
|
383
|
+
expectedStatus: merged.expectedStatus,
|
|
384
|
+
intervalSeconds: merged.intervalSeconds,
|
|
385
|
+
timeoutMs: merged.timeoutMs,
|
|
386
|
+
retryCount: merged.retryCount,
|
|
387
|
+
enabled: merged.enabled
|
|
388
|
+
});
|
|
389
|
+
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;
|
|
390
|
+
const status = normalized.enabled ? checkDefinitionChanged || !current.enabled ? "unknown" : current.status : "paused";
|
|
391
|
+
return {
|
|
392
|
+
...current,
|
|
393
|
+
name: normalized.name,
|
|
394
|
+
kind: normalized.kind,
|
|
395
|
+
url: normalized.url ?? null,
|
|
396
|
+
host: normalized.host ?? null,
|
|
397
|
+
port: normalized.port ?? null,
|
|
398
|
+
method: normalized.method,
|
|
399
|
+
expectedStatus: normalized.expectedStatus,
|
|
400
|
+
intervalSeconds: normalized.intervalSeconds,
|
|
401
|
+
timeoutMs: normalized.timeoutMs,
|
|
402
|
+
retryCount: normalized.retryCount,
|
|
403
|
+
enabled: normalized.enabled,
|
|
404
|
+
status,
|
|
405
|
+
lastCheckedAt: checkDefinitionChanged ? null : current.lastCheckedAt,
|
|
406
|
+
updatedAt
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function normalizeHttpUrl(value) {
|
|
410
|
+
const raw = value?.trim();
|
|
411
|
+
if (!raw)
|
|
412
|
+
throw new Error("HTTP monitors require url");
|
|
413
|
+
const parsed = new URL(raw);
|
|
414
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
415
|
+
throw new Error("HTTP monitor url must use http or https");
|
|
416
|
+
}
|
|
417
|
+
return parsed.toString();
|
|
418
|
+
}
|
|
419
|
+
function normalizeMethod(value) {
|
|
420
|
+
const method = value.trim().toUpperCase();
|
|
421
|
+
if (!/^[A-Z]+$/.test(method))
|
|
422
|
+
throw new Error("HTTP method must contain only letters");
|
|
423
|
+
return method;
|
|
424
|
+
}
|
|
425
|
+
function normalizeExpectedStatus(value) {
|
|
426
|
+
if (value == null)
|
|
427
|
+
return null;
|
|
428
|
+
if (!Number.isInteger(value) || value < 100 || value > 599) {
|
|
429
|
+
throw new Error("expectedStatus must be an HTTP status from 100 to 599");
|
|
430
|
+
}
|
|
431
|
+
return value;
|
|
432
|
+
}
|
|
433
|
+
function monitorFromRow(row) {
|
|
434
|
+
return {
|
|
435
|
+
id: row.id,
|
|
436
|
+
name: row.name,
|
|
437
|
+
kind: row.kind,
|
|
438
|
+
url: row.url,
|
|
439
|
+
host: row.host,
|
|
440
|
+
port: row.port,
|
|
441
|
+
method: row.method,
|
|
442
|
+
expectedStatus: row.expected_status,
|
|
443
|
+
intervalSeconds: row.interval_seconds,
|
|
444
|
+
timeoutMs: row.timeout_ms,
|
|
445
|
+
retryCount: row.retry_count,
|
|
446
|
+
enabled: Boolean(row.enabled),
|
|
447
|
+
status: row.status,
|
|
448
|
+
lastCheckedAt: row.last_checked_at,
|
|
449
|
+
createdAt: row.created_at,
|
|
450
|
+
updatedAt: row.updated_at
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
function checkResultFromRow(row) {
|
|
454
|
+
return {
|
|
455
|
+
id: row.id,
|
|
456
|
+
monitorId: row.monitor_id,
|
|
457
|
+
checkedAt: row.checked_at,
|
|
458
|
+
status: row.status,
|
|
459
|
+
latencyMs: row.latency_ms,
|
|
460
|
+
statusCode: row.status_code,
|
|
461
|
+
error: row.error,
|
|
462
|
+
attemptCount: row.attempt_count
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
function incidentFromRow(row) {
|
|
466
|
+
return {
|
|
467
|
+
id: row.id,
|
|
468
|
+
monitorId: row.monitor_id,
|
|
469
|
+
status: row.status,
|
|
470
|
+
openedAt: row.opened_at,
|
|
471
|
+
closedAt: row.closed_at,
|
|
472
|
+
lastFailureAt: row.last_failure_at,
|
|
473
|
+
failureCount: row.failure_count,
|
|
474
|
+
recoveryCheckId: row.recovery_check_id,
|
|
475
|
+
reason: row.reason
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
function newId(prefix) {
|
|
479
|
+
return `${prefix}_${randomUUID().replace(/-/g, "").slice(0, 18)}`;
|
|
480
|
+
}
|
|
481
|
+
function boundedInteger(value, label, min, max) {
|
|
482
|
+
if (!Number.isInteger(value) || value < min || value > max) {
|
|
483
|
+
throw new Error(`${label} must be an integer from ${min} to ${max}`);
|
|
484
|
+
}
|
|
485
|
+
return value;
|
|
486
|
+
}
|
|
487
|
+
function clampLimit(value) {
|
|
488
|
+
if (!Number.isFinite(value))
|
|
489
|
+
return 50;
|
|
490
|
+
return Math.max(1, Math.min(Math.floor(value), MAX_RESULT_LIMIT));
|
|
491
|
+
}
|
|
492
|
+
function round(value, places) {
|
|
493
|
+
const factor = 10 ** places;
|
|
494
|
+
return Math.round(value * factor) / factor;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// src/service.ts
|
|
498
|
+
class UptimeService {
|
|
499
|
+
store;
|
|
500
|
+
checkRunner;
|
|
501
|
+
inFlightChecks = new Set;
|
|
502
|
+
constructor(options = {}) {
|
|
503
|
+
this.store = options.store ?? new UptimeStore(options);
|
|
504
|
+
this.checkRunner = options.checkRunner ?? runMonitorCheck;
|
|
505
|
+
}
|
|
506
|
+
close() {
|
|
507
|
+
this.store.close();
|
|
508
|
+
}
|
|
509
|
+
createMonitor(input) {
|
|
510
|
+
return this.store.createMonitor(input);
|
|
511
|
+
}
|
|
512
|
+
updateMonitor(idOrName, input) {
|
|
513
|
+
return this.store.updateMonitor(idOrName, input);
|
|
514
|
+
}
|
|
515
|
+
deleteMonitor(idOrName) {
|
|
516
|
+
return this.store.deleteMonitor(idOrName);
|
|
517
|
+
}
|
|
518
|
+
listMonitors(options = {}) {
|
|
519
|
+
return this.store.listMonitors(options);
|
|
520
|
+
}
|
|
521
|
+
getMonitor(idOrName) {
|
|
522
|
+
return this.store.getMonitor(idOrName);
|
|
523
|
+
}
|
|
524
|
+
listResults(options = {}) {
|
|
525
|
+
return this.store.listResults(options);
|
|
526
|
+
}
|
|
527
|
+
listIncidents(options = {}) {
|
|
528
|
+
return this.store.listIncidents(options);
|
|
529
|
+
}
|
|
530
|
+
summary() {
|
|
531
|
+
return this.store.summary();
|
|
532
|
+
}
|
|
533
|
+
async checkMonitor(idOrName) {
|
|
534
|
+
const monitor = this.store.getMonitor(idOrName);
|
|
535
|
+
if (!monitor)
|
|
536
|
+
throw new Error(`Monitor not found: ${idOrName}`);
|
|
537
|
+
if (!monitor.enabled)
|
|
538
|
+
throw new Error(`Monitor is disabled: ${monitor.name}`);
|
|
539
|
+
if (this.inFlightChecks.has(monitor.id))
|
|
540
|
+
throw new Error(`Monitor check already in progress: ${monitor.name}`);
|
|
541
|
+
this.inFlightChecks.add(monitor.id);
|
|
542
|
+
try {
|
|
543
|
+
let attemptCount = 0;
|
|
544
|
+
let last = null;
|
|
545
|
+
const maxAttempts = Math.max(1, monitor.retryCount + 1);
|
|
546
|
+
while (attemptCount < maxAttempts) {
|
|
547
|
+
attemptCount += 1;
|
|
548
|
+
last = await this.checkRunner(monitor);
|
|
549
|
+
if (last.status === "up")
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
return this.store.recordCheckResult({
|
|
553
|
+
monitorId: monitor.id,
|
|
554
|
+
status: last.status,
|
|
555
|
+
latencyMs: last.latencyMs,
|
|
556
|
+
statusCode: last.statusCode ?? null,
|
|
557
|
+
error: last.error ?? null,
|
|
558
|
+
attemptCount
|
|
559
|
+
});
|
|
560
|
+
} finally {
|
|
561
|
+
this.inFlightChecks.delete(monitor.id);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
async checkAll() {
|
|
565
|
+
const monitors = this.store.listMonitors();
|
|
566
|
+
const results = [];
|
|
567
|
+
for (const monitor of monitors) {
|
|
568
|
+
results.push(await this.checkMonitor(monitor.id));
|
|
569
|
+
}
|
|
570
|
+
return results;
|
|
571
|
+
}
|
|
572
|
+
startScheduler(options = {}) {
|
|
573
|
+
const tickMs = options.tickMs ?? 1000;
|
|
574
|
+
const timer = setInterval(() => {
|
|
575
|
+
this.runDueChecks().catch((error) => {
|
|
576
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
577
|
+
});
|
|
578
|
+
}, tickMs);
|
|
579
|
+
return {
|
|
580
|
+
stop: () => clearInterval(timer)
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
async runDueChecks(now = new Date) {
|
|
584
|
+
const due = this.store.listMonitors().filter((monitor) => this.isDue(monitor, now));
|
|
585
|
+
const results = [];
|
|
586
|
+
for (const monitor of due) {
|
|
587
|
+
const current = this.store.getMonitor(monitor.id);
|
|
588
|
+
if (!current || !this.isDue(current, now))
|
|
589
|
+
continue;
|
|
590
|
+
results.push(await this.checkMonitor(current.id));
|
|
591
|
+
}
|
|
592
|
+
return results;
|
|
593
|
+
}
|
|
594
|
+
isDue(monitor, now) {
|
|
595
|
+
if (!monitor.enabled)
|
|
596
|
+
return false;
|
|
597
|
+
if (this.inFlightChecks.has(monitor.id))
|
|
598
|
+
return false;
|
|
599
|
+
if (!monitor.lastCheckedAt)
|
|
600
|
+
return true;
|
|
601
|
+
const last = new Date(monitor.lastCheckedAt).getTime();
|
|
602
|
+
return now.getTime() - last >= monitor.intervalSeconds * 1000;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
function createUptimeClient(options = {}) {
|
|
606
|
+
return new UptimeService(options);
|
|
607
|
+
}
|
|
608
|
+
export {
|
|
609
|
+
createUptimeClient,
|
|
610
|
+
UptimeService
|
|
611
|
+
};
|