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.
Files changed (257) hide show
  1. package/ai/skills/elastic-report/SKILLS.md +32 -0
  2. package/client-v2.d.ts +2 -0
  3. package/client-v2.js +1 -0
  4. package/client.d.ts +2 -0
  5. package/client.js +1 -0
  6. package/dist/client/index.js +10 -0
  7. package/dist/client-v2/851.488fdbf5bc5b6a36.js +10 -0
  8. package/dist/client-v2/index.js +10 -0
  9. package/dist/externalVersion.js +20 -0
  10. package/dist/index.js +48 -0
  11. package/dist/locale/en-US.json +99 -0
  12. package/dist/locale/vi-VN.json +99 -0
  13. package/dist/locale/zh-CN.json +99 -0
  14. package/dist/node_modules/@elastic/elasticsearch/.dockerignore +11 -0
  15. package/dist/node_modules/@elastic/elasticsearch/LICENSE +202 -0
  16. package/dist/node_modules/@elastic/elasticsearch/NOTICE.txt +2 -0
  17. package/dist/node_modules/@elastic/elasticsearch/catalog-info.yaml +50 -0
  18. package/dist/node_modules/@elastic/elasticsearch/codecov.yml +14 -0
  19. package/dist/node_modules/@elastic/elasticsearch/index.d.ts +28 -0
  20. package/dist/node_modules/@elastic/elasticsearch/index.js +85525 -0
  21. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/async_search.d.ts +39 -0
  22. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/async_search.js +155 -0
  23. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/autoscaling.d.ts +39 -0
  24. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/autoscaling.js +130 -0
  25. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/bulk.d.ts +14 -0
  26. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/bulk.js +60 -0
  27. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/capabilities.d.ts +14 -0
  28. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/capabilities.js +43 -0
  29. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/cat.d.ts +193 -0
  30. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/cat.js +766 -0
  31. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ccr.d.ts +102 -0
  32. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ccr.js +395 -0
  33. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/clear_scroll.d.ts +14 -0
  34. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/clear_scroll.js +60 -0
  35. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/close_point_in_time.d.ts +14 -0
  36. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/close_point_in_time.js +56 -0
  37. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/cluster.d.ts +123 -0
  38. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/cluster.js +482 -0
  39. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/connector.d.ts +245 -0
  40. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/connector.js +1121 -0
  41. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/count.d.ts +14 -0
  42. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/count.js +68 -0
  43. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/create.d.ts +14 -0
  44. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/create.js +53 -0
  45. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/dangling_indices.d.ts +32 -0
  46. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/dangling_indices.js +100 -0
  47. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete.d.ts +14 -0
  48. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete.js +46 -0
  49. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_by_query.d.ts +14 -0
  50. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_by_query.js +59 -0
  51. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_by_query_rethrottle.d.ts +14 -0
  52. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_by_query_rethrottle.js +45 -0
  53. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_script.d.ts +14 -0
  54. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/delete_script.js +45 -0
  55. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/enrich.d.ts +46 -0
  56. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/enrich.js +169 -0
  57. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/eql.d.ts +39 -0
  58. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/eql.js +139 -0
  59. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/esql.d.ts +46 -0
  60. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/esql.js +170 -0
  61. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/exists.d.ts +14 -0
  62. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/exists.js +46 -0
  63. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/exists_source.d.ts +14 -0
  64. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/exists_source.js +46 -0
  65. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/explain.d.ts +14 -0
  66. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/explain.js +60 -0
  67. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/features.d.ts +25 -0
  68. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/features.js +75 -0
  69. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/field_caps.d.ts +14 -0
  70. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/field_caps.js +68 -0
  71. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/fleet.d.ts +50 -0
  72. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/fleet.js +211 -0
  73. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get.d.ts +14 -0
  74. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get.js +46 -0
  75. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script.d.ts +14 -0
  76. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script.js +45 -0
  77. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script_context.d.ts +14 -0
  78. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script_context.js +43 -0
  79. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script_languages.d.ts +14 -0
  80. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_script_languages.js +43 -0
  81. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_source.d.ts +14 -0
  82. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/get_source.js +46 -0
  83. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/graph.d.ts +18 -0
  84. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/graph.js +70 -0
  85. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/health_report.d.ts +14 -0
  86. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/health_report.js +54 -0
  87. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ilm.d.ts +88 -0
  88. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ilm.js +329 -0
  89. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/index.d.ts +14 -0
  90. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/index.js +61 -0
  91. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/indices.d.ts +494 -0
  92. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/indices.js +2133 -0
  93. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/inference.d.ts +221 -0
  94. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/inference.js +1167 -0
  95. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/info.d.ts +14 -0
  96. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/info.js +43 -0
  97. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ingest.d.ts +95 -0
  98. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ingest.js +389 -0
  99. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/knn_search.d.ts +14 -0
  100. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/knn_search.js +59 -0
  101. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/license.d.ts +60 -0
  102. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/license.js +194 -0
  103. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/logstash.d.ts +32 -0
  104. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/logstash.js +118 -0
  105. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/mget.d.ts +14 -0
  106. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/mget.js +68 -0
  107. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/migration.d.ts +32 -0
  108. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/migration.js +107 -0
  109. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ml.d.ts +522 -0
  110. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ml.js +2468 -0
  111. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/monitoring.d.ts +18 -0
  112. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/monitoring.js +60 -0
  113. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/msearch.d.ts +14 -0
  114. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/msearch.js +60 -0
  115. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/msearch_template.d.ts +14 -0
  116. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/msearch_template.js +60 -0
  117. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/mtermvectors.d.ts +14 -0
  118. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/mtermvectors.js +68 -0
  119. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/nodes.d.ts +60 -0
  120. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/nodes.js +290 -0
  121. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/open_point_in_time.d.ts +14 -0
  122. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/open_point_in_time.js +59 -0
  123. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ping.d.ts +14 -0
  124. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ping.js +43 -0
  125. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/profiling.d.ts +39 -0
  126. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/profiling.js +135 -0
  127. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/put_script.d.ts +14 -0
  128. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/put_script.js +68 -0
  129. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/query_rules.d.ts +67 -0
  130. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/query_rules.js +260 -0
  131. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/rank_eval.d.ts +14 -0
  132. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/rank_eval.js +67 -0
  133. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/reindex.d.ts +14 -0
  134. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/reindex.js +56 -0
  135. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/reindex_rethrottle.d.ts +14 -0
  136. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/reindex_rethrottle.js +45 -0
  137. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/render_search_template.d.ts +14 -0
  138. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/render_search_template.js +68 -0
  139. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/rollup.d.ts +67 -0
  140. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/rollup.js +263 -0
  141. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/scripts_painless_execute.d.ts +14 -0
  142. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/scripts_painless_execute.js +57 -0
  143. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/scroll.d.ts +14 -0
  144. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/scroll.js +59 -0
  145. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search.d.ts +14 -0
  146. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search.js +75 -0
  147. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_application.d.ts +81 -0
  148. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_application.js +313 -0
  149. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_mvt.d.ts +14 -0
  150. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_mvt.js +63 -0
  151. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_shards.d.ts +14 -0
  152. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_shards.js +54 -0
  153. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_template.d.ts +14 -0
  154. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/search_template.js +68 -0
  155. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/searchable_snapshots.d.ts +39 -0
  156. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/searchable_snapshots.js +167 -0
  157. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/security.d.ts +459 -0
  158. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/security.js +1982 -0
  159. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/shutdown.d.ts +32 -0
  160. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/shutdown.js +125 -0
  161. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/simulate.d.ts +18 -0
  162. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/simulate.js +78 -0
  163. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/slm.d.ts +74 -0
  164. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/slm.js +253 -0
  165. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/snapshot.d.ts +102 -0
  166. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/snapshot.js +410 -0
  167. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/sql.d.ts +53 -0
  168. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/sql.js +205 -0
  169. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ssl.d.ts +18 -0
  170. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/ssl.js +54 -0
  171. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/streams.d.ts +32 -0
  172. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/streams.js +96 -0
  173. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/synonyms.d.ts +60 -0
  174. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/synonyms.js +223 -0
  175. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/tasks.d.ts +32 -0
  176. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/tasks.js +109 -0
  177. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/terms_enum.d.ts +14 -0
  178. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/terms_enum.js +59 -0
  179. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/termvectors.d.ts +14 -0
  180. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/termvectors.js +68 -0
  181. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/text_structure.d.ts +39 -0
  182. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/text_structure.js +148 -0
  183. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/transform.d.ts +101 -0
  184. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/transform.js +386 -0
  185. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update.d.ts +14 -0
  186. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update.js +60 -0
  187. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update_by_query.d.ts +14 -0
  188. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update_by_query.js +59 -0
  189. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update_by_query_rethrottle.d.ts +14 -0
  190. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/update_by_query_rethrottle.js +45 -0
  191. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/watcher.d.ts +102 -0
  192. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/watcher.js +405 -0
  193. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/xpack.d.ts +25 -0
  194. package/dist/node_modules/@elastic/elasticsearch/lib/api/api/xpack.js +75 -0
  195. package/dist/node_modules/@elastic/elasticsearch/lib/api/index.d.ts +265 -0
  196. package/dist/node_modules/@elastic/elasticsearch/lib/api/index.js +632 -0
  197. package/dist/node_modules/@elastic/elasticsearch/lib/api/types.d.ts +19933 -0
  198. package/dist/node_modules/@elastic/elasticsearch/lib/api/types.js +21 -0
  199. package/dist/node_modules/@elastic/elasticsearch/lib/api/typesWithBodyKey.d.ts +20606 -0
  200. package/dist/node_modules/@elastic/elasticsearch/lib/api/typesWithBodyKey.js +21 -0
  201. package/dist/node_modules/@elastic/elasticsearch/lib/client.d.ts +152 -0
  202. package/dist/node_modules/@elastic/elasticsearch/lib/client.js +369 -0
  203. package/dist/node_modules/@elastic/elasticsearch/lib/helpers.d.ts +184 -0
  204. package/dist/node_modules/@elastic/elasticsearch/lib/helpers.js +889 -0
  205. package/dist/node_modules/@elastic/elasticsearch/lib/sniffingTransport.d.ts +4 -0
  206. package/dist/node_modules/@elastic/elasticsearch/lib/sniffingTransport.js +56 -0
  207. package/dist/node_modules/@elastic/elasticsearch/node_modules/.bin/arrow2csv +15 -0
  208. package/dist/node_modules/@elastic/elasticsearch/node_modules/.bin/arrow2csv.cmd +7 -0
  209. package/dist/node_modules/@elastic/elasticsearch/package.json +1 -0
  210. package/dist/node_modules/@elastic/elasticsearch/tsconfig.json +38 -0
  211. package/dist/server/acl-guard.js +107 -0
  212. package/dist/server/collections/es-report-data-views.js +79 -0
  213. package/dist/server/collections/es-reports.js +95 -0
  214. package/dist/server/index.js +58 -0
  215. package/dist/server/mcp/ElasticReportMcpServer.js +344 -0
  216. package/dist/server/mcp/ElasticReportToolCatalog.js +417 -0
  217. package/dist/server/plugin.js +395 -0
  218. package/dist/server/services/ElasticReportService.js +263 -0
  219. package/dist/server/services/ElasticsearchConnectionService.js +332 -0
  220. package/dist/server/services/ReportQueryCompiler.js +858 -0
  221. package/dist/shared/constants.js +81 -0
  222. package/dist/shared/types.js +24 -0
  223. package/package.json +61 -4
  224. package/server.d.ts +2 -0
  225. package/server.js +1 -0
  226. package/src/client/index.tsx +19 -0
  227. package/src/client-v2/__tests__/elastic-report.test.tsx +311 -0
  228. package/src/client-v2/api.ts +224 -0
  229. package/src/client-v2/components/ElasticBasicCharts.tsx +399 -0
  230. package/src/client-v2/components/ElasticReportBlock.tsx +160 -0
  231. package/src/client-v2/components/ElasticReportRenderer.tsx +707 -0
  232. package/src/client-v2/components/HighlightText.tsx +50 -0
  233. package/src/client-v2/components/ReportSelect.tsx +72 -0
  234. package/src/client-v2/index.tsx +14 -0
  235. package/src/client-v2/locale.ts +23 -0
  236. package/src/client-v2/models/ElasticReportBlockModel.tsx +85 -0
  237. package/src/client-v2/pages/ElasticReportManager.tsx +1047 -0
  238. package/src/client-v2/plugin.tsx +52 -0
  239. package/src/index.ts +11 -0
  240. package/src/locale/en-US.json +99 -0
  241. package/src/locale/vi-VN.json +99 -0
  242. package/src/locale/zh-CN.json +99 -0
  243. package/src/server/__tests__/query-compiler.test.ts +231 -0
  244. package/src/server/__tests__/report-mcp-acl.integration.test.ts +377 -0
  245. package/src/server/acl-guard.ts +103 -0
  246. package/src/server/collections/es-report-data-views.ts +58 -0
  247. package/src/server/collections/es-reports.ts +74 -0
  248. package/src/server/index.ts +15 -0
  249. package/src/server/mcp/ElasticReportMcpServer.ts +391 -0
  250. package/src/server/mcp/ElasticReportToolCatalog.ts +417 -0
  251. package/src/server/plugin.ts +431 -0
  252. package/src/server/services/ElasticReportService.ts +326 -0
  253. package/src/server/services/ElasticsearchConnectionService.ts +401 -0
  254. package/src/server/services/ReportQueryCompiler.ts +1017 -0
  255. package/src/shared/constants.ts +29 -0
  256. package/src/shared/types.ts +175 -0
  257. package/README.md +0 -3
@@ -0,0 +1,858 @@
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
+ buildFieldCapabilityMap: () => buildFieldCapabilityMap,
30
+ compileFilterItem: () => compileFilterItem,
31
+ compileKqlToQueryDsl: () => compileKqlToQueryDsl,
32
+ compileSearchRequest: () => compileSearchRequest,
33
+ compileTimeRange: () => compileTimeRange,
34
+ mergeSpecWithOverrides: () => mergeSpecWithOverrides,
35
+ normalizeReportFilters: () => normalizeReportFilters,
36
+ normalizeReportSort: () => normalizeReportSort,
37
+ normalizeReportSpec: () => normalizeReportSpec,
38
+ normalizeTimeRange: () => normalizeTimeRange,
39
+ parseSearchResponse: () => parseSearchResponse,
40
+ resolveAggregatableField: () => resolveAggregatableField
41
+ });
42
+ module.exports = __toCommonJS(ReportQueryCompiler_exports);
43
+ var import_constants = require("../../shared/constants");
44
+ const VALID_VISUALIZATIONS = /* @__PURE__ */ new Set([
45
+ "table",
46
+ "bar",
47
+ "line",
48
+ "area",
49
+ "pie",
50
+ "metric"
51
+ ]);
52
+ const VALID_OPERATORS = /* @__PURE__ */ new Set([
53
+ "eq",
54
+ "ne",
55
+ "in",
56
+ "not_in",
57
+ "gt",
58
+ "gte",
59
+ "lt",
60
+ "lte",
61
+ "between",
62
+ "contains",
63
+ "exists",
64
+ "not_exists"
65
+ ]);
66
+ const VALID_PRESETS = /* @__PURE__ */ new Set([
67
+ "last_15m",
68
+ "last_1h",
69
+ "last_24h",
70
+ "last_7d",
71
+ "last_30d",
72
+ "last_90d",
73
+ "all",
74
+ "custom"
75
+ ]);
76
+ const VALID_METRICS = /* @__PURE__ */ new Set(["count", "sum", "avg", "min", "max", "cardinality"]);
77
+ const VALID_BUCKETS = /* @__PURE__ */ new Set(["date_histogram", "terms"]);
78
+ const VALID_INTERVALS = /* @__PURE__ */ new Set([
79
+ "auto",
80
+ "1m",
81
+ "5m",
82
+ "15m",
83
+ "1h",
84
+ "1d",
85
+ "1w",
86
+ "1M"
87
+ ]);
88
+ const PRESET_TO_ES_EXPR = {
89
+ last_15m: "now-15m",
90
+ last_1h: "now-1h",
91
+ last_24h: "now-24h",
92
+ last_7d: "now-7d",
93
+ last_30d: "now-30d",
94
+ last_90d: "now-90d"
95
+ };
96
+ function isRecord(value) {
97
+ return typeof value === "object" && value !== null && !Array.isArray(value);
98
+ }
99
+ function clamp(num, min, max) {
100
+ if (!Number.isFinite(num)) return min;
101
+ return Math.max(min, Math.min(max, Math.trunc(num)));
102
+ }
103
+ function toScalarValue(raw) {
104
+ if (typeof raw === "string") return raw;
105
+ if (typeof raw === "number" && Number.isFinite(raw)) return raw;
106
+ if (typeof raw === "boolean") return raw;
107
+ return void 0;
108
+ }
109
+ function coerceLiteral(raw, fieldMeta) {
110
+ const trimmed = raw.trim();
111
+ if (fieldMeta) {
112
+ if (fieldMeta.type === "boolean") {
113
+ if (trimmed.toLowerCase() === "true") return true;
114
+ if (trimmed.toLowerCase() === "false") return false;
115
+ }
116
+ if (["long", "integer", "short", "byte", "double", "float", "half_float", "scaled_float"].includes(fieldMeta.type)) {
117
+ const parsed = Number(trimmed);
118
+ if (Number.isFinite(parsed)) return parsed;
119
+ }
120
+ }
121
+ if (trimmed.toLowerCase() === "true") return true;
122
+ if (trimmed.toLowerCase() === "false") return false;
123
+ if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
124
+ const parsed = Number(trimmed);
125
+ if (Number.isFinite(parsed)) return parsed;
126
+ }
127
+ return trimmed;
128
+ }
129
+ function buildFieldCapabilityMap(fields) {
130
+ const map = /* @__PURE__ */ new Map();
131
+ if (!Array.isArray(fields)) return map;
132
+ for (const field of fields) {
133
+ if (field && typeof field.name === "string" && field.name.trim()) {
134
+ map.set(field.name.trim(), field);
135
+ }
136
+ }
137
+ return map;
138
+ }
139
+ function resolveAggregatableField(fieldName, fieldMap) {
140
+ var _a;
141
+ const clean = fieldName.trim();
142
+ const meta = fieldMap.get(clean);
143
+ if (!meta) return clean;
144
+ if (meta.aggregatable) return clean;
145
+ if (meta.keywordSubfield) return meta.keywordSubfield;
146
+ const candidate = `${clean}.keyword`;
147
+ if ((_a = fieldMap.get(candidate)) == null ? void 0 : _a.aggregatable) return candidate;
148
+ return clean;
149
+ }
150
+ function resolveUnmappedSortType(fieldName, fieldMap) {
151
+ const meta = fieldMap.get(fieldName);
152
+ if (!meta) return void 0;
153
+ switch (meta.type) {
154
+ case "date":
155
+ case "date_nanos":
156
+ return "date";
157
+ case "long":
158
+ case "integer":
159
+ case "short":
160
+ case "byte":
161
+ return "long";
162
+ case "double":
163
+ case "float":
164
+ case "half_float":
165
+ case "scaled_float":
166
+ return "double";
167
+ case "boolean":
168
+ return "boolean";
169
+ case "keyword":
170
+ case "constant_keyword":
171
+ case "wildcard":
172
+ return "keyword";
173
+ default:
174
+ return void 0;
175
+ }
176
+ }
177
+ function normalizeReportFilters(raw) {
178
+ if (!Array.isArray(raw)) return [];
179
+ const items = [];
180
+ for (const entry of raw) {
181
+ if (!isRecord(entry)) continue;
182
+ const field = typeof entry.field === "string" ? entry.field.trim() : "";
183
+ const operator = typeof entry.operator === "string" ? entry.operator : "eq";
184
+ if (!field || !VALID_OPERATORS.has(operator)) continue;
185
+ let value;
186
+ if (Array.isArray(entry.value)) {
187
+ const arr = entry.value.map(toScalarValue).filter((v) => v !== void 0);
188
+ value = arr;
189
+ } else if (typeof entry.value === "string" && (operator === "in" || operator === "not_in")) {
190
+ value = entry.value.split(",").map((part) => part.trim()).filter(Boolean);
191
+ } else {
192
+ value = toScalarValue(entry.value);
193
+ }
194
+ const valueTo = toScalarValue(entry.valueTo);
195
+ items.push({
196
+ ...typeof entry.id === "string" && entry.id ? { id: entry.id } : {},
197
+ field,
198
+ operator,
199
+ ...value !== void 0 ? { value } : {},
200
+ ...valueTo !== void 0 ? { valueTo } : {},
201
+ enabled: entry.enabled !== false
202
+ });
203
+ }
204
+ return items;
205
+ }
206
+ function normalizeReportSort(raw) {
207
+ if (!Array.isArray(raw)) return [];
208
+ const items = [];
209
+ for (const entry of raw) {
210
+ if (typeof entry === "string" && entry.trim()) {
211
+ const trimmed = entry.trim();
212
+ if (trimmed.startsWith("-")) {
213
+ items.push({ field: trimmed.slice(1), order: "desc" });
214
+ } else {
215
+ items.push({ field: trimmed, order: "asc" });
216
+ }
217
+ continue;
218
+ }
219
+ if (!isRecord(entry)) continue;
220
+ const field = typeof entry.field === "string" ? entry.field.trim() : "";
221
+ if (!field) continue;
222
+ const order = entry.order === "asc" ? "asc" : "desc";
223
+ items.push({ field, order });
224
+ }
225
+ return items;
226
+ }
227
+ function normalizeTimeRange(raw) {
228
+ if (!isRecord(raw)) return void 0;
229
+ const preset = typeof raw.preset === "string" && VALID_PRESETS.has(raw.preset) ? raw.preset : "last_24h";
230
+ const from = typeof raw.from === "string" && raw.from.trim() ? raw.from.trim() : void 0;
231
+ const to = typeof raw.to === "string" && raw.to.trim() ? raw.to.trim() : void 0;
232
+ return {
233
+ preset,
234
+ ...from ? { from } : {},
235
+ ...to ? { to } : {}
236
+ };
237
+ }
238
+ function normalizeReportSpec(raw, fallbackVisualization) {
239
+ const obj = isRecord(raw) ? raw : {};
240
+ const visCandidate = typeof obj.visualization === "string" ? obj.visualization : fallbackVisualization;
241
+ const visualization = typeof visCandidate === "string" && VALID_VISUALIZATIONS.has(visCandidate) ? visCandidate : "table";
242
+ const query = typeof obj.query === "string" ? obj.query.trim() : "";
243
+ const queryMode = obj.queryMode === "simple" ? "simple" : "kql";
244
+ const filters = normalizeReportFilters(obj.filters);
245
+ const sort = normalizeReportSort(obj.sort);
246
+ const timeRange = normalizeTimeRange(obj.timeRange);
247
+ const columns = Array.isArray(obj.columns) ? obj.columns.filter((col) => typeof col === "string" && col.trim().length > 0).map((col) => col.trim()) : [];
248
+ const pageSize = clamp(typeof obj.pageSize === "number" ? obj.pageSize : import_constants.DEFAULT_PAGE_SIZE, 1, import_constants.MAX_UI_PAGE_SIZE);
249
+ const rawChart = isRecord(obj.chart) ? obj.chart : {};
250
+ const metricType = typeof rawChart.metricType === "string" && VALID_METRICS.has(rawChart.metricType) ? rawChart.metricType : "count";
251
+ const bucketType = typeof rawChart.bucketType === "string" && VALID_BUCKETS.has(rawChart.bucketType) ? rawChart.bucketType : void 0;
252
+ const calendarInterval = typeof rawChart.calendarInterval === "string" && VALID_INTERVALS.has(rawChart.calendarInterval) ? rawChart.calendarInterval : "auto";
253
+ return {
254
+ query,
255
+ queryMode,
256
+ filters,
257
+ sort,
258
+ ...timeRange ? { timeRange } : {},
259
+ columns,
260
+ pageSize,
261
+ visualization,
262
+ chart: {
263
+ ...bucketType ? { bucketType } : {},
264
+ ...typeof rawChart.bucketField === "string" && rawChart.bucketField.trim() ? { bucketField: rawChart.bucketField.trim() } : {},
265
+ calendarInterval,
266
+ topN: clamp(typeof rawChart.topN === "number" ? rawChart.topN : 10, 1, import_constants.MAX_TERMS_BUCKETS),
267
+ metricType,
268
+ ...typeof rawChart.metricField === "string" && rawChart.metricField.trim() ? { metricField: rawChart.metricField.trim() } : {},
269
+ ...typeof rawChart.metricLabel === "string" && rawChart.metricLabel.trim() ? { metricLabel: rawChart.metricLabel.trim() } : {},
270
+ showHistogram: rawChart.showHistogram !== false
271
+ },
272
+ allowInteractiveControls: obj.allowInteractiveControls !== false
273
+ };
274
+ }
275
+ function splitTopLevelBoolean(input, keyword) {
276
+ const parts = [];
277
+ let depth = 0;
278
+ let inQuotes = false;
279
+ let quoteChar = "";
280
+ let lastIdx = 0;
281
+ for (let i = 0; i < input.length; i++) {
282
+ const ch = input[i];
283
+ if (inQuotes) {
284
+ if (ch === "\\") {
285
+ i++;
286
+ continue;
287
+ }
288
+ if (ch === quoteChar) {
289
+ inQuotes = false;
290
+ }
291
+ continue;
292
+ }
293
+ if (ch === '"' || ch === "'") {
294
+ inQuotes = true;
295
+ quoteChar = ch;
296
+ continue;
297
+ }
298
+ if (ch === "(") {
299
+ depth++;
300
+ continue;
301
+ }
302
+ if (ch === ")") {
303
+ depth = Math.max(0, depth - 1);
304
+ continue;
305
+ }
306
+ if (depth === 0) {
307
+ const prevBoundary = i === 0 || /\s/.test(input[i - 1]);
308
+ const slice = input.slice(i, i + keyword.length);
309
+ const nextChar = input[i + keyword.length];
310
+ const nextBoundary = nextChar !== void 0 && /\s/.test(nextChar);
311
+ if (prevBoundary && nextBoundary && slice.toUpperCase() === keyword) {
312
+ parts.push(input.slice(lastIdx, i).trim());
313
+ i += keyword.length;
314
+ lastIdx = i;
315
+ }
316
+ }
317
+ }
318
+ parts.push(input.slice(lastIdx).trim());
319
+ return parts.filter(Boolean);
320
+ }
321
+ function hasBalancedOuterParens(expr) {
322
+ if (!expr.startsWith("(") || !expr.endsWith(")")) return false;
323
+ let depth = 0;
324
+ let inQuotes = false;
325
+ let quoteChar = "";
326
+ for (let i = 0; i < expr.length; i++) {
327
+ const ch = expr[i];
328
+ if (inQuotes) {
329
+ if (ch === "\\") {
330
+ i++;
331
+ continue;
332
+ }
333
+ if (ch === quoteChar) inQuotes = false;
334
+ continue;
335
+ }
336
+ if (ch === '"' || ch === "'") {
337
+ inQuotes = true;
338
+ quoteChar = ch;
339
+ continue;
340
+ }
341
+ if (ch === "(") depth++;
342
+ if (ch === ")") {
343
+ depth--;
344
+ if (depth === 0 && i < expr.length - 1) return false;
345
+ }
346
+ }
347
+ return depth === 0;
348
+ }
349
+ function unquote(raw) {
350
+ const trimmed = raw.trim();
351
+ if (trimmed.startsWith('"') && trimmed.endsWith('"') && trimmed.length >= 2 || trimmed.startsWith("'") && trimmed.endsWith("'") && trimmed.length >= 2) {
352
+ return {
353
+ value: trimmed.slice(1, -1).replace(/\\(["'\\])/g, "$1"),
354
+ wasQuoted: true
355
+ };
356
+ }
357
+ return { value: trimmed, wasQuoted: false };
358
+ }
359
+ function compileKqlToQueryDsl(rawQuery, fieldMap = /* @__PURE__ */ new Map()) {
360
+ const expr = rawQuery.trim();
361
+ if (!expr) return null;
362
+ if (hasBalancedOuterParens(expr)) {
363
+ return compileKqlToQueryDsl(expr.slice(1, -1), fieldMap);
364
+ }
365
+ const orParts = splitTopLevelBoolean(expr, "OR");
366
+ if (orParts.length > 1) {
367
+ const should = orParts.map((part) => compileKqlToQueryDsl(part, fieldMap)).filter((item) => item !== null);
368
+ if (should.length === 0) return null;
369
+ if (should.length === 1) return should[0];
370
+ return { bool: { should, minimum_should_match: 1 } };
371
+ }
372
+ const andParts = splitTopLevelBoolean(expr, "AND");
373
+ if (andParts.length > 1) {
374
+ const must = andParts.map((part) => compileKqlToQueryDsl(part, fieldMap)).filter((item) => item !== null);
375
+ if (must.length === 0) return null;
376
+ if (must.length === 1) return must[0];
377
+ return { bool: { must } };
378
+ }
379
+ if (/^NOT\s+/i.test(expr)) {
380
+ const inner = compileKqlToQueryDsl(expr.replace(/^NOT\s+/i, ""), fieldMap);
381
+ return inner ? { bool: { must_not: [inner] } } : null;
382
+ }
383
+ const rangeMatch = expr.match(/^([A-Za-z0-9_@.-]+)\s*(>=|<=|>|<)\s*(.+)$/);
384
+ if (rangeMatch) {
385
+ const [, rawField, op, rawVal] = rangeMatch;
386
+ const { value } = unquote(rawVal);
387
+ const coerced = coerceLiteral(value, fieldMap.get(rawField));
388
+ const opKey = op === ">=" ? "gte" : op === "<=" ? "lte" : op === ">" ? "gt" : "lt";
389
+ return { range: { [rawField]: { [opKey]: coerced } } };
390
+ }
391
+ const colonMatch = expr.match(/^([A-Za-z0-9_@.-]+)\s*:\s*(.+)$/);
392
+ if (colonMatch) {
393
+ const [, rawField, rawVal] = colonMatch;
394
+ const { value, wasQuoted } = unquote(rawVal);
395
+ if (!wasQuoted && value === "*") {
396
+ return { exists: { field: rawField } };
397
+ }
398
+ const fieldMeta = fieldMap.get(rawField);
399
+ if (!wasQuoted && (value.includes("*") || value.includes("?"))) {
400
+ const wildcardField = resolveAggregatableField(rawField, fieldMap);
401
+ return {
402
+ wildcard: {
403
+ [wildcardField]: {
404
+ value,
405
+ case_insensitive: true
406
+ }
407
+ }
408
+ };
409
+ }
410
+ if (wasQuoted) {
411
+ if ((fieldMeta == null ? void 0 : fieldMeta.type) === "keyword" || (fieldMeta == null ? void 0 : fieldMeta.type) === "constant_keyword") {
412
+ return { term: { [rawField]: value } };
413
+ }
414
+ return { match_phrase: { [rawField]: value } };
415
+ }
416
+ const coerced = coerceLiteral(value, fieldMeta);
417
+ if (typeof coerced === "number" || typeof coerced === "boolean") {
418
+ return { term: { [rawField]: coerced } };
419
+ }
420
+ if ((fieldMeta == null ? void 0 : fieldMeta.type) === "keyword" || (fieldMeta == null ? void 0 : fieldMeta.type) === "constant_keyword") {
421
+ return { term: { [rawField]: coerced } };
422
+ }
423
+ return { match_phrase: { [rawField]: String(coerced) } };
424
+ }
425
+ return {
426
+ simple_query_string: {
427
+ query: expr,
428
+ default_operator: "and",
429
+ lenient: true
430
+ }
431
+ };
432
+ }
433
+ function compileFilterItem(item, fieldMap) {
434
+ if (item.enabled === false || !item.field.trim()) return null;
435
+ const field = item.field.trim();
436
+ const fieldMeta = fieldMap.get(field);
437
+ const keywordField = resolveAggregatableField(field, fieldMap);
438
+ switch (item.operator) {
439
+ case "exists":
440
+ return { clause: { exists: { field } }, negated: false };
441
+ case "not_exists":
442
+ return { clause: { exists: { field } }, negated: true };
443
+ case "eq":
444
+ case "ne": {
445
+ if (item.value === void 0 || Array.isArray(item.value)) return null;
446
+ const val = typeof item.value === "string" ? coerceLiteral(item.value, fieldMeta) : item.value;
447
+ const useTerm = typeof val !== "string" || (fieldMeta == null ? void 0 : fieldMeta.aggregatable) === true || keywordField !== field;
448
+ const clause = useTerm ? { term: { [keywordField]: val } } : { match_phrase: { [field]: val } };
449
+ return { clause, negated: item.operator === "ne" };
450
+ }
451
+ case "in":
452
+ case "not_in": {
453
+ const values = Array.isArray(item.value) ? item.value : item.value !== void 0 ? [item.value] : [];
454
+ if (values.length === 0) return null;
455
+ const normalized = values.map((v) => typeof v === "string" ? coerceLiteral(v, fieldMeta) : v);
456
+ return {
457
+ clause: { terms: { [keywordField]: normalized } },
458
+ negated: item.operator === "not_in"
459
+ };
460
+ }
461
+ case "gt":
462
+ case "gte":
463
+ case "lt":
464
+ case "lte": {
465
+ if (item.value === void 0 || Array.isArray(item.value)) return null;
466
+ const val = typeof item.value === "string" ? coerceLiteral(item.value, fieldMeta) : item.value;
467
+ return {
468
+ clause: { range: { [field]: { [item.operator]: val } } },
469
+ negated: false
470
+ };
471
+ }
472
+ case "between": {
473
+ const rangeBody = {};
474
+ if (item.value !== void 0 && !Array.isArray(item.value)) {
475
+ rangeBody.gte = typeof item.value === "string" ? coerceLiteral(item.value, fieldMeta) : item.value;
476
+ }
477
+ if (item.valueTo !== void 0) {
478
+ rangeBody.lte = typeof item.valueTo === "string" ? coerceLiteral(item.valueTo, fieldMeta) : item.valueTo;
479
+ }
480
+ if (Object.keys(rangeBody).length === 0) return null;
481
+ return {
482
+ clause: { range: { [field]: rangeBody } },
483
+ negated: false
484
+ };
485
+ }
486
+ case "contains": {
487
+ if (item.value === void 0 || Array.isArray(item.value)) return null;
488
+ const rawText = String(item.value).trim();
489
+ if (!rawText) return null;
490
+ if (fieldMeta && !fieldMeta.aggregatable && !fieldMeta.keywordSubfield) {
491
+ return { clause: { match_phrase: { [field]: rawText } }, negated: false };
492
+ }
493
+ const escaped = rawText.replace(/([*?\\])/g, "\\$1");
494
+ return {
495
+ clause: {
496
+ wildcard: {
497
+ [keywordField]: {
498
+ value: `*${escaped}*`,
499
+ case_insensitive: true
500
+ }
501
+ }
502
+ },
503
+ negated: false
504
+ };
505
+ }
506
+ default:
507
+ return null;
508
+ }
509
+ }
510
+ function compileTimeRange(timeRange, timeField) {
511
+ const cleanField = typeof timeField === "string" ? timeField.trim() : "";
512
+ if (!cleanField || !timeRange || timeRange.preset === "all") return null;
513
+ if (timeRange.preset === "custom") {
514
+ const bounds = {};
515
+ if (timeRange.from) bounds.gte = timeRange.from;
516
+ if (timeRange.to) bounds.lte = timeRange.to;
517
+ if (Object.keys(bounds).length === 0) return null;
518
+ return { range: { [cleanField]: bounds } };
519
+ }
520
+ const expr = PRESET_TO_ES_EXPR[timeRange.preset];
521
+ if (!expr) return null;
522
+ return {
523
+ range: {
524
+ [cleanField]: {
525
+ gte: expr,
526
+ lte: "now"
527
+ }
528
+ }
529
+ };
530
+ }
531
+ function mergeSpecWithOverrides(spec, overrides) {
532
+ if (!overrides) {
533
+ return {
534
+ effectiveSpec: spec,
535
+ page: 1,
536
+ pageSize: spec.pageSize ?? import_constants.DEFAULT_PAGE_SIZE
537
+ };
538
+ }
539
+ const effectiveSpec = {
540
+ ...spec,
541
+ query: overrides.query !== void 0 ? overrides.query.trim() : spec.query,
542
+ filters: overrides.filters !== void 0 ? normalizeReportFilters(overrides.filters) : spec.filters,
543
+ sort: overrides.sort !== void 0 ? normalizeReportSort(overrides.sort) : spec.sort,
544
+ timeRange: overrides.timeRange !== void 0 ? normalizeTimeRange(overrides.timeRange) : spec.timeRange
545
+ };
546
+ const page = clamp(typeof overrides.page === "number" ? overrides.page : 1, 1, 1e4);
547
+ const pageSize = clamp(
548
+ typeof overrides.pageSize === "number" ? overrides.pageSize : spec.pageSize ?? import_constants.DEFAULT_PAGE_SIZE,
549
+ 1,
550
+ import_constants.MAX_UI_PAGE_SIZE
551
+ );
552
+ const searchAfter = Array.isArray(overrides.searchAfter) && overrides.searchAfter.length > 0 ? overrides.searchAfter : void 0;
553
+ return {
554
+ effectiveSpec,
555
+ page,
556
+ pageSize,
557
+ ...searchAfter ? { searchAfter } : {}
558
+ };
559
+ }
560
+ function compileSearchRequest(options) {
561
+ var _a, _b, _c, _d, _e, _f, _g, _h;
562
+ const fieldMap = buildFieldCapabilityMap(options.fields);
563
+ const { effectiveSpec, page, pageSize, searchAfter } = mergeSpecWithOverrides(options.spec, options.overrides);
564
+ const maxCap = options.maxRowsCap ? clamp(options.maxRowsCap, 1, import_constants.MAX_UI_PAGE_SIZE) : import_constants.MAX_UI_PAGE_SIZE;
565
+ const effectivePageSize = Math.min(pageSize, maxCap);
566
+ const timeField = ((_a = options.timeField) == null ? void 0 : _a.trim()) || null;
567
+ const must = [];
568
+ const filter = [];
569
+ const mustNot = [];
570
+ if (effectiveSpec.query && effectiveSpec.query.trim()) {
571
+ if (effectiveSpec.queryMode === "simple") {
572
+ must.push({
573
+ simple_query_string: {
574
+ query: effectiveSpec.query.trim(),
575
+ default_operator: "and",
576
+ lenient: true
577
+ }
578
+ });
579
+ } else {
580
+ const parsedQuery = compileKqlToQueryDsl(effectiveSpec.query, fieldMap);
581
+ if (parsedQuery) {
582
+ must.push(parsedQuery);
583
+ }
584
+ }
585
+ }
586
+ for (const item of effectiveSpec.filters ?? []) {
587
+ const compiled = compileFilterItem(item, fieldMap);
588
+ if (!compiled) continue;
589
+ if (compiled.negated) {
590
+ mustNot.push(compiled.clause);
591
+ } else {
592
+ filter.push(compiled.clause);
593
+ }
594
+ }
595
+ const timeClause = compileTimeRange(effectiveSpec.timeRange, timeField);
596
+ if (timeClause) {
597
+ filter.push(timeClause);
598
+ }
599
+ let queryDsl;
600
+ if (must.length === 0 && filter.length === 0 && mustNot.length === 0) {
601
+ queryDsl = { match_all: {} };
602
+ } else {
603
+ queryDsl = {
604
+ bool: {
605
+ ...must.length > 0 ? { must } : {},
606
+ ...filter.length > 0 ? { filter } : {},
607
+ ...mustNot.length > 0 ? { must_not: mustNot } : {}
608
+ }
609
+ };
610
+ }
611
+ const body = {
612
+ query: queryDsl,
613
+ track_total_hits: import_constants.MAX_RESULT_WINDOW
614
+ };
615
+ const requestedSorts = effectiveSpec.sort && effectiveSpec.sort.length > 0 ? effectiveSpec.sort : timeField ? [{ field: timeField, order: "desc" }] : [];
616
+ if (effectiveSpec.visualization === "table") {
617
+ body.size = effectivePageSize;
618
+ if (requestedSorts.length > 0) {
619
+ body.sort = requestedSorts.map((sortItem) => {
620
+ const targetField = resolveAggregatableField(sortItem.field, fieldMap);
621
+ const unmappedType = resolveUnmappedSortType(targetField, fieldMap);
622
+ return {
623
+ [targetField]: {
624
+ order: sortItem.order,
625
+ ...unmappedType ? { unmapped_type: unmappedType } : {}
626
+ }
627
+ };
628
+ });
629
+ }
630
+ if (searchAfter && body.sort) {
631
+ body.search_after = searchAfter;
632
+ } else {
633
+ const rawFrom = (page - 1) * effectivePageSize;
634
+ body.from = Math.min(rawFrom, Math.max(0, import_constants.MAX_RESULT_WINDOW - effectivePageSize));
635
+ }
636
+ if (effectiveSpec.columns && effectiveSpec.columns.length > 0) {
637
+ const sourceFields = new Set(effectiveSpec.columns);
638
+ if (timeField) sourceFields.add(timeField);
639
+ body._source = Array.from(sourceFields);
640
+ }
641
+ if (effectiveSpec.query && effectiveSpec.query.trim()) {
642
+ body.highlight = {
643
+ pre_tags: [import_constants.HIGHLIGHT_PRE_TAG],
644
+ post_tags: [import_constants.HIGHLIGHT_POST_TAG],
645
+ fields: { "*": {} },
646
+ fragment_size: 160,
647
+ number_of_fragments: 2
648
+ };
649
+ }
650
+ if (timeField && ((_b = effectiveSpec.chart) == null ? void 0 : _b.showHistogram) !== false) {
651
+ body.aggs = {
652
+ es_time_histogram: {
653
+ auto_date_histogram: {
654
+ field: timeField,
655
+ buckets: 24
656
+ }
657
+ }
658
+ };
659
+ }
660
+ } else if (effectiveSpec.visualization === "metric") {
661
+ body.size = 0;
662
+ const metricType = ((_c = effectiveSpec.chart) == null ? void 0 : _c.metricType) ?? "count";
663
+ const metricField = (_e = (_d = effectiveSpec.chart) == null ? void 0 : _d.metricField) == null ? void 0 : _e.trim();
664
+ if (metricType !== "count" && metricField) {
665
+ const targetMetricField = resolveAggregatableField(metricField, fieldMap);
666
+ body.aggs = {
667
+ es_metric: {
668
+ [metricType]: {
669
+ field: targetMetricField
670
+ }
671
+ }
672
+ };
673
+ }
674
+ } else {
675
+ body.size = 0;
676
+ const chartCfg = effectiveSpec.chart ?? {};
677
+ const defaultBucketType = timeField && (effectiveSpec.visualization === "line" || effectiveSpec.visualization === "area") ? "date_histogram" : chartCfg.bucketType ?? (timeField ? "date_histogram" : "terms");
678
+ 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";
679
+ const bucketField = resolveAggregatableField(rawBucketField, fieldMap);
680
+ const metricType = chartCfg.metricType ?? "count";
681
+ const rawMetricField = (_h = chartCfg.metricField) == null ? void 0 : _h.trim();
682
+ const subAggs = metricType !== "count" && rawMetricField ? {
683
+ es_metric: {
684
+ [metricType]: {
685
+ field: resolveAggregatableField(rawMetricField, fieldMap)
686
+ }
687
+ }
688
+ } : void 0;
689
+ if (defaultBucketType === "date_histogram") {
690
+ const interval = chartCfg.calendarInterval ?? "auto";
691
+ const bucketAgg = interval === "auto" ? {
692
+ auto_date_histogram: {
693
+ field: bucketField,
694
+ buckets: 24
695
+ }
696
+ } : {
697
+ date_histogram: {
698
+ field: bucketField,
699
+ calendar_interval: interval,
700
+ min_doc_count: 0
701
+ }
702
+ };
703
+ body.aggs = {
704
+ es_buckets: {
705
+ ...bucketAgg,
706
+ ...subAggs ? { aggs: subAggs } : {}
707
+ }
708
+ };
709
+ } else {
710
+ const topN = clamp(chartCfg.topN ?? 10, 1, import_constants.MAX_TERMS_BUCKETS);
711
+ body.aggs = {
712
+ es_buckets: {
713
+ terms: {
714
+ field: bucketField,
715
+ size: topN,
716
+ ...subAggs ? { order: { es_metric: "desc" } } : {}
717
+ },
718
+ ...subAggs ? { aggs: subAggs } : {}
719
+ }
720
+ };
721
+ }
722
+ }
723
+ return { body, effectiveSpec };
724
+ }
725
+ function deriveColumns(rows, configuredColumns, timeField) {
726
+ if (configuredColumns && configuredColumns.length > 0) {
727
+ return configuredColumns;
728
+ }
729
+ const seen = /* @__PURE__ */ new Set();
730
+ if (timeField && rows.some((r) => timeField in r.source)) {
731
+ seen.add(timeField);
732
+ }
733
+ for (const row of rows) {
734
+ for (const key of Object.keys(row.source)) {
735
+ if (seen.size >= 8) break;
736
+ seen.add(key);
737
+ }
738
+ if (seen.size >= 8) break;
739
+ }
740
+ return Array.from(seen);
741
+ }
742
+ function buildDefaultMetricLabel(spec) {
743
+ var _a, _b, _c;
744
+ if ((_a = spec.chart) == null ? void 0 : _a.metricLabel) return spec.chart.metricLabel;
745
+ const metricType = ((_b = spec.chart) == null ? void 0 : _b.metricType) ?? "count";
746
+ if (metricType === "count") return "Count";
747
+ const field = ((_c = spec.chart) == null ? void 0 : _c.metricField) ?? "";
748
+ return field ? `${metricType.toUpperCase()}(${field})` : metricType.toUpperCase();
749
+ }
750
+ function parseSearchResponse(rawResponse, compiledDsl, spec, timeField) {
751
+ var _a, _b;
752
+ const res = isRecord(rawResponse) ? rawResponse : {};
753
+ const tookMs = typeof res.took === "number" ? res.took : 0;
754
+ const hitsContainer = isRecord(res.hits) ? res.hits : {};
755
+ const rawTotal = hitsContainer.total;
756
+ let total = 0;
757
+ let totalRelation = "eq";
758
+ if (typeof rawTotal === "number") {
759
+ total = rawTotal;
760
+ } else if (isRecord(rawTotal)) {
761
+ total = typeof rawTotal.value === "number" ? rawTotal.value : 0;
762
+ totalRelation = rawTotal.relation === "gte" ? "gte" : "eq";
763
+ }
764
+ const rawHits = Array.isArray(hitsContainer.hits) ? hitsContainer.hits : [];
765
+ const rows = rawHits.filter(isRecord).map((hit) => {
766
+ const source = isRecord(hit._source) ? hit._source : {};
767
+ let highlights;
768
+ if (isRecord(hit.highlight)) {
769
+ highlights = {};
770
+ for (const [hKey, hVal] of Object.entries(hit.highlight)) {
771
+ if (Array.isArray(hVal)) {
772
+ highlights[hKey] = hVal.filter((v) => typeof v === "string");
773
+ }
774
+ }
775
+ }
776
+ const sortArr = Array.isArray(hit.sort) ? hit.sort.map(
777
+ (s) => typeof s === "string" || typeof s === "number" || typeof s === "boolean" || s === null ? s : String(s)
778
+ ) : void 0;
779
+ return {
780
+ _id: typeof hit._id === "string" ? hit._id : String(hit._id ?? ""),
781
+ _index: typeof hit._index === "string" ? hit._index : String(hit._index ?? ""),
782
+ _score: typeof hit._score === "number" ? hit._score : null,
783
+ ...sortArr ? { _sort: sortArr } : {},
784
+ source,
785
+ ...highlights ? { highlights } : {}
786
+ };
787
+ });
788
+ const lastRow = rows.length > 0 ? rows[rows.length - 1] : void 0;
789
+ const nextSearchAfter = (lastRow == null ? void 0 : lastRow._sort) && lastRow._sort.length > 0 ? lastRow._sort : void 0;
790
+ const columns = deriveColumns(rows, spec.columns, timeField);
791
+ const aggs = isRecord(res.aggregations) ? res.aggregations : {};
792
+ const histogram = [];
793
+ if (isRecord(aggs.es_time_histogram) && Array.isArray(aggs.es_time_histogram.buckets)) {
794
+ for (const bucket of aggs.es_time_histogram.buckets) {
795
+ if (!isRecord(bucket)) continue;
796
+ const key = typeof bucket.key_as_string === "string" ? bucket.key_as_string : String(bucket.key ?? "");
797
+ histogram.push({
798
+ key,
799
+ ...typeof bucket.key === "number" ? { timestamp: bucket.key } : {},
800
+ count: typeof bucket.doc_count === "number" ? bucket.doc_count : 0
801
+ });
802
+ }
803
+ }
804
+ const series = [];
805
+ if (isRecord(aggs.es_buckets) && Array.isArray(aggs.es_buckets.buckets)) {
806
+ for (const bucket of aggs.es_buckets.buckets) {
807
+ if (!isRecord(bucket)) continue;
808
+ const label = typeof bucket.key_as_string === "string" ? bucket.key_as_string : String(bucket.key ?? "");
809
+ const docCount = typeof bucket.doc_count === "number" ? bucket.doc_count : 0;
810
+ const subMetric = isRecord(bucket.es_metric) ? bucket.es_metric : void 0;
811
+ const metricVal = subMetric && typeof subMetric.value === "number" && Number.isFinite(subMetric.value) ? Number(subMetric.value.toFixed(4)) : docCount;
812
+ series.push({
813
+ key: String(bucket.key ?? label),
814
+ label,
815
+ value: metricVal,
816
+ count: docCount
817
+ });
818
+ }
819
+ }
820
+ let metricValue = null;
821
+ if (spec.visualization === "metric") {
822
+ const metricType = ((_a = spec.chart) == null ? void 0 : _a.metricType) ?? "count";
823
+ if (metricType === "count" || !((_b = spec.chart) == null ? void 0 : _b.metricField)) {
824
+ metricValue = total;
825
+ } else if (isRecord(aggs.es_metric) && typeof aggs.es_metric.value === "number") {
826
+ metricValue = Number.isFinite(aggs.es_metric.value) ? Number(aggs.es_metric.value.toFixed(4)) : null;
827
+ }
828
+ }
829
+ return {
830
+ total,
831
+ totalRelation,
832
+ tookMs,
833
+ visualization: spec.visualization,
834
+ rows,
835
+ columns,
836
+ ...nextSearchAfter ? { nextSearchAfter } : {},
837
+ series,
838
+ histogram,
839
+ metricValue,
840
+ metricLabel: buildDefaultMetricLabel(spec),
841
+ compiledDsl
842
+ };
843
+ }
844
+ // Annotate the CommonJS export names for ESM import in node:
845
+ 0 && (module.exports = {
846
+ buildFieldCapabilityMap,
847
+ compileFilterItem,
848
+ compileKqlToQueryDsl,
849
+ compileSearchRequest,
850
+ compileTimeRange,
851
+ mergeSpecWithOverrides,
852
+ normalizeReportFilters,
853
+ normalizeReportSort,
854
+ normalizeReportSpec,
855
+ normalizeTimeRange,
856
+ parseSearchResponse,
857
+ resolveAggregatableField
858
+ });