plugin-elastic-report 0.0.0-stage → 0.7.0
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/ai/skills/elastic-report/SKILLS.md +34 -0
- package/client-v2.d.ts +2 -0
- package/client-v2.js +1 -0
- package/client.d.ts +2 -0
- package/client.js +1 -0
- package/dist/client/357.0dd385384cab5dbf.js +10 -0
- package/dist/client/index.js +10 -0
- package/dist/client-v2/851.142c93fc6d666b28.js +10 -0
- package/dist/client-v2/857.a5bf9637d5fd4ef5.js +10 -0
- package/dist/client-v2/877.f4095a3dbcadf98f.js +10 -0
- package/dist/client-v2/index.js +10 -0
- package/dist/externalVersion.js +21 -0
- package/dist/index.js +48 -0
- package/dist/locale/en-US.json +198 -0
- package/dist/locale/vi-VN.json +198 -0
- package/dist/locale/zh-CN.json +198 -0
- package/dist/node_modules/@elastic/elasticsearch/.dockerignore +11 -0
- package/dist/node_modules/@elastic/elasticsearch/LICENSE +202 -0
- package/dist/node_modules/@elastic/elasticsearch/NOTICE.txt +2 -0
- package/dist/node_modules/@elastic/elasticsearch/catalog-info.yaml +50 -0
- package/dist/node_modules/@elastic/elasticsearch/codecov.yml +14 -0
- package/dist/node_modules/@elastic/elasticsearch/index.d.ts +28 -0
- package/dist/node_modules/@elastic/elasticsearch/index.js +85525 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/async_search.d.ts +39 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/async_search.js +155 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/autoscaling.d.ts +39 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/autoscaling.js +130 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/bulk.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/bulk.js +60 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/capabilities.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/capabilities.js +43 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/cat.d.ts +193 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/cat.js +766 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ccr.d.ts +102 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ccr.js +395 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/clear_scroll.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/clear_scroll.js +60 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/close_point_in_time.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/close_point_in_time.js +56 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/cluster.d.ts +123 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/cluster.js +482 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/connector.d.ts +245 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/connector.js +1121 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/count.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/count.js +68 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/create.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/create.js +53 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/dangling_indices.d.ts +32 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/dangling_indices.js +100 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete.js +46 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_by_query.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_by_query.js +59 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_by_query_rethrottle.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_by_query_rethrottle.js +45 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_script.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_script.js +45 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/enrich.d.ts +46 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/enrich.js +169 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/eql.d.ts +39 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/eql.js +139 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/esql.d.ts +46 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/esql.js +170 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/exists.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/exists.js +46 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/exists_source.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/exists_source.js +46 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/explain.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/explain.js +60 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/features.d.ts +25 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/features.js +75 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/field_caps.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/field_caps.js +68 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/fleet.d.ts +50 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/fleet.js +211 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get.js +46 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script.js +45 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script_context.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script_context.js +43 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script_languages.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script_languages.js +43 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_source.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_source.js +46 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/graph.d.ts +18 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/graph.js +70 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/health_report.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/health_report.js +54 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ilm.d.ts +88 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ilm.js +329 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/index.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/index.js +61 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/indices.d.ts +494 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/indices.js +2133 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/inference.d.ts +221 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/inference.js +1167 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/info.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/info.js +43 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ingest.d.ts +95 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ingest.js +389 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/knn_search.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/knn_search.js +59 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/license.d.ts +60 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/license.js +194 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/logstash.d.ts +32 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/logstash.js +118 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/mget.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/mget.js +68 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/migration.d.ts +32 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/migration.js +107 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ml.d.ts +522 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ml.js +2468 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/monitoring.d.ts +18 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/monitoring.js +60 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/msearch.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/msearch.js +60 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/msearch_template.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/msearch_template.js +60 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/mtermvectors.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/mtermvectors.js +68 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/nodes.d.ts +60 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/nodes.js +290 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/open_point_in_time.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/open_point_in_time.js +59 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ping.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ping.js +43 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/profiling.d.ts +39 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/profiling.js +135 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/put_script.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/put_script.js +68 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/query_rules.d.ts +67 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/query_rules.js +260 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/rank_eval.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/rank_eval.js +67 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/reindex.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/reindex.js +56 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/reindex_rethrottle.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/reindex_rethrottle.js +45 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/render_search_template.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/render_search_template.js +68 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/rollup.d.ts +67 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/rollup.js +263 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/scripts_painless_execute.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/scripts_painless_execute.js +57 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/scroll.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/scroll.js +59 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search.js +75 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_application.d.ts +81 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_application.js +313 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_mvt.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_mvt.js +63 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_shards.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_shards.js +54 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_template.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_template.js +68 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/searchable_snapshots.d.ts +39 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/searchable_snapshots.js +167 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/security.d.ts +459 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/security.js +1982 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/shutdown.d.ts +32 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/shutdown.js +125 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/simulate.d.ts +18 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/simulate.js +78 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/slm.d.ts +74 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/slm.js +253 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/snapshot.d.ts +102 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/snapshot.js +410 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/sql.d.ts +53 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/sql.js +205 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ssl.d.ts +18 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ssl.js +54 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/streams.d.ts +32 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/streams.js +96 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/synonyms.d.ts +60 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/synonyms.js +223 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/tasks.d.ts +32 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/tasks.js +109 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/terms_enum.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/terms_enum.js +59 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/termvectors.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/termvectors.js +68 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/text_structure.d.ts +39 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/text_structure.js +148 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/transform.d.ts +101 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/transform.js +386 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update.js +60 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update_by_query.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update_by_query.js +59 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update_by_query_rethrottle.d.ts +14 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update_by_query_rethrottle.js +45 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/watcher.d.ts +102 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/watcher.js +405 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/xpack.d.ts +25 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/api/xpack.js +75 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/index.d.ts +265 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/index.js +632 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/types.d.ts +19933 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/types.js +21 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/typesWithBodyKey.d.ts +20606 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/api/typesWithBodyKey.js +21 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/client.d.ts +152 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/client.js +369 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/helpers.d.ts +184 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/helpers.js +889 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/sniffingTransport.d.ts +4 -0
- package/dist/node_modules/@elastic/elasticsearch/lib/sniffingTransport.js +56 -0
- package/dist/node_modules/@elastic/elasticsearch/node_modules/.bin/arrow2csv +15 -0
- package/dist/node_modules/@elastic/elasticsearch/node_modules/.bin/arrow2csv.cmd +7 -0
- package/dist/node_modules/@elastic/elasticsearch/package.json +1 -0
- package/dist/node_modules/@elastic/elasticsearch/tsconfig.json +38 -0
- package/dist/server/acl-guard.js +179 -0
- package/dist/server/collections/es-report-data-views.js +85 -0
- package/dist/server/collections/es-reports.js +101 -0
- package/dist/server/index.js +58 -0
- package/dist/server/mcp/ElasticReportMcpServer.js +344 -0
- package/dist/server/mcp/ElasticReportToolCatalog.js +639 -0
- package/dist/server/plugin.js +529 -0
- package/dist/server/services/ElasticReportService.js +377 -0
- package/dist/server/services/ElasticsearchConnectionService.js +572 -0
- package/dist/server/services/ReportExportService.js +243 -0
- package/dist/server/services/ReportQueryCompiler.js +1299 -0
- package/dist/server/workflow/ElasticReportQueryInstruction.js +123 -0
- package/dist/shared/constants.js +96 -0
- package/dist/shared/types.js +24 -0
- package/package.json +62 -4
- package/server.d.ts +2 -0
- package/server.js +1 -0
- package/src/client/index.tsx +34 -0
- package/src/client/workflow-instructions.tsx +44 -0
- package/src/client-v2/__tests__/elastic-report.test.tsx +791 -0
- package/src/client-v2/__tests__/kql-autocomplete.test.ts +178 -0
- package/src/client-v2/api.ts +320 -0
- package/src/client-v2/bus/DashboardFilterBus.ts +212 -0
- package/src/client-v2/components/ElasticBasicCharts.tsx +652 -0
- package/src/client-v2/components/ElasticFilterBar.tsx +372 -0
- package/src/client-v2/components/ElasticKqlInput.tsx +421 -0
- package/src/client-v2/components/ElasticReportBlock.tsx +306 -0
- package/src/client-v2/components/ElasticReportRenderer.tsx +1257 -0
- package/src/client-v2/components/HighlightText.tsx +50 -0
- package/src/client-v2/components/ReportSelect.tsx +85 -0
- package/src/client-v2/index.tsx +14 -0
- package/src/client-v2/locale.ts +23 -0
- package/src/client-v2/models/ElasticFilterBarBlockModel.tsx +115 -0
- package/src/client-v2/models/ElasticReportBlockModel.tsx +118 -0
- package/src/client-v2/pages/ElasticReportManager.tsx +1453 -0
- package/src/client-v2/plugin.tsx +72 -0
- package/src/client-v2/utils/kqlAutocomplete.ts +861 -0
- package/src/client-v2/workflow/instructions.tsx +149 -0
- package/src/index.ts +11 -0
- package/src/locale/en-US.json +198 -0
- package/src/locale/vi-VN.json +198 -0
- package/src/locale/zh-CN.json +198 -0
- package/src/server/__tests__/query-compiler.test.ts +620 -0
- package/src/server/__tests__/report-mcp-acl.integration.test.ts +645 -0
- package/src/server/acl-guard.ts +194 -0
- package/src/server/collections/es-report-data-views.ts +64 -0
- package/src/server/collections/es-reports.ts +80 -0
- package/src/server/index.ts +15 -0
- package/src/server/mcp/ElasticReportMcpServer.ts +391 -0
- package/src/server/mcp/ElasticReportToolCatalog.ts +663 -0
- package/src/server/plugin.ts +599 -0
- package/src/server/services/ElasticReportService.ts +486 -0
- package/src/server/services/ElasticsearchConnectionService.ts +728 -0
- package/src/server/services/ReportExportService.ts +282 -0
- package/src/server/services/ReportQueryCompiler.ts +1552 -0
- package/src/server/workflow/ElasticReportQueryInstruction.ts +124 -0
- package/src/shared/constants.ts +36 -0
- package/src/shared/types.ts +272 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var ReportQueryCompiler_exports = {};
|
|
28
|
+
__export(ReportQueryCompiler_exports, {
|
|
29
|
+
KqlSyntaxError: () => KqlSyntaxError,
|
|
30
|
+
buildFieldCapabilityMap: () => buildFieldCapabilityMap,
|
|
31
|
+
compileFilterItem: () => compileFilterItem,
|
|
32
|
+
compileKqlToQueryDsl: () => compileKqlToQueryDsl,
|
|
33
|
+
compilePreviousTimeRange: () => compilePreviousTimeRange,
|
|
34
|
+
compileSearchRequest: () => compileSearchRequest,
|
|
35
|
+
compileTimeRange: () => compileTimeRange,
|
|
36
|
+
mergeSpecWithOverrides: () => mergeSpecWithOverrides,
|
|
37
|
+
normalizeReportFilters: () => normalizeReportFilters,
|
|
38
|
+
normalizeReportSort: () => normalizeReportSort,
|
|
39
|
+
normalizeReportSpec: () => normalizeReportSpec,
|
|
40
|
+
normalizeTimeRange: () => normalizeTimeRange,
|
|
41
|
+
parseSearchResponse: () => parseSearchResponse,
|
|
42
|
+
resolveAggregatableField: () => resolveAggregatableField,
|
|
43
|
+
resolveServerLockedFilters: () => resolveServerLockedFilters,
|
|
44
|
+
sanitizeTimeZone: () => sanitizeTimeZone
|
|
45
|
+
});
|
|
46
|
+
module.exports = __toCommonJS(ReportQueryCompiler_exports);
|
|
47
|
+
var import_constants = require("../../shared/constants");
|
|
48
|
+
class KqlSyntaxError extends Error {
|
|
49
|
+
status = 400;
|
|
50
|
+
constructor(message) {
|
|
51
|
+
super(message);
|
|
52
|
+
this.name = "KqlSyntaxError";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const VALID_VISUALIZATIONS = /* @__PURE__ */ new Set([
|
|
56
|
+
"table",
|
|
57
|
+
"bar",
|
|
58
|
+
"line",
|
|
59
|
+
"area",
|
|
60
|
+
"pie",
|
|
61
|
+
"metric"
|
|
62
|
+
]);
|
|
63
|
+
const VALID_OPERATORS = /* @__PURE__ */ new Set([
|
|
64
|
+
"eq",
|
|
65
|
+
"ne",
|
|
66
|
+
"in",
|
|
67
|
+
"not_in",
|
|
68
|
+
"gt",
|
|
69
|
+
"gte",
|
|
70
|
+
"lt",
|
|
71
|
+
"lte",
|
|
72
|
+
"between",
|
|
73
|
+
"contains",
|
|
74
|
+
"exists",
|
|
75
|
+
"not_exists"
|
|
76
|
+
]);
|
|
77
|
+
const VALID_PRESETS = /* @__PURE__ */ new Set([
|
|
78
|
+
"last_15m",
|
|
79
|
+
"last_1h",
|
|
80
|
+
"last_24h",
|
|
81
|
+
"last_7d",
|
|
82
|
+
"last_30d",
|
|
83
|
+
"last_90d",
|
|
84
|
+
"all",
|
|
85
|
+
"custom"
|
|
86
|
+
]);
|
|
87
|
+
const VALID_METRICS = /* @__PURE__ */ new Set([
|
|
88
|
+
"count",
|
|
89
|
+
"sum",
|
|
90
|
+
"avg",
|
|
91
|
+
"min",
|
|
92
|
+
"max",
|
|
93
|
+
"cardinality",
|
|
94
|
+
"p50",
|
|
95
|
+
"p95",
|
|
96
|
+
"p99"
|
|
97
|
+
]);
|
|
98
|
+
const VALID_STACK_MODES = /* @__PURE__ */ new Set(["none", "stacked", "percent"]);
|
|
99
|
+
const VALID_FIELD_FORMATS = /* @__PURE__ */ new Set([
|
|
100
|
+
"auto",
|
|
101
|
+
"duration_ms",
|
|
102
|
+
"bytes",
|
|
103
|
+
"percent",
|
|
104
|
+
"number",
|
|
105
|
+
"status_badge",
|
|
106
|
+
"url"
|
|
107
|
+
]);
|
|
108
|
+
const VALID_BUCKETS = /* @__PURE__ */ new Set(["date_histogram", "terms"]);
|
|
109
|
+
const VALID_INTERVALS = /* @__PURE__ */ new Set([
|
|
110
|
+
"auto",
|
|
111
|
+
"1m",
|
|
112
|
+
"5m",
|
|
113
|
+
"15m",
|
|
114
|
+
"1h",
|
|
115
|
+
"1d",
|
|
116
|
+
"1w",
|
|
117
|
+
"1M"
|
|
118
|
+
]);
|
|
119
|
+
const CALENDAR_UNIT_INTERVALS = /* @__PURE__ */ new Set(["1m", "1h", "1d", "1w", "1M"]);
|
|
120
|
+
const PRESET_TO_ES_EXPR = {
|
|
121
|
+
last_15m: "now-15m",
|
|
122
|
+
last_1h: "now-1h",
|
|
123
|
+
last_24h: "now-24h",
|
|
124
|
+
last_7d: "now-7d",
|
|
125
|
+
last_30d: "now-30d",
|
|
126
|
+
last_90d: "now-90d"
|
|
127
|
+
};
|
|
128
|
+
const PRESET_TO_PREV_ES_EXPR = {
|
|
129
|
+
last_15m: { gte: "now-30m", lt: "now-15m" },
|
|
130
|
+
last_1h: { gte: "now-2h", lt: "now-1h" },
|
|
131
|
+
last_24h: { gte: "now-48h", lt: "now-24h" },
|
|
132
|
+
last_7d: { gte: "now-14d", lt: "now-7d" },
|
|
133
|
+
last_30d: { gte: "now-60d", lt: "now-30d" },
|
|
134
|
+
last_90d: { gte: "now-180d", lt: "now-90d" }
|
|
135
|
+
};
|
|
136
|
+
function sanitizeTimeZone(raw) {
|
|
137
|
+
if (typeof raw !== "string" || !raw.trim()) return void 0;
|
|
138
|
+
const candidate = raw.trim();
|
|
139
|
+
try {
|
|
140
|
+
Intl.DateTimeFormat(void 0, { timeZone: candidate });
|
|
141
|
+
return candidate;
|
|
142
|
+
} catch {
|
|
143
|
+
return void 0;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function isRecord(value) {
|
|
147
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
148
|
+
}
|
|
149
|
+
function clamp(num, min, max) {
|
|
150
|
+
if (!Number.isFinite(num)) return min;
|
|
151
|
+
return Math.max(min, Math.min(max, Math.trunc(num)));
|
|
152
|
+
}
|
|
153
|
+
function toScalarValue(raw) {
|
|
154
|
+
if (typeof raw === "string") return raw;
|
|
155
|
+
if (typeof raw === "number" && Number.isFinite(raw)) return raw;
|
|
156
|
+
if (typeof raw === "boolean") return raw;
|
|
157
|
+
return void 0;
|
|
158
|
+
}
|
|
159
|
+
function coerceLiteral(raw, fieldMeta) {
|
|
160
|
+
const trimmed = raw.trim();
|
|
161
|
+
if (fieldMeta) {
|
|
162
|
+
if (fieldMeta.type === "boolean") {
|
|
163
|
+
if (trimmed.toLowerCase() === "true") return true;
|
|
164
|
+
if (trimmed.toLowerCase() === "false") return false;
|
|
165
|
+
}
|
|
166
|
+
if (["long", "integer", "short", "byte", "double", "float", "half_float", "scaled_float"].includes(fieldMeta.type)) {
|
|
167
|
+
const parsed = Number(trimmed);
|
|
168
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (trimmed.toLowerCase() === "true") return true;
|
|
172
|
+
if (trimmed.toLowerCase() === "false") return false;
|
|
173
|
+
if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
|
|
174
|
+
const parsed = Number(trimmed);
|
|
175
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
176
|
+
}
|
|
177
|
+
return trimmed;
|
|
178
|
+
}
|
|
179
|
+
function buildFieldCapabilityMap(fields) {
|
|
180
|
+
const map = /* @__PURE__ */ new Map();
|
|
181
|
+
if (!Array.isArray(fields)) return map;
|
|
182
|
+
for (const field of fields) {
|
|
183
|
+
if (field && typeof field.name === "string" && field.name.trim()) {
|
|
184
|
+
map.set(field.name.trim(), field);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return map;
|
|
188
|
+
}
|
|
189
|
+
function resolveAggregatableField(fieldName, fieldMap) {
|
|
190
|
+
var _a;
|
|
191
|
+
const clean = fieldName.trim();
|
|
192
|
+
const meta = fieldMap.get(clean);
|
|
193
|
+
if (!meta) return clean;
|
|
194
|
+
if (meta.aggregatable) return clean;
|
|
195
|
+
if (meta.keywordSubfield) return meta.keywordSubfield;
|
|
196
|
+
const candidate = `${clean}.keyword`;
|
|
197
|
+
if ((_a = fieldMap.get(candidate)) == null ? void 0 : _a.aggregatable) return candidate;
|
|
198
|
+
return clean;
|
|
199
|
+
}
|
|
200
|
+
function resolveUnmappedSortType(fieldName, fieldMap) {
|
|
201
|
+
const meta = fieldMap.get(fieldName);
|
|
202
|
+
if (!meta) return void 0;
|
|
203
|
+
switch (meta.type) {
|
|
204
|
+
case "date":
|
|
205
|
+
case "date_nanos":
|
|
206
|
+
return "date";
|
|
207
|
+
case "long":
|
|
208
|
+
case "integer":
|
|
209
|
+
case "short":
|
|
210
|
+
case "byte":
|
|
211
|
+
return "long";
|
|
212
|
+
case "double":
|
|
213
|
+
case "float":
|
|
214
|
+
case "half_float":
|
|
215
|
+
case "scaled_float":
|
|
216
|
+
return "double";
|
|
217
|
+
case "boolean":
|
|
218
|
+
return "boolean";
|
|
219
|
+
case "keyword":
|
|
220
|
+
case "constant_keyword":
|
|
221
|
+
case "wildcard":
|
|
222
|
+
return "keyword";
|
|
223
|
+
default:
|
|
224
|
+
return void 0;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function normalizeReportFilters(raw) {
|
|
228
|
+
if (!Array.isArray(raw)) return [];
|
|
229
|
+
const items = [];
|
|
230
|
+
for (const entry of raw) {
|
|
231
|
+
if (!isRecord(entry)) continue;
|
|
232
|
+
const field = typeof entry.field === "string" ? entry.field.trim() : "";
|
|
233
|
+
const operator = typeof entry.operator === "string" ? entry.operator : "eq";
|
|
234
|
+
if (!field || !VALID_OPERATORS.has(operator)) continue;
|
|
235
|
+
let value;
|
|
236
|
+
if (Array.isArray(entry.value)) {
|
|
237
|
+
const arr = entry.value.map(toScalarValue).filter((v) => v !== void 0);
|
|
238
|
+
value = arr;
|
|
239
|
+
} else if (typeof entry.value === "string" && (operator === "in" || operator === "not_in")) {
|
|
240
|
+
value = entry.value.split(",").map((part) => part.trim()).filter(Boolean);
|
|
241
|
+
} else {
|
|
242
|
+
value = toScalarValue(entry.value);
|
|
243
|
+
}
|
|
244
|
+
const valueTo = toScalarValue(entry.valueTo);
|
|
245
|
+
items.push({
|
|
246
|
+
...typeof entry.id === "string" && entry.id ? { id: entry.id } : {},
|
|
247
|
+
field,
|
|
248
|
+
operator,
|
|
249
|
+
...value !== void 0 ? { value } : {},
|
|
250
|
+
...valueTo !== void 0 ? { valueTo } : {},
|
|
251
|
+
enabled: entry.enabled !== false
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
return items;
|
|
255
|
+
}
|
|
256
|
+
function normalizeReportSort(raw) {
|
|
257
|
+
if (!Array.isArray(raw)) return [];
|
|
258
|
+
const items = [];
|
|
259
|
+
for (const entry of raw) {
|
|
260
|
+
if (typeof entry === "string" && entry.trim()) {
|
|
261
|
+
const trimmed = entry.trim();
|
|
262
|
+
if (trimmed.startsWith("-")) {
|
|
263
|
+
items.push({ field: trimmed.slice(1), order: "desc" });
|
|
264
|
+
} else {
|
|
265
|
+
items.push({ field: trimmed, order: "asc" });
|
|
266
|
+
}
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (!isRecord(entry)) continue;
|
|
270
|
+
const field = typeof entry.field === "string" ? entry.field.trim() : "";
|
|
271
|
+
if (!field) continue;
|
|
272
|
+
const order = entry.order === "asc" ? "asc" : "desc";
|
|
273
|
+
items.push({ field, order });
|
|
274
|
+
}
|
|
275
|
+
return items;
|
|
276
|
+
}
|
|
277
|
+
function normalizeTimeRange(raw) {
|
|
278
|
+
if (!isRecord(raw)) return void 0;
|
|
279
|
+
const preset = typeof raw.preset === "string" && VALID_PRESETS.has(raw.preset) ? raw.preset : "last_24h";
|
|
280
|
+
const from = typeof raw.from === "string" && raw.from.trim() ? raw.from.trim() : void 0;
|
|
281
|
+
const to = typeof raw.to === "string" && raw.to.trim() ? raw.to.trim() : void 0;
|
|
282
|
+
const timeZone = sanitizeTimeZone(raw.timeZone);
|
|
283
|
+
return {
|
|
284
|
+
preset,
|
|
285
|
+
...from ? { from } : {},
|
|
286
|
+
...to ? { to } : {},
|
|
287
|
+
...timeZone ? { timeZone } : {}
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
function normalizeFieldFormats(raw) {
|
|
291
|
+
if (!isRecord(raw)) return void 0;
|
|
292
|
+
const out = {};
|
|
293
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
294
|
+
const fieldName = k.trim();
|
|
295
|
+
if (fieldName && typeof v === "string" && VALID_FIELD_FORMATS.has(v) && v !== "auto") {
|
|
296
|
+
out[fieldName] = v;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
300
|
+
}
|
|
301
|
+
function normalizeContextBindings(raw) {
|
|
302
|
+
if (!Array.isArray(raw)) return void 0;
|
|
303
|
+
const out = [];
|
|
304
|
+
for (const item of raw) {
|
|
305
|
+
if (!isRecord(item)) continue;
|
|
306
|
+
const esField = typeof item.esField === "string" ? item.esField.trim() : "";
|
|
307
|
+
const recordFieldPath = typeof item.recordFieldPath === "string" ? item.recordFieldPath.trim() : "";
|
|
308
|
+
if (esField && recordFieldPath) {
|
|
309
|
+
out.push({ esField, recordFieldPath });
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return out.length > 0 ? out : void 0;
|
|
313
|
+
}
|
|
314
|
+
function resolveTokenScalar(val, userCtx) {
|
|
315
|
+
var _a, _b, _c;
|
|
316
|
+
if (typeof val !== "string") return val;
|
|
317
|
+
const trimmed = val.trim();
|
|
318
|
+
const tokenMatch = trimmed.match(/^\{\{\s*\$(user\.(?:id|username|email)|role)\s*\}\}$/);
|
|
319
|
+
if (!tokenMatch) return val;
|
|
320
|
+
const tokenName = tokenMatch[1];
|
|
321
|
+
let resolved;
|
|
322
|
+
if (tokenName === "user.id" && userCtx.userId !== void 0 && userCtx.userId !== null && String(userCtx.userId) !== "") {
|
|
323
|
+
resolved = userCtx.userId;
|
|
324
|
+
} else if (tokenName === "user.username" && ((_a = userCtx.username) == null ? void 0 : _a.trim())) {
|
|
325
|
+
resolved = userCtx.username.trim();
|
|
326
|
+
} else if (tokenName === "user.email" && ((_b = userCtx.email) == null ? void 0 : _b.trim())) {
|
|
327
|
+
resolved = userCtx.email.trim();
|
|
328
|
+
} else if (tokenName === "role" && ((_c = userCtx.role) == null ? void 0 : _c.trim())) {
|
|
329
|
+
resolved = userCtx.role.trim();
|
|
330
|
+
}
|
|
331
|
+
if (resolved === void 0) {
|
|
332
|
+
const err = new Error(
|
|
333
|
+
`Permission denied: missing authenticated value for locked filter token "$${tokenName}"`
|
|
334
|
+
);
|
|
335
|
+
err.status = 403;
|
|
336
|
+
throw err;
|
|
337
|
+
}
|
|
338
|
+
return resolved;
|
|
339
|
+
}
|
|
340
|
+
function resolveServerLockedFilters(lockedFilters, userCtx) {
|
|
341
|
+
if (!lockedFilters || lockedFilters.length === 0) return [];
|
|
342
|
+
const ctx = userCtx ?? {};
|
|
343
|
+
return lockedFilters.map((item) => {
|
|
344
|
+
const nextVal = Array.isArray(item.value) ? item.value.map((v) => resolveTokenScalar(v, ctx)) : item.value !== void 0 ? resolveTokenScalar(item.value, ctx) : void 0;
|
|
345
|
+
const nextValTo = item.valueTo !== void 0 ? resolveTokenScalar(item.valueTo, ctx) : void 0;
|
|
346
|
+
return {
|
|
347
|
+
...item,
|
|
348
|
+
...nextVal !== void 0 ? { value: nextVal } : {},
|
|
349
|
+
...nextValTo !== void 0 ? { valueTo: nextValTo } : {},
|
|
350
|
+
enabled: true
|
|
351
|
+
};
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
function normalizeReportSpec(raw, fallbackVisualization) {
|
|
355
|
+
const obj = isRecord(raw) ? raw : {};
|
|
356
|
+
const visCandidate = typeof obj.visualization === "string" ? obj.visualization : fallbackVisualization;
|
|
357
|
+
const visualization = typeof visCandidate === "string" && VALID_VISUALIZATIONS.has(visCandidate) ? visCandidate : "table";
|
|
358
|
+
const query = typeof obj.query === "string" ? obj.query.trim() : "";
|
|
359
|
+
const queryMode = obj.queryMode === "simple" ? "simple" : "kql";
|
|
360
|
+
const filters = normalizeReportFilters(obj.filters);
|
|
361
|
+
const lockedFilters = normalizeReportFilters(obj.lockedFilters).map((item) => ({
|
|
362
|
+
...item,
|
|
363
|
+
enabled: true
|
|
364
|
+
}));
|
|
365
|
+
const sort = normalizeReportSort(obj.sort);
|
|
366
|
+
const timeRange = normalizeTimeRange(obj.timeRange);
|
|
367
|
+
const columns = Array.isArray(obj.columns) ? obj.columns.filter((col) => typeof col === "string" && col.trim().length > 0).map((col) => col.trim()) : [];
|
|
368
|
+
const fieldFormats = normalizeFieldFormats(obj.fieldFormats);
|
|
369
|
+
const contextBindings = normalizeContextBindings(obj.contextBindings);
|
|
370
|
+
const pageSize = clamp(typeof obj.pageSize === "number" ? obj.pageSize : import_constants.DEFAULT_PAGE_SIZE, 1, import_constants.MAX_UI_PAGE_SIZE);
|
|
371
|
+
const rawChart = isRecord(obj.chart) ? obj.chart : {};
|
|
372
|
+
const metricType = typeof rawChart.metricType === "string" && VALID_METRICS.has(rawChart.metricType) ? rawChart.metricType : "count";
|
|
373
|
+
const bucketType = typeof rawChart.bucketType === "string" && VALID_BUCKETS.has(rawChart.bucketType) ? rawChart.bucketType : void 0;
|
|
374
|
+
const calendarInterval = typeof rawChart.calendarInterval === "string" && VALID_INTERVALS.has(rawChart.calendarInterval) ? rawChart.calendarInterval : "auto";
|
|
375
|
+
const stackMode = typeof rawChart.stackMode === "string" && VALID_STACK_MODES.has(rawChart.stackMode) ? rawChart.stackMode : "none";
|
|
376
|
+
return {
|
|
377
|
+
query,
|
|
378
|
+
queryMode,
|
|
379
|
+
filters,
|
|
380
|
+
lockedFilters,
|
|
381
|
+
sort,
|
|
382
|
+
...timeRange ? { timeRange } : {},
|
|
383
|
+
columns,
|
|
384
|
+
...fieldFormats ? { fieldFormats } : {},
|
|
385
|
+
...contextBindings ? { contextBindings } : {},
|
|
386
|
+
pageSize,
|
|
387
|
+
visualization,
|
|
388
|
+
chart: {
|
|
389
|
+
...bucketType ? { bucketType } : {},
|
|
390
|
+
...typeof rawChart.bucketField === "string" && rawChart.bucketField.trim() ? { bucketField: rawChart.bucketField.trim() } : {},
|
|
391
|
+
calendarInterval,
|
|
392
|
+
topN: clamp(typeof rawChart.topN === "number" ? rawChart.topN : 10, 1, import_constants.MAX_TERMS_BUCKETS),
|
|
393
|
+
metricType,
|
|
394
|
+
...typeof rawChart.metricField === "string" && rawChart.metricField.trim() ? { metricField: rawChart.metricField.trim() } : {},
|
|
395
|
+
...typeof rawChart.metricLabel === "string" && rawChart.metricLabel.trim() ? { metricLabel: rawChart.metricLabel.trim() } : {},
|
|
396
|
+
showHistogram: rawChart.showHistogram !== false,
|
|
397
|
+
...typeof rawChart.splitSeriesField === "string" && rawChart.splitSeriesField.trim() ? { splitSeriesField: rawChart.splitSeriesField.trim() } : {},
|
|
398
|
+
...typeof rawChart.splitSeriesTopN === "number" ? { splitSeriesTopN: clamp(rawChart.splitSeriesTopN, 1, import_constants.MAX_SPLIT_SERIES) } : {},
|
|
399
|
+
...stackMode !== "none" ? { stackMode } : {},
|
|
400
|
+
...rawChart.comparePreviousPeriod === true ? { comparePreviousPeriod: true } : {}
|
|
401
|
+
},
|
|
402
|
+
allowInteractiveControls: obj.allowInteractiveControls !== false,
|
|
403
|
+
allowExport: obj.allowExport !== false
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
function assertBalancedKqlDelimiters(input) {
|
|
407
|
+
let inQuotes = false;
|
|
408
|
+
let quoteChar = "";
|
|
409
|
+
const openParenPositions = [];
|
|
410
|
+
for (let i = 0; i < input.length; i++) {
|
|
411
|
+
const ch = input[i];
|
|
412
|
+
if (inQuotes) {
|
|
413
|
+
if (ch === "\\") {
|
|
414
|
+
i++;
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
if (ch === quoteChar) {
|
|
418
|
+
inQuotes = false;
|
|
419
|
+
}
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
if (ch === '"' || ch === "'") {
|
|
423
|
+
inQuotes = true;
|
|
424
|
+
quoteChar = ch;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (ch === "(") {
|
|
428
|
+
openParenPositions.push(i);
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
if (ch === ")") {
|
|
432
|
+
const openIdx = openParenPositions.pop();
|
|
433
|
+
if (openIdx === void 0) {
|
|
434
|
+
throw new KqlSyntaxError('Invalid KQL syntax: unexpected closing parenthesis ")"');
|
|
435
|
+
}
|
|
436
|
+
if (!input.slice(openIdx + 1, i).trim()) {
|
|
437
|
+
throw new KqlSyntaxError('Invalid KQL syntax: empty parentheses "()"');
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (inQuotes) {
|
|
442
|
+
throw new KqlSyntaxError("Invalid KQL syntax: unclosed quotation mark");
|
|
443
|
+
}
|
|
444
|
+
if (openParenPositions.length > 0) {
|
|
445
|
+
throw new KqlSyntaxError('Invalid KQL syntax: unclosed parenthesis "("');
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function splitTopLevelBoolean(input, keyword) {
|
|
449
|
+
const parts = [];
|
|
450
|
+
let depth = 0;
|
|
451
|
+
let inQuotes = false;
|
|
452
|
+
let quoteChar = "";
|
|
453
|
+
let lastIdx = 0;
|
|
454
|
+
let splitFound = false;
|
|
455
|
+
for (let i = 0; i < input.length; i++) {
|
|
456
|
+
const ch = input[i];
|
|
457
|
+
if (inQuotes) {
|
|
458
|
+
if (ch === "\\") {
|
|
459
|
+
i++;
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
if (ch === quoteChar) {
|
|
463
|
+
inQuotes = false;
|
|
464
|
+
}
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (ch === '"' || ch === "'") {
|
|
468
|
+
inQuotes = true;
|
|
469
|
+
quoteChar = ch;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (ch === "(") {
|
|
473
|
+
depth++;
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
if (ch === ")") {
|
|
477
|
+
depth = Math.max(0, depth - 1);
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
if (depth === 0) {
|
|
481
|
+
const prevBoundary = i === 0 || /[\s)]/.test(input[i - 1]);
|
|
482
|
+
const slice = input.slice(i, i + keyword.length);
|
|
483
|
+
const nextChar = input[i + keyword.length];
|
|
484
|
+
const nextBoundary = nextChar === void 0 || /[\s(]/.test(nextChar);
|
|
485
|
+
if (prevBoundary && nextBoundary && slice.toUpperCase() === keyword) {
|
|
486
|
+
splitFound = true;
|
|
487
|
+
parts.push(input.slice(lastIdx, i).trim());
|
|
488
|
+
i += keyword.length - 1;
|
|
489
|
+
lastIdx = i + 1;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
parts.push(input.slice(lastIdx).trim());
|
|
494
|
+
if (splitFound && parts.some((part) => !part)) {
|
|
495
|
+
throw new KqlSyntaxError(`Invalid KQL syntax: missing operand around "${keyword}"`);
|
|
496
|
+
}
|
|
497
|
+
return parts.filter(Boolean);
|
|
498
|
+
}
|
|
499
|
+
function hasBalancedOuterParens(expr) {
|
|
500
|
+
if (!expr.startsWith("(") || !expr.endsWith(")")) return false;
|
|
501
|
+
let depth = 0;
|
|
502
|
+
let inQuotes = false;
|
|
503
|
+
let quoteChar = "";
|
|
504
|
+
for (let i = 0; i < expr.length; i++) {
|
|
505
|
+
const ch = expr[i];
|
|
506
|
+
if (inQuotes) {
|
|
507
|
+
if (ch === "\\") {
|
|
508
|
+
i++;
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
if (ch === quoteChar) inQuotes = false;
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
if (ch === '"' || ch === "'") {
|
|
515
|
+
inQuotes = true;
|
|
516
|
+
quoteChar = ch;
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
if (ch === "(") depth++;
|
|
520
|
+
if (ch === ")") {
|
|
521
|
+
depth--;
|
|
522
|
+
if (depth === 0 && i < expr.length - 1) return false;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return depth === 0;
|
|
526
|
+
}
|
|
527
|
+
function unquote(raw) {
|
|
528
|
+
const trimmed = raw.trim();
|
|
529
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"') && trimmed.length >= 2 || trimmed.startsWith("'") && trimmed.endsWith("'") && trimmed.length >= 2) {
|
|
530
|
+
return {
|
|
531
|
+
value: trimmed.slice(1, -1).replace(/\\(["'\\])/g, "$1"),
|
|
532
|
+
wasQuoted: true
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
return { value: trimmed, wasQuoted: false };
|
|
536
|
+
}
|
|
537
|
+
function compileSingleFieldValue(rawField, rawVal, fieldMap) {
|
|
538
|
+
const trimmed = rawVal.trim();
|
|
539
|
+
if (!trimmed) {
|
|
540
|
+
throw new KqlSyntaxError(`Invalid KQL syntax: missing value for field "${rawField}"`);
|
|
541
|
+
}
|
|
542
|
+
const inlineRangeMatch = trimmed.match(/^(>=|<=|>|<)\s*(.+)$/);
|
|
543
|
+
if (inlineRangeMatch) {
|
|
544
|
+
const [, op, rangeRhs] = inlineRangeMatch;
|
|
545
|
+
const { value: value2 } = unquote(rangeRhs);
|
|
546
|
+
if (!value2) {
|
|
547
|
+
throw new KqlSyntaxError(`Invalid KQL syntax: missing range bound for field "${rawField}"`);
|
|
548
|
+
}
|
|
549
|
+
const coerced2 = coerceLiteral(value2, fieldMap.get(rawField));
|
|
550
|
+
const opKey = op === ">=" ? "gte" : op === "<=" ? "lte" : op === ">" ? "gt" : "lt";
|
|
551
|
+
return { range: { [rawField]: { [opKey]: coerced2 } } };
|
|
552
|
+
}
|
|
553
|
+
const { value, wasQuoted } = unquote(trimmed);
|
|
554
|
+
if (!wasQuoted) {
|
|
555
|
+
if (/^[[{].*[\]}]$/.test(value)) {
|
|
556
|
+
throw new KqlSyntaxError("Lucene range brackets [a TO b] are not valid KQL; use >= and <= instead");
|
|
557
|
+
}
|
|
558
|
+
if (/^\/.*\/$/.test(value)) {
|
|
559
|
+
throw new KqlSyntaxError("Regular expressions /.../ are not valid KQL; use * or ? wildcards instead");
|
|
560
|
+
}
|
|
561
|
+
if (value.includes("(") || value.includes(")")) {
|
|
562
|
+
throw new KqlSyntaxError(`Unsupported KQL field value syntax near "${value}"`);
|
|
563
|
+
}
|
|
564
|
+
if (value.includes(":")) {
|
|
565
|
+
throw new KqlSyntaxError(`Unexpected ":" in unquoted KQL value "${value}"; wrap values containing ":" in quotes`);
|
|
566
|
+
}
|
|
567
|
+
if (value === "*") {
|
|
568
|
+
return { exists: { field: rawField } };
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
const fieldMeta = fieldMap.get(rawField);
|
|
572
|
+
if (!wasQuoted && (value.includes("*") || value.includes("?"))) {
|
|
573
|
+
const wildcardField = resolveAggregatableField(rawField, fieldMap);
|
|
574
|
+
return {
|
|
575
|
+
wildcard: {
|
|
576
|
+
[wildcardField]: {
|
|
577
|
+
value,
|
|
578
|
+
case_insensitive: true
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
if (wasQuoted) {
|
|
584
|
+
if ((fieldMeta == null ? void 0 : fieldMeta.type) === "keyword" || (fieldMeta == null ? void 0 : fieldMeta.type) === "constant_keyword") {
|
|
585
|
+
return { term: { [rawField]: value } };
|
|
586
|
+
}
|
|
587
|
+
return { match_phrase: { [rawField]: value } };
|
|
588
|
+
}
|
|
589
|
+
const coerced = coerceLiteral(value, fieldMeta);
|
|
590
|
+
if (typeof coerced === "number" || typeof coerced === "boolean") {
|
|
591
|
+
return { term: { [rawField]: coerced } };
|
|
592
|
+
}
|
|
593
|
+
if ((fieldMeta == null ? void 0 : fieldMeta.type) === "keyword" || (fieldMeta == null ? void 0 : fieldMeta.type) === "constant_keyword") {
|
|
594
|
+
return { term: { [rawField]: coerced } };
|
|
595
|
+
}
|
|
596
|
+
return { match_phrase: { [rawField]: String(coerced) } };
|
|
597
|
+
}
|
|
598
|
+
function compileFieldGroupClause(fieldName, groupBody, fieldMap) {
|
|
599
|
+
const expr = groupBody.trim();
|
|
600
|
+
if (!expr) {
|
|
601
|
+
throw new KqlSyntaxError(`Invalid KQL syntax: empty value group for field "${fieldName}"`);
|
|
602
|
+
}
|
|
603
|
+
if (/^NOT$/i.test(expr)) {
|
|
604
|
+
throw new KqlSyntaxError(`Invalid KQL syntax: incomplete "NOT" inside "${fieldName}: (...)"`);
|
|
605
|
+
}
|
|
606
|
+
if (hasBalancedOuterParens(expr)) {
|
|
607
|
+
return compileFieldGroupClause(fieldName, expr.slice(1, -1), fieldMap);
|
|
608
|
+
}
|
|
609
|
+
const orParts = splitTopLevelBoolean(expr, "OR");
|
|
610
|
+
if (orParts.length > 1) {
|
|
611
|
+
const should = orParts.map((part) => compileFieldGroupClause(fieldName, part, fieldMap));
|
|
612
|
+
return should.length === 1 ? should[0] : { bool: { should, minimum_should_match: 1 } };
|
|
613
|
+
}
|
|
614
|
+
const andParts = splitTopLevelBoolean(expr, "AND");
|
|
615
|
+
if (andParts.length > 1) {
|
|
616
|
+
const must = andParts.map((part) => compileFieldGroupClause(fieldName, part, fieldMap));
|
|
617
|
+
return must.length === 1 ? must[0] : { bool: { must } };
|
|
618
|
+
}
|
|
619
|
+
if (/^NOT\s+/i.test(expr)) {
|
|
620
|
+
const inner = compileFieldGroupClause(fieldName, expr.replace(/^NOT\s+/i, ""), fieldMap);
|
|
621
|
+
return { bool: { must_not: [inner] } };
|
|
622
|
+
}
|
|
623
|
+
return compileSingleFieldValue(fieldName, expr, fieldMap);
|
|
624
|
+
}
|
|
625
|
+
function compileKqlAst(exprInput, fieldMap) {
|
|
626
|
+
const expr = exprInput.trim();
|
|
627
|
+
if (!expr) return null;
|
|
628
|
+
if (/^NOT$/i.test(expr)) {
|
|
629
|
+
throw new KqlSyntaxError('Invalid KQL syntax: incomplete "NOT" operator');
|
|
630
|
+
}
|
|
631
|
+
if (hasBalancedOuterParens(expr)) {
|
|
632
|
+
return compileKqlAst(expr.slice(1, -1), fieldMap);
|
|
633
|
+
}
|
|
634
|
+
const orParts = splitTopLevelBoolean(expr, "OR");
|
|
635
|
+
if (orParts.length > 1) {
|
|
636
|
+
const should = orParts.map((part) => compileKqlAst(part, fieldMap)).filter((item) => item !== null);
|
|
637
|
+
if (should.length === 0) return null;
|
|
638
|
+
if (should.length === 1) return should[0];
|
|
639
|
+
return { bool: { should, minimum_should_match: 1 } };
|
|
640
|
+
}
|
|
641
|
+
const andParts = splitTopLevelBoolean(expr, "AND");
|
|
642
|
+
if (andParts.length > 1) {
|
|
643
|
+
const must = andParts.map((part) => compileKqlAst(part, fieldMap)).filter((item) => item !== null);
|
|
644
|
+
if (must.length === 0) return null;
|
|
645
|
+
if (must.length === 1) return must[0];
|
|
646
|
+
return { bool: { must } };
|
|
647
|
+
}
|
|
648
|
+
if (/^NOT\s+/i.test(expr)) {
|
|
649
|
+
const rest = expr.replace(/^NOT\s+/i, "").trim();
|
|
650
|
+
if (!rest) {
|
|
651
|
+
throw new KqlSyntaxError('Invalid KQL syntax: incomplete "NOT" operator');
|
|
652
|
+
}
|
|
653
|
+
const inner = compileKqlAst(rest, fieldMap);
|
|
654
|
+
return inner ? { bool: { must_not: [inner] } } : null;
|
|
655
|
+
}
|
|
656
|
+
if (/^[A-Za-z0-9_@.-]+\s*(:|>=|<=|>|<)\s*$/.test(expr)) {
|
|
657
|
+
throw new KqlSyntaxError("Invalid KQL syntax: missing value after field operator");
|
|
658
|
+
}
|
|
659
|
+
const rangeMatch = expr.match(/^([A-Za-z0-9_@.-]+)\s*(>=|<=|>|<)\s*(.+)$/);
|
|
660
|
+
if (rangeMatch) {
|
|
661
|
+
const [, rawField, op, rawVal] = rangeMatch;
|
|
662
|
+
return compileSingleFieldValue(rawField, `${op} ${rawVal}`, fieldMap);
|
|
663
|
+
}
|
|
664
|
+
const colonMatch = expr.match(/^([A-Za-z0-9_@.-]+)\s*:\s*(.+)$/);
|
|
665
|
+
if (colonMatch) {
|
|
666
|
+
const [, rawField, rawVal] = colonMatch;
|
|
667
|
+
const rhs = rawVal.trim();
|
|
668
|
+
if (hasBalancedOuterParens(rhs)) {
|
|
669
|
+
return compileFieldGroupClause(rawField, rhs.slice(1, -1), fieldMap);
|
|
670
|
+
}
|
|
671
|
+
return compileSingleFieldValue(rawField, rhs, fieldMap);
|
|
672
|
+
}
|
|
673
|
+
if (!unquote(expr).wasQuoted && (/^[[{].*[\]}]$/.test(expr) || /^\/.*\/$/.test(expr))) {
|
|
674
|
+
throw new KqlSyntaxError(`Unsupported KQL expression "${expr}"`);
|
|
675
|
+
}
|
|
676
|
+
return {
|
|
677
|
+
simple_query_string: {
|
|
678
|
+
query: expr,
|
|
679
|
+
default_operator: "and",
|
|
680
|
+
lenient: true
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
function compileKqlToQueryDsl(rawQuery, fieldMap = /* @__PURE__ */ new Map()) {
|
|
685
|
+
const expr = rawQuery.trim();
|
|
686
|
+
if (!expr) return null;
|
|
687
|
+
assertBalancedKqlDelimiters(expr);
|
|
688
|
+
return compileKqlAst(expr, fieldMap);
|
|
689
|
+
}
|
|
690
|
+
function compileFilterItem(item, fieldMap) {
|
|
691
|
+
if (item.enabled === false || !item.field.trim()) return null;
|
|
692
|
+
const field = item.field.trim();
|
|
693
|
+
const fieldMeta = fieldMap.get(field);
|
|
694
|
+
const keywordField = resolveAggregatableField(field, fieldMap);
|
|
695
|
+
switch (item.operator) {
|
|
696
|
+
case "exists":
|
|
697
|
+
return { clause: { exists: { field } }, negated: false };
|
|
698
|
+
case "not_exists":
|
|
699
|
+
return { clause: { exists: { field } }, negated: true };
|
|
700
|
+
case "eq":
|
|
701
|
+
case "ne": {
|
|
702
|
+
if (item.value === void 0 || Array.isArray(item.value)) return null;
|
|
703
|
+
const val = typeof item.value === "string" ? coerceLiteral(item.value, fieldMeta) : item.value;
|
|
704
|
+
const useTerm = typeof val !== "string" || (fieldMeta == null ? void 0 : fieldMeta.aggregatable) === true || keywordField !== field;
|
|
705
|
+
const clause = useTerm ? { term: { [keywordField]: val } } : { match_phrase: { [field]: val } };
|
|
706
|
+
return { clause, negated: item.operator === "ne" };
|
|
707
|
+
}
|
|
708
|
+
case "in":
|
|
709
|
+
case "not_in": {
|
|
710
|
+
const values = Array.isArray(item.value) ? item.value : item.value !== void 0 ? [item.value] : [];
|
|
711
|
+
if (values.length === 0) return null;
|
|
712
|
+
const normalized = values.map((v) => typeof v === "string" ? coerceLiteral(v, fieldMeta) : v);
|
|
713
|
+
return {
|
|
714
|
+
clause: { terms: { [keywordField]: normalized } },
|
|
715
|
+
negated: item.operator === "not_in"
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
case "gt":
|
|
719
|
+
case "gte":
|
|
720
|
+
case "lt":
|
|
721
|
+
case "lte": {
|
|
722
|
+
if (item.value === void 0 || Array.isArray(item.value)) return null;
|
|
723
|
+
const val = typeof item.value === "string" ? coerceLiteral(item.value, fieldMeta) : item.value;
|
|
724
|
+
return {
|
|
725
|
+
clause: { range: { [field]: { [item.operator]: val } } },
|
|
726
|
+
negated: false
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
case "between": {
|
|
730
|
+
const rangeBody = {};
|
|
731
|
+
if (item.value !== void 0 && !Array.isArray(item.value)) {
|
|
732
|
+
rangeBody.gte = typeof item.value === "string" ? coerceLiteral(item.value, fieldMeta) : item.value;
|
|
733
|
+
}
|
|
734
|
+
if (item.valueTo !== void 0) {
|
|
735
|
+
rangeBody.lte = typeof item.valueTo === "string" ? coerceLiteral(item.valueTo, fieldMeta) : item.valueTo;
|
|
736
|
+
}
|
|
737
|
+
if (Object.keys(rangeBody).length === 0) return null;
|
|
738
|
+
return {
|
|
739
|
+
clause: { range: { [field]: rangeBody } },
|
|
740
|
+
negated: false
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
case "contains": {
|
|
744
|
+
if (item.value === void 0 || Array.isArray(item.value)) return null;
|
|
745
|
+
const rawText = String(item.value).trim();
|
|
746
|
+
if (!rawText) return null;
|
|
747
|
+
if (fieldMeta && !fieldMeta.aggregatable && !fieldMeta.keywordSubfield) {
|
|
748
|
+
return { clause: { match_phrase: { [field]: rawText } }, negated: false };
|
|
749
|
+
}
|
|
750
|
+
const escaped = rawText.replace(/([*?\\])/g, "\\$1");
|
|
751
|
+
return {
|
|
752
|
+
clause: {
|
|
753
|
+
wildcard: {
|
|
754
|
+
[keywordField]: {
|
|
755
|
+
value: `*${escaped}*`,
|
|
756
|
+
case_insensitive: true
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
},
|
|
760
|
+
negated: false
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
default:
|
|
764
|
+
return null;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
function compileTimeRange(timeRange, timeField, fallbackTimeZone) {
|
|
768
|
+
const cleanField = typeof timeField === "string" ? timeField.trim() : "";
|
|
769
|
+
if (!cleanField || !timeRange || timeRange.preset === "all") return null;
|
|
770
|
+
const tz = sanitizeTimeZone(timeRange.timeZone ?? fallbackTimeZone);
|
|
771
|
+
if (timeRange.preset === "custom") {
|
|
772
|
+
const bounds = {};
|
|
773
|
+
if (timeRange.from) bounds.gte = timeRange.from;
|
|
774
|
+
if (timeRange.to) bounds.lte = timeRange.to;
|
|
775
|
+
if (Object.keys(bounds).length === 0) return null;
|
|
776
|
+
if (tz) bounds.time_zone = tz;
|
|
777
|
+
return { range: { [cleanField]: bounds } };
|
|
778
|
+
}
|
|
779
|
+
const expr = PRESET_TO_ES_EXPR[timeRange.preset];
|
|
780
|
+
if (!expr) return null;
|
|
781
|
+
return {
|
|
782
|
+
range: {
|
|
783
|
+
[cleanField]: {
|
|
784
|
+
gte: expr,
|
|
785
|
+
lte: "now",
|
|
786
|
+
...tz ? { time_zone: tz } : {}
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
function compilePreviousTimeRange(timeRange, timeField, fallbackTimeZone) {
|
|
792
|
+
const cleanField = typeof timeField === "string" ? timeField.trim() : "";
|
|
793
|
+
if (!cleanField || !timeRange || timeRange.preset === "all") return null;
|
|
794
|
+
const tz = sanitizeTimeZone(timeRange.timeZone ?? fallbackTimeZone);
|
|
795
|
+
if (timeRange.preset === "custom") {
|
|
796
|
+
const fromMs = timeRange.from ? Date.parse(timeRange.from) : NaN;
|
|
797
|
+
const toMs = timeRange.to ? Date.parse(timeRange.to) : NaN;
|
|
798
|
+
if (!Number.isFinite(fromMs) || !Number.isFinite(toMs) || toMs <= fromMs) {
|
|
799
|
+
return null;
|
|
800
|
+
}
|
|
801
|
+
const duration = toMs - fromMs;
|
|
802
|
+
return {
|
|
803
|
+
range: {
|
|
804
|
+
[cleanField]: {
|
|
805
|
+
gte: new Date(fromMs - duration).toISOString(),
|
|
806
|
+
lt: new Date(fromMs).toISOString(),
|
|
807
|
+
...tz ? { time_zone: tz } : {}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
const prev = PRESET_TO_PREV_ES_EXPR[timeRange.preset];
|
|
813
|
+
if (!prev) return null;
|
|
814
|
+
return {
|
|
815
|
+
range: {
|
|
816
|
+
[cleanField]: {
|
|
817
|
+
gte: prev.gte,
|
|
818
|
+
lt: prev.lt,
|
|
819
|
+
...tz ? { time_zone: tz } : {}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
function buildMetricAggDefinition(metricType, metricField, fieldMap) {
|
|
825
|
+
if (metricType === "count" || !(metricField == null ? void 0 : metricField.trim())) return void 0;
|
|
826
|
+
const targetField = resolveAggregatableField(metricField, fieldMap);
|
|
827
|
+
if (metricType === "p50" || metricType === "p95" || metricType === "p99") {
|
|
828
|
+
const pct = metricType === "p50" ? 50 : metricType === "p95" ? 95 : 99;
|
|
829
|
+
return {
|
|
830
|
+
percentiles: {
|
|
831
|
+
field: targetField,
|
|
832
|
+
percents: [pct]
|
|
833
|
+
}
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
return {
|
|
837
|
+
[metricType]: {
|
|
838
|
+
field: targetField
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
function extractMetricValue(aggNode, metricType, fallbackCount) {
|
|
843
|
+
if (metricType === "count") {
|
|
844
|
+
return fallbackCount;
|
|
845
|
+
}
|
|
846
|
+
if (!isRecord(aggNode)) return null;
|
|
847
|
+
if (metricType === "p50" || metricType === "p95" || metricType === "p99") {
|
|
848
|
+
const values = isRecord(aggNode.values) ? aggNode.values : void 0;
|
|
849
|
+
if (!values) return null;
|
|
850
|
+
const exactKey = metricType === "p50" ? "50.0" : metricType === "p95" ? "95.0" : "99.0";
|
|
851
|
+
const shortKey = metricType === "p50" ? "50" : metricType === "p95" ? "95" : "99";
|
|
852
|
+
const raw = values[exactKey] ?? values[shortKey];
|
|
853
|
+
return typeof raw === "number" && Number.isFinite(raw) ? Number(raw.toFixed(4)) : null;
|
|
854
|
+
}
|
|
855
|
+
return typeof aggNode.value === "number" && Number.isFinite(aggNode.value) ? Number(aggNode.value.toFixed(4)) : null;
|
|
856
|
+
}
|
|
857
|
+
function mergeSpecWithOverrides(spec, overrides) {
|
|
858
|
+
var _a, _b;
|
|
859
|
+
if (!overrides) {
|
|
860
|
+
return {
|
|
861
|
+
effectiveSpec: spec,
|
|
862
|
+
extraRuntimeFilters: [],
|
|
863
|
+
effectiveTimeZone: sanitizeTimeZone((_a = spec.timeRange) == null ? void 0 : _a.timeZone),
|
|
864
|
+
page: 1,
|
|
865
|
+
pageSize: spec.pageSize ?? import_constants.DEFAULT_PAGE_SIZE
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
const effectiveTimeRange = overrides.timeRange !== void 0 ? normalizeTimeRange(overrides.timeRange) : spec.timeRange;
|
|
869
|
+
const effectiveTimeZone = sanitizeTimeZone(
|
|
870
|
+
overrides.timeZone ?? (effectiveTimeRange == null ? void 0 : effectiveTimeRange.timeZone) ?? ((_b = spec.timeRange) == null ? void 0 : _b.timeZone)
|
|
871
|
+
);
|
|
872
|
+
const effectiveSpec = {
|
|
873
|
+
...spec,
|
|
874
|
+
query: overrides.query !== void 0 ? overrides.query.trim() : spec.query,
|
|
875
|
+
filters: overrides.filters !== void 0 ? normalizeReportFilters(overrides.filters) : spec.filters,
|
|
876
|
+
// Security invariant: lockedFilters are server-owned and can never be replaced or cleared by runtime overrides.
|
|
877
|
+
lockedFilters: spec.lockedFilters,
|
|
878
|
+
sort: overrides.sort !== void 0 ? normalizeReportSort(overrides.sort) : spec.sort,
|
|
879
|
+
timeRange: effectiveTimeRange
|
|
880
|
+
};
|
|
881
|
+
const extraRuntimeFilters = [
|
|
882
|
+
...normalizeReportFilters(overrides.sharedFilters),
|
|
883
|
+
...normalizeReportFilters(overrides.contextFilters)
|
|
884
|
+
];
|
|
885
|
+
const page = clamp(typeof overrides.page === "number" ? overrides.page : 1, 1, 1e4);
|
|
886
|
+
const pageSize = clamp(
|
|
887
|
+
typeof overrides.pageSize === "number" ? overrides.pageSize : spec.pageSize ?? import_constants.DEFAULT_PAGE_SIZE,
|
|
888
|
+
1,
|
|
889
|
+
import_constants.MAX_UI_PAGE_SIZE
|
|
890
|
+
);
|
|
891
|
+
const searchAfter = Array.isArray(overrides.searchAfter) && overrides.searchAfter.length > 0 ? overrides.searchAfter : void 0;
|
|
892
|
+
return {
|
|
893
|
+
effectiveSpec,
|
|
894
|
+
extraRuntimeFilters,
|
|
895
|
+
...effectiveTimeZone ? { effectiveTimeZone } : {},
|
|
896
|
+
page,
|
|
897
|
+
pageSize,
|
|
898
|
+
...searchAfter ? { searchAfter } : {}
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
function compileSearchRequest(options) {
|
|
902
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
903
|
+
const fieldMap = buildFieldCapabilityMap(options.fields);
|
|
904
|
+
const { effectiveSpec, extraRuntimeFilters, effectiveTimeZone, page, pageSize, searchAfter } = mergeSpecWithOverrides(
|
|
905
|
+
options.spec,
|
|
906
|
+
options.overrides
|
|
907
|
+
);
|
|
908
|
+
const maxCap = options.maxRowsCap ? clamp(options.maxRowsCap, 1, import_constants.MAX_UI_PAGE_SIZE) : import_constants.MAX_UI_PAGE_SIZE;
|
|
909
|
+
const effectivePageSize = Math.min(pageSize, maxCap);
|
|
910
|
+
const timeField = ((_a = options.timeField) == null ? void 0 : _a.trim()) || null;
|
|
911
|
+
const must = [];
|
|
912
|
+
const filter = [];
|
|
913
|
+
const mustNot = [];
|
|
914
|
+
if (effectiveSpec.query && effectiveSpec.query.trim()) {
|
|
915
|
+
if (effectiveSpec.queryMode === "simple") {
|
|
916
|
+
must.push({
|
|
917
|
+
simple_query_string: {
|
|
918
|
+
query: effectiveSpec.query.trim(),
|
|
919
|
+
default_operator: "and",
|
|
920
|
+
lenient: true
|
|
921
|
+
}
|
|
922
|
+
});
|
|
923
|
+
} else {
|
|
924
|
+
const parsedQuery = compileKqlToQueryDsl(effectiveSpec.query, fieldMap);
|
|
925
|
+
if (parsedQuery) {
|
|
926
|
+
must.push(parsedQuery);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
const resolvedLockedFilters = resolveServerLockedFilters(effectiveSpec.lockedFilters, options.userContext);
|
|
931
|
+
const combinedFilters = [
|
|
932
|
+
...resolvedLockedFilters,
|
|
933
|
+
...extraRuntimeFilters,
|
|
934
|
+
...effectiveSpec.filters ?? []
|
|
935
|
+
];
|
|
936
|
+
for (const item of combinedFilters) {
|
|
937
|
+
const compiled = compileFilterItem(item, fieldMap);
|
|
938
|
+
if (!compiled) continue;
|
|
939
|
+
if (compiled.negated) {
|
|
940
|
+
mustNot.push(compiled.clause);
|
|
941
|
+
} else {
|
|
942
|
+
filter.push(compiled.clause);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
const currentTimeClause = compileTimeRange(effectiveSpec.timeRange, timeField, effectiveTimeZone);
|
|
946
|
+
const previousTimeClause = effectiveSpec.visualization === "metric" && ((_b = effectiveSpec.chart) == null ? void 0 : _b.comparePreviousPeriod) === true ? compilePreviousTimeRange(effectiveSpec.timeRange, timeField, effectiveTimeZone) : null;
|
|
947
|
+
const useDualPeriodMetric = Boolean(currentTimeClause && previousTimeClause);
|
|
948
|
+
if (currentTimeClause && !useDualPeriodMetric) {
|
|
949
|
+
filter.push(currentTimeClause);
|
|
950
|
+
}
|
|
951
|
+
let queryDsl;
|
|
952
|
+
if (must.length === 0 && filter.length === 0 && mustNot.length === 0) {
|
|
953
|
+
queryDsl = { match_all: {} };
|
|
954
|
+
} else {
|
|
955
|
+
queryDsl = {
|
|
956
|
+
bool: {
|
|
957
|
+
...must.length > 0 ? { must } : {},
|
|
958
|
+
...filter.length > 0 ? { filter } : {},
|
|
959
|
+
...mustNot.length > 0 ? { must_not: mustNot } : {}
|
|
960
|
+
}
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
const body = {
|
|
964
|
+
query: queryDsl,
|
|
965
|
+
track_total_hits: effectiveSpec.visualization === "metric" ? true : import_constants.MAX_RESULT_WINDOW
|
|
966
|
+
};
|
|
967
|
+
const requestedSorts = effectiveSpec.sort && effectiveSpec.sort.length > 0 ? effectiveSpec.sort : timeField ? [{ field: timeField, order: "desc" }] : [];
|
|
968
|
+
if (effectiveSpec.visualization === "table") {
|
|
969
|
+
body.size = effectivePageSize;
|
|
970
|
+
if (requestedSorts.length > 0) {
|
|
971
|
+
const sortClauses = requestedSorts.map((sortItem) => {
|
|
972
|
+
const targetField = resolveAggregatableField(sortItem.field, fieldMap);
|
|
973
|
+
const unmappedType = resolveUnmappedSortType(targetField, fieldMap);
|
|
974
|
+
return {
|
|
975
|
+
[targetField]: {
|
|
976
|
+
order: sortItem.order,
|
|
977
|
+
...unmappedType ? { unmapped_type: unmappedType } : {}
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
});
|
|
981
|
+
const hasTieBreaker = requestedSorts.some(
|
|
982
|
+
(s) => s.field === "_id" || s.field === "_shard_doc" || s.field === "_doc"
|
|
983
|
+
);
|
|
984
|
+
const legacyCursorMatchesUserSortOnly = searchAfter !== void 0 && searchAfter.length === requestedSorts.length && !hasTieBreaker;
|
|
985
|
+
if (!hasTieBreaker && !legacyCursorMatchesUserSortOnly) {
|
|
986
|
+
const tieBreakerField = options.usePit ? "_shard_doc" : "_doc";
|
|
987
|
+
sortClauses.push({ [tieBreakerField]: { order: "asc" } });
|
|
988
|
+
}
|
|
989
|
+
body.sort = sortClauses;
|
|
990
|
+
}
|
|
991
|
+
if (searchAfter && body.sort) {
|
|
992
|
+
body.search_after = searchAfter;
|
|
993
|
+
} else {
|
|
994
|
+
const rawFrom = (page - 1) * effectivePageSize;
|
|
995
|
+
body.from = Math.min(rawFrom, Math.max(0, import_constants.MAX_RESULT_WINDOW - effectivePageSize));
|
|
996
|
+
}
|
|
997
|
+
if (effectiveSpec.columns && effectiveSpec.columns.length > 0) {
|
|
998
|
+
const sourceFields = new Set(effectiveSpec.columns);
|
|
999
|
+
if (timeField) sourceFields.add(timeField);
|
|
1000
|
+
body._source = Array.from(sourceFields);
|
|
1001
|
+
}
|
|
1002
|
+
if (effectiveSpec.query && effectiveSpec.query.trim()) {
|
|
1003
|
+
body.highlight = {
|
|
1004
|
+
pre_tags: [import_constants.HIGHLIGHT_PRE_TAG],
|
|
1005
|
+
post_tags: [import_constants.HIGHLIGHT_POST_TAG],
|
|
1006
|
+
fields: { "*": {} },
|
|
1007
|
+
fragment_size: 160,
|
|
1008
|
+
number_of_fragments: 2
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
if (timeField && ((_c = effectiveSpec.chart) == null ? void 0 : _c.showHistogram) !== false) {
|
|
1012
|
+
body.aggs = {
|
|
1013
|
+
es_time_histogram: {
|
|
1014
|
+
auto_date_histogram: {
|
|
1015
|
+
field: timeField,
|
|
1016
|
+
buckets: 24,
|
|
1017
|
+
...effectiveTimeZone ? { time_zone: effectiveTimeZone } : {}
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
} else if (effectiveSpec.visualization === "metric") {
|
|
1023
|
+
body.size = 0;
|
|
1024
|
+
const metricType = ((_d = effectiveSpec.chart) == null ? void 0 : _d.metricType) ?? "count";
|
|
1025
|
+
const metricDef = buildMetricAggDefinition(metricType, (_e = effectiveSpec.chart) == null ? void 0 : _e.metricField, fieldMap);
|
|
1026
|
+
if (useDualPeriodMetric && currentTimeClause && previousTimeClause) {
|
|
1027
|
+
body.aggs = {
|
|
1028
|
+
es_current_window: {
|
|
1029
|
+
filter: currentTimeClause,
|
|
1030
|
+
...metricDef ? { aggs: { es_metric: metricDef } } : {}
|
|
1031
|
+
},
|
|
1032
|
+
es_previous_window: {
|
|
1033
|
+
filter: previousTimeClause,
|
|
1034
|
+
...metricDef ? { aggs: { es_metric: metricDef } } : {}
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
} else if (metricDef) {
|
|
1038
|
+
body.aggs = {
|
|
1039
|
+
es_metric: metricDef
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
} else {
|
|
1043
|
+
body.size = 0;
|
|
1044
|
+
const chartCfg = effectiveSpec.chart ?? {};
|
|
1045
|
+
const defaultBucketType = timeField && (effectiveSpec.visualization === "line" || effectiveSpec.visualization === "area") ? "date_histogram" : chartCfg.bucketType ?? (timeField ? "date_histogram" : "terms");
|
|
1046
|
+
const rawBucketField = ((_f = chartCfg.bucketField) == null ? void 0 : _f.trim()) || (defaultBucketType === "date_histogram" ? timeField : "") || ((_g = Array.from(fieldMap.values()).find((f) => f.aggregatable)) == null ? void 0 : _g.name) || "_index";
|
|
1047
|
+
const bucketField = resolveAggregatableField(rawBucketField, fieldMap);
|
|
1048
|
+
const metricType = chartCfg.metricType ?? "count";
|
|
1049
|
+
const metricDef = buildMetricAggDefinition(metricType, chartCfg.metricField, fieldMap);
|
|
1050
|
+
const canOrderByMetric = Boolean(metricDef) && metricType !== "p50" && metricType !== "p95" && metricType !== "p99";
|
|
1051
|
+
const rawSplitField = (_h = chartCfg.splitSeriesField) == null ? void 0 : _h.trim();
|
|
1052
|
+
const splitField = rawSplitField ? resolveAggregatableField(rawSplitField, fieldMap) : void 0;
|
|
1053
|
+
const splitTopN = clamp(chartCfg.splitSeriesTopN ?? 5, 1, import_constants.MAX_SPLIT_SERIES);
|
|
1054
|
+
const bucketChildAggs = {};
|
|
1055
|
+
if (metricDef) {
|
|
1056
|
+
bucketChildAggs.es_metric = metricDef;
|
|
1057
|
+
}
|
|
1058
|
+
if (splitField) {
|
|
1059
|
+
bucketChildAggs.es_split = {
|
|
1060
|
+
terms: {
|
|
1061
|
+
field: splitField,
|
|
1062
|
+
size: splitTopN,
|
|
1063
|
+
...canOrderByMetric ? { order: { es_metric: "desc" } } : {}
|
|
1064
|
+
},
|
|
1065
|
+
...metricDef ? { aggs: { es_metric: metricDef } } : {}
|
|
1066
|
+
};
|
|
1067
|
+
}
|
|
1068
|
+
const hasChildAggs = Object.keys(bucketChildAggs).length > 0;
|
|
1069
|
+
if (defaultBucketType === "date_histogram") {
|
|
1070
|
+
const interval = chartCfg.calendarInterval ?? "auto";
|
|
1071
|
+
const intervalKey = interval !== "auto" && CALENDAR_UNIT_INTERVALS.has(interval) ? "calendar_interval" : "fixed_interval";
|
|
1072
|
+
const bucketAgg = interval === "auto" ? {
|
|
1073
|
+
auto_date_histogram: {
|
|
1074
|
+
field: bucketField,
|
|
1075
|
+
buckets: 24,
|
|
1076
|
+
...effectiveTimeZone ? { time_zone: effectiveTimeZone } : {}
|
|
1077
|
+
}
|
|
1078
|
+
} : {
|
|
1079
|
+
date_histogram: {
|
|
1080
|
+
field: bucketField,
|
|
1081
|
+
[intervalKey]: interval,
|
|
1082
|
+
min_doc_count: 0,
|
|
1083
|
+
...effectiveTimeZone ? { time_zone: effectiveTimeZone } : {}
|
|
1084
|
+
}
|
|
1085
|
+
};
|
|
1086
|
+
body.aggs = {
|
|
1087
|
+
es_buckets: {
|
|
1088
|
+
...bucketAgg,
|
|
1089
|
+
...hasChildAggs ? { aggs: bucketChildAggs } : {}
|
|
1090
|
+
}
|
|
1091
|
+
};
|
|
1092
|
+
} else {
|
|
1093
|
+
const topN = clamp(chartCfg.topN ?? 10, 1, import_constants.MAX_TERMS_BUCKETS);
|
|
1094
|
+
body.aggs = {
|
|
1095
|
+
es_buckets: {
|
|
1096
|
+
terms: {
|
|
1097
|
+
field: bucketField,
|
|
1098
|
+
size: topN,
|
|
1099
|
+
...canOrderByMetric ? { order: { es_metric: "desc" } } : {}
|
|
1100
|
+
},
|
|
1101
|
+
...hasChildAggs ? { aggs: bucketChildAggs } : {}
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
return { body, effectiveSpec };
|
|
1107
|
+
}
|
|
1108
|
+
function deriveColumns(rows, configuredColumns, timeField) {
|
|
1109
|
+
if (configuredColumns && configuredColumns.length > 0) {
|
|
1110
|
+
return configuredColumns;
|
|
1111
|
+
}
|
|
1112
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1113
|
+
if (timeField && rows.some((r) => timeField in r.source)) {
|
|
1114
|
+
seen.add(timeField);
|
|
1115
|
+
}
|
|
1116
|
+
for (const row of rows) {
|
|
1117
|
+
for (const key of Object.keys(row.source)) {
|
|
1118
|
+
if (seen.size >= 8) break;
|
|
1119
|
+
seen.add(key);
|
|
1120
|
+
}
|
|
1121
|
+
if (seen.size >= 8) break;
|
|
1122
|
+
}
|
|
1123
|
+
return Array.from(seen);
|
|
1124
|
+
}
|
|
1125
|
+
function buildDefaultMetricLabel(spec) {
|
|
1126
|
+
var _a, _b, _c;
|
|
1127
|
+
if ((_a = spec.chart) == null ? void 0 : _a.metricLabel) return spec.chart.metricLabel;
|
|
1128
|
+
const metricType = ((_b = spec.chart) == null ? void 0 : _b.metricType) ?? "count";
|
|
1129
|
+
if (metricType === "count") return "Count";
|
|
1130
|
+
const field = ((_c = spec.chart) == null ? void 0 : _c.metricField) ?? "";
|
|
1131
|
+
return field ? `${metricType.toUpperCase()}(${field})` : metricType.toUpperCase();
|
|
1132
|
+
}
|
|
1133
|
+
function parseSearchResponse(rawResponse, compiledDsl, spec, timeField) {
|
|
1134
|
+
var _a, _b, _c, _d;
|
|
1135
|
+
const res = isRecord(rawResponse) ? rawResponse : {};
|
|
1136
|
+
const tookMs = typeof res.took === "number" ? res.took : 0;
|
|
1137
|
+
const timedOut = res.timed_out === true;
|
|
1138
|
+
const shards = isRecord(res._shards) ? {
|
|
1139
|
+
total: typeof res._shards.total === "number" ? res._shards.total : 0,
|
|
1140
|
+
successful: typeof res._shards.successful === "number" ? res._shards.successful : 0,
|
|
1141
|
+
skipped: typeof res._shards.skipped === "number" ? res._shards.skipped : 0,
|
|
1142
|
+
failed: typeof res._shards.failed === "number" ? res._shards.failed : 0
|
|
1143
|
+
} : void 0;
|
|
1144
|
+
const warnings = [];
|
|
1145
|
+
if (timedOut) {
|
|
1146
|
+
warnings.push("Elasticsearch query timed out; displayed results may be incomplete.");
|
|
1147
|
+
}
|
|
1148
|
+
if (shards && shards.failed > 0) {
|
|
1149
|
+
warnings.push(`Partial results: ${shards.failed} of ${shards.total} Elasticsearch shards failed.`);
|
|
1150
|
+
}
|
|
1151
|
+
const hitsContainer = isRecord(res.hits) ? res.hits : {};
|
|
1152
|
+
const rawTotal = hitsContainer.total;
|
|
1153
|
+
let total = 0;
|
|
1154
|
+
let totalRelation = "eq";
|
|
1155
|
+
if (typeof rawTotal === "number") {
|
|
1156
|
+
total = rawTotal;
|
|
1157
|
+
} else if (isRecord(rawTotal)) {
|
|
1158
|
+
total = typeof rawTotal.value === "number" ? rawTotal.value : 0;
|
|
1159
|
+
totalRelation = rawTotal.relation === "gte" ? "gte" : "eq";
|
|
1160
|
+
}
|
|
1161
|
+
const rawHits = Array.isArray(hitsContainer.hits) ? hitsContainer.hits : [];
|
|
1162
|
+
const rows = rawHits.filter(isRecord).map((hit) => {
|
|
1163
|
+
const source = isRecord(hit._source) ? hit._source : {};
|
|
1164
|
+
let highlights;
|
|
1165
|
+
if (isRecord(hit.highlight)) {
|
|
1166
|
+
highlights = {};
|
|
1167
|
+
for (const [hKey, hVal] of Object.entries(hit.highlight)) {
|
|
1168
|
+
if (Array.isArray(hVal)) {
|
|
1169
|
+
highlights[hKey] = hVal.filter((v) => typeof v === "string");
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
const sortArr = Array.isArray(hit.sort) ? hit.sort.map(
|
|
1174
|
+
(s) => typeof s === "string" || typeof s === "number" || typeof s === "boolean" || s === null ? s : String(s)
|
|
1175
|
+
) : void 0;
|
|
1176
|
+
return {
|
|
1177
|
+
_id: typeof hit._id === "string" ? hit._id : String(hit._id ?? ""),
|
|
1178
|
+
_index: typeof hit._index === "string" ? hit._index : String(hit._index ?? ""),
|
|
1179
|
+
_score: typeof hit._score === "number" ? hit._score : null,
|
|
1180
|
+
...sortArr ? { _sort: sortArr } : {},
|
|
1181
|
+
source,
|
|
1182
|
+
...highlights ? { highlights } : {}
|
|
1183
|
+
};
|
|
1184
|
+
});
|
|
1185
|
+
const lastRow = rows.length > 0 ? rows[rows.length - 1] : void 0;
|
|
1186
|
+
const nextSearchAfter = (lastRow == null ? void 0 : lastRow._sort) && lastRow._sort.length > 0 ? lastRow._sort : void 0;
|
|
1187
|
+
const columns = deriveColumns(rows, spec.columns, timeField);
|
|
1188
|
+
const aggs = isRecord(res.aggregations) ? res.aggregations : {};
|
|
1189
|
+
const histogram = [];
|
|
1190
|
+
if (isRecord(aggs.es_time_histogram) && Array.isArray(aggs.es_time_histogram.buckets)) {
|
|
1191
|
+
for (const bucket of aggs.es_time_histogram.buckets) {
|
|
1192
|
+
if (!isRecord(bucket)) continue;
|
|
1193
|
+
const key = typeof bucket.key_as_string === "string" ? bucket.key_as_string : String(bucket.key ?? "");
|
|
1194
|
+
histogram.push({
|
|
1195
|
+
key,
|
|
1196
|
+
...typeof bucket.key === "number" ? { timestamp: bucket.key } : {},
|
|
1197
|
+
count: typeof bucket.doc_count === "number" ? bucket.doc_count : 0
|
|
1198
|
+
});
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
const metricType = ((_a = spec.chart) == null ? void 0 : _a.metricType) ?? "count";
|
|
1202
|
+
const series = [];
|
|
1203
|
+
const seenSeriesKeys = /* @__PURE__ */ new Set();
|
|
1204
|
+
if (isRecord(aggs.es_buckets) && Array.isArray(aggs.es_buckets.buckets)) {
|
|
1205
|
+
for (const bucket of aggs.es_buckets.buckets) {
|
|
1206
|
+
if (!isRecord(bucket)) continue;
|
|
1207
|
+
const label = typeof bucket.key_as_string === "string" ? bucket.key_as_string : String(bucket.key ?? "");
|
|
1208
|
+
const docCount = typeof bucket.doc_count === "number" ? bucket.doc_count : 0;
|
|
1209
|
+
const extracted = extractMetricValue(bucket.es_metric, metricType, docCount);
|
|
1210
|
+
const metricVal = extracted ?? docCount;
|
|
1211
|
+
let breakdown;
|
|
1212
|
+
if (isRecord(bucket.es_split) && Array.isArray(bucket.es_split.buckets)) {
|
|
1213
|
+
breakdown = [];
|
|
1214
|
+
for (const sub of bucket.es_split.buckets) {
|
|
1215
|
+
if (!isRecord(sub)) continue;
|
|
1216
|
+
const sKey = typeof sub.key_as_string === "string" ? sub.key_as_string : String(sub.key ?? "");
|
|
1217
|
+
const sCount = typeof sub.doc_count === "number" ? sub.doc_count : 0;
|
|
1218
|
+
const sVal = extractMetricValue(sub.es_metric, metricType, sCount) ?? sCount;
|
|
1219
|
+
seenSeriesKeys.add(sKey);
|
|
1220
|
+
breakdown.push({
|
|
1221
|
+
seriesKey: sKey,
|
|
1222
|
+
value: sVal,
|
|
1223
|
+
count: sCount
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
series.push({
|
|
1228
|
+
key: String(bucket.key ?? label),
|
|
1229
|
+
label,
|
|
1230
|
+
value: metricVal,
|
|
1231
|
+
count: docCount,
|
|
1232
|
+
...breakdown ? { breakdown } : {}
|
|
1233
|
+
});
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
let metricValue = null;
|
|
1237
|
+
let previousMetricValue;
|
|
1238
|
+
let deltaPercent;
|
|
1239
|
+
if (spec.visualization === "metric") {
|
|
1240
|
+
if (isRecord(aggs.es_current_window) && isRecord(aggs.es_previous_window)) {
|
|
1241
|
+
const curCount = typeof aggs.es_current_window.doc_count === "number" ? aggs.es_current_window.doc_count : 0;
|
|
1242
|
+
const prevCount = typeof aggs.es_previous_window.doc_count === "number" ? aggs.es_previous_window.doc_count : 0;
|
|
1243
|
+
total = curCount;
|
|
1244
|
+
totalRelation = "eq";
|
|
1245
|
+
metricValue = metricType === "count" || !((_b = spec.chart) == null ? void 0 : _b.metricField) ? curCount : extractMetricValue(aggs.es_current_window.es_metric, metricType, curCount);
|
|
1246
|
+
previousMetricValue = metricType === "count" || !((_c = spec.chart) == null ? void 0 : _c.metricField) ? prevCount : extractMetricValue(aggs.es_previous_window.es_metric, metricType, prevCount);
|
|
1247
|
+
if (metricValue !== null && previousMetricValue !== null && previousMetricValue !== 0) {
|
|
1248
|
+
deltaPercent = Number(((metricValue - previousMetricValue) / Math.abs(previousMetricValue) * 100).toFixed(2));
|
|
1249
|
+
} else {
|
|
1250
|
+
deltaPercent = null;
|
|
1251
|
+
}
|
|
1252
|
+
} else if (metricType === "count" || !((_d = spec.chart) == null ? void 0 : _d.metricField)) {
|
|
1253
|
+
metricValue = total;
|
|
1254
|
+
} else {
|
|
1255
|
+
metricValue = extractMetricValue(aggs.es_metric, metricType, total);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
const responsePitId = typeof res.pit_id === "string" && res.pit_id ? res.pit_id : void 0;
|
|
1259
|
+
return {
|
|
1260
|
+
total,
|
|
1261
|
+
totalRelation,
|
|
1262
|
+
tookMs,
|
|
1263
|
+
...timedOut ? { timedOut: true } : {},
|
|
1264
|
+
...shards ? { shards } : {},
|
|
1265
|
+
...warnings.length > 0 ? { warnings } : {},
|
|
1266
|
+
visualization: spec.visualization,
|
|
1267
|
+
rows,
|
|
1268
|
+
columns,
|
|
1269
|
+
...nextSearchAfter ? { nextSearchAfter } : {},
|
|
1270
|
+
...responsePitId ? { pitId: responsePitId } : {},
|
|
1271
|
+
series,
|
|
1272
|
+
...seenSeriesKeys.size > 0 ? { seriesKeys: Array.from(seenSeriesKeys) } : {},
|
|
1273
|
+
histogram,
|
|
1274
|
+
metricValue,
|
|
1275
|
+
...previousMetricValue !== void 0 ? { previousMetricValue } : {},
|
|
1276
|
+
...deltaPercent !== void 0 ? { deltaPercent } : {},
|
|
1277
|
+
metricLabel: buildDefaultMetricLabel(spec),
|
|
1278
|
+
compiledDsl
|
|
1279
|
+
};
|
|
1280
|
+
}
|
|
1281
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1282
|
+
0 && (module.exports = {
|
|
1283
|
+
KqlSyntaxError,
|
|
1284
|
+
buildFieldCapabilityMap,
|
|
1285
|
+
compileFilterItem,
|
|
1286
|
+
compileKqlToQueryDsl,
|
|
1287
|
+
compilePreviousTimeRange,
|
|
1288
|
+
compileSearchRequest,
|
|
1289
|
+
compileTimeRange,
|
|
1290
|
+
mergeSpecWithOverrides,
|
|
1291
|
+
normalizeReportFilters,
|
|
1292
|
+
normalizeReportSort,
|
|
1293
|
+
normalizeReportSpec,
|
|
1294
|
+
normalizeTimeRange,
|
|
1295
|
+
parseSearchResponse,
|
|
1296
|
+
resolveAggregatableField,
|
|
1297
|
+
resolveServerLockedFilters,
|
|
1298
|
+
sanitizeTimeZone
|
|
1299
|
+
});
|