postgresai 0.14.0-dev.8 → 0.14.0-dev.80
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/README.md +161 -61
- package/bin/postgres-ai.ts +2596 -428
- package/bun.lock +258 -0
- package/bunfig.toml +20 -0
- package/dist/bin/postgres-ai.js +31218 -1575
- package/dist/sql/01.role.sql +16 -0
- package/dist/sql/02.extensions.sql +8 -0
- package/dist/sql/03.permissions.sql +38 -0
- package/dist/sql/04.optional_rds.sql +6 -0
- package/dist/sql/05.optional_self_managed.sql +8 -0
- package/dist/sql/06.helpers.sql +439 -0
- package/dist/sql/sql/01.role.sql +16 -0
- package/dist/sql/sql/02.extensions.sql +8 -0
- package/dist/sql/sql/03.permissions.sql +38 -0
- package/dist/sql/sql/04.optional_rds.sql +6 -0
- package/dist/sql/sql/05.optional_self_managed.sql +8 -0
- package/dist/sql/sql/06.helpers.sql +439 -0
- package/dist/sql/sql/uninit/01.helpers.sql +5 -0
- package/dist/sql/sql/uninit/02.permissions.sql +30 -0
- package/dist/sql/sql/uninit/03.role.sql +27 -0
- package/dist/sql/uninit/01.helpers.sql +5 -0
- package/dist/sql/uninit/02.permissions.sql +30 -0
- package/dist/sql/uninit/03.role.sql +27 -0
- package/lib/auth-server.ts +124 -106
- package/lib/checkup-api.ts +386 -0
- package/lib/checkup-dictionary.ts +113 -0
- package/lib/checkup.ts +1435 -0
- package/lib/config.ts +6 -3
- package/lib/init.ts +655 -189
- package/lib/issues.ts +848 -193
- package/lib/mcp-server.ts +391 -91
- package/lib/metrics-loader.ts +127 -0
- package/lib/supabase.ts +824 -0
- package/lib/util.ts +61 -0
- package/package.json +22 -10
- package/packages/postgres-ai/README.md +26 -0
- package/packages/postgres-ai/bin/postgres-ai.js +27 -0
- package/packages/postgres-ai/package.json +27 -0
- package/scripts/embed-checkup-dictionary.ts +106 -0
- package/scripts/embed-metrics.ts +154 -0
- package/sql/01.role.sql +16 -0
- package/sql/02.extensions.sql +8 -0
- package/sql/03.permissions.sql +38 -0
- package/sql/04.optional_rds.sql +6 -0
- package/sql/05.optional_self_managed.sql +8 -0
- package/sql/06.helpers.sql +439 -0
- package/sql/uninit/01.helpers.sql +5 -0
- package/sql/uninit/02.permissions.sql +30 -0
- package/sql/uninit/03.role.sql +27 -0
- package/test/auth.test.ts +258 -0
- package/test/checkup.integration.test.ts +321 -0
- package/test/checkup.test.ts +1116 -0
- package/test/config-consistency.test.ts +36 -0
- package/test/init.integration.test.ts +508 -0
- package/test/init.test.ts +916 -0
- package/test/issues.cli.test.ts +538 -0
- package/test/issues.test.ts +456 -0
- package/test/mcp-server.test.ts +1527 -0
- package/test/schema-validation.test.ts +81 -0
- package/test/supabase.test.ts +568 -0
- package/test/test-utils.ts +128 -0
- package/tsconfig.json +12 -20
- package/dist/bin/postgres-ai.d.ts +0 -3
- package/dist/bin/postgres-ai.d.ts.map +0 -1
- package/dist/bin/postgres-ai.js.map +0 -1
- package/dist/lib/auth-server.d.ts +0 -31
- package/dist/lib/auth-server.d.ts.map +0 -1
- package/dist/lib/auth-server.js +0 -263
- package/dist/lib/auth-server.js.map +0 -1
- package/dist/lib/config.d.ts +0 -45
- package/dist/lib/config.d.ts.map +0 -1
- package/dist/lib/config.js +0 -181
- package/dist/lib/config.js.map +0 -1
- package/dist/lib/init.d.ts +0 -64
- package/dist/lib/init.d.ts.map +0 -1
- package/dist/lib/init.js +0 -399
- package/dist/lib/init.js.map +0 -1
- package/dist/lib/issues.d.ts +0 -75
- package/dist/lib/issues.d.ts.map +0 -1
- package/dist/lib/issues.js +0 -336
- package/dist/lib/issues.js.map +0 -1
- package/dist/lib/mcp-server.d.ts +0 -9
- package/dist/lib/mcp-server.d.ts.map +0 -1
- package/dist/lib/mcp-server.js +0 -168
- package/dist/lib/mcp-server.js.map +0 -1
- package/dist/lib/pkce.d.ts +0 -32
- package/dist/lib/pkce.d.ts.map +0 -1
- package/dist/lib/pkce.js +0 -101
- package/dist/lib/pkce.js.map +0 -1
- package/dist/lib/util.d.ts +0 -27
- package/dist/lib/util.d.ts.map +0 -1
- package/dist/lib/util.js +0 -46
- package/dist/lib/util.js.map +0 -1
- package/dist/package.json +0 -46
- package/test/init.integration.test.cjs +0 -269
- package/test/init.test.cjs +0 -76
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import * as https from "https";
|
|
2
|
+
import { URL } from "url";
|
|
3
|
+
import { normalizeBaseUrl } from "./util";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Retry configuration for network operations
|
|
7
|
+
*/
|
|
8
|
+
export interface RetryConfig {
|
|
9
|
+
maxAttempts: number;
|
|
10
|
+
initialDelayMs: number;
|
|
11
|
+
maxDelayMs: number;
|
|
12
|
+
backoffMultiplier: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const DEFAULT_RETRY_CONFIG: RetryConfig = {
|
|
16
|
+
maxAttempts: 3,
|
|
17
|
+
initialDelayMs: 1000,
|
|
18
|
+
maxDelayMs: 10000,
|
|
19
|
+
backoffMultiplier: 2,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Check if an error is retryable (network errors, timeouts, 5xx errors)
|
|
24
|
+
*/
|
|
25
|
+
function isRetryableError(err: unknown): boolean {
|
|
26
|
+
if (err instanceof RpcError) {
|
|
27
|
+
// Retry on server errors (5xx), not on client errors (4xx)
|
|
28
|
+
return err.statusCode >= 500 && err.statusCode < 600;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Check for Node.js error codes (works on Error and Error-like objects)
|
|
32
|
+
if (typeof err === "object" && err !== null && "code" in err) {
|
|
33
|
+
const code = String((err as { code: unknown }).code);
|
|
34
|
+
if (["ECONNRESET", "ECONNREFUSED", "ENOTFOUND", "ETIMEDOUT"].includes(code)) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (err instanceof Error) {
|
|
40
|
+
const msg = err.message.toLowerCase();
|
|
41
|
+
// Retry on network-related errors based on message content
|
|
42
|
+
return (
|
|
43
|
+
msg.includes("timeout") ||
|
|
44
|
+
msg.includes("timed out") ||
|
|
45
|
+
msg.includes("econnreset") ||
|
|
46
|
+
msg.includes("econnrefused") ||
|
|
47
|
+
msg.includes("enotfound") ||
|
|
48
|
+
msg.includes("socket hang up") ||
|
|
49
|
+
msg.includes("network")
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Execute an async function with exponential backoff retry.
|
|
58
|
+
* Retries on network errors, timeouts, and 5xx server errors.
|
|
59
|
+
* Does not retry on 4xx client errors.
|
|
60
|
+
*
|
|
61
|
+
* @param fn - Async function to execute
|
|
62
|
+
* @param config - Optional retry configuration (uses defaults if not provided)
|
|
63
|
+
* @param onRetry - Optional callback invoked before each retry attempt
|
|
64
|
+
* @returns Promise resolving to the function result
|
|
65
|
+
* @throws The last error if all retry attempts fail or error is non-retryable
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const result = await withRetry(
|
|
70
|
+
* () => fetchData(),
|
|
71
|
+
* { maxAttempts: 3 },
|
|
72
|
+
* (attempt, err, delay) => console.log(`Retry ${attempt}, waiting ${delay}ms`)
|
|
73
|
+
* );
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export async function withRetry<T>(
|
|
77
|
+
fn: () => Promise<T>,
|
|
78
|
+
config: Partial<RetryConfig> = {},
|
|
79
|
+
onRetry?: (attempt: number, error: unknown, delayMs: number) => void
|
|
80
|
+
): Promise<T> {
|
|
81
|
+
const { maxAttempts, initialDelayMs, maxDelayMs, backoffMultiplier } = {
|
|
82
|
+
...DEFAULT_RETRY_CONFIG,
|
|
83
|
+
...config,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
let lastError: unknown;
|
|
87
|
+
let delayMs = initialDelayMs;
|
|
88
|
+
|
|
89
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
90
|
+
try {
|
|
91
|
+
return await fn();
|
|
92
|
+
} catch (err) {
|
|
93
|
+
lastError = err;
|
|
94
|
+
|
|
95
|
+
if (attempt === maxAttempts || !isRetryableError(err)) {
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (onRetry) {
|
|
100
|
+
onRetry(attempt, err, delayMs);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
104
|
+
delayMs = Math.min(delayMs * backoffMultiplier, maxDelayMs);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
throw lastError;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Error thrown when an RPC call to the PostgresAI API fails.
|
|
113
|
+
* Contains detailed information about the failure for debugging and display.
|
|
114
|
+
*/
|
|
115
|
+
export class RpcError extends Error {
|
|
116
|
+
/** Name of the RPC endpoint that failed */
|
|
117
|
+
rpcName: string;
|
|
118
|
+
/** HTTP status code returned by the server */
|
|
119
|
+
statusCode: number;
|
|
120
|
+
/** Raw response body text */
|
|
121
|
+
payloadText: string;
|
|
122
|
+
/** Parsed JSON response body, or null if parsing failed */
|
|
123
|
+
payloadJson: any | null;
|
|
124
|
+
|
|
125
|
+
constructor(params: { rpcName: string; statusCode: number; payloadText: string; payloadJson: any | null }) {
|
|
126
|
+
const { rpcName, statusCode, payloadText, payloadJson } = params;
|
|
127
|
+
super(`RPC ${rpcName} failed: HTTP ${statusCode}`);
|
|
128
|
+
this.name = "RpcError";
|
|
129
|
+
this.rpcName = rpcName;
|
|
130
|
+
this.statusCode = statusCode;
|
|
131
|
+
this.payloadText = payloadText;
|
|
132
|
+
this.payloadJson = payloadJson;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Format an RpcError for human-readable console display.
|
|
138
|
+
* Extracts message, details, and hint from the error payload if available.
|
|
139
|
+
*
|
|
140
|
+
* @param err - The RpcError to format
|
|
141
|
+
* @returns Array of lines suitable for console output
|
|
142
|
+
*/
|
|
143
|
+
export function formatRpcErrorForDisplay(err: RpcError): string[] {
|
|
144
|
+
const lines: string[] = [];
|
|
145
|
+
lines.push(`Error: RPC ${err.rpcName} failed: HTTP ${err.statusCode}`);
|
|
146
|
+
|
|
147
|
+
const obj = err.payloadJson && typeof err.payloadJson === "object" ? err.payloadJson : null;
|
|
148
|
+
const details = obj && typeof (obj as any).details === "string" ? (obj as any).details : "";
|
|
149
|
+
const hint = obj && typeof (obj as any).hint === "string" ? (obj as any).hint : "";
|
|
150
|
+
const message = obj && typeof (obj as any).message === "string" ? (obj as any).message : "";
|
|
151
|
+
|
|
152
|
+
if (message) lines.push(`Message: ${message}`);
|
|
153
|
+
if (details) lines.push(`Details: ${details}`);
|
|
154
|
+
if (hint) lines.push(`Hint: ${hint}`);
|
|
155
|
+
|
|
156
|
+
// Fallback to raw payload if we couldn't extract anything useful.
|
|
157
|
+
if (!message && !details && !hint) {
|
|
158
|
+
const t = (err.payloadText || "").trim();
|
|
159
|
+
if (t) lines.push(t);
|
|
160
|
+
}
|
|
161
|
+
return lines;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function unwrapRpcResponse(parsed: unknown): any {
|
|
165
|
+
// Some deployments return a plain object, others return an array of rows,
|
|
166
|
+
// and some wrap OUT params under a "result" key.
|
|
167
|
+
if (Array.isArray(parsed)) {
|
|
168
|
+
if (parsed.length === 1) return unwrapRpcResponse(parsed[0]);
|
|
169
|
+
return parsed;
|
|
170
|
+
}
|
|
171
|
+
if (parsed && typeof parsed === "object") {
|
|
172
|
+
const obj = parsed as any;
|
|
173
|
+
if (obj.result !== undefined) return obj.result;
|
|
174
|
+
}
|
|
175
|
+
return parsed as any;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Default timeout for HTTP requests (30 seconds)
|
|
179
|
+
const HTTP_TIMEOUT_MS = 30_000;
|
|
180
|
+
|
|
181
|
+
async function postRpc<T>(params: {
|
|
182
|
+
apiKey: string;
|
|
183
|
+
apiBaseUrl: string;
|
|
184
|
+
rpcName: string;
|
|
185
|
+
bodyObj: Record<string, unknown>;
|
|
186
|
+
timeoutMs?: number;
|
|
187
|
+
}): Promise<T> {
|
|
188
|
+
const { apiKey, apiBaseUrl, rpcName, bodyObj, timeoutMs = HTTP_TIMEOUT_MS } = params;
|
|
189
|
+
if (!apiKey) throw new Error("API key is required");
|
|
190
|
+
const base = normalizeBaseUrl(apiBaseUrl);
|
|
191
|
+
const url = new URL(`${base}/rpc/${rpcName}`);
|
|
192
|
+
const body = JSON.stringify(bodyObj);
|
|
193
|
+
|
|
194
|
+
const headers: Record<string, string> = {
|
|
195
|
+
// API key is sent in BOTH header and body (see bodyObj.access_token):
|
|
196
|
+
// - Header: Used by the API gateway/proxy for HTTP authentication
|
|
197
|
+
// - Body: Passed to PostgreSQL RPC function for in-database authorization
|
|
198
|
+
// This is intentional for defense-in-depth; backend validates both.
|
|
199
|
+
"access-token": apiKey,
|
|
200
|
+
"Prefer": "return=representation",
|
|
201
|
+
"Content-Type": "application/json",
|
|
202
|
+
"Content-Length": Buffer.byteLength(body).toString(),
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// Use AbortController for clean timeout handling
|
|
206
|
+
const controller = new AbortController();
|
|
207
|
+
let timeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
208
|
+
let settled = false;
|
|
209
|
+
|
|
210
|
+
return new Promise((resolve, reject) => {
|
|
211
|
+
const settledReject = (err: Error) => {
|
|
212
|
+
if (settled) return;
|
|
213
|
+
settled = true;
|
|
214
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
215
|
+
reject(err);
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const settledResolve = (value: T) => {
|
|
219
|
+
if (settled) return;
|
|
220
|
+
settled = true;
|
|
221
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
222
|
+
resolve(value);
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const req = https.request(
|
|
226
|
+
url,
|
|
227
|
+
{
|
|
228
|
+
method: "POST",
|
|
229
|
+
headers,
|
|
230
|
+
signal: controller.signal,
|
|
231
|
+
},
|
|
232
|
+
(res) => {
|
|
233
|
+
// Response started (headers received) - clear the connection timeout.
|
|
234
|
+
// Once the server starts responding, we let it complete rather than
|
|
235
|
+
// timing out mid-response which would cause confusing errors.
|
|
236
|
+
if (timeoutId) {
|
|
237
|
+
clearTimeout(timeoutId);
|
|
238
|
+
timeoutId = null;
|
|
239
|
+
}
|
|
240
|
+
let data = "";
|
|
241
|
+
res.on("data", (chunk) => (data += chunk));
|
|
242
|
+
res.on("end", () => {
|
|
243
|
+
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
|
244
|
+
try {
|
|
245
|
+
const parsed = JSON.parse(data);
|
|
246
|
+
settledResolve(unwrapRpcResponse(parsed) as T);
|
|
247
|
+
} catch {
|
|
248
|
+
settledReject(new Error(`Failed to parse RPC response: ${data}`));
|
|
249
|
+
}
|
|
250
|
+
} else {
|
|
251
|
+
const statusCode = res.statusCode || 0;
|
|
252
|
+
let payloadJson: any | null = null;
|
|
253
|
+
if (data) {
|
|
254
|
+
try {
|
|
255
|
+
payloadJson = JSON.parse(data);
|
|
256
|
+
} catch {
|
|
257
|
+
payloadJson = null;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
settledReject(new RpcError({ rpcName, statusCode, payloadText: data, payloadJson }));
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
res.on("error", (err) => {
|
|
264
|
+
settledReject(err);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
// Set up connection timeout - applies until response headers are received.
|
|
270
|
+
// Once response starts, timeout is cleared (see response callback above).
|
|
271
|
+
timeoutId = setTimeout(() => {
|
|
272
|
+
controller.abort();
|
|
273
|
+
req.destroy(); // Backup: ensure request is terminated
|
|
274
|
+
settledReject(new Error(`RPC ${rpcName} timed out after ${timeoutMs}ms (no response)`));
|
|
275
|
+
}, timeoutMs);
|
|
276
|
+
|
|
277
|
+
req.on("error", (err: Error) => {
|
|
278
|
+
// Handle abort as timeout (may already be rejected by timeout handler)
|
|
279
|
+
if (err.name === "AbortError" || (err as any).code === "ABORT_ERR") {
|
|
280
|
+
settledReject(new Error(`RPC ${rpcName} timed out after ${timeoutMs}ms`));
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
// Provide clearer error for common network issues
|
|
284
|
+
if ((err as any).code === "ECONNREFUSED") {
|
|
285
|
+
settledReject(new Error(`RPC ${rpcName} failed: connection refused to ${url.host}`));
|
|
286
|
+
} else if ((err as any).code === "ENOTFOUND") {
|
|
287
|
+
settledReject(new Error(`RPC ${rpcName} failed: DNS lookup failed for ${url.host}`));
|
|
288
|
+
} else if ((err as any).code === "ECONNRESET") {
|
|
289
|
+
settledReject(new Error(`RPC ${rpcName} failed: connection reset by server`));
|
|
290
|
+
} else {
|
|
291
|
+
settledReject(err);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
req.write(body);
|
|
296
|
+
req.end();
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Create a new checkup report in the PostgresAI backend.
|
|
302
|
+
* This creates the parent report container; individual check results
|
|
303
|
+
* are uploaded separately via uploadCheckupReportJson().
|
|
304
|
+
*
|
|
305
|
+
* @param params - Configuration for report creation
|
|
306
|
+
* @param params.apiKey - PostgresAI API access token
|
|
307
|
+
* @param params.apiBaseUrl - Base URL of the PostgresAI API
|
|
308
|
+
* @param params.project - Project name or ID to associate the report with
|
|
309
|
+
* @param params.status - Optional initial status for the report
|
|
310
|
+
* @returns Promise resolving to the created report ID
|
|
311
|
+
* @throws {RpcError} On API failures (4xx/5xx responses)
|
|
312
|
+
* @throws {Error} On network errors or unexpected response format
|
|
313
|
+
*/
|
|
314
|
+
export async function createCheckupReport(params: {
|
|
315
|
+
apiKey: string;
|
|
316
|
+
apiBaseUrl: string;
|
|
317
|
+
project: string;
|
|
318
|
+
status?: string;
|
|
319
|
+
}): Promise<{ reportId: number }> {
|
|
320
|
+
const { apiKey, apiBaseUrl, project, status } = params;
|
|
321
|
+
const bodyObj: Record<string, unknown> = {
|
|
322
|
+
access_token: apiKey,
|
|
323
|
+
project,
|
|
324
|
+
};
|
|
325
|
+
if (status) bodyObj.status = status;
|
|
326
|
+
|
|
327
|
+
const resp = await postRpc<any>({
|
|
328
|
+
apiKey,
|
|
329
|
+
apiBaseUrl,
|
|
330
|
+
rpcName: "checkup_report_create",
|
|
331
|
+
bodyObj,
|
|
332
|
+
});
|
|
333
|
+
const reportId = Number(resp?.report_id);
|
|
334
|
+
if (!Number.isFinite(reportId) || reportId <= 0) {
|
|
335
|
+
throw new Error(`Unexpected checkup_report_create response: ${JSON.stringify(resp)}`);
|
|
336
|
+
}
|
|
337
|
+
return { reportId };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Upload a JSON check result to an existing checkup report.
|
|
342
|
+
* Each check (e.g., H001, A003) is uploaded as a separate JSON file.
|
|
343
|
+
*
|
|
344
|
+
* @param params - Configuration for the upload
|
|
345
|
+
* @param params.apiKey - PostgresAI API access token
|
|
346
|
+
* @param params.apiBaseUrl - Base URL of the PostgresAI API
|
|
347
|
+
* @param params.reportId - ID of the parent report (from createCheckupReport)
|
|
348
|
+
* @param params.filename - Filename for the uploaded JSON (e.g., "H001.json")
|
|
349
|
+
* @param params.checkId - Check identifier (e.g., "H001", "A003")
|
|
350
|
+
* @param params.jsonText - JSON content as a string
|
|
351
|
+
* @returns Promise resolving to the created report chunk ID
|
|
352
|
+
* @throws {RpcError} On API failures (4xx/5xx responses)
|
|
353
|
+
* @throws {Error} On network errors or unexpected response format
|
|
354
|
+
*/
|
|
355
|
+
export async function uploadCheckupReportJson(params: {
|
|
356
|
+
apiKey: string;
|
|
357
|
+
apiBaseUrl: string;
|
|
358
|
+
reportId: number;
|
|
359
|
+
filename: string;
|
|
360
|
+
checkId: string;
|
|
361
|
+
jsonText: string;
|
|
362
|
+
}): Promise<{ reportChunkId: number }> {
|
|
363
|
+
const { apiKey, apiBaseUrl, reportId, filename, checkId, jsonText } = params;
|
|
364
|
+
const bodyObj: Record<string, unknown> = {
|
|
365
|
+
access_token: apiKey,
|
|
366
|
+
checkup_report_id: reportId,
|
|
367
|
+
filename,
|
|
368
|
+
check_id: checkId,
|
|
369
|
+
data: jsonText,
|
|
370
|
+
type: "json",
|
|
371
|
+
generate_issue: true,
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const resp = await postRpc<any>({
|
|
375
|
+
apiKey,
|
|
376
|
+
apiBaseUrl,
|
|
377
|
+
rpcName: "checkup_report_file_post",
|
|
378
|
+
bodyObj,
|
|
379
|
+
});
|
|
380
|
+
// Backend has a typo: "report_chunck_id" (with 'ck') - handle both spellings for compatibility
|
|
381
|
+
const chunkId = Number(resp?.report_chunck_id ?? resp?.report_chunk_id);
|
|
382
|
+
if (!Number.isFinite(chunkId) || chunkId <= 0) {
|
|
383
|
+
throw new Error(`Unexpected checkup_report_file_post response: ${JSON.stringify(resp)}`);
|
|
384
|
+
}
|
|
385
|
+
return { reportChunkId: chunkId };
|
|
386
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checkup Dictionary Module
|
|
3
|
+
* =========================
|
|
4
|
+
* Provides access to the checkup report dictionary data embedded at build time.
|
|
5
|
+
*
|
|
6
|
+
* The dictionary is fetched from https://postgres.ai/api/general/checkup_dictionary
|
|
7
|
+
* during the build process and embedded into checkup-dictionary-embedded.ts.
|
|
8
|
+
*
|
|
9
|
+
* This ensures no API calls are made at runtime while keeping the data up-to-date.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { CHECKUP_DICTIONARY_DATA } from "./checkup-dictionary-embedded";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A checkup dictionary entry describing a single check type.
|
|
16
|
+
*/
|
|
17
|
+
export interface CheckupDictionaryEntry {
|
|
18
|
+
/** Unique check code (e.g., "A001", "H002") */
|
|
19
|
+
code: string;
|
|
20
|
+
/** Human-readable title for the check */
|
|
21
|
+
title: string;
|
|
22
|
+
/** Brief description of what the check covers */
|
|
23
|
+
description: string;
|
|
24
|
+
/** Category grouping (e.g., "system", "indexes", "vacuum") */
|
|
25
|
+
category: string;
|
|
26
|
+
/** Optional sort order within category */
|
|
27
|
+
sort_order: number | null;
|
|
28
|
+
/** Whether this is a system-level report */
|
|
29
|
+
is_system_report: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Module-level cache for O(1) lookups by code.
|
|
34
|
+
* Initialized at module load time from embedded data.
|
|
35
|
+
*/
|
|
36
|
+
const dictionaryByCode: Map<string, CheckupDictionaryEntry> = new Map(
|
|
37
|
+
CHECKUP_DICTIONARY_DATA.map((entry) => [entry.code, entry])
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Get all checkup dictionary entries.
|
|
42
|
+
*
|
|
43
|
+
* @returns Array of all checkup dictionary entries
|
|
44
|
+
*/
|
|
45
|
+
export function getAllCheckupEntries(): CheckupDictionaryEntry[] {
|
|
46
|
+
return CHECKUP_DICTIONARY_DATA;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get a checkup dictionary entry by its code.
|
|
51
|
+
*
|
|
52
|
+
* @param code - The check code (e.g., "A001", "H002")
|
|
53
|
+
* @returns The dictionary entry or null if not found
|
|
54
|
+
*/
|
|
55
|
+
export function getCheckupEntry(code: string): CheckupDictionaryEntry | null {
|
|
56
|
+
return dictionaryByCode.get(code.toUpperCase()) ?? null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get the title for a checkup code.
|
|
61
|
+
*
|
|
62
|
+
* @param code - The check code (e.g., "A001", "H002")
|
|
63
|
+
* @returns The title or the code itself if not found
|
|
64
|
+
*/
|
|
65
|
+
export function getCheckupTitle(code: string): string {
|
|
66
|
+
const entry = getCheckupEntry(code);
|
|
67
|
+
return entry?.title ?? code;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Check if a code exists in the dictionary.
|
|
72
|
+
*
|
|
73
|
+
* @param code - The check code to validate
|
|
74
|
+
* @returns True if the code exists in the dictionary
|
|
75
|
+
*/
|
|
76
|
+
export function isValidCheckupCode(code: string): boolean {
|
|
77
|
+
return dictionaryByCode.has(code.toUpperCase());
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get all check codes as an array.
|
|
82
|
+
*
|
|
83
|
+
* @returns Array of all check codes (e.g., ["A001", "A002", ...])
|
|
84
|
+
*/
|
|
85
|
+
export function getAllCheckupCodes(): string[] {
|
|
86
|
+
return CHECKUP_DICTIONARY_DATA.map((entry) => entry.code);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get checkup entries filtered by category.
|
|
91
|
+
*
|
|
92
|
+
* @param category - The category to filter by (e.g., "indexes", "vacuum")
|
|
93
|
+
* @returns Array of entries in the specified category
|
|
94
|
+
*/
|
|
95
|
+
export function getCheckupEntriesByCategory(category: string): CheckupDictionaryEntry[] {
|
|
96
|
+
return CHECKUP_DICTIONARY_DATA.filter(
|
|
97
|
+
(entry) => entry.category.toLowerCase() === category.toLowerCase()
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Build a code-to-title mapping object.
|
|
103
|
+
* Useful for backwards compatibility with CHECK_INFO style usage.
|
|
104
|
+
*
|
|
105
|
+
* @returns Object mapping check codes to titles (e.g., { "A001": "System information", ... })
|
|
106
|
+
*/
|
|
107
|
+
export function buildCheckInfoMap(): Record<string, string> {
|
|
108
|
+
const result: Record<string, string> = {};
|
|
109
|
+
for (const entry of CHECKUP_DICTIONARY_DATA) {
|
|
110
|
+
result[entry.code] = entry.title;
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
}
|