plugin-elastic-report 0.7.0 → 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.
- package/client-v2.d.ts +2 -2
- package/client-v2.js +1 -1
- package/client.d.ts +2 -2
- package/client.js +1 -1
- package/{ai → dist/ai}/skills/elastic-report/SKILLS.md +52 -34
- package/dist/client-v2/851.35e416bfb3fb565f.js +10 -0
- package/dist/client-v2/877.81b10beb7b7f4bd7.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +2 -1
- package/dist/locale/en-US.json +213 -198
- package/dist/locale/vi-VN.json +213 -198
- package/dist/locale/zh-CN.json +213 -198
- package/dist/node_modules/@elastic/elasticsearch/package.json +1 -1
- package/dist/server/mcp/ElasticReportToolCatalog.js +6 -4
- package/dist/server/plugin.js +4 -4
- package/dist/server/report-block-tools.js +340 -0
- package/dist/server/services/ElasticReportService.js +12 -6
- package/dist/shared/constants.js +9 -0
- package/package.json +2 -3
- package/server.d.ts +2 -2
- package/server.js +1 -1
- package/src/ai/skills/elastic-report/SKILLS.md +52 -0
- package/src/client/index.tsx +34 -34
- package/src/client/workflow-instructions.tsx +44 -44
- package/src/client-v2/__tests__/elastic-report.test.tsx +879 -791
- package/src/client-v2/__tests__/kql-autocomplete.test.ts +178 -178
- package/src/client-v2/api.ts +320 -320
- package/src/client-v2/bus/DashboardFilterBus.ts +212 -212
- package/src/client-v2/components/ElasticBasicCharts.tsx +652 -652
- package/src/client-v2/components/ElasticFilterBar.tsx +372 -372
- package/src/client-v2/components/ElasticKqlInput.tsx +421 -421
- package/src/client-v2/components/ElasticReportBlock.tsx +306 -306
- package/src/client-v2/components/ElasticReportRenderer.tsx +1257 -1257
- package/src/client-v2/components/HighlightText.tsx +50 -50
- package/src/client-v2/components/ReportSelect.tsx +85 -85
- package/src/client-v2/index.tsx +14 -14
- package/src/client-v2/locale.ts +23 -23
- package/src/client-v2/models/ElasticFilterBarBlockModel.tsx +115 -115
- package/src/client-v2/models/ElasticReportBlockModel.tsx +118 -118
- package/src/client-v2/pages/ElasticReportManager.tsx +1453 -1453
- package/src/client-v2/plugin.tsx +107 -72
- package/src/client-v2/report-destination.tsx +126 -0
- package/src/client-v2/utils/kqlAutocomplete.ts +861 -861
- package/src/client-v2/workflow/instructions.tsx +149 -149
- package/src/index.ts +11 -11
- package/src/locale/en-US.json +213 -198
- package/src/locale/vi-VN.json +213 -198
- package/src/locale/zh-CN.json +213 -198
- package/src/server/__tests__/ai-blocks.integration.test.ts +292 -0
- package/src/server/__tests__/query-compiler.test.ts +620 -620
- package/src/server/__tests__/report-mcp-acl.integration.test.ts +645 -645
- package/src/server/acl-guard.ts +194 -194
- package/src/server/collections/es-report-data-views.ts +64 -64
- package/src/server/collections/es-reports.ts +80 -80
- package/src/server/index.ts +15 -15
- package/src/server/mcp/ElasticReportMcpServer.ts +391 -391
- package/src/server/mcp/ElasticReportToolCatalog.ts +667 -663
- package/src/server/plugin.ts +599 -599
- package/src/server/report-block-tools.ts +400 -0
- package/src/server/services/ElasticReportService.ts +494 -486
- package/src/server/services/ElasticsearchConnectionService.ts +728 -728
- package/src/server/services/ReportExportService.ts +282 -282
- package/src/server/services/ReportQueryCompiler.ts +1552 -1552
- package/src/server/workflow/ElasticReportQueryInstruction.ts +124 -124
- package/src/shared/constants.ts +39 -36
- package/src/shared/types.ts +272 -272
- package/dist/client-v2/851.142c93fc6d666b28.js +0 -10
- package/dist/client-v2/877.f4095a3dbcadf98f.js +0 -10
- package/dist/node_modules/@elastic/elasticsearch/node_modules/.bin/arrow2csv +0 -15
- package/dist/node_modules/@elastic/elasticsearch/node_modules/.bin/arrow2csv.cmd +0 -7
package/src/client-v2/api.ts
CHANGED
|
@@ -1,320 +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
|
-
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
|
-
}
|
|
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
|
+
}
|