feat(elasticsearch): improve REST commands and visual filters
This commit is contained in:
15 files changed
+1151
-115
No files matched your search
+167
@@ -65,6 +65,14 @@ function sqlServerConn(id: string): ConnectionConfig {
|
||||
};
|
||||
}
|
||||
|
||||
function elasticsearchConn(id: string): ConnectionConfig {
|
||||
return {
|
||||
...conn(id),
|
||||
db_type: "elasticsearch",
|
||||
port: 9200,
|
||||
};
|
||||
}
|
||||
|
||||
function sparkConn(id: string): ConnectionConfig {
|
||||
return {
|
||||
...conn(id),
|
||||
@@ -4898,6 +4906,165 @@ test("query results keep readable table source labels with active database conte
|
||||
}
|
||||
});
|
||||
|
||||
test("Elasticsearch execute all runs each REST request separately", async () => {
|
||||
const restoreStorage = installMemoryStorage();
|
||||
setActivePinia(createPinia());
|
||||
const connectionStore = useConnectionStore();
|
||||
const store = useQueryStore();
|
||||
const originalFetch = globalThis.fetch;
|
||||
const executedRequests: string[] = [];
|
||||
|
||||
connectionStore.addEphemeralConnection(elasticsearchConn("es-1"));
|
||||
const tabId = store.createTab("es-1", "", "Elasticsearch query");
|
||||
globalThis.fetch = withConnectionHealthMock(async (input, init) => {
|
||||
const url = String(input);
|
||||
if (url === "/api/query/execute") {
|
||||
const body = JSON.parse(String(init?.body ?? "{}"));
|
||||
executedRequests.push(body.sql);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
columns: ["status", "response"],
|
||||
rows: [[200, body.sql.startsWith("HEAD") ? "null" : '{"ok":true}']],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
}
|
||||
return new Response("unexpected request", { status: 500 });
|
||||
});
|
||||
|
||||
const sql = `/* 查看节点 JVM 信息 */
|
||||
GET /_nodes/stats/jvm?pretty
|
||||
|
||||
# 判断索引是否存在
|
||||
HEAD /dbx-orders
|
||||
|
||||
// 查询文档
|
||||
POST /dbx-orders/_search
|
||||
{"size":1}`;
|
||||
|
||||
try {
|
||||
await store.executeTabSql(tabId, sql);
|
||||
assert.deepEqual(executedRequests, ["GET /_nodes/stats/jvm?pretty", "HEAD /dbx-orders", 'POST /dbx-orders/_search\n{"size":1}']);
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId);
|
||||
assert.deepEqual(
|
||||
tab?.results?.map((result) => result.sourceStatement),
|
||||
executedRequests,
|
||||
);
|
||||
assert.equal(tab?.activeResultIndex, 0);
|
||||
assert.equal(tab?.result?.rows[0]?.[0], 200);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
restoreStorage();
|
||||
}
|
||||
});
|
||||
|
||||
test("Elasticsearch execute all stops after an HTTP error by default", async () => {
|
||||
const restoreStorage = installMemoryStorage();
|
||||
setActivePinia(createPinia());
|
||||
const connectionStore = useConnectionStore();
|
||||
const store = useQueryStore();
|
||||
const settingsStore = useSettingsStore();
|
||||
const originalFetch = globalThis.fetch;
|
||||
const executedRequests: string[] = [];
|
||||
|
||||
settingsStore.updateEditorSettings({ continueOnErrorOnBatch: false });
|
||||
connectionStore.addEphemeralConnection(elasticsearchConn("es-stop-on-error"));
|
||||
const tabId = store.createTab("es-stop-on-error", "", "Elasticsearch query");
|
||||
globalThis.fetch = withConnectionHealthMock(async (input, init) => {
|
||||
if (String(input) === "/api/query/execute") {
|
||||
const body = JSON.parse(String(init?.body ?? "{}"));
|
||||
executedRequests.push(body.sql);
|
||||
const status = body.sql.includes("missing") ? 404 : 200;
|
||||
return new Response(JSON.stringify({ columns: ["status", "response"], rows: [[status, status === 404 ? '{"error":"missing"}' : '{"ok":true}']], affected_rows: 0, execution_time_ms: 1 }), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
return new Response("unexpected request", { status: 500 });
|
||||
});
|
||||
|
||||
try {
|
||||
await store.executeTabSql(tabId, "GET /missing/_mapping\n\nGET /_cluster/health");
|
||||
assert.deepEqual(executedRequests, ["GET /missing/_mapping"]);
|
||||
assert.equal(store.tabs.find((item) => item.id === tabId)?.result?.rows[0]?.[0], 404);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
restoreStorage();
|
||||
}
|
||||
});
|
||||
|
||||
test("Elasticsearch execute all continues after an HTTP error when enabled", async () => {
|
||||
const restoreStorage = installMemoryStorage();
|
||||
setActivePinia(createPinia());
|
||||
const connectionStore = useConnectionStore();
|
||||
const store = useQueryStore();
|
||||
const settingsStore = useSettingsStore();
|
||||
const originalFetch = globalThis.fetch;
|
||||
const executedRequests: string[] = [];
|
||||
|
||||
settingsStore.updateEditorSettings({ continueOnErrorOnBatch: true });
|
||||
connectionStore.addEphemeralConnection(elasticsearchConn("es-continue-on-error"));
|
||||
const tabId = store.createTab("es-continue-on-error", "", "Elasticsearch query");
|
||||
globalThis.fetch = withConnectionHealthMock(async (input, init) => {
|
||||
if (String(input) === "/api/query/execute") {
|
||||
const body = JSON.parse(String(init?.body ?? "{}"));
|
||||
executedRequests.push(body.sql);
|
||||
const status = body.sql.includes("missing") ? 404 : 200;
|
||||
return new Response(JSON.stringify({ columns: ["status", "response"], rows: [[status, status === 404 ? '{"error":"missing"}' : '{"ok":true}']], affected_rows: 0, execution_time_ms: 1 }), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
return new Response("unexpected request", { status: 500 });
|
||||
});
|
||||
|
||||
try {
|
||||
await store.executeTabSql(tabId, "GET /missing/_mapping\n\nGET /_cluster/health");
|
||||
assert.deepEqual(executedRequests, ["GET /missing/_mapping", "GET /_cluster/health"]);
|
||||
const tab = store.tabs.find((item) => item.id === tabId);
|
||||
assert.equal(tab?.results?.length, 2);
|
||||
assert.equal(tab?.activeResultIndex, 0);
|
||||
assert.equal(tab?.result?.rows[0]?.[0], 404);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
restoreStorage();
|
||||
}
|
||||
});
|
||||
|
||||
test("Elasticsearch executes a single REST request after a block comment", async () => {
|
||||
const restoreStorage = installMemoryStorage();
|
||||
setActivePinia(createPinia());
|
||||
const connectionStore = useConnectionStore();
|
||||
const store = useQueryStore();
|
||||
const originalFetch = globalThis.fetch;
|
||||
const executedRequests: string[] = [];
|
||||
|
||||
connectionStore.addEphemeralConnection(elasticsearchConn("es-single"));
|
||||
const tabId = store.createTab("es-single", "", "Elasticsearch query");
|
||||
globalThis.fetch = withConnectionHealthMock(async (input, init) => {
|
||||
if (String(input) === "/api/query/execute") {
|
||||
const body = JSON.parse(String(init?.body ?? "{}"));
|
||||
executedRequests.push(body.sql);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
columns: ["status", "response"],
|
||||
rows: [[200, '{"nodes":{}}']],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 1,
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
}
|
||||
return new Response("unexpected request", { status: 500 });
|
||||
});
|
||||
|
||||
try {
|
||||
await store.executeTabSql(tabId, "/* 查看节点 JVM 信息 */\nGET /_nodes/stats/jvm?pretty");
|
||||
assert.deepEqual(executedRequests, ["GET /_nodes/stats/jvm?pretty"]);
|
||||
assert.equal(store.tabs.find((item) => item.id === tabId)?.result?.sourceStatement, "GET /_nodes/stats/jvm?pretty");
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
restoreStorage();
|
||||
}
|
||||
});
|
||||
|
||||
test("tab reuse is scoped by mode and schema instead of title alone", () => {
|
||||
const restoreStorage = installMemoryStorage();
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user