Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
230 lines
7.3 KiB
TypeScript
230 lines
7.3 KiB
TypeScript
/**
|
|
* Admin diagnostics client — GET /api/admin/diagnostics
|
|
* Operational health only (no secrets). Distinct from /admin/analytics marketing charts.
|
|
*/
|
|
import { api, ApiError } from "$lib/api";
|
|
|
|
export const ADMIN_DIAGNOSTICS_PATH = "/api/admin/diagnostics";
|
|
|
|
export type DiagCheckStatus = "ok" | "warn" | "fail" | "skip" | string;
|
|
|
|
export type DiagCheck = {
|
|
name: string;
|
|
status: DiagCheckStatus;
|
|
detail?: string;
|
|
latency_ms?: number;
|
|
enabled?: boolean;
|
|
configured?: boolean;
|
|
dry_run?: boolean;
|
|
host_set?: boolean;
|
|
};
|
|
|
|
export type DiagQueue = {
|
|
driver?: string;
|
|
by_status?: Record<string, number>;
|
|
total?: number;
|
|
stuck_running?: number;
|
|
failed?: number;
|
|
running?: number;
|
|
pending?: number;
|
|
completed?: number;
|
|
cancelled?: number;
|
|
[key: string]: unknown;
|
|
};
|
|
|
|
export type DiagJobFailure = {
|
|
id: string;
|
|
company_id: string;
|
|
status: string;
|
|
total_products?: number;
|
|
processed_products?: number;
|
|
error?: string;
|
|
created_at?: string;
|
|
updated_at?: string;
|
|
};
|
|
|
|
export type DiagAIFailure = {
|
|
id: string;
|
|
ticket_id: string;
|
|
company_id: string;
|
|
kind: string;
|
|
created_at?: string;
|
|
};
|
|
|
|
export type DiagConfigSanity = {
|
|
app_env?: string;
|
|
maintenance_mode?: boolean;
|
|
read_only_mode?: boolean;
|
|
session_secure?: boolean;
|
|
smtp_enabled?: boolean;
|
|
email_dry_run?: boolean;
|
|
smtp_host_set?: boolean;
|
|
stripe_mock?: boolean;
|
|
eprel_enabled?: boolean;
|
|
processing_rpm?: number;
|
|
processing_batch_size?: number;
|
|
processing_max_retries?: number;
|
|
upload_dir_configured?: boolean;
|
|
trusted_proxies_configured?: boolean;
|
|
web_origin_set?: boolean;
|
|
public_api_url_set?: boolean;
|
|
token_signing_secret_set?: boolean;
|
|
openai_key_set?: boolean;
|
|
pinecone_key_set?: boolean;
|
|
stripe_secret_set?: boolean;
|
|
stripe_webhook_secret_set?: boolean;
|
|
stripe_mock_rejected_in_prod?: boolean;
|
|
credentials_encryption_key_set?: boolean;
|
|
[key: string]: unknown;
|
|
};
|
|
|
|
export type DiagCutoverGoose = {
|
|
status?: DiagCheckStatus;
|
|
detail?: string;
|
|
version_max?: number;
|
|
expected_min?: number;
|
|
required?: Record<string, boolean>;
|
|
};
|
|
|
|
export type DiagCutoverWorker = {
|
|
status?: DiagCheckStatus | "missing" | "stale" | "unavailable";
|
|
detail?: string;
|
|
last_seen_age_s?: number;
|
|
stale_after_s?: number;
|
|
};
|
|
|
|
export type DiagCutover = {
|
|
status?: DiagCheckStatus;
|
|
detail?: string;
|
|
goose?: DiagCutoverGoose;
|
|
worker?: DiagCutoverWorker;
|
|
companies_without_plan?: number;
|
|
/** Companies with zero non-revoked api_keys (reissue inventory; keys never ETL'd). */
|
|
companies_without_api_keys?: number;
|
|
};
|
|
|
|
/** Read-only ETL gap COUNTs (blobs metadata-only + jobs/history). Not an import path. */
|
|
export type DiagMigrationInventory = {
|
|
status?: DiagCheckStatus;
|
|
detail?: string;
|
|
files_total?: number;
|
|
files_metadata_only?: number;
|
|
processing_jobs_total?: number;
|
|
processing_jobs_migrated?: number;
|
|
tasks_total?: number;
|
|
jobs_domain_ran?: boolean;
|
|
notes?: string[];
|
|
};
|
|
|
|
export type AdminDiagnostics = {
|
|
status: "ok" | "degraded" | "fail" | string;
|
|
generated_at?: string;
|
|
checks: DiagCheck[];
|
|
queue: DiagQueue;
|
|
cutover?: DiagCutover;
|
|
migration_inventory?: DiagMigrationInventory;
|
|
config: DiagConfigSanity;
|
|
recent_failures: DiagJobFailure[];
|
|
recent_ai_failures?: DiagAIFailure[];
|
|
filters?: { status?: string; failures_limit?: number };
|
|
links?: Record<string, string>;
|
|
notes?: string[];
|
|
};
|
|
|
|
export type LoadDiagnosticsOpts = {
|
|
status?: string;
|
|
failuresLimit?: number;
|
|
};
|
|
|
|
/** True when the Go API returned a JSON error envelope (not HTML/proxy text). */
|
|
function isApiJsonErrorBody(body: unknown): boolean {
|
|
if (!body || typeof body !== "object") return false;
|
|
const rec = body as Record<string, unknown>;
|
|
return typeof rec.error === "string" || typeof rec.message === "string";
|
|
}
|
|
|
|
/**
|
|
* Endpoint missing on the API (chi JSON 404/501).
|
|
* Do NOT treat SPA/Vite HTML 404s or proxy text as "not on this deployment" —
|
|
* those are misconfig/reachability issues and must surface as load failures.
|
|
*/
|
|
export function isDiagnosticsUnavailable(err: unknown): boolean {
|
|
return (
|
|
err instanceof ApiError &&
|
|
(err.status === 404 || err.status === 501) &&
|
|
isApiJsonErrorBody(err.body)
|
|
);
|
|
}
|
|
|
|
export function isDiagnosticsRateLimited(err: unknown): boolean {
|
|
return err instanceof ApiError && err.status === 429;
|
|
}
|
|
|
|
/** Non-JSON 404/502/etc. — usually proxy down or PUBLIC_API_URL misaligned. */
|
|
export function isDiagnosticsUnreachable(err: unknown): boolean {
|
|
if (!(err instanceof ApiError)) return false;
|
|
if (err.status === 502 || err.status === 503 || err.status === 504) return true;
|
|
if ((err.status === 404 || err.status === 501) && !isApiJsonErrorBody(err.body)) return true;
|
|
return false;
|
|
}
|
|
|
|
export async function loadAdminDiagnostics(opts: LoadDiagnosticsOpts = {}): Promise<AdminDiagnostics> {
|
|
const params = new URLSearchParams();
|
|
const status = (opts.status ?? "").trim().toLowerCase();
|
|
if (status && status !== "all") params.set("status", status);
|
|
if (opts.failuresLimit && opts.failuresLimit > 0) {
|
|
params.set("failures_limit", String(Math.min(opts.failuresLimit, 50)));
|
|
}
|
|
const q = params.toString();
|
|
const path = q ? `${ADMIN_DIAGNOSTICS_PATH}?${q}` : ADMIN_DIAGNOSTICS_PATH;
|
|
return api<AdminDiagnostics>(path);
|
|
}
|
|
|
|
export function checkStatusVariant(
|
|
status: string
|
|
): "success" | "warning" | "destructive" | "secondary" | "outline" {
|
|
switch (String(status).toLowerCase()) {
|
|
case "ok":
|
|
case "ready":
|
|
return "success";
|
|
case "warn":
|
|
case "degraded":
|
|
case "warning":
|
|
case "missing":
|
|
case "stale":
|
|
case "unavailable":
|
|
return "warning";
|
|
case "fail":
|
|
case "failed":
|
|
case "error":
|
|
return "destructive";
|
|
case "skip":
|
|
return "secondary";
|
|
default:
|
|
return "outline";
|
|
}
|
|
}
|
|
|
|
/** Config keys that are boolean presence/flag indicators (safe to show as Yes/No). */
|
|
export const CONFIG_FLAG_LABELS: { key: keyof DiagConfigSanity; labelKey: string }[] = [
|
|
{ key: "maintenance_mode", labelKey: "admin.diagnostics.config.maintenance_mode" },
|
|
{ key: "read_only_mode", labelKey: "admin.diagnostics.config.read_only_mode" },
|
|
{ key: "session_secure", labelKey: "admin.diagnostics.config.session_secure" },
|
|
{ key: "smtp_enabled", labelKey: "admin.diagnostics.config.smtp_enabled" },
|
|
{ key: "email_dry_run", labelKey: "admin.diagnostics.config.email_dry_run" },
|
|
{ key: "smtp_host_set", labelKey: "admin.diagnostics.config.smtp_host_set" },
|
|
{ key: "stripe_mock", labelKey: "admin.diagnostics.config.stripe_mock" },
|
|
{ key: "eprel_enabled", labelKey: "admin.diagnostics.config.eprel_enabled" },
|
|
{ key: "upload_dir_configured", labelKey: "admin.diagnostics.config.upload_dir_configured" },
|
|
{ key: "trusted_proxies_configured", labelKey: "admin.diagnostics.config.trusted_proxies_configured" },
|
|
{ key: "web_origin_set", labelKey: "admin.diagnostics.config.web_origin_set" },
|
|
{ key: "public_api_url_set", labelKey: "admin.diagnostics.config.public_api_url_set" },
|
|
{ key: "token_signing_secret_set", labelKey: "admin.diagnostics.config.token_signing_secret_set" },
|
|
{ key: "openai_key_set", labelKey: "admin.diagnostics.config.openai_key_set" },
|
|
{ key: "pinecone_key_set", labelKey: "admin.diagnostics.config.pinecone_key_set" },
|
|
{ key: "stripe_secret_set", labelKey: "admin.diagnostics.config.stripe_secret_set" },
|
|
{ key: "stripe_webhook_secret_set", labelKey: "admin.diagnostics.config.stripe_webhook_secret_set" },
|
|
{ key: "stripe_mock_rejected_in_prod", labelKey: "admin.diagnostics.config.stripe_mock_rejected_in_prod" },
|
|
{ key: "credentials_encryption_key_set", labelKey: "admin.diagnostics.config.credentials_encryption_key_set" }
|
|
];
|