feat(query): cross-database timing details, drop OceanBase audit query

This commit is contained in:
efeire authored and GitHub committed 2026-09-24 19:17:27 +08:00
1 parent 4b9a1c4d5c
commit d0497e0ca6
69 files changed
+973 -180

No files matched your search

@@ -0,0 +1,30 @@
import { afterEach, expect, it, vi } from "vitest";
import { createQueryRequestTiming } from "../queryRequestTiming";
afterEach(() => vi.restoreAllMocks());
it("separates preparation, backend waits and local conversion across multiple calls", async () => {
let now = 100;
vi.spyOn(performance, "now").mockImplementation(() => now);
const timing = createQueryRequestTiming(80);
const value = await timing.run(async () => {
now = 130;
return "result";
});
expect(value).toBe("result");
now = 140;
await timing.run(async () => {
now = 160;
});
now = 165;
const result = { columns: [], rows: [], affected_rows: 0, execution_time_ms: 99 };
expect(timing.finish(result)).toMatchObject({ client_prepare_ms: 20, client_request_wait_ms: 50, client_result_ms: 15, timing_page_count: 2, execution_time_ms: 99 });
});
it("does not invent request timings for a local command and propagates failures without replay", async () => {
const result = { columns: [], rows: [], affected_rows: 0, execution_time_ms: 0 };
expect(createQueryRequestTiming(0).finish(result)).not.toHaveProperty("client_request_wait_ms");
const call = vi.fn().mockRejectedValue(new Error("query failed"));
await expect(createQueryRequestTiming(0).run(call)).rejects.toThrow("query failed");
expect(call).toHaveBeenCalledTimes(1);
});
@@ -17,6 +17,18 @@ function backend(name: ResultCacheBackend["name"], overrides: Partial<ResultCach
}
describe("tab result cache statement execution metadata", () => {
it("retains timing details for active, array and historical results through cache restore", async () => {
const { buildTabResultSnapshot } = await import("@/lib/tabs/tabResultCache");
const timing = { query_timings_ms: { agent_total: 40, pool_acquire: 20, pool_release: 2 }, client_prepare_ms: 3, client_result_ms: 4, timing_page_count: 2 };
const result = { columns: ["N"], rows: [[1]], affected_rows: 0, execution_time_ms: 50, ...timing };
const tab = { result, results: [result], resultRuns: [{ id: "timed-run", title: "Run", sequence: 1, sql: "SELECT 1", createdAt: 1, result, results: [result] }] };
const snapshot = buildTabResultSnapshot(tab as Parameters<typeof buildTabResultSnapshot>[0])!;
expect(snapshot.result?.query_timings_ms).not.toBe(result.query_timings_ms);
const restored = decodeTabResultSnapshot(encodeTabResultSnapshot(snapshot));
for (const value of [restored.result, restored.results?.[0], restored.resultRuns?.[0].result, restored.resultRuns?.[0].results?.[0]]) {
expect(value).toMatchObject({ rows: [[1]], ...timing });
}
});
it("selects one authoritative backend before fallback", () => {
expect(resultCacheBackendOrder(true)).toEqual(["runtime", "indexed-db"]);
expect(resultCacheBackendOrder(false)).toEqual(["indexed-db", "runtime"]);
@@ -0,0 +1,28 @@
import type { QueryResult } from "@/types/database";
/** Local command timing; run wraps only backend calls, never result conversion. */
export function createQueryRequestTiming(prepareStartedAt: number) {
let firstRequestAt: number | undefined;
let waitMs = 0;
let requests = 0;
return {
async run<T, A extends unknown[]>(request: (...args: A) => Promise<T>, ...args: A): Promise<T> {
const startedAt = performance.now();
firstRequestAt ??= startedAt;
requests += 1;
try {
return await request(...args);
} finally {
waitMs += Math.max(0, performance.now() - startedAt);
}
},
finish<T extends QueryResult>(result: T): T {
if (firstRequestAt === undefined || result.execution_error) return result;
result.client_prepare_ms = Math.max(0, firstRequestAt - prepareStartedAt);
result.client_request_wait_ms = waitMs;
result.client_result_ms = Math.max(0, performance.now() - firstRequestAt - waitMs);
result.timing_page_count = requests;
return result;
},
};
}
@@ -71,6 +71,10 @@ interface ColumnarQueryResult {
execution_time_ms: number;
server_execute_time_us?: number;
client_request_wait_ms?: number;
query_timings_ms?: QueryResult["query_timings_ms"];
client_prepare_ms?: number;
client_result_ms?: number;
timing_page_count?: number;
truncated?: boolean;
has_more?: boolean;
sourceLabel?: string;
@@ -361,6 +365,10 @@ function stripSessionIds(result: QueryResult | undefined): QueryResult | undefin
execution_time_ms: result.execution_time_ms,
server_execute_time_us: result.server_execute_time_us,
client_request_wait_ms: result.client_request_wait_ms,
query_timings_ms: result.query_timings_ms ? { ...result.query_timings_ms } : undefined,
client_prepare_ms: result.client_prepare_ms,
client_result_ms: result.client_result_ms,
timing_page_count: result.timing_page_count,
truncated: result.truncated,
session_id: undefined,
has_more: result.has_more,
@@ -420,6 +428,10 @@ function toColumnarResult(result: QueryResult | undefined): ColumnarQueryResult
execution_time_ms: result.execution_time_ms,
server_execute_time_us: result.server_execute_time_us,
client_request_wait_ms: result.client_request_wait_ms,
query_timings_ms: result.query_timings_ms ? { ...result.query_timings_ms } : undefined,
client_prepare_ms: result.client_prepare_ms,
client_result_ms: result.client_result_ms,
timing_page_count: result.timing_page_count,
truncated: result.truncated,
has_more: result.has_more,
sourceLabel: result.sourceLabel,
@@ -451,6 +463,10 @@ function fromColumnarResult(result: ColumnarQueryResult | undefined): QueryResul
execution_time_ms: result.execution_time_ms,
server_execute_time_us: result.server_execute_time_us,
client_request_wait_ms: result.client_request_wait_ms,
query_timings_ms: result.query_timings_ms ? { ...result.query_timings_ms } : undefined,
client_prepare_ms: result.client_prepare_ms,
client_result_ms: result.client_result_ms,
timing_page_count: result.timing_page_count,
truncated: result.truncated,
session_id: undefined,
has_more: result.has_more,