plugin-elastic-report 0.1.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/client-v2.d.ts +2 -2
  2. package/client-v2.js +1 -1
  3. package/client.d.ts +2 -2
  4. package/client.js +1 -1
  5. package/dist/ai/skills/elastic-report/SKILLS.md +52 -0
  6. package/dist/client/357.0dd385384cab5dbf.js +10 -0
  7. package/dist/client/index.js +1 -1
  8. package/dist/client-v2/851.35e416bfb3fb565f.js +10 -0
  9. package/dist/client-v2/857.a5bf9637d5fd4ef5.js +10 -0
  10. package/dist/client-v2/877.81b10beb7b7f4bd7.js +10 -0
  11. package/dist/client-v2/index.js +1 -1
  12. package/dist/externalVersion.js +5 -3
  13. package/dist/locale/en-US.json +213 -99
  14. package/dist/locale/vi-VN.json +213 -99
  15. package/dist/locale/zh-CN.json +213 -99
  16. package/dist/node_modules/@elastic/elasticsearch/package.json +1 -1
  17. package/dist/server/acl-guard.js +97 -25
  18. package/dist/server/collections/es-report-data-views.js +6 -0
  19. package/dist/server/collections/es-reports.js +6 -0
  20. package/dist/server/mcp/ElasticReportToolCatalog.js +238 -14
  21. package/dist/server/plugin.js +156 -22
  22. package/dist/server/report-block-tools.js +340 -0
  23. package/dist/server/services/ElasticReportService.js +137 -17
  24. package/dist/server/services/ElasticsearchConnectionService.js +274 -34
  25. package/dist/server/services/ReportExportService.js +243 -0
  26. package/dist/server/services/ReportQueryCompiler.js +532 -91
  27. package/dist/server/workflow/ElasticReportQueryInstruction.js +123 -0
  28. package/dist/shared/constants.js +26 -2
  29. package/package.json +3 -3
  30. package/server.d.ts +2 -2
  31. package/server.js +1 -1
  32. package/src/ai/skills/elastic-report/SKILLS.md +52 -0
  33. package/src/client/index.tsx +34 -19
  34. package/src/client/workflow-instructions.tsx +44 -0
  35. package/src/client-v2/__tests__/elastic-report.test.tsx +879 -311
  36. package/src/client-v2/__tests__/kql-autocomplete.test.ts +178 -0
  37. package/src/client-v2/api.ts +320 -224
  38. package/src/client-v2/bus/DashboardFilterBus.ts +212 -0
  39. package/src/client-v2/components/ElasticBasicCharts.tsx +652 -399
  40. package/src/client-v2/components/ElasticFilterBar.tsx +372 -0
  41. package/src/client-v2/components/ElasticKqlInput.tsx +421 -0
  42. package/src/client-v2/components/ElasticReportBlock.tsx +306 -160
  43. package/src/client-v2/components/ElasticReportRenderer.tsx +1257 -707
  44. package/src/client-v2/components/HighlightText.tsx +50 -50
  45. package/src/client-v2/components/ReportSelect.tsx +85 -72
  46. package/src/client-v2/index.tsx +14 -14
  47. package/src/client-v2/locale.ts +23 -23
  48. package/src/client-v2/models/ElasticFilterBarBlockModel.tsx +115 -0
  49. package/src/client-v2/models/ElasticReportBlockModel.tsx +118 -85
  50. package/src/client-v2/pages/ElasticReportManager.tsx +1453 -1047
  51. package/src/client-v2/plugin.tsx +107 -52
  52. package/src/client-v2/report-destination.tsx +126 -0
  53. package/src/client-v2/utils/kqlAutocomplete.ts +861 -0
  54. package/src/client-v2/workflow/instructions.tsx +149 -0
  55. package/src/index.ts +11 -11
  56. package/src/locale/en-US.json +213 -99
  57. package/src/locale/vi-VN.json +213 -99
  58. package/src/locale/zh-CN.json +213 -99
  59. package/src/server/__tests__/ai-blocks.integration.test.ts +292 -0
  60. package/src/server/__tests__/query-compiler.test.ts +620 -231
  61. package/src/server/__tests__/report-mcp-acl.integration.test.ts +645 -377
  62. package/src/server/acl-guard.ts +194 -103
  63. package/src/server/collections/es-report-data-views.ts +64 -58
  64. package/src/server/collections/es-reports.ts +80 -74
  65. package/src/server/index.ts +15 -15
  66. package/src/server/mcp/ElasticReportMcpServer.ts +391 -391
  67. package/src/server/mcp/ElasticReportToolCatalog.ts +667 -417
  68. package/src/server/plugin.ts +599 -431
  69. package/src/server/report-block-tools.ts +400 -0
  70. package/src/server/services/ElasticReportService.ts +494 -326
  71. package/src/server/services/ElasticsearchConnectionService.ts +728 -401
  72. package/src/server/services/ReportExportService.ts +282 -0
  73. package/src/server/services/ReportQueryCompiler.ts +1552 -1017
  74. package/src/server/workflow/ElasticReportQueryInstruction.ts +124 -0
  75. package/src/shared/constants.ts +39 -29
  76. package/src/shared/types.ts +272 -175
  77. package/ai/skills/elastic-report/SKILLS.md +0 -32
  78. package/dist/client-v2/851.488fdbf5bc5b6a36.js +0 -10
  79. package/dist/node_modules/@elastic/elasticsearch/node_modules/.bin/arrow2csv +0 -15
  80. package/dist/node_modules/@elastic/elasticsearch/node_modules/.bin/arrow2csv.cmd +0 -7
@@ -1,224 +1,320 @@
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 { DATA_VIEWS_COLLECTION, REPORTS_COLLECTION, SERVICE_RESOURCE } from '../shared/constants';
11
- import type {
12
- EsConnectionSummary,
13
- EsDataViewRecord,
14
- EsFieldCapability,
15
- EsReportRecord,
16
- ID,
17
- ReportExecutionResult,
18
- ReportRuntimeOverrides,
19
- ReportSpec,
20
- } from '../shared/types';
21
-
22
- export interface ApiClientLike {
23
- request: (config: {
24
- url: string;
25
- method?: 'get' | 'post';
26
- params?: Record<string, unknown>;
27
- data?: Record<string, unknown>;
28
- }) => Promise<unknown>;
29
- }
30
-
31
- export interface McpInfoPayload {
32
- serverName: string;
33
- endpointPath: string;
34
- internalUrl: string;
35
- useUserContext: boolean;
36
- tools: Array<{
37
- name: string;
38
- description: string;
39
- inputSchema: Record<string, unknown>;
40
- }>;
41
- }
42
-
43
- function isRecord(value: unknown): value is Record<string, unknown> {
44
- return typeof value === 'object' && value !== null && !Array.isArray(value);
45
- }
46
-
47
- export function unwrapApiData<T>(raw: unknown): T {
48
- if (!isRecord(raw)) return raw as T;
49
- const outer = raw.data;
50
- if (isRecord(outer) && 'data' in outer) {
51
- return outer.data as T;
52
- }
53
- return (outer ?? raw) as T;
54
- }
55
-
56
- export async function fetchEsConnections(api: ApiClientLike): Promise<EsConnectionSummary[]> {
57
- const res = await api.request({
58
- url: `${SERVICE_RESOURCE}:listConnections`,
59
- method: 'get',
60
- });
61
- const data = unwrapApiData<unknown>(res);
62
- return Array.isArray(data) ? (data as EsConnectionSummary[]) : [];
63
- }
64
-
65
- export async function fetchEsIndices(api: ApiClientLike, dataSourceKey: string, indexPattern = '*'): Promise<string[]> {
66
- const res = await api.request({
67
- url: `${SERVICE_RESOURCE}:listIndices`,
68
- method: 'post',
69
- data: { dataSourceKey, indexPattern },
70
- });
71
- const data = unwrapApiData<unknown>(res);
72
- return Array.isArray(data) ? data.filter((v): v is string => typeof v === 'string') : [];
73
- }
74
-
75
- export async function fetchDataViews(api: ApiClientLike): Promise<EsDataViewRecord[]> {
76
- const res = await api.request({
77
- url: `${DATA_VIEWS_COLLECTION}:list`,
78
- method: 'get',
79
- params: { pageSize: 200, sort: ['-id'] },
80
- });
81
- const data = unwrapApiData<unknown>(res);
82
- return Array.isArray(data) ? (data as EsDataViewRecord[]) : [];
83
- }
84
-
85
- export async function saveDataView(
86
- api: ApiClientLike,
87
- payload: {
88
- id?: ID;
89
- title: string;
90
- dataSourceKey: string;
91
- indexPattern: string;
92
- timeField?: string | null;
93
- description?: string | null;
94
- },
95
- ): Promise<EsDataViewRecord> {
96
- const res = await api.request({
97
- url: `${SERVICE_RESOURCE}:upsertDataView`,
98
- method: 'post',
99
- data: payload as Record<string, unknown>,
100
- });
101
- return unwrapApiData<EsDataViewRecord>(res);
102
- }
103
-
104
- export async function removeDataView(api: ApiClientLike, id: ID): Promise<void> {
105
- await api.request({
106
- url: `${DATA_VIEWS_COLLECTION}:destroy`,
107
- method: 'post',
108
- params: { filterByTk: id },
109
- });
110
- }
111
-
112
- export async function fetchDataViewFields(
113
- api: ApiClientLike,
114
- dataViewId: ID,
115
- refresh = false,
116
- ): Promise<{ dataView: EsDataViewRecord; fields: EsFieldCapability[] }> {
117
- const res = await api.request({
118
- url: `${SERVICE_RESOURCE}:inspectFields`,
119
- method: 'post',
120
- data: { dataViewId, refresh },
121
- });
122
- return unwrapApiData<{ dataView: EsDataViewRecord; fields: EsFieldCapability[] }>(res);
123
- }
124
-
125
- export async function fetchAllReports(api: ApiClientLike): Promise<EsReportRecord[]> {
126
- const res = await api.request({
127
- url: `${REPORTS_COLLECTION}:list`,
128
- method: 'get',
129
- params: { pageSize: 200, appends: ['dataView'], sort: ['-updatedAt', '-id'] },
130
- });
131
- const data = unwrapApiData<unknown>(res);
132
- return Array.isArray(data) ? (data as EsReportRecord[]) : [];
133
- }
134
-
135
- export async function fetchPublishedReports(api: ApiClientLike, search?: string): Promise<EsReportRecord[]> {
136
- const res = await api.request({
137
- url: `${SERVICE_RESOURCE}:listPublishedReports`,
138
- method: 'get',
139
- params: search ? { search } : undefined,
140
- });
141
- const data = unwrapApiData<unknown>(res);
142
- return Array.isArray(data) ? (data as EsReportRecord[]) : [];
143
- }
144
-
145
- export async function persistReport(
146
- api: ApiClientLike,
147
- payload: {
148
- id?: ID;
149
- title: string;
150
- description?: string | null;
151
- dataViewId: ID;
152
- spec: ReportSpec;
153
- published: boolean;
154
- },
155
- ): Promise<EsReportRecord> {
156
- const res = await api.request({
157
- url: `${SERVICE_RESOURCE}:saveReport`,
158
- method: 'post',
159
- data: payload as unknown as Record<string, unknown>,
160
- });
161
- return unwrapApiData<EsReportRecord>(res);
162
- }
163
-
164
- export async function removeReport(api: ApiClientLike, id: ID): Promise<void> {
165
- await api.request({
166
- url: `${REPORTS_COLLECTION}:destroy`,
167
- method: 'post',
168
- params: { filterByTk: id },
169
- });
170
- }
171
-
172
- export async function executeReportPreview(
173
- api: ApiClientLike,
174
- dataViewId: ID,
175
- spec: ReportSpec,
176
- overrides?: ReportRuntimeOverrides,
177
- ): Promise<{ dataView: EsDataViewRecord; result: ReportExecutionResult }> {
178
- const res = await api.request({
179
- url: `${SERVICE_RESOURCE}:previewReport`,
180
- method: 'post',
181
- data: {
182
- dataViewId,
183
- spec,
184
- ...(overrides ? { overrides } : {}),
185
- } as unknown as Record<string, unknown>,
186
- });
187
- return unwrapApiData<{ dataView: EsDataViewRecord; result: ReportExecutionResult }>(res);
188
- }
189
-
190
- export async function fetchReportMeta(
191
- api: ApiClientLike,
192
- reportId: ID,
193
- ): Promise<{ report: EsReportRecord; dataView: EsDataViewRecord; fields: EsFieldCapability[] }> {
194
- const res = await api.request({
195
- url: `${SERVICE_RESOURCE}:getReportMeta`,
196
- method: 'get',
197
- params: { reportId },
198
- });
199
- return unwrapApiData<{ report: EsReportRecord; dataView: EsDataViewRecord; fields: EsFieldCapability[] }>(res);
200
- }
201
-
202
- export async function executeSavedReport(
203
- api: ApiClientLike,
204
- reportId: ID,
205
- overrides?: ReportRuntimeOverrides,
206
- ): Promise<{ report: EsReportRecord; dataView: EsDataViewRecord; result: ReportExecutionResult }> {
207
- const res = await api.request({
208
- url: `${SERVICE_RESOURCE}:runReport`,
209
- method: 'post',
210
- data: {
211
- reportId,
212
- ...(overrides ? { overrides } : {}),
213
- },
214
- });
215
- return unwrapApiData<{ report: EsReportRecord; dataView: EsDataViewRecord; result: ReportExecutionResult }>(res);
216
- }
217
-
218
- export async function fetchMcpInfo(api: ApiClientLike): Promise<McpInfoPayload> {
219
- const res = await api.request({
220
- url: `${SERVICE_RESOURCE}:getMcpInfo`,
221
- method: 'get',
222
- });
223
- return unwrapApiData<McpInfoPayload>(res);
224
- }
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 { DATA_VIEWS_COLLECTION, REPORTS_COLLECTION, SERVICE_RESOURCE } from '../shared/constants';
11
+ import type {
12
+ EsConnectionHealthResult,
13
+ EsConnectionSummary,
14
+ EsDataViewRecord,
15
+ EsFieldCapability,
16
+ EsReportRecord,
17
+ ExportFormat,
18
+ FieldTopValuesResult,
19
+ ID,
20
+ ReportExecutionResult,
21
+ ReportRuntimeOverrides,
22
+ ReportSpec,
23
+ RoleSummary,
24
+ } from '../shared/types';
25
+
26
+ export interface ApiClientLike {
27
+ request: (config: {
28
+ url: string;
29
+ method?: 'get' | 'post';
30
+ params?: Record<string, unknown>;
31
+ data?: Record<string, unknown>;
32
+ responseType?: 'blob' | 'text' | 'json';
33
+ }) => Promise<unknown>;
34
+ }
35
+
36
+ export interface McpInfoPayload {
37
+ serverName: string;
38
+ endpointPath: string;
39
+ internalUrl: string;
40
+ useUserContext: boolean;
41
+ tools: Array<{
42
+ name: string;
43
+ description: string;
44
+ inputSchema: Record<string, unknown>;
45
+ }>;
46
+ }
47
+
48
+ function isRecord(value: unknown): value is Record<string, unknown> {
49
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
50
+ }
51
+
52
+ export function unwrapApiData<T>(raw: unknown): T {
53
+ if (!isRecord(raw)) return raw as T;
54
+ const outer = raw.data;
55
+ if (isRecord(outer) && 'data' in outer) {
56
+ return outer.data as T;
57
+ }
58
+ return (outer ?? raw) as T;
59
+ }
60
+
61
+ export async function fetchEsConnections(api: ApiClientLike): Promise<EsConnectionSummary[]> {
62
+ const res = await api.request({
63
+ url: `${SERVICE_RESOURCE}:listConnections`,
64
+ method: 'get',
65
+ });
66
+ const data = unwrapApiData<unknown>(res);
67
+ return Array.isArray(data) ? (data as EsConnectionSummary[]) : [];
68
+ }
69
+
70
+ export async function testEsConnection(
71
+ api: ApiClientLike,
72
+ dataSourceKey: string,
73
+ indexPattern = '*',
74
+ ): Promise<EsConnectionHealthResult> {
75
+ const res = await api.request({
76
+ url: `${SERVICE_RESOURCE}:testConnection`,
77
+ method: 'post',
78
+ data: { dataSourceKey, indexPattern },
79
+ });
80
+ return unwrapApiData<EsConnectionHealthResult>(res);
81
+ }
82
+
83
+ export async function fetchAvailableRoles(api: ApiClientLike): Promise<RoleSummary[]> {
84
+ const res = await api.request({
85
+ url: `${SERVICE_RESOURCE}:listRoles`,
86
+ method: 'get',
87
+ });
88
+ const data = unwrapApiData<unknown>(res);
89
+ return Array.isArray(data) ? (data as RoleSummary[]) : [];
90
+ }
91
+
92
+ export async function fetchEsIndices(api: ApiClientLike, dataSourceKey: string, indexPattern = '*'): Promise<string[]> {
93
+ const res = await api.request({
94
+ url: `${SERVICE_RESOURCE}:listIndices`,
95
+ method: 'post',
96
+ data: { dataSourceKey, indexPattern },
97
+ });
98
+ const data = unwrapApiData<unknown>(res);
99
+ return Array.isArray(data) ? data.filter((v): v is string => typeof v === 'string') : [];
100
+ }
101
+
102
+ export async function fetchDataViews(api: ApiClientLike): Promise<EsDataViewRecord[]> {
103
+ const res = await api.request({
104
+ url: `${DATA_VIEWS_COLLECTION}:list`,
105
+ method: 'get',
106
+ params: { pageSize: 200, sort: ['-id'] },
107
+ });
108
+ const data = unwrapApiData<unknown>(res);
109
+ return Array.isArray(data) ? (data as EsDataViewRecord[]) : [];
110
+ }
111
+
112
+ export async function saveDataView(
113
+ api: ApiClientLike,
114
+ payload: {
115
+ id?: ID;
116
+ title: string;
117
+ dataSourceKey: string;
118
+ indexPattern: string;
119
+ timeField?: string | null;
120
+ description?: string | null;
121
+ allowedRoles?: string[];
122
+ },
123
+ ): Promise<EsDataViewRecord> {
124
+ const res = await api.request({
125
+ url: `${SERVICE_RESOURCE}:upsertDataView`,
126
+ method: 'post',
127
+ data: payload as Record<string, unknown>,
128
+ });
129
+ return unwrapApiData<EsDataViewRecord>(res);
130
+ }
131
+
132
+ export async function removeDataView(api: ApiClientLike, id: ID): Promise<void> {
133
+ await api.request({
134
+ url: `${DATA_VIEWS_COLLECTION}:destroy`,
135
+ method: 'post',
136
+ params: { filterByTk: id },
137
+ });
138
+ }
139
+
140
+ export async function fetchDataViewFields(
141
+ api: ApiClientLike,
142
+ dataViewId: ID,
143
+ refresh = false,
144
+ ): Promise<{ dataView: EsDataViewRecord; fields: EsFieldCapability[] }> {
145
+ const res = await api.request({
146
+ url: `${SERVICE_RESOURCE}:inspectFields`,
147
+ method: 'post',
148
+ data: { dataViewId, refresh },
149
+ });
150
+ return unwrapApiData<{ dataView: EsDataViewRecord; fields: EsFieldCapability[] }>(res);
151
+ }
152
+
153
+ export async function fetchAllReports(api: ApiClientLike): Promise<EsReportRecord[]> {
154
+ const res = await api.request({
155
+ url: `${REPORTS_COLLECTION}:list`,
156
+ method: 'get',
157
+ params: { pageSize: 200, appends: ['dataView'], sort: ['-updatedAt', '-id'] },
158
+ });
159
+ const data = unwrapApiData<unknown>(res);
160
+ return Array.isArray(data) ? (data as EsReportRecord[]) : [];
161
+ }
162
+
163
+ export async function fetchPublishedReports(api: ApiClientLike, search?: string): Promise<EsReportRecord[]> {
164
+ const res = await api.request({
165
+ url: `${SERVICE_RESOURCE}:listPublishedReports`,
166
+ method: 'get',
167
+ params: search ? { search } : undefined,
168
+ });
169
+ const data = unwrapApiData<unknown>(res);
170
+ return Array.isArray(data) ? (data as EsReportRecord[]) : [];
171
+ }
172
+
173
+ export async function persistReport(
174
+ api: ApiClientLike,
175
+ payload: {
176
+ id?: ID;
177
+ title: string;
178
+ description?: string | null;
179
+ dataViewId: ID;
180
+ spec: ReportSpec;
181
+ published: boolean;
182
+ allowedRoles?: string[];
183
+ },
184
+ ): Promise<EsReportRecord> {
185
+ const res = await api.request({
186
+ url: `${SERVICE_RESOURCE}:saveReport`,
187
+ method: 'post',
188
+ data: payload as unknown as Record<string, unknown>,
189
+ });
190
+ return unwrapApiData<EsReportRecord>(res);
191
+ }
192
+
193
+ export async function removeReport(api: ApiClientLike, id: ID): Promise<void> {
194
+ await api.request({
195
+ url: `${REPORTS_COLLECTION}:destroy`,
196
+ method: 'post',
197
+ params: { filterByTk: id },
198
+ });
199
+ }
200
+
201
+ export async function executeReportPreview(
202
+ api: ApiClientLike,
203
+ dataViewId: ID,
204
+ spec: ReportSpec,
205
+ overrides?: ReportRuntimeOverrides,
206
+ ): Promise<{ dataView: EsDataViewRecord; result: ReportExecutionResult }> {
207
+ const res = await api.request({
208
+ url: `${SERVICE_RESOURCE}:previewReport`,
209
+ method: 'post',
210
+ data: {
211
+ dataViewId,
212
+ spec,
213
+ ...(overrides ? { overrides } : {}),
214
+ } as unknown as Record<string, unknown>,
215
+ });
216
+ return unwrapApiData<{ dataView: EsDataViewRecord; result: ReportExecutionResult }>(res);
217
+ }
218
+
219
+ export async function fetchReportMeta(
220
+ api: ApiClientLike,
221
+ reportId: ID,
222
+ ): Promise<{ report: EsReportRecord; dataView: EsDataViewRecord; fields: EsFieldCapability[] }> {
223
+ const res = await api.request({
224
+ url: `${SERVICE_RESOURCE}:getReportMeta`,
225
+ method: 'get',
226
+ params: { reportId },
227
+ });
228
+ return unwrapApiData<{ report: EsReportRecord; dataView: EsDataViewRecord; fields: EsFieldCapability[] }>(res);
229
+ }
230
+
231
+ export async function executeSavedReport(
232
+ api: ApiClientLike,
233
+ reportId: ID,
234
+ overrides?: ReportRuntimeOverrides,
235
+ ): Promise<{ report: EsReportRecord; dataView: EsDataViewRecord; result: ReportExecutionResult }> {
236
+ const res = await api.request({
237
+ url: `${SERVICE_RESOURCE}:runReport`,
238
+ method: 'post',
239
+ data: {
240
+ reportId,
241
+ ...(overrides ? { overrides } : {}),
242
+ },
243
+ });
244
+ return unwrapApiData<{ report: EsReportRecord; dataView: EsDataViewRecord; result: ReportExecutionResult }>(res);
245
+ }
246
+
247
+ export async function fetchMcpInfo(api: ApiClientLike): Promise<McpInfoPayload> {
248
+ const res = await api.request({
249
+ url: `${SERVICE_RESOURCE}:getMcpInfo`,
250
+ method: 'get',
251
+ });
252
+ return unwrapApiData<McpInfoPayload>(res);
253
+ }
254
+
255
+ export async function fetchFieldTopValues(
256
+ api: ApiClientLike,
257
+ params: {
258
+ dataViewId?: ID;
259
+ reportId?: ID;
260
+ field: string;
261
+ size?: number;
262
+ overrides?: ReportRuntimeOverrides;
263
+ },
264
+ ): Promise<FieldTopValuesResult> {
265
+ const res = await api.request({
266
+ url: `${SERVICE_RESOURCE}:inspectTopValues`,
267
+ method: 'post',
268
+ data: params as unknown as Record<string, unknown>,
269
+ });
270
+ return unwrapApiData<FieldTopValuesResult>(res);
271
+ }
272
+
273
+ export async function downloadReportExport(
274
+ api: ApiClientLike,
275
+ params: {
276
+ reportId: ID;
277
+ reportTitle?: string;
278
+ format: ExportFormat;
279
+ overrides?: ReportRuntimeOverrides;
280
+ columns?: string[];
281
+ },
282
+ ): Promise<void> {
283
+ const res = await api.request({
284
+ url: `${SERVICE_RESOURCE}:exportReport`,
285
+ method: 'post',
286
+ responseType: 'blob',
287
+ data: {
288
+ reportId: params.reportId,
289
+ format: params.format,
290
+ ...(params.overrides ? { overrides: params.overrides } : {}),
291
+ ...(params.columns && params.columns.length > 0 ? { columns: params.columns } : {}),
292
+ },
293
+ });
294
+
295
+ const rawPayload = isRecord(res) && 'data' in res ? res.data : res;
296
+ const mime = params.format === 'ndjson' ? 'application/x-ndjson;charset=utf-8' : 'text/csv;charset=utf-8';
297
+ const blob =
298
+ typeof Blob !== 'undefined' && rawPayload instanceof Blob
299
+ ? rawPayload
300
+ : new Blob([typeof rawPayload === 'string' ? rawPayload : JSON.stringify(rawPayload ?? '')], { type: mime });
301
+
302
+ if (typeof window === 'undefined' || typeof document === 'undefined' || typeof URL.createObjectURL !== 'function') {
303
+ return;
304
+ }
305
+
306
+ const slug =
307
+ (params.reportTitle || `report-${String(params.reportId)}`)
308
+ .toLowerCase()
309
+ .replace(/[^a-z0-9_-]+/g, '-')
310
+ .replace(/^-+|-+$/g, '')
311
+ .slice(0, 48) || 'report';
312
+ const objectUrl = URL.createObjectURL(blob);
313
+ const anchor = document.createElement('a');
314
+ anchor.href = objectUrl;
315
+ anchor.download = `${slug}.${params.format}`;
316
+ document.body.appendChild(anchor);
317
+ anchor.click();
318
+ document.body.removeChild(anchor);
319
+ URL.revokeObjectURL(objectUrl);
320
+ }