fix(elasticsearch): correct totals and CAT JSON output

This commit is contained in:
onenewcode authored and GitHub committed 2026-07-23 17:06:23 +08:00
1 parent a8e5e8d8f9
commit f07509b5ad
27 files changed
+855 -70

No files matched your search

@@ -0,0 +1,45 @@
import { describe, expect, it } from "vitest";
import { clampDocumentPage, documentPageRequestLimit, resetElasticsearchDocumentTotals, resolveElasticsearchDocumentTotals } from "@/lib/document/elasticsearchDocumentTotals";
describe("Elasticsearch document totals", () => {
it("keeps a lower-bound search total separate from an exact background count", () => {
expect(resolveElasticsearchDocumentTotals(10_000, false)).toEqual({
total: 10_000,
totalIsExact: false,
paginationTotal: 10_000,
});
expect(resolveElasticsearchDocumentTotals(10_000, false, 552_033)).toEqual({
total: 552_033,
totalIsExact: true,
paginationTotal: 10_000,
});
});
it("uses exact search totals for both display and pagination", () => {
expect(resolveElasticsearchDocumentTotals(42, true, 100)).toEqual({
total: 42,
totalIsExact: true,
paginationTotal: 42,
});
});
it("clears a stale display total without losing the safe page cap during a refresh", () => {
expect(resetElasticsearchDocumentTotals(10_000, true)).toEqual({
total: undefined,
totalIsExact: true,
paginationTotal: 10_000,
});
expect(resetElasticsearchDocumentTotals(10_000)).toEqual({
total: undefined,
totalIsExact: true,
paginationTotal: undefined,
});
});
it("clamps the final request without exceeding the conservative page cap", () => {
expect(clampDocumentPage(30, 333, 10_000)).toBe(30);
expect(clampDocumentPage(31, 333, 10_000)).toBe(30);
expect(documentPageRequestLimit(30, 333, 10_000)).toBe(10);
expect(documentPageRequestLimit(0, 100, undefined)).toBe(100);
});
});
@@ -0,0 +1,19 @@
import { describe, expect, it } from "vitest";
import { elasticsearchJsonResponseForResult } from "@/lib/elasticsearch/elasticsearchJsonResponse";
import type { QueryResult } from "@/types/database";
const catJsonResult: QueryResult = {
columns: ["status", "response"],
rows: [[200, '[{"index":"data_pack_and_box_index_v1","docs.count":"42"}]']],
affected_rows: 0,
execution_time_ms: 1,
};
describe("Elasticsearch JSON response detection", () => {
it("routes an unformatted CAT response to the JSON renderer", () => {
expect(elasticsearchJsonResponseForResult("elasticsearch", "GET /_cat/indices/data_pack_and_box_index_v1", catJsonResult)).toEqual({
status: 200,
body: '[{"index":"data_pack_and_box_index_v1","docs.count":"42"}]',
});
});
});
+2
View File
@@ -491,6 +491,7 @@ export const mongoDropDatabase = forward("mongoDropDatabase");
export const mongoDropCollection = forward("mongoDropCollection");
export const mongoRenameCollection = forward("mongoRenameCollection");
export const documentFindDocuments = forward("documentFindDocuments");
export const elasticsearchCountDocuments = forward("elasticsearchCountDocuments");
export const mongoFindDocuments = forward("mongoFindDocuments");
export const mongoParseShellCommand = forward("mongoParseShellCommand");
export const mongoFindOne = forward("mongoFindOne");
@@ -611,6 +612,7 @@ export type {
KvPutOptions,
KvPutResponse,
KvDeleteResponse,
DocumentQueryResult,
MongoDocumentResult,
HistoryEntry,
HistoryConnectionFilter,
+6 -1
View File
@@ -75,6 +75,7 @@ import type {
KvPutOptions,
KvPutResponse,
KvDeleteResponse,
DocumentQueryResult,
MongoDocumentResult,
MongoCollectionStatsResult,
MongoGridFsBucketInfo,
@@ -2178,10 +2179,14 @@ export async function mongoFindOne(connectionId: string, database: string, colle
return post("/api/mongo/find-one", { connectionId, database, collection, filter, projection, options, executionId });
}
export async function documentFindDocuments(connectionId: string, database: string, collection: string, skip: number, limit: number, filter?: string, projection?: string, sort?: string, executionId?: string): Promise<MongoDocumentResult> {
export async function documentFindDocuments(connectionId: string, database: string, collection: string, skip: number, limit: number, filter?: string, projection?: string, sort?: string, executionId?: string): Promise<DocumentQueryResult> {
return post("/api/document-store/find-documents", { connectionId, database, collection, skip, limit, filter, projection, sort, executionId });
}
export async function elasticsearchCountDocuments(connectionId: string, index: string, filter?: string, executionId?: string): Promise<number> {
return post("/api/document-store/elasticsearch-count-documents", { connectionId, index, filter, executionId });
}
export async function mongoCountDocuments(connectionId: string, database: string, collection: string, filter?: string, mode?: "accurate" | "legacy", executionId?: string): Promise<number> {
return post("/api/mongo/count-documents", { connectionId, database, collection, filter, mode, executionId });
}
+11 -3
View File
@@ -1849,14 +1849,18 @@ export async function zookeeperDelete(connectionId: string, key: string): Promis
return invoke("zookeeper_delete", { connectionId, key });
}
// --- MongoDB ---
export interface MongoDocumentResult {
// --- Document stores ---
export interface DocumentQueryResult {
documents: any[];
raw_documents?: string[];
extended_documents?: any[];
total: number;
total_is_exact?: boolean;
}
// Kept for callers that are specifically using MongoDB APIs.
export type MongoDocumentResult = DocumentQueryResult;
export interface MongoCollectionStatsResult {
count: unknown;
size: unknown;
@@ -1942,10 +1946,14 @@ export async function mongoParseShellCommand(source: string): Promise<MongoComma
return normalizeRustMongoCommand(raw);
}
export async function documentFindDocuments(connectionId: string, database: string, collection: string, skip: number, limit: number, filter?: string, projection?: string, sort?: string, executionId?: string): Promise<MongoDocumentResult> {
export async function documentFindDocuments(connectionId: string, database: string, collection: string, skip: number, limit: number, filter?: string, projection?: string, sort?: string, executionId?: string): Promise<DocumentQueryResult> {
return invoke("document_find_documents", { connectionId, database, collection, skip, limit, filter, projection, sort, executionId });
}
export async function elasticsearchCountDocuments(connectionId: string, index: string, filter?: string, executionId?: string): Promise<number> {
return invoke("elasticsearch_count_documents", { connectionId, index, filter, executionId });
}
export async function mongoCountDocuments(connectionId: string, database: string, collection: string, filter?: string, mode?: "accurate" | "legacy", executionId?: string): Promise<number> {
return invoke("mongo_count_documents", { connectionId, database, collection, filter, mode, executionId });
}
@@ -0,0 +1,49 @@
export interface ElasticsearchDocumentTotals {
total: number;
totalIsExact: boolean;
paginationTotal: number;
}
export interface ResetElasticsearchDocumentTotals {
total: undefined;
totalIsExact: boolean;
paginationTotal?: number;
}
export function resolveElasticsearchDocumentTotals(searchTotal: number, searchTotalIsExact: boolean, exactCount?: number): ElasticsearchDocumentTotals {
if (searchTotalIsExact || exactCount === undefined) {
return {
total: searchTotal,
totalIsExact: searchTotalIsExact,
paginationTotal: searchTotal,
};
}
return {
total: exactCount,
totalIsExact: true,
paginationTotal: Math.min(searchTotal, exactCount),
};
}
export function resetElasticsearchDocumentTotals(paginationTotal: number | undefined, preservePaginationTotal = false): ResetElasticsearchDocumentTotals {
return {
total: undefined,
totalIsExact: true,
paginationTotal: preservePaginationTotal ? paginationTotal : undefined,
};
}
export function clampDocumentPage(page: number, pageSize: number, paginationTotal?: number): number {
const normalizedPage = Math.max(0, Math.floor(page));
if (paginationTotal === undefined) return normalizedPage;
if (paginationTotal <= 0) return 0;
const lastPage = Math.max(0, Math.ceil(paginationTotal / Math.max(1, pageSize)) - 1);
return Math.min(normalizedPage, lastPage);
}
export function documentPageRequestLimit(page: number, pageSize: number, paginationTotal?: number): number {
const normalizedPageSize = Math.max(1, Math.floor(pageSize));
if (paginationTotal === undefined) return normalizedPageSize;
const remaining = paginationTotal - Math.max(0, Math.floor(page)) * normalizedPageSize;
return remaining > 0 ? Math.min(normalizedPageSize, remaining) : normalizedPageSize;
}
@@ -9,9 +9,8 @@ export interface ElasticsearchJsonResponse {
const ELASTICSEARCH_REST_STATEMENT = /^(?:GET|POST|PUT|DELETE|HEAD)\s+\S+/i;
/**
* Detect the result shape emitted for a JSON response to an explicit
* Elasticsearch REST request. SQL and text (such as CAT) results keep using
* the normal data-grid path.
* Detect the raw HTTP result emitted for an Elasticsearch REST request.
* DBX asks unformatted CAT requests for JSON so they use this response panel.
*/
export function elasticsearchJsonResponseForResult(databaseType: DatabaseType | undefined, sourceStatement: string | undefined, result: QueryResult | undefined): ElasticsearchJsonResponse | undefined {
if (databaseType !== "elasticsearch" || !result || typeof sourceStatement !== "string") return undefined;