plugin-elastic-report 0.0.0-stage → 0.1.1
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 +32 -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/index.js +10 -0
- package/dist/client-v2/851.488fdbf5bc5b6a36.js +10 -0
- package/dist/client-v2/index.js +10 -0
- package/dist/externalVersion.js +20 -0
- package/dist/index.js +48 -0
- package/dist/locale/en-US.json +99 -0
- package/dist/locale/vi-VN.json +99 -0
- package/dist/locale/zh-CN.json +99 -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 +107 -0
- package/dist/server/collections/es-report-data-views.js +79 -0
- package/dist/server/collections/es-reports.js +95 -0
- package/dist/server/index.js +58 -0
- package/dist/server/mcp/ElasticReportMcpServer.js +344 -0
- package/dist/server/mcp/ElasticReportToolCatalog.js +417 -0
- package/dist/server/plugin.js +395 -0
- package/dist/server/services/ElasticReportService.js +263 -0
- package/dist/server/services/ElasticsearchConnectionService.js +332 -0
- package/dist/server/services/ReportQueryCompiler.js +858 -0
- package/dist/shared/constants.js +81 -0
- package/dist/shared/types.js +24 -0
- package/package.json +61 -4
- package/server.d.ts +2 -0
- package/server.js +1 -0
- package/src/client/index.tsx +19 -0
- package/src/client-v2/__tests__/elastic-report.test.tsx +311 -0
- package/src/client-v2/api.ts +224 -0
- package/src/client-v2/components/ElasticBasicCharts.tsx +399 -0
- package/src/client-v2/components/ElasticReportBlock.tsx +160 -0
- package/src/client-v2/components/ElasticReportRenderer.tsx +707 -0
- package/src/client-v2/components/HighlightText.tsx +50 -0
- package/src/client-v2/components/ReportSelect.tsx +72 -0
- package/src/client-v2/index.tsx +14 -0
- package/src/client-v2/locale.ts +23 -0
- package/src/client-v2/models/ElasticReportBlockModel.tsx +85 -0
- package/src/client-v2/pages/ElasticReportManager.tsx +1047 -0
- package/src/client-v2/plugin.tsx +52 -0
- package/src/index.ts +11 -0
- package/src/locale/en-US.json +99 -0
- package/src/locale/vi-VN.json +99 -0
- package/src/locale/zh-CN.json +99 -0
- package/src/server/__tests__/query-compiler.test.ts +231 -0
- package/src/server/__tests__/report-mcp-acl.integration.test.ts +377 -0
- package/src/server/acl-guard.ts +103 -0
- package/src/server/collections/es-report-data-views.ts +58 -0
- package/src/server/collections/es-reports.ts +74 -0
- package/src/server/index.ts +15 -0
- package/src/server/mcp/ElasticReportMcpServer.ts +391 -0
- package/src/server/mcp/ElasticReportToolCatalog.ts +417 -0
- package/src/server/plugin.ts +431 -0
- package/src/server/services/ElasticReportService.ts +326 -0
- package/src/server/services/ElasticsearchConnectionService.ts +401 -0
- package/src/server/services/ReportQueryCompiler.ts +1017 -0
- package/src/shared/constants.ts +29 -0
- package/src/shared/types.ts +175 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1017 @@
|
|
|
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
|
+
import {
|
|
11
|
+
DEFAULT_PAGE_SIZE,
|
|
12
|
+
HIGHLIGHT_POST_TAG,
|
|
13
|
+
HIGHLIGHT_PRE_TAG,
|
|
14
|
+
MAX_RESULT_WINDOW,
|
|
15
|
+
MAX_TERMS_BUCKETS,
|
|
16
|
+
MAX_UI_PAGE_SIZE,
|
|
17
|
+
} from '../../shared/constants';
|
|
18
|
+
import type {
|
|
19
|
+
BucketAggType,
|
|
20
|
+
CalendarIntervalOption,
|
|
21
|
+
EsFieldCapability,
|
|
22
|
+
FilterOperator,
|
|
23
|
+
MetricAggType,
|
|
24
|
+
ReportExecutionResult,
|
|
25
|
+
ReportFilterItem,
|
|
26
|
+
ReportHistogramBucket,
|
|
27
|
+
ReportRowHit,
|
|
28
|
+
ReportRuntimeOverrides,
|
|
29
|
+
ReportSeriesPoint,
|
|
30
|
+
ReportSortItem,
|
|
31
|
+
ReportSpec,
|
|
32
|
+
ReportTimeRange,
|
|
33
|
+
ReportVisualizationType,
|
|
34
|
+
ScalarFilterValue,
|
|
35
|
+
TimeRangePreset,
|
|
36
|
+
} from '../../shared/types';
|
|
37
|
+
|
|
38
|
+
const VALID_VISUALIZATIONS: ReadonlySet<ReportVisualizationType> = new Set([
|
|
39
|
+
'table',
|
|
40
|
+
'bar',
|
|
41
|
+
'line',
|
|
42
|
+
'area',
|
|
43
|
+
'pie',
|
|
44
|
+
'metric',
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
const VALID_OPERATORS: ReadonlySet<FilterOperator> = new Set([
|
|
48
|
+
'eq',
|
|
49
|
+
'ne',
|
|
50
|
+
'in',
|
|
51
|
+
'not_in',
|
|
52
|
+
'gt',
|
|
53
|
+
'gte',
|
|
54
|
+
'lt',
|
|
55
|
+
'lte',
|
|
56
|
+
'between',
|
|
57
|
+
'contains',
|
|
58
|
+
'exists',
|
|
59
|
+
'not_exists',
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
const VALID_PRESETS: ReadonlySet<TimeRangePreset> = new Set([
|
|
63
|
+
'last_15m',
|
|
64
|
+
'last_1h',
|
|
65
|
+
'last_24h',
|
|
66
|
+
'last_7d',
|
|
67
|
+
'last_30d',
|
|
68
|
+
'last_90d',
|
|
69
|
+
'all',
|
|
70
|
+
'custom',
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
const VALID_METRICS: ReadonlySet<MetricAggType> = new Set(['count', 'sum', 'avg', 'min', 'max', 'cardinality']);
|
|
74
|
+
const VALID_BUCKETS: ReadonlySet<BucketAggType> = new Set(['date_histogram', 'terms']);
|
|
75
|
+
const VALID_INTERVALS: ReadonlySet<CalendarIntervalOption> = new Set([
|
|
76
|
+
'auto',
|
|
77
|
+
'1m',
|
|
78
|
+
'5m',
|
|
79
|
+
'15m',
|
|
80
|
+
'1h',
|
|
81
|
+
'1d',
|
|
82
|
+
'1w',
|
|
83
|
+
'1M',
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
const PRESET_TO_ES_EXPR: Record<Exclude<TimeRangePreset, 'all' | 'custom'>, string> = {
|
|
87
|
+
last_15m: 'now-15m',
|
|
88
|
+
last_1h: 'now-1h',
|
|
89
|
+
last_24h: 'now-24h',
|
|
90
|
+
last_7d: 'now-7d',
|
|
91
|
+
last_30d: 'now-30d',
|
|
92
|
+
last_90d: 'now-90d',
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
96
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function clamp(num: number, min: number, max: number): number {
|
|
100
|
+
if (!Number.isFinite(num)) return min;
|
|
101
|
+
return Math.max(min, Math.min(max, Math.trunc(num)));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function toScalarValue(raw: unknown): ScalarFilterValue | undefined {
|
|
105
|
+
if (typeof raw === 'string') return raw;
|
|
106
|
+
if (typeof raw === 'number' && Number.isFinite(raw)) return raw;
|
|
107
|
+
if (typeof raw === 'boolean') return raw;
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function coerceLiteral(raw: string, fieldMeta?: EsFieldCapability): ScalarFilterValue {
|
|
112
|
+
const trimmed = raw.trim();
|
|
113
|
+
if (fieldMeta) {
|
|
114
|
+
if (fieldMeta.type === 'boolean') {
|
|
115
|
+
if (trimmed.toLowerCase() === 'true') return true;
|
|
116
|
+
if (trimmed.toLowerCase() === 'false') return false;
|
|
117
|
+
}
|
|
118
|
+
if (
|
|
119
|
+
['long', 'integer', 'short', 'byte', 'double', 'float', 'half_float', 'scaled_float'].includes(fieldMeta.type)
|
|
120
|
+
) {
|
|
121
|
+
const parsed = Number(trimmed);
|
|
122
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (trimmed.toLowerCase() === 'true') return true;
|
|
126
|
+
if (trimmed.toLowerCase() === 'false') return false;
|
|
127
|
+
if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
|
|
128
|
+
const parsed = Number(trimmed);
|
|
129
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
130
|
+
}
|
|
131
|
+
return trimmed;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function buildFieldCapabilityMap(fields?: EsFieldCapability[] | null): Map<string, EsFieldCapability> {
|
|
135
|
+
const map = new Map<string, EsFieldCapability>();
|
|
136
|
+
if (!Array.isArray(fields)) return map;
|
|
137
|
+
for (const field of fields) {
|
|
138
|
+
if (field && typeof field.name === 'string' && field.name.trim()) {
|
|
139
|
+
map.set(field.name.trim(), field);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return map;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function resolveAggregatableField(fieldName: string, fieldMap: Map<string, EsFieldCapability>): string {
|
|
146
|
+
const clean = fieldName.trim();
|
|
147
|
+
const meta = fieldMap.get(clean);
|
|
148
|
+
if (!meta) return clean;
|
|
149
|
+
if (meta.aggregatable) return clean;
|
|
150
|
+
if (meta.keywordSubfield) return meta.keywordSubfield;
|
|
151
|
+
const candidate = `${clean}.keyword`;
|
|
152
|
+
if (fieldMap.get(candidate)?.aggregatable) return candidate;
|
|
153
|
+
return clean;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function resolveUnmappedSortType(fieldName: string, fieldMap: Map<string, EsFieldCapability>): string | undefined {
|
|
157
|
+
const meta = fieldMap.get(fieldName);
|
|
158
|
+
if (!meta) return undefined;
|
|
159
|
+
switch (meta.type) {
|
|
160
|
+
case 'date':
|
|
161
|
+
case 'date_nanos':
|
|
162
|
+
return 'date';
|
|
163
|
+
case 'long':
|
|
164
|
+
case 'integer':
|
|
165
|
+
case 'short':
|
|
166
|
+
case 'byte':
|
|
167
|
+
return 'long';
|
|
168
|
+
case 'double':
|
|
169
|
+
case 'float':
|
|
170
|
+
case 'half_float':
|
|
171
|
+
case 'scaled_float':
|
|
172
|
+
return 'double';
|
|
173
|
+
case 'boolean':
|
|
174
|
+
return 'boolean';
|
|
175
|
+
case 'keyword':
|
|
176
|
+
case 'constant_keyword':
|
|
177
|
+
case 'wildcard':
|
|
178
|
+
return 'keyword';
|
|
179
|
+
default:
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function normalizeReportFilters(raw: unknown): ReportFilterItem[] {
|
|
185
|
+
if (!Array.isArray(raw)) return [];
|
|
186
|
+
const items: ReportFilterItem[] = [];
|
|
187
|
+
for (const entry of raw) {
|
|
188
|
+
if (!isRecord(entry)) continue;
|
|
189
|
+
const field = typeof entry.field === 'string' ? entry.field.trim() : '';
|
|
190
|
+
const operator = typeof entry.operator === 'string' ? (entry.operator as FilterOperator) : 'eq';
|
|
191
|
+
if (!field || !VALID_OPERATORS.has(operator)) continue;
|
|
192
|
+
|
|
193
|
+
let value: ScalarFilterValue | ScalarFilterValue[] | undefined;
|
|
194
|
+
if (Array.isArray(entry.value)) {
|
|
195
|
+
const arr = entry.value.map(toScalarValue).filter((v): v is ScalarFilterValue => v !== undefined);
|
|
196
|
+
value = arr;
|
|
197
|
+
} else if (typeof entry.value === 'string' && (operator === 'in' || operator === 'not_in')) {
|
|
198
|
+
value = entry.value
|
|
199
|
+
.split(',')
|
|
200
|
+
.map((part) => part.trim())
|
|
201
|
+
.filter(Boolean);
|
|
202
|
+
} else {
|
|
203
|
+
value = toScalarValue(entry.value);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const valueTo = toScalarValue(entry.valueTo);
|
|
207
|
+
items.push({
|
|
208
|
+
...(typeof entry.id === 'string' && entry.id ? { id: entry.id } : {}),
|
|
209
|
+
field,
|
|
210
|
+
operator,
|
|
211
|
+
...(value !== undefined ? { value } : {}),
|
|
212
|
+
...(valueTo !== undefined ? { valueTo } : {}),
|
|
213
|
+
enabled: entry.enabled !== false,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return items;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function normalizeReportSort(raw: unknown): ReportSortItem[] {
|
|
220
|
+
if (!Array.isArray(raw)) return [];
|
|
221
|
+
const items: ReportSortItem[] = [];
|
|
222
|
+
for (const entry of raw) {
|
|
223
|
+
if (typeof entry === 'string' && entry.trim()) {
|
|
224
|
+
const trimmed = entry.trim();
|
|
225
|
+
if (trimmed.startsWith('-')) {
|
|
226
|
+
items.push({ field: trimmed.slice(1), order: 'desc' });
|
|
227
|
+
} else {
|
|
228
|
+
items.push({ field: trimmed, order: 'asc' });
|
|
229
|
+
}
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (!isRecord(entry)) continue;
|
|
233
|
+
const field = typeof entry.field === 'string' ? entry.field.trim() : '';
|
|
234
|
+
if (!field) continue;
|
|
235
|
+
const order = entry.order === 'asc' ? 'asc' : 'desc';
|
|
236
|
+
items.push({ field, order });
|
|
237
|
+
}
|
|
238
|
+
return items;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function normalizeTimeRange(raw: unknown): ReportTimeRange | undefined {
|
|
242
|
+
if (!isRecord(raw)) return undefined;
|
|
243
|
+
const preset =
|
|
244
|
+
typeof raw.preset === 'string' && VALID_PRESETS.has(raw.preset as TimeRangePreset)
|
|
245
|
+
? (raw.preset as TimeRangePreset)
|
|
246
|
+
: 'last_24h';
|
|
247
|
+
const from = typeof raw.from === 'string' && raw.from.trim() ? raw.from.trim() : undefined;
|
|
248
|
+
const to = typeof raw.to === 'string' && raw.to.trim() ? raw.to.trim() : undefined;
|
|
249
|
+
return {
|
|
250
|
+
preset,
|
|
251
|
+
...(from ? { from } : {}),
|
|
252
|
+
...(to ? { to } : {}),
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function normalizeReportSpec(raw: unknown, fallbackVisualization?: unknown): ReportSpec {
|
|
257
|
+
const obj = isRecord(raw) ? raw : {};
|
|
258
|
+
const visCandidate = typeof obj.visualization === 'string' ? obj.visualization : fallbackVisualization;
|
|
259
|
+
const visualization: ReportVisualizationType =
|
|
260
|
+
typeof visCandidate === 'string' && VALID_VISUALIZATIONS.has(visCandidate as ReportVisualizationType)
|
|
261
|
+
? (visCandidate as ReportVisualizationType)
|
|
262
|
+
: 'table';
|
|
263
|
+
|
|
264
|
+
const query = typeof obj.query === 'string' ? obj.query.trim() : '';
|
|
265
|
+
const queryMode = obj.queryMode === 'simple' ? 'simple' : 'kql';
|
|
266
|
+
const filters = normalizeReportFilters(obj.filters);
|
|
267
|
+
const sort = normalizeReportSort(obj.sort);
|
|
268
|
+
const timeRange = normalizeTimeRange(obj.timeRange);
|
|
269
|
+
const columns = Array.isArray(obj.columns)
|
|
270
|
+
? obj.columns
|
|
271
|
+
.filter((col): col is string => typeof col === 'string' && col.trim().length > 0)
|
|
272
|
+
.map((col) => col.trim())
|
|
273
|
+
: [];
|
|
274
|
+
const pageSize = clamp(typeof obj.pageSize === 'number' ? obj.pageSize : DEFAULT_PAGE_SIZE, 1, MAX_UI_PAGE_SIZE);
|
|
275
|
+
|
|
276
|
+
const rawChart = isRecord(obj.chart) ? obj.chart : {};
|
|
277
|
+
const metricType: MetricAggType =
|
|
278
|
+
typeof rawChart.metricType === 'string' && VALID_METRICS.has(rawChart.metricType as MetricAggType)
|
|
279
|
+
? (rawChart.metricType as MetricAggType)
|
|
280
|
+
: 'count';
|
|
281
|
+
const bucketType: BucketAggType | undefined =
|
|
282
|
+
typeof rawChart.bucketType === 'string' && VALID_BUCKETS.has(rawChart.bucketType as BucketAggType)
|
|
283
|
+
? (rawChart.bucketType as BucketAggType)
|
|
284
|
+
: undefined;
|
|
285
|
+
const calendarInterval: CalendarIntervalOption =
|
|
286
|
+
typeof rawChart.calendarInterval === 'string' &&
|
|
287
|
+
VALID_INTERVALS.has(rawChart.calendarInterval as CalendarIntervalOption)
|
|
288
|
+
? (rawChart.calendarInterval as CalendarIntervalOption)
|
|
289
|
+
: 'auto';
|
|
290
|
+
|
|
291
|
+
return {
|
|
292
|
+
query,
|
|
293
|
+
queryMode,
|
|
294
|
+
filters,
|
|
295
|
+
sort,
|
|
296
|
+
...(timeRange ? { timeRange } : {}),
|
|
297
|
+
columns,
|
|
298
|
+
pageSize,
|
|
299
|
+
visualization,
|
|
300
|
+
chart: {
|
|
301
|
+
...(bucketType ? { bucketType } : {}),
|
|
302
|
+
...(typeof rawChart.bucketField === 'string' && rawChart.bucketField.trim()
|
|
303
|
+
? { bucketField: rawChart.bucketField.trim() }
|
|
304
|
+
: {}),
|
|
305
|
+
calendarInterval,
|
|
306
|
+
topN: clamp(typeof rawChart.topN === 'number' ? rawChart.topN : 10, 1, MAX_TERMS_BUCKETS),
|
|
307
|
+
metricType,
|
|
308
|
+
...(typeof rawChart.metricField === 'string' && rawChart.metricField.trim()
|
|
309
|
+
? { metricField: rawChart.metricField.trim() }
|
|
310
|
+
: {}),
|
|
311
|
+
...(typeof rawChart.metricLabel === 'string' && rawChart.metricLabel.trim()
|
|
312
|
+
? { metricLabel: rawChart.metricLabel.trim() }
|
|
313
|
+
: {}),
|
|
314
|
+
showHistogram: rawChart.showHistogram !== false,
|
|
315
|
+
},
|
|
316
|
+
allowInteractiveControls: obj.allowInteractiveControls !== false,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Split a KQL expression on top-level boolean connectors (`AND` / `OR`) while
|
|
322
|
+
* respecting quoted strings and nested parentheses.
|
|
323
|
+
*/
|
|
324
|
+
function splitTopLevelBoolean(input: string, keyword: 'AND' | 'OR'): string[] {
|
|
325
|
+
const parts: string[] = [];
|
|
326
|
+
let depth = 0;
|
|
327
|
+
let inQuotes = false;
|
|
328
|
+
let quoteChar = '';
|
|
329
|
+
let lastIdx = 0;
|
|
330
|
+
|
|
331
|
+
for (let i = 0; i < input.length; i++) {
|
|
332
|
+
const ch = input[i];
|
|
333
|
+
if (inQuotes) {
|
|
334
|
+
if (ch === '\\') {
|
|
335
|
+
i++;
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
if (ch === quoteChar) {
|
|
339
|
+
inQuotes = false;
|
|
340
|
+
}
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (ch === '"' || ch === "'") {
|
|
344
|
+
inQuotes = true;
|
|
345
|
+
quoteChar = ch;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
if (ch === '(') {
|
|
349
|
+
depth++;
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
if (ch === ')') {
|
|
353
|
+
depth = Math.max(0, depth - 1);
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (depth === 0) {
|
|
357
|
+
const prevBoundary = i === 0 || /\s/.test(input[i - 1]);
|
|
358
|
+
const slice = input.slice(i, i + keyword.length);
|
|
359
|
+
const nextChar = input[i + keyword.length];
|
|
360
|
+
const nextBoundary = nextChar !== undefined && /\s/.test(nextChar);
|
|
361
|
+
if (prevBoundary && nextBoundary && slice.toUpperCase() === keyword) {
|
|
362
|
+
parts.push(input.slice(lastIdx, i).trim());
|
|
363
|
+
i += keyword.length;
|
|
364
|
+
lastIdx = i;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
parts.push(input.slice(lastIdx).trim());
|
|
369
|
+
return parts.filter(Boolean);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function hasBalancedOuterParens(expr: string): boolean {
|
|
373
|
+
if (!expr.startsWith('(') || !expr.endsWith(')')) return false;
|
|
374
|
+
let depth = 0;
|
|
375
|
+
let inQuotes = false;
|
|
376
|
+
let quoteChar = '';
|
|
377
|
+
for (let i = 0; i < expr.length; i++) {
|
|
378
|
+
const ch = expr[i];
|
|
379
|
+
if (inQuotes) {
|
|
380
|
+
if (ch === '\\') {
|
|
381
|
+
i++;
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (ch === quoteChar) inQuotes = false;
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
if (ch === '"' || ch === "'") {
|
|
388
|
+
inQuotes = true;
|
|
389
|
+
quoteChar = ch;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (ch === '(') depth++;
|
|
393
|
+
if (ch === ')') {
|
|
394
|
+
depth--;
|
|
395
|
+
if (depth === 0 && i < expr.length - 1) return false;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return depth === 0;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function unquote(raw: string): { value: string; wasQuoted: boolean } {
|
|
402
|
+
const trimmed = raw.trim();
|
|
403
|
+
if (
|
|
404
|
+
(trimmed.startsWith('"') && trimmed.endsWith('"') && trimmed.length >= 2) ||
|
|
405
|
+
(trimmed.startsWith("'") && trimmed.endsWith("'") && trimmed.length >= 2)
|
|
406
|
+
) {
|
|
407
|
+
return {
|
|
408
|
+
value: trimmed.slice(1, -1).replace(/\\(["'\\])/g, '$1'),
|
|
409
|
+
wasQuoted: true,
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
return { value: trimmed, wasQuoted: false };
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Compile a KQL-compatible search string into universal Elasticsearch Query DSL
|
|
417
|
+
* so queries run reliably across ES 7.x, 8.x, 9.x, and OpenSearch.
|
|
418
|
+
*/
|
|
419
|
+
export function compileKqlToQueryDsl(
|
|
420
|
+
rawQuery: string,
|
|
421
|
+
fieldMap: Map<string, EsFieldCapability> = new Map(),
|
|
422
|
+
): Record<string, unknown> | null {
|
|
423
|
+
const expr = rawQuery.trim();
|
|
424
|
+
if (!expr) return null;
|
|
425
|
+
|
|
426
|
+
if (hasBalancedOuterParens(expr)) {
|
|
427
|
+
return compileKqlToQueryDsl(expr.slice(1, -1), fieldMap);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const orParts = splitTopLevelBoolean(expr, 'OR');
|
|
431
|
+
if (orParts.length > 1) {
|
|
432
|
+
const should = orParts
|
|
433
|
+
.map((part) => compileKqlToQueryDsl(part, fieldMap))
|
|
434
|
+
.filter((item): item is Record<string, unknown> => item !== null);
|
|
435
|
+
if (should.length === 0) return null;
|
|
436
|
+
if (should.length === 1) return should[0];
|
|
437
|
+
return { bool: { should, minimum_should_match: 1 } };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const andParts = splitTopLevelBoolean(expr, 'AND');
|
|
441
|
+
if (andParts.length > 1) {
|
|
442
|
+
const must = andParts
|
|
443
|
+
.map((part) => compileKqlToQueryDsl(part, fieldMap))
|
|
444
|
+
.filter((item): item is Record<string, unknown> => item !== null);
|
|
445
|
+
if (must.length === 0) return null;
|
|
446
|
+
if (must.length === 1) return must[0];
|
|
447
|
+
return { bool: { must } };
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (/^NOT\s+/i.test(expr)) {
|
|
451
|
+
const inner = compileKqlToQueryDsl(expr.replace(/^NOT\s+/i, ''), fieldMap);
|
|
452
|
+
return inner ? { bool: { must_not: [inner] } } : null;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Range expression: field >= value, field > value, field <= value, field < value
|
|
456
|
+
const rangeMatch = expr.match(/^([A-Za-z0-9_@.-]+)\s*(>=|<=|>|<)\s*(.+)$/);
|
|
457
|
+
if (rangeMatch) {
|
|
458
|
+
const [, rawField, op, rawVal] = rangeMatch;
|
|
459
|
+
const { value } = unquote(rawVal);
|
|
460
|
+
const coerced = coerceLiteral(value, fieldMap.get(rawField));
|
|
461
|
+
const opKey = op === '>=' ? 'gte' : op === '<=' ? 'lte' : op === '>' ? 'gt' : 'lt';
|
|
462
|
+
return { range: { [rawField]: { [opKey]: coerced } } };
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// Field-value expression: field : value
|
|
466
|
+
const colonMatch = expr.match(/^([A-Za-z0-9_@.-]+)\s*:\s*(.+)$/);
|
|
467
|
+
if (colonMatch) {
|
|
468
|
+
const [, rawField, rawVal] = colonMatch;
|
|
469
|
+
const { value, wasQuoted } = unquote(rawVal);
|
|
470
|
+
|
|
471
|
+
if (!wasQuoted && value === '*') {
|
|
472
|
+
return { exists: { field: rawField } };
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const fieldMeta = fieldMap.get(rawField);
|
|
476
|
+
if (!wasQuoted && (value.includes('*') || value.includes('?'))) {
|
|
477
|
+
const wildcardField = resolveAggregatableField(rawField, fieldMap);
|
|
478
|
+
return {
|
|
479
|
+
wildcard: {
|
|
480
|
+
[wildcardField]: {
|
|
481
|
+
value,
|
|
482
|
+
case_insensitive: true,
|
|
483
|
+
},
|
|
484
|
+
},
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (wasQuoted) {
|
|
489
|
+
if (fieldMeta?.type === 'keyword' || fieldMeta?.type === 'constant_keyword') {
|
|
490
|
+
return { term: { [rawField]: value } };
|
|
491
|
+
}
|
|
492
|
+
return { match_phrase: { [rawField]: value } };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const coerced = coerceLiteral(value, fieldMeta);
|
|
496
|
+
if (typeof coerced === 'number' || typeof coerced === 'boolean') {
|
|
497
|
+
return { term: { [rawField]: coerced } };
|
|
498
|
+
}
|
|
499
|
+
if (fieldMeta?.type === 'keyword' || fieldMeta?.type === 'constant_keyword') {
|
|
500
|
+
return { term: { [rawField]: coerced } };
|
|
501
|
+
}
|
|
502
|
+
return { match_phrase: { [rawField]: String(coerced) } };
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
return {
|
|
506
|
+
simple_query_string: {
|
|
507
|
+
query: expr,
|
|
508
|
+
default_operator: 'and',
|
|
509
|
+
lenient: true,
|
|
510
|
+
},
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export function compileFilterItem(
|
|
515
|
+
item: ReportFilterItem,
|
|
516
|
+
fieldMap: Map<string, EsFieldCapability>,
|
|
517
|
+
): { clause: Record<string, unknown>; negated: boolean } | null {
|
|
518
|
+
if (item.enabled === false || !item.field.trim()) return null;
|
|
519
|
+
const field = item.field.trim();
|
|
520
|
+
const fieldMeta = fieldMap.get(field);
|
|
521
|
+
const keywordField = resolveAggregatableField(field, fieldMap);
|
|
522
|
+
|
|
523
|
+
switch (item.operator) {
|
|
524
|
+
case 'exists':
|
|
525
|
+
return { clause: { exists: { field } }, negated: false };
|
|
526
|
+
case 'not_exists':
|
|
527
|
+
return { clause: { exists: { field } }, negated: true };
|
|
528
|
+
case 'eq':
|
|
529
|
+
case 'ne': {
|
|
530
|
+
if (item.value === undefined || Array.isArray(item.value)) return null;
|
|
531
|
+
const val = typeof item.value === 'string' ? coerceLiteral(item.value, fieldMeta) : item.value;
|
|
532
|
+
const useTerm = typeof val !== 'string' || fieldMeta?.aggregatable === true || keywordField !== field;
|
|
533
|
+
const clause = useTerm ? { term: { [keywordField]: val } } : { match_phrase: { [field]: val } };
|
|
534
|
+
return { clause, negated: item.operator === 'ne' };
|
|
535
|
+
}
|
|
536
|
+
case 'in':
|
|
537
|
+
case 'not_in': {
|
|
538
|
+
const values = Array.isArray(item.value) ? item.value : item.value !== undefined ? [item.value] : [];
|
|
539
|
+
if (values.length === 0) return null;
|
|
540
|
+
const normalized = values.map((v) => (typeof v === 'string' ? coerceLiteral(v, fieldMeta) : v));
|
|
541
|
+
return {
|
|
542
|
+
clause: { terms: { [keywordField]: normalized } },
|
|
543
|
+
negated: item.operator === 'not_in',
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
case 'gt':
|
|
547
|
+
case 'gte':
|
|
548
|
+
case 'lt':
|
|
549
|
+
case 'lte': {
|
|
550
|
+
if (item.value === undefined || Array.isArray(item.value)) return null;
|
|
551
|
+
const val = typeof item.value === 'string' ? coerceLiteral(item.value, fieldMeta) : item.value;
|
|
552
|
+
return {
|
|
553
|
+
clause: { range: { [field]: { [item.operator]: val } } },
|
|
554
|
+
negated: false,
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
case 'between': {
|
|
558
|
+
const rangeBody: Record<string, ScalarFilterValue> = {};
|
|
559
|
+
if (item.value !== undefined && !Array.isArray(item.value)) {
|
|
560
|
+
rangeBody.gte = typeof item.value === 'string' ? coerceLiteral(item.value, fieldMeta) : item.value;
|
|
561
|
+
}
|
|
562
|
+
if (item.valueTo !== undefined) {
|
|
563
|
+
rangeBody.lte = typeof item.valueTo === 'string' ? coerceLiteral(item.valueTo, fieldMeta) : item.valueTo;
|
|
564
|
+
}
|
|
565
|
+
if (Object.keys(rangeBody).length === 0) return null;
|
|
566
|
+
return {
|
|
567
|
+
clause: { range: { [field]: rangeBody } },
|
|
568
|
+
negated: false,
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
case 'contains': {
|
|
572
|
+
if (item.value === undefined || Array.isArray(item.value)) return null;
|
|
573
|
+
const rawText = String(item.value).trim();
|
|
574
|
+
if (!rawText) return null;
|
|
575
|
+
if (fieldMeta && !fieldMeta.aggregatable && !fieldMeta.keywordSubfield) {
|
|
576
|
+
return { clause: { match_phrase: { [field]: rawText } }, negated: false };
|
|
577
|
+
}
|
|
578
|
+
const escaped = rawText.replace(/([*?\\])/g, '\\$1');
|
|
579
|
+
return {
|
|
580
|
+
clause: {
|
|
581
|
+
wildcard: {
|
|
582
|
+
[keywordField]: {
|
|
583
|
+
value: `*${escaped}*`,
|
|
584
|
+
case_insensitive: true,
|
|
585
|
+
},
|
|
586
|
+
},
|
|
587
|
+
},
|
|
588
|
+
negated: false,
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
default:
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
export function compileTimeRange(
|
|
597
|
+
timeRange: ReportTimeRange | undefined,
|
|
598
|
+
timeField: string | null | undefined,
|
|
599
|
+
): Record<string, unknown> | null {
|
|
600
|
+
const cleanField = typeof timeField === 'string' ? timeField.trim() : '';
|
|
601
|
+
if (!cleanField || !timeRange || timeRange.preset === 'all') return null;
|
|
602
|
+
|
|
603
|
+
if (timeRange.preset === 'custom') {
|
|
604
|
+
const bounds: Record<string, string> = {};
|
|
605
|
+
if (timeRange.from) bounds.gte = timeRange.from;
|
|
606
|
+
if (timeRange.to) bounds.lte = timeRange.to;
|
|
607
|
+
if (Object.keys(bounds).length === 0) return null;
|
|
608
|
+
return { range: { [cleanField]: bounds } };
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const expr = PRESET_TO_ES_EXPR[timeRange.preset];
|
|
612
|
+
if (!expr) return null;
|
|
613
|
+
return {
|
|
614
|
+
range: {
|
|
615
|
+
[cleanField]: {
|
|
616
|
+
gte: expr,
|
|
617
|
+
lte: 'now',
|
|
618
|
+
},
|
|
619
|
+
},
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
export interface CompileSearchOptions {
|
|
624
|
+
spec: ReportSpec;
|
|
625
|
+
overrides?: ReportRuntimeOverrides;
|
|
626
|
+
timeField?: string | null;
|
|
627
|
+
fields?: EsFieldCapability[] | null;
|
|
628
|
+
maxRowsCap?: number;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
export function mergeSpecWithOverrides(
|
|
632
|
+
spec: ReportSpec,
|
|
633
|
+
overrides?: ReportRuntimeOverrides,
|
|
634
|
+
): {
|
|
635
|
+
effectiveSpec: ReportSpec;
|
|
636
|
+
page: number;
|
|
637
|
+
pageSize: number;
|
|
638
|
+
searchAfter?: Array<string | number | boolean | null>;
|
|
639
|
+
} {
|
|
640
|
+
if (!overrides) {
|
|
641
|
+
return {
|
|
642
|
+
effectiveSpec: spec,
|
|
643
|
+
page: 1,
|
|
644
|
+
pageSize: spec.pageSize ?? DEFAULT_PAGE_SIZE,
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const effectiveSpec: ReportSpec = {
|
|
649
|
+
...spec,
|
|
650
|
+
query: overrides.query !== undefined ? overrides.query.trim() : spec.query,
|
|
651
|
+
filters: overrides.filters !== undefined ? normalizeReportFilters(overrides.filters) : spec.filters,
|
|
652
|
+
sort: overrides.sort !== undefined ? normalizeReportSort(overrides.sort) : spec.sort,
|
|
653
|
+
timeRange: overrides.timeRange !== undefined ? normalizeTimeRange(overrides.timeRange) : spec.timeRange,
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
const page = clamp(typeof overrides.page === 'number' ? overrides.page : 1, 1, 10000);
|
|
657
|
+
const pageSize = clamp(
|
|
658
|
+
typeof overrides.pageSize === 'number' ? overrides.pageSize : spec.pageSize ?? DEFAULT_PAGE_SIZE,
|
|
659
|
+
1,
|
|
660
|
+
MAX_UI_PAGE_SIZE,
|
|
661
|
+
);
|
|
662
|
+
const searchAfter =
|
|
663
|
+
Array.isArray(overrides.searchAfter) && overrides.searchAfter.length > 0 ? overrides.searchAfter : undefined;
|
|
664
|
+
|
|
665
|
+
return {
|
|
666
|
+
effectiveSpec,
|
|
667
|
+
page,
|
|
668
|
+
pageSize,
|
|
669
|
+
...(searchAfter ? { searchAfter } : {}),
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
export function compileSearchRequest(options: CompileSearchOptions): {
|
|
674
|
+
body: Record<string, unknown>;
|
|
675
|
+
effectiveSpec: ReportSpec;
|
|
676
|
+
} {
|
|
677
|
+
const fieldMap = buildFieldCapabilityMap(options.fields);
|
|
678
|
+
const { effectiveSpec, page, pageSize, searchAfter } = mergeSpecWithOverrides(options.spec, options.overrides);
|
|
679
|
+
const maxCap = options.maxRowsCap ? clamp(options.maxRowsCap, 1, MAX_UI_PAGE_SIZE) : MAX_UI_PAGE_SIZE;
|
|
680
|
+
const effectivePageSize = Math.min(pageSize, maxCap);
|
|
681
|
+
const timeField = options.timeField?.trim() || null;
|
|
682
|
+
|
|
683
|
+
const must: Array<Record<string, unknown>> = [];
|
|
684
|
+
const filter: Array<Record<string, unknown>> = [];
|
|
685
|
+
const mustNot: Array<Record<string, unknown>> = [];
|
|
686
|
+
|
|
687
|
+
if (effectiveSpec.query && effectiveSpec.query.trim()) {
|
|
688
|
+
if (effectiveSpec.queryMode === 'simple') {
|
|
689
|
+
must.push({
|
|
690
|
+
simple_query_string: {
|
|
691
|
+
query: effectiveSpec.query.trim(),
|
|
692
|
+
default_operator: 'and',
|
|
693
|
+
lenient: true,
|
|
694
|
+
},
|
|
695
|
+
});
|
|
696
|
+
} else {
|
|
697
|
+
const parsedQuery = compileKqlToQueryDsl(effectiveSpec.query, fieldMap);
|
|
698
|
+
if (parsedQuery) {
|
|
699
|
+
must.push(parsedQuery);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
for (const item of effectiveSpec.filters ?? []) {
|
|
705
|
+
const compiled = compileFilterItem(item, fieldMap);
|
|
706
|
+
if (!compiled) continue;
|
|
707
|
+
if (compiled.negated) {
|
|
708
|
+
mustNot.push(compiled.clause);
|
|
709
|
+
} else {
|
|
710
|
+
filter.push(compiled.clause);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
const timeClause = compileTimeRange(effectiveSpec.timeRange, timeField);
|
|
715
|
+
if (timeClause) {
|
|
716
|
+
filter.push(timeClause);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
let queryDsl: Record<string, unknown>;
|
|
720
|
+
if (must.length === 0 && filter.length === 0 && mustNot.length === 0) {
|
|
721
|
+
queryDsl = { match_all: {} };
|
|
722
|
+
} else {
|
|
723
|
+
queryDsl = {
|
|
724
|
+
bool: {
|
|
725
|
+
...(must.length > 0 ? { must } : {}),
|
|
726
|
+
...(filter.length > 0 ? { filter } : {}),
|
|
727
|
+
...(mustNot.length > 0 ? { must_not: mustNot } : {}),
|
|
728
|
+
},
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
const body: Record<string, unknown> = {
|
|
733
|
+
query: queryDsl,
|
|
734
|
+
track_total_hits: MAX_RESULT_WINDOW,
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
// Build sort clauses
|
|
738
|
+
const requestedSorts =
|
|
739
|
+
effectiveSpec.sort && effectiveSpec.sort.length > 0
|
|
740
|
+
? effectiveSpec.sort
|
|
741
|
+
: timeField
|
|
742
|
+
? [{ field: timeField, order: 'desc' as const }]
|
|
743
|
+
: [];
|
|
744
|
+
|
|
745
|
+
if (effectiveSpec.visualization === 'table') {
|
|
746
|
+
body.size = effectivePageSize;
|
|
747
|
+
|
|
748
|
+
if (requestedSorts.length > 0) {
|
|
749
|
+
body.sort = requestedSorts.map((sortItem) => {
|
|
750
|
+
const targetField = resolveAggregatableField(sortItem.field, fieldMap);
|
|
751
|
+
const unmappedType = resolveUnmappedSortType(targetField, fieldMap);
|
|
752
|
+
return {
|
|
753
|
+
[targetField]: {
|
|
754
|
+
order: sortItem.order,
|
|
755
|
+
...(unmappedType ? { unmapped_type: unmappedType } : {}),
|
|
756
|
+
},
|
|
757
|
+
};
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
if (searchAfter && body.sort) {
|
|
762
|
+
body.search_after = searchAfter;
|
|
763
|
+
} else {
|
|
764
|
+
const rawFrom = (page - 1) * effectivePageSize;
|
|
765
|
+
body.from = Math.min(rawFrom, Math.max(0, MAX_RESULT_WINDOW - effectivePageSize));
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
if (effectiveSpec.columns && effectiveSpec.columns.length > 0) {
|
|
769
|
+
const sourceFields = new Set(effectiveSpec.columns);
|
|
770
|
+
if (timeField) sourceFields.add(timeField);
|
|
771
|
+
body._source = Array.from(sourceFields);
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
if (effectiveSpec.query && effectiveSpec.query.trim()) {
|
|
775
|
+
body.highlight = {
|
|
776
|
+
pre_tags: [HIGHLIGHT_PRE_TAG],
|
|
777
|
+
post_tags: [HIGHLIGHT_POST_TAG],
|
|
778
|
+
fields: { '*': {} },
|
|
779
|
+
fragment_size: 160,
|
|
780
|
+
number_of_fragments: 2,
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
if (timeField && effectiveSpec.chart?.showHistogram !== false) {
|
|
785
|
+
body.aggs = {
|
|
786
|
+
es_time_histogram: {
|
|
787
|
+
auto_date_histogram: {
|
|
788
|
+
field: timeField,
|
|
789
|
+
buckets: 24,
|
|
790
|
+
},
|
|
791
|
+
},
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
} else if (effectiveSpec.visualization === 'metric') {
|
|
795
|
+
body.size = 0;
|
|
796
|
+
const metricType = effectiveSpec.chart?.metricType ?? 'count';
|
|
797
|
+
const metricField = effectiveSpec.chart?.metricField?.trim();
|
|
798
|
+
if (metricType !== 'count' && metricField) {
|
|
799
|
+
const targetMetricField = resolveAggregatableField(metricField, fieldMap);
|
|
800
|
+
body.aggs = {
|
|
801
|
+
es_metric: {
|
|
802
|
+
[metricType]: {
|
|
803
|
+
field: targetMetricField,
|
|
804
|
+
},
|
|
805
|
+
},
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
} else {
|
|
809
|
+
// Chart visualizations: bar, line, area, pie
|
|
810
|
+
body.size = 0;
|
|
811
|
+
const chartCfg = effectiveSpec.chart ?? {};
|
|
812
|
+
const defaultBucketType: BucketAggType =
|
|
813
|
+
timeField && (effectiveSpec.visualization === 'line' || effectiveSpec.visualization === 'area')
|
|
814
|
+
? 'date_histogram'
|
|
815
|
+
: chartCfg.bucketType ?? (timeField ? 'date_histogram' : 'terms');
|
|
816
|
+
|
|
817
|
+
const rawBucketField =
|
|
818
|
+
chartCfg.bucketField?.trim() ||
|
|
819
|
+
(defaultBucketType === 'date_histogram' ? timeField : '') ||
|
|
820
|
+
Array.from(fieldMap.values()).find((f) => f.aggregatable)?.name ||
|
|
821
|
+
'_index';
|
|
822
|
+
|
|
823
|
+
const bucketField = resolveAggregatableField(rawBucketField, fieldMap);
|
|
824
|
+
const metricType = chartCfg.metricType ?? 'count';
|
|
825
|
+
const rawMetricField = chartCfg.metricField?.trim();
|
|
826
|
+
const subAggs =
|
|
827
|
+
metricType !== 'count' && rawMetricField
|
|
828
|
+
? {
|
|
829
|
+
es_metric: {
|
|
830
|
+
[metricType]: {
|
|
831
|
+
field: resolveAggregatableField(rawMetricField, fieldMap),
|
|
832
|
+
},
|
|
833
|
+
},
|
|
834
|
+
}
|
|
835
|
+
: undefined;
|
|
836
|
+
|
|
837
|
+
if (defaultBucketType === 'date_histogram') {
|
|
838
|
+
const interval = chartCfg.calendarInterval ?? 'auto';
|
|
839
|
+
const bucketAgg =
|
|
840
|
+
interval === 'auto'
|
|
841
|
+
? {
|
|
842
|
+
auto_date_histogram: {
|
|
843
|
+
field: bucketField,
|
|
844
|
+
buckets: 24,
|
|
845
|
+
},
|
|
846
|
+
}
|
|
847
|
+
: {
|
|
848
|
+
date_histogram: {
|
|
849
|
+
field: bucketField,
|
|
850
|
+
calendar_interval: interval,
|
|
851
|
+
min_doc_count: 0,
|
|
852
|
+
},
|
|
853
|
+
};
|
|
854
|
+
body.aggs = {
|
|
855
|
+
es_buckets: {
|
|
856
|
+
...bucketAgg,
|
|
857
|
+
...(subAggs ? { aggs: subAggs } : {}),
|
|
858
|
+
},
|
|
859
|
+
};
|
|
860
|
+
} else {
|
|
861
|
+
const topN = clamp(chartCfg.topN ?? 10, 1, MAX_TERMS_BUCKETS);
|
|
862
|
+
body.aggs = {
|
|
863
|
+
es_buckets: {
|
|
864
|
+
terms: {
|
|
865
|
+
field: bucketField,
|
|
866
|
+
size: topN,
|
|
867
|
+
...(subAggs ? { order: { es_metric: 'desc' } } : {}),
|
|
868
|
+
},
|
|
869
|
+
...(subAggs ? { aggs: subAggs } : {}),
|
|
870
|
+
},
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
return { body, effectiveSpec };
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
function deriveColumns(
|
|
879
|
+
rows: ReportRowHit[],
|
|
880
|
+
configuredColumns: string[] | undefined,
|
|
881
|
+
timeField?: string | null,
|
|
882
|
+
): string[] {
|
|
883
|
+
if (configuredColumns && configuredColumns.length > 0) {
|
|
884
|
+
return configuredColumns;
|
|
885
|
+
}
|
|
886
|
+
const seen = new Set<string>();
|
|
887
|
+
if (timeField && rows.some((r) => timeField in r.source)) {
|
|
888
|
+
seen.add(timeField);
|
|
889
|
+
}
|
|
890
|
+
for (const row of rows) {
|
|
891
|
+
for (const key of Object.keys(row.source)) {
|
|
892
|
+
if (seen.size >= 8) break;
|
|
893
|
+
seen.add(key);
|
|
894
|
+
}
|
|
895
|
+
if (seen.size >= 8) break;
|
|
896
|
+
}
|
|
897
|
+
return Array.from(seen);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
function buildDefaultMetricLabel(spec: ReportSpec): string {
|
|
901
|
+
if (spec.chart?.metricLabel) return spec.chart.metricLabel;
|
|
902
|
+
const metricType = spec.chart?.metricType ?? 'count';
|
|
903
|
+
if (metricType === 'count') return 'Count';
|
|
904
|
+
const field = spec.chart?.metricField ?? '';
|
|
905
|
+
return field ? `${metricType.toUpperCase()}(${field})` : metricType.toUpperCase();
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
export function parseSearchResponse(
|
|
909
|
+
rawResponse: unknown,
|
|
910
|
+
compiledDsl: Record<string, unknown>,
|
|
911
|
+
spec: ReportSpec,
|
|
912
|
+
timeField?: string | null,
|
|
913
|
+
): ReportExecutionResult {
|
|
914
|
+
const res = isRecord(rawResponse) ? rawResponse : {};
|
|
915
|
+
const tookMs = typeof res.took === 'number' ? res.took : 0;
|
|
916
|
+
|
|
917
|
+
const hitsContainer = isRecord(res.hits) ? res.hits : {};
|
|
918
|
+
const rawTotal = hitsContainer.total;
|
|
919
|
+
let total = 0;
|
|
920
|
+
let totalRelation: 'eq' | 'gte' = 'eq';
|
|
921
|
+
if (typeof rawTotal === 'number') {
|
|
922
|
+
total = rawTotal;
|
|
923
|
+
} else if (isRecord(rawTotal)) {
|
|
924
|
+
total = typeof rawTotal.value === 'number' ? rawTotal.value : 0;
|
|
925
|
+
totalRelation = rawTotal.relation === 'gte' ? 'gte' : 'eq';
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
const rawHits = Array.isArray(hitsContainer.hits) ? hitsContainer.hits : [];
|
|
929
|
+
const rows: ReportRowHit[] = rawHits.filter(isRecord).map((hit) => {
|
|
930
|
+
const source = isRecord(hit._source) ? hit._source : {};
|
|
931
|
+
let highlights: Record<string, string[]> | undefined;
|
|
932
|
+
if (isRecord(hit.highlight)) {
|
|
933
|
+
highlights = {};
|
|
934
|
+
for (const [hKey, hVal] of Object.entries(hit.highlight)) {
|
|
935
|
+
if (Array.isArray(hVal)) {
|
|
936
|
+
highlights[hKey] = hVal.filter((v): v is string => typeof v === 'string');
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
const sortArr = Array.isArray(hit.sort)
|
|
941
|
+
? hit.sort.map((s) =>
|
|
942
|
+
typeof s === 'string' || typeof s === 'number' || typeof s === 'boolean' || s === null ? s : String(s),
|
|
943
|
+
)
|
|
944
|
+
: undefined;
|
|
945
|
+
return {
|
|
946
|
+
_id: typeof hit._id === 'string' ? hit._id : String(hit._id ?? ''),
|
|
947
|
+
_index: typeof hit._index === 'string' ? hit._index : String(hit._index ?? ''),
|
|
948
|
+
_score: typeof hit._score === 'number' ? hit._score : null,
|
|
949
|
+
...(sortArr ? { _sort: sortArr } : {}),
|
|
950
|
+
source,
|
|
951
|
+
...(highlights ? { highlights } : {}),
|
|
952
|
+
};
|
|
953
|
+
});
|
|
954
|
+
|
|
955
|
+
const lastRow = rows.length > 0 ? rows[rows.length - 1] : undefined;
|
|
956
|
+
const nextSearchAfter = lastRow?._sort && lastRow._sort.length > 0 ? lastRow._sort : undefined;
|
|
957
|
+
const columns = deriveColumns(rows, spec.columns, timeField);
|
|
958
|
+
|
|
959
|
+
const aggs = isRecord(res.aggregations) ? res.aggregations : {};
|
|
960
|
+
const histogram: ReportHistogramBucket[] = [];
|
|
961
|
+
if (isRecord(aggs.es_time_histogram) && Array.isArray(aggs.es_time_histogram.buckets)) {
|
|
962
|
+
for (const bucket of aggs.es_time_histogram.buckets) {
|
|
963
|
+
if (!isRecord(bucket)) continue;
|
|
964
|
+
const key = typeof bucket.key_as_string === 'string' ? bucket.key_as_string : String(bucket.key ?? '');
|
|
965
|
+
histogram.push({
|
|
966
|
+
key,
|
|
967
|
+
...(typeof bucket.key === 'number' ? { timestamp: bucket.key } : {}),
|
|
968
|
+
count: typeof bucket.doc_count === 'number' ? bucket.doc_count : 0,
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
const series: ReportSeriesPoint[] = [];
|
|
974
|
+
if (isRecord(aggs.es_buckets) && Array.isArray(aggs.es_buckets.buckets)) {
|
|
975
|
+
for (const bucket of aggs.es_buckets.buckets) {
|
|
976
|
+
if (!isRecord(bucket)) continue;
|
|
977
|
+
const label = typeof bucket.key_as_string === 'string' ? bucket.key_as_string : String(bucket.key ?? '');
|
|
978
|
+
const docCount = typeof bucket.doc_count === 'number' ? bucket.doc_count : 0;
|
|
979
|
+
const subMetric = isRecord(bucket.es_metric) ? bucket.es_metric : undefined;
|
|
980
|
+
const metricVal =
|
|
981
|
+
subMetric && typeof subMetric.value === 'number' && Number.isFinite(subMetric.value)
|
|
982
|
+
? Number(subMetric.value.toFixed(4))
|
|
983
|
+
: docCount;
|
|
984
|
+
series.push({
|
|
985
|
+
key: String(bucket.key ?? label),
|
|
986
|
+
label,
|
|
987
|
+
value: metricVal,
|
|
988
|
+
count: docCount,
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
let metricValue: number | null = null;
|
|
994
|
+
if (spec.visualization === 'metric') {
|
|
995
|
+
const metricType = spec.chart?.metricType ?? 'count';
|
|
996
|
+
if (metricType === 'count' || !spec.chart?.metricField) {
|
|
997
|
+
metricValue = total;
|
|
998
|
+
} else if (isRecord(aggs.es_metric) && typeof aggs.es_metric.value === 'number') {
|
|
999
|
+
metricValue = Number.isFinite(aggs.es_metric.value) ? Number(aggs.es_metric.value.toFixed(4)) : null;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
return {
|
|
1004
|
+
total,
|
|
1005
|
+
totalRelation,
|
|
1006
|
+
tookMs,
|
|
1007
|
+
visualization: spec.visualization,
|
|
1008
|
+
rows,
|
|
1009
|
+
columns,
|
|
1010
|
+
...(nextSearchAfter ? { nextSearchAfter } : {}),
|
|
1011
|
+
series,
|
|
1012
|
+
histogram,
|
|
1013
|
+
metricValue,
|
|
1014
|
+
metricLabel: buildDefaultMetricLabel(spec),
|
|
1015
|
+
compiledDsl,
|
|
1016
|
+
};
|
|
1017
|
+
}
|