plugin-elastic-report 0.1.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ai/skills/elastic-report/SKILLS.md +5 -3
- package/dist/client/357.0dd385384cab5dbf.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/851.142c93fc6d666b28.js +10 -0
- package/dist/client-v2/857.a5bf9637d5fd4ef5.js +10 -0
- package/dist/client-v2/877.f4095a3dbcadf98f.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +3 -2
- package/dist/locale/en-US.json +100 -1
- package/dist/locale/vi-VN.json +100 -1
- package/dist/locale/zh-CN.json +100 -1
- package/dist/node_modules/@elastic/elasticsearch/package.json +1 -1
- package/dist/server/acl-guard.js +97 -25
- package/dist/server/collections/es-report-data-views.js +6 -0
- package/dist/server/collections/es-reports.js +6 -0
- package/dist/server/mcp/ElasticReportToolCatalog.js +232 -10
- package/dist/server/plugin.js +153 -19
- package/dist/server/services/ElasticReportService.js +125 -11
- package/dist/server/services/ElasticsearchConnectionService.js +274 -34
- package/dist/server/services/ReportExportService.js +243 -0
- package/dist/server/services/ReportQueryCompiler.js +532 -91
- package/dist/server/workflow/ElasticReportQueryInstruction.js +123 -0
- package/dist/shared/constants.js +17 -2
- package/package.json +3 -2
- package/src/client/index.tsx +16 -1
- package/src/client/workflow-instructions.tsx +44 -0
- package/src/client-v2/__tests__/elastic-report.test.tsx +519 -39
- package/src/client-v2/__tests__/kql-autocomplete.test.ts +178 -0
- package/src/client-v2/api.ts +96 -0
- package/src/client-v2/bus/DashboardFilterBus.ts +212 -0
- package/src/client-v2/components/ElasticBasicCharts.tsx +345 -92
- package/src/client-v2/components/ElasticFilterBar.tsx +372 -0
- package/src/client-v2/components/ElasticKqlInput.tsx +421 -0
- package/src/client-v2/components/ElasticReportBlock.tsx +157 -11
- package/src/client-v2/components/ElasticReportRenderer.tsx +621 -71
- package/src/client-v2/components/ReportSelect.tsx +39 -26
- package/src/client-v2/models/ElasticFilterBarBlockModel.tsx +115 -0
- package/src/client-v2/models/ElasticReportBlockModel.tsx +36 -3
- package/src/client-v2/pages/ElasticReportManager.tsx +432 -26
- package/src/client-v2/plugin.tsx +20 -0
- package/src/client-v2/utils/kqlAutocomplete.ts +861 -0
- package/src/client-v2/workflow/instructions.tsx +149 -0
- package/src/locale/en-US.json +100 -1
- package/src/locale/vi-VN.json +100 -1
- package/src/locale/zh-CN.json +100 -1
- package/src/server/__tests__/query-compiler.test.ts +390 -1
- package/src/server/__tests__/report-mcp-acl.integration.test.ts +271 -3
- package/src/server/acl-guard.ts +119 -28
- package/src/server/collections/es-report-data-views.ts +6 -0
- package/src/server/collections/es-reports.ts +6 -0
- package/src/server/mcp/ElasticReportToolCatalog.ts +255 -9
- package/src/server/plugin.ts +189 -21
- package/src/server/services/ElasticReportService.ts +165 -5
- package/src/server/services/ElasticsearchConnectionService.ts +375 -48
- package/src/server/services/ReportExportService.ts +282 -0
- package/src/server/services/ReportQueryCompiler.ts +629 -94
- package/src/server/workflow/ElasticReportQueryInstruction.ts +124 -0
- package/src/shared/constants.ts +7 -0
- package/src/shared/types.ts +98 -1
- package/dist/client-v2/851.488fdbf5bc5b6a36.js +0 -10
|
@@ -0,0 +1,421 @@
|
|
|
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 { SearchOutlined } from '@ant-design/icons';
|
|
11
|
+
import { AutoComplete, Input, Space, Spin, Tag, Typography } from 'antd';
|
|
12
|
+
import type { AutoCompleteProps, InputRef } from 'antd';
|
|
13
|
+
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
14
|
+
import type { EsFieldCapability, FieldTopValueBucket, FieldTopValuesResult, ReportRowHit } from '../../shared/types';
|
|
15
|
+
import { useT } from '../locale';
|
|
16
|
+
import {
|
|
17
|
+
type KqlSuggestionCategory,
|
|
18
|
+
type KqlSuggestionItem,
|
|
19
|
+
buildKqlSuggestions,
|
|
20
|
+
canFetchFieldTopValues,
|
|
21
|
+
} from '../utils/kqlAutocomplete';
|
|
22
|
+
|
|
23
|
+
const { Text } = Typography;
|
|
24
|
+
|
|
25
|
+
const RUN_FREETEXT_SENTINEL = '__es_kql_run_freetext__';
|
|
26
|
+
const LOADING_VALUES_SENTINEL = '__es_kql_loading_values__';
|
|
27
|
+
|
|
28
|
+
export interface ElasticKqlInputProps {
|
|
29
|
+
id?: string;
|
|
30
|
+
value?: string;
|
|
31
|
+
placeholder?: string;
|
|
32
|
+
ariaLabel?: string;
|
|
33
|
+
style?: React.CSSProperties;
|
|
34
|
+
disabled?: boolean;
|
|
35
|
+
fields?: EsFieldCapability[];
|
|
36
|
+
sampleRows?: ReportRowHit[];
|
|
37
|
+
onInspectTopValues?: (field: string) => Promise<FieldTopValuesResult>;
|
|
38
|
+
onFocusEnsureFields?: () => void;
|
|
39
|
+
onChange?: (nextValue: string) => void;
|
|
40
|
+
onClear?: () => void;
|
|
41
|
+
onSubmit?: (submittedValue: string) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const ElasticKqlInput: React.FC<ElasticKqlInputProps> = ({
|
|
45
|
+
id,
|
|
46
|
+
value = '',
|
|
47
|
+
placeholder,
|
|
48
|
+
ariaLabel,
|
|
49
|
+
style,
|
|
50
|
+
disabled = false,
|
|
51
|
+
fields = [],
|
|
52
|
+
sampleRows,
|
|
53
|
+
onInspectTopValues,
|
|
54
|
+
onFocusEnsureFields,
|
|
55
|
+
onChange,
|
|
56
|
+
onClear,
|
|
57
|
+
onSubmit,
|
|
58
|
+
}) => {
|
|
59
|
+
const t = useT();
|
|
60
|
+
const inputRef = useRef<InputRef>(null);
|
|
61
|
+
|
|
62
|
+
const [open, setOpen] = useState(false);
|
|
63
|
+
const [cursorPos, setCursorPos] = useState<number>(value.length);
|
|
64
|
+
const [topValuesCache, setTopValuesCache] = useState<Record<string, FieldTopValueBucket[]>>({});
|
|
65
|
+
const [loadingField, setLoadingField] = useState<string | null>(null);
|
|
66
|
+
|
|
67
|
+
const inFlightFieldsRef = useRef<Set<string>>(new Set());
|
|
68
|
+
const onInspectRef = useRef(onInspectTopValues);
|
|
69
|
+
onInspectRef.current = onInspectTopValues;
|
|
70
|
+
|
|
71
|
+
const justSelectedOptionRef = useRef(false);
|
|
72
|
+
const keepOpenAfterSelectRef = useRef(false);
|
|
73
|
+
const lastCompositionEndAtRef = useRef<number | null>(null);
|
|
74
|
+
|
|
75
|
+
// Reset cached top values when field schema changes (e.g. switching Data View or Report)
|
|
76
|
+
const fieldsSignature = useMemo(() => fields.map((f) => `${f.name}:${f.type}`).join('|'), [fields]);
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
setTopValuesCache({});
|
|
79
|
+
inFlightFieldsRef.current.clear();
|
|
80
|
+
setLoadingField(null);
|
|
81
|
+
}, [fieldsSignature]);
|
|
82
|
+
|
|
83
|
+
// Clamp cursor position if external value shrinks
|
|
84
|
+
const effectiveCursorPos = Math.min(cursorPos, value.length);
|
|
85
|
+
|
|
86
|
+
const prelim = useMemo(
|
|
87
|
+
() =>
|
|
88
|
+
buildKqlSuggestions({
|
|
89
|
+
input: value,
|
|
90
|
+
cursorPos: effectiveCursorPos,
|
|
91
|
+
fields,
|
|
92
|
+
sampleRows,
|
|
93
|
+
t,
|
|
94
|
+
}),
|
|
95
|
+
[effectiveCursorPos, fields, sampleRows, t, value],
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const activeValueField = prelim.context.stage === 'value' ? prelim.context.field : undefined;
|
|
99
|
+
const activeValueFieldMeta = prelim.context.fieldMeta;
|
|
100
|
+
|
|
101
|
+
// Lazy-fetch top field values from Elasticsearch when user enters value position
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
if (!open || !activeValueField || !onInspectRef.current) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (!canFetchFieldTopValues(activeValueFieldMeta)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (activeValueField in topValuesCache || inFlightFieldsRef.current.has(activeValueField)) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let cancelled = false;
|
|
114
|
+
inFlightFieldsRef.current.add(activeValueField);
|
|
115
|
+
setLoadingField(activeValueField);
|
|
116
|
+
|
|
117
|
+
const loadFieldValues = async () => {
|
|
118
|
+
try {
|
|
119
|
+
const res = await onInspectRef.current?.(activeValueField);
|
|
120
|
+
if (!cancelled) {
|
|
121
|
+
setTopValuesCache((prev) => ({
|
|
122
|
+
...prev,
|
|
123
|
+
[activeValueField]: res?.buckets ?? [],
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
} catch {
|
|
127
|
+
if (!cancelled) {
|
|
128
|
+
setTopValuesCache((prev) => ({
|
|
129
|
+
...prev,
|
|
130
|
+
[activeValueField]: [],
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
} finally {
|
|
134
|
+
inFlightFieldsRef.current.delete(activeValueField);
|
|
135
|
+
if (!cancelled) {
|
|
136
|
+
setLoadingField((prev) => (prev === activeValueField ? null : prev));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
loadFieldValues();
|
|
142
|
+
return () => {
|
|
143
|
+
cancelled = true;
|
|
144
|
+
};
|
|
145
|
+
}, [activeValueField, activeValueFieldMeta, open, topValuesCache]);
|
|
146
|
+
|
|
147
|
+
const { context, items } = useMemo(
|
|
148
|
+
() =>
|
|
149
|
+
buildKqlSuggestions({
|
|
150
|
+
input: value,
|
|
151
|
+
cursorPos: effectiveCursorPos,
|
|
152
|
+
fields,
|
|
153
|
+
topValueBuckets: activeValueField ? topValuesCache[activeValueField] ?? [] : [],
|
|
154
|
+
sampleRows,
|
|
155
|
+
t,
|
|
156
|
+
}),
|
|
157
|
+
[activeValueField, effectiveCursorPos, fields, sampleRows, t, topValuesCache, value],
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const { groupedOptions, itemByOptionValue } = useMemo(() => {
|
|
161
|
+
const map = new Map<string, KqlSuggestionItem>();
|
|
162
|
+
const byCat = new Map<
|
|
163
|
+
KqlSuggestionCategory,
|
|
164
|
+
Array<{ value: string; label: React.ReactNode; disabled?: boolean }>
|
|
165
|
+
>();
|
|
166
|
+
|
|
167
|
+
const pushRow = (
|
|
168
|
+
cat: KqlSuggestionCategory,
|
|
169
|
+
row: { value: string; label: React.ReactNode; disabled?: boolean },
|
|
170
|
+
) => {
|
|
171
|
+
const list = byCat.get(cat) ?? [];
|
|
172
|
+
list.push(row);
|
|
173
|
+
byCat.set(cat, list);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
if (context.stage === 'value' && activeValueField && loadingField === activeValueField) {
|
|
177
|
+
pushRow('value', {
|
|
178
|
+
value: LOADING_VALUES_SENTINEL,
|
|
179
|
+
disabled: true,
|
|
180
|
+
label: (
|
|
181
|
+
<Space size={6}>
|
|
182
|
+
<Spin size="small" />
|
|
183
|
+
<Text type="secondary" style={{ fontSize: 12 }}>
|
|
184
|
+
{t('Loading field values...')}
|
|
185
|
+
</Text>
|
|
186
|
+
</Space>
|
|
187
|
+
),
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
for (const item of items) {
|
|
192
|
+
const optionValue = item.submitImmediately ? RUN_FREETEXT_SENTINEL : item.nextQuery;
|
|
193
|
+
if (map.has(optionValue)) continue;
|
|
194
|
+
map.set(optionValue, item);
|
|
195
|
+
|
|
196
|
+
pushRow(item.category, {
|
|
197
|
+
value: optionValue,
|
|
198
|
+
label: (
|
|
199
|
+
<div
|
|
200
|
+
style={{
|
|
201
|
+
display: 'flex',
|
|
202
|
+
alignItems: 'center',
|
|
203
|
+
justifyContent: 'space-between',
|
|
204
|
+
gap: 8,
|
|
205
|
+
width: '100%',
|
|
206
|
+
minWidth: 0,
|
|
207
|
+
}}
|
|
208
|
+
>
|
|
209
|
+
<Text
|
|
210
|
+
code
|
|
211
|
+
style={{
|
|
212
|
+
fontSize: 12,
|
|
213
|
+
margin: 0,
|
|
214
|
+
overflow: 'hidden',
|
|
215
|
+
textOverflow: 'ellipsis',
|
|
216
|
+
whiteSpace: 'nowrap',
|
|
217
|
+
maxWidth: '58%',
|
|
218
|
+
}}
|
|
219
|
+
>
|
|
220
|
+
{item.displayToken}
|
|
221
|
+
</Text>
|
|
222
|
+
<span
|
|
223
|
+
style={{
|
|
224
|
+
display: 'inline-flex',
|
|
225
|
+
alignItems: 'center',
|
|
226
|
+
gap: 4,
|
|
227
|
+
minWidth: 0,
|
|
228
|
+
flexShrink: 1,
|
|
229
|
+
overflow: 'hidden',
|
|
230
|
+
}}
|
|
231
|
+
>
|
|
232
|
+
{item.badge && (
|
|
233
|
+
<Tag bordered={false} style={{ marginInlineEnd: 0, fontSize: 11, lineHeight: '18px' }}>
|
|
234
|
+
{item.badge}
|
|
235
|
+
</Tag>
|
|
236
|
+
)}
|
|
237
|
+
<Text
|
|
238
|
+
type="secondary"
|
|
239
|
+
style={{
|
|
240
|
+
fontSize: 12,
|
|
241
|
+
overflow: 'hidden',
|
|
242
|
+
textOverflow: 'ellipsis',
|
|
243
|
+
whiteSpace: 'nowrap',
|
|
244
|
+
}}
|
|
245
|
+
>
|
|
246
|
+
{item.description}
|
|
247
|
+
</Text>
|
|
248
|
+
</span>
|
|
249
|
+
</div>
|
|
250
|
+
),
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const categoryOrder: Array<{ cat: KqlSuggestionCategory; title: string }> = [
|
|
255
|
+
{
|
|
256
|
+
cat: 'field',
|
|
257
|
+
title: !value.trim()
|
|
258
|
+
? `${t('Suggested Fields')} — ${t('Type keywords freely or select a field below')}`
|
|
259
|
+
: t('Suggested Fields'),
|
|
260
|
+
},
|
|
261
|
+
{ cat: 'operator', title: t('Comparisons & Operators') },
|
|
262
|
+
{ cat: 'value', title: t('Suggested Values') },
|
|
263
|
+
{ cat: 'connector', title: t('Combine Conditions') },
|
|
264
|
+
{ cat: 'free_text', title: t('Free-text Search') },
|
|
265
|
+
];
|
|
266
|
+
|
|
267
|
+
const groups: NonNullable<AutoCompleteProps['options']> = [];
|
|
268
|
+
for (const { cat, title } of categoryOrder) {
|
|
269
|
+
const rows = byCat.get(cat);
|
|
270
|
+
if (rows && rows.length > 0) {
|
|
271
|
+
groups.push({
|
|
272
|
+
label: (
|
|
273
|
+
<Text type="secondary" style={{ fontSize: 11, fontWeight: 600 }}>
|
|
274
|
+
{title}
|
|
275
|
+
</Text>
|
|
276
|
+
),
|
|
277
|
+
options: rows,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return { groupedOptions: groups, itemByOptionValue: map };
|
|
283
|
+
}, [activeValueField, context.stage, items, loadingField, t, value]);
|
|
284
|
+
|
|
285
|
+
const syncCursorFromDom = (target?: HTMLInputElement | null) => {
|
|
286
|
+
const el = target ?? inputRef.current?.input ?? null;
|
|
287
|
+
if (el && typeof el.selectionStart === 'number') {
|
|
288
|
+
setCursorPos(el.selectionStart);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
const handleSelect = (selectedVal: string) => {
|
|
293
|
+
const picked = itemByOptionValue.get(selectedVal);
|
|
294
|
+
justSelectedOptionRef.current = true;
|
|
295
|
+
queueMicrotask(() => {
|
|
296
|
+
justSelectedOptionRef.current = false;
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
if (!picked) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (picked.submitImmediately) {
|
|
304
|
+
keepOpenAfterSelectRef.current = false;
|
|
305
|
+
setOpen(false);
|
|
306
|
+
onSubmit?.(value);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
setCursorPos(picked.nextCursor);
|
|
311
|
+
onChange?.(picked.nextQuery);
|
|
312
|
+
|
|
313
|
+
if (picked.keepOpen) {
|
|
314
|
+
keepOpenAfterSelectRef.current = true;
|
|
315
|
+
setOpen(true);
|
|
316
|
+
} else {
|
|
317
|
+
keepOpenAfterSelectRef.current = false;
|
|
318
|
+
setOpen(false);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const focusAndPlaceCaret = () => {
|
|
322
|
+
const el = inputRef.current?.input;
|
|
323
|
+
if (el) {
|
|
324
|
+
el.focus();
|
|
325
|
+
try {
|
|
326
|
+
el.setSelectionRange(picked.nextCursor, picked.nextCursor);
|
|
327
|
+
} catch {
|
|
328
|
+
// Ignore selection errors on unsupported input types
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
focusAndPlaceCaret();
|
|
334
|
+
if (typeof window !== 'undefined' && typeof window.requestAnimationFrame === 'function') {
|
|
335
|
+
window.requestAnimationFrame(focusAndPlaceCaret);
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
return (
|
|
340
|
+
<AutoComplete
|
|
341
|
+
id={id}
|
|
342
|
+
value={value}
|
|
343
|
+
options={groupedOptions}
|
|
344
|
+
open={open && groupedOptions.length > 0}
|
|
345
|
+
disabled={disabled}
|
|
346
|
+
filterOption={false}
|
|
347
|
+
defaultActiveFirstOption={false}
|
|
348
|
+
popupMatchSelectWidth={false}
|
|
349
|
+
dropdownStyle={{ minWidth: 320, maxWidth: 'min(480px, calc(100vw - 32px))' }}
|
|
350
|
+
style={style}
|
|
351
|
+
onDropdownVisibleChange={(nextVisible) => {
|
|
352
|
+
if (!nextVisible && keepOpenAfterSelectRef.current) {
|
|
353
|
+
keepOpenAfterSelectRef.current = false;
|
|
354
|
+
setOpen(true);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
setOpen(nextVisible);
|
|
358
|
+
}}
|
|
359
|
+
onChange={(nextVal) => {
|
|
360
|
+
if (nextVal === RUN_FREETEXT_SENTINEL || nextVal === LOADING_VALUES_SENTINEL) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
onChange?.(nextVal);
|
|
364
|
+
}}
|
|
365
|
+
onSelect={handleSelect}
|
|
366
|
+
>
|
|
367
|
+
<Input
|
|
368
|
+
ref={inputRef}
|
|
369
|
+
allowClear
|
|
370
|
+
aria-label={ariaLabel ?? t('KQL / Search Query')}
|
|
371
|
+
placeholder={placeholder ?? t('Search KQL, e.g. status >= 500 AND service: "api"')}
|
|
372
|
+
prefix={<SearchOutlined aria-hidden="true" />}
|
|
373
|
+
onFocus={(e) => {
|
|
374
|
+
onFocusEnsureFields?.();
|
|
375
|
+
syncCursorFromDom(e.currentTarget);
|
|
376
|
+
setOpen(true);
|
|
377
|
+
}}
|
|
378
|
+
onClick={(e) => {
|
|
379
|
+
syncCursorFromDom(e.currentTarget);
|
|
380
|
+
}}
|
|
381
|
+
onChange={(e) => {
|
|
382
|
+
const nextVal = e.target.value;
|
|
383
|
+
setCursorPos(e.target.selectionStart ?? nextVal.length);
|
|
384
|
+
if (nextVal === '' && e.type === 'click') {
|
|
385
|
+
onClear?.();
|
|
386
|
+
}
|
|
387
|
+
}}
|
|
388
|
+
onKeyUp={(e) => {
|
|
389
|
+
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight' || e.key === 'Home' || e.key === 'End') {
|
|
390
|
+
syncCursorFromDom(e.currentTarget);
|
|
391
|
+
}
|
|
392
|
+
}}
|
|
393
|
+
onCompositionEnd={(e) => {
|
|
394
|
+
lastCompositionEndAtRef.current = e.timeStamp;
|
|
395
|
+
}}
|
|
396
|
+
onKeyDown={(e) => {
|
|
397
|
+
if (e.key === 'Enter') {
|
|
398
|
+
if (justSelectedOptionRef.current) {
|
|
399
|
+
justSelectedOptionRef.current = false;
|
|
400
|
+
e.preventDefault();
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
// IME-safe Enter guard (modern-web-guidance: ime-safe-enter-submit)
|
|
404
|
+
if (
|
|
405
|
+
e.nativeEvent.isComposing ||
|
|
406
|
+
e.keyCode === 229 ||
|
|
407
|
+
(lastCompositionEndAtRef.current !== null && Math.abs(e.timeStamp - lastCompositionEndAtRef.current) < 50)
|
|
408
|
+
) {
|
|
409
|
+
e.preventDefault();
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
e.preventDefault();
|
|
413
|
+
keepOpenAfterSelectRef.current = false;
|
|
414
|
+
setOpen(false);
|
|
415
|
+
onSubmit?.(value);
|
|
416
|
+
}
|
|
417
|
+
}}
|
|
418
|
+
/>
|
|
419
|
+
</AutoComplete>
|
|
420
|
+
);
|
|
421
|
+
};
|
|
@@ -8,17 +8,32 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { useApp } from '@nocobase/client-v2';
|
|
11
|
-
import { Card, Empty } from 'antd';
|
|
12
|
-
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
11
|
+
import { Card, Empty, Tag } from 'antd';
|
|
12
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
13
13
|
import type {
|
|
14
14
|
EsDataViewRecord,
|
|
15
15
|
EsFieldCapability,
|
|
16
16
|
EsReportRecord,
|
|
17
|
+
ExportFormat,
|
|
18
|
+
FieldTopValuesResult,
|
|
17
19
|
ID,
|
|
18
20
|
ReportExecutionResult,
|
|
21
|
+
ReportFilterItem,
|
|
19
22
|
ReportRuntimeOverrides,
|
|
20
23
|
} from '../../shared/types';
|
|
21
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
downloadReportExport,
|
|
26
|
+
executeSavedReport,
|
|
27
|
+
fetchFieldTopValues,
|
|
28
|
+
fetchReportMeta,
|
|
29
|
+
type ApiClientLike,
|
|
30
|
+
} from '../api';
|
|
31
|
+
import {
|
|
32
|
+
buildRecordContextFilters,
|
|
33
|
+
detectBrowserTimeZone,
|
|
34
|
+
upsertDashboardFilter,
|
|
35
|
+
useDashboardScope,
|
|
36
|
+
} from '../bus/DashboardFilterBus';
|
|
22
37
|
import { useT } from '../locale';
|
|
23
38
|
import { ElasticReportRenderer } from './ElasticReportRenderer';
|
|
24
39
|
|
|
@@ -26,12 +41,18 @@ export interface ElasticReportBlockProps {
|
|
|
26
41
|
reportId?: ID;
|
|
27
42
|
showToolbar?: boolean;
|
|
28
43
|
compact?: boolean;
|
|
44
|
+
scopeKey?: string;
|
|
45
|
+
subscribeDashboardFilters?: boolean;
|
|
46
|
+
contextRecord?: unknown;
|
|
29
47
|
}
|
|
30
48
|
|
|
31
49
|
export const ElasticReportBlock: React.FC<ElasticReportBlockProps> = ({
|
|
32
50
|
reportId,
|
|
33
51
|
showToolbar = true,
|
|
34
52
|
compact = false,
|
|
53
|
+
scopeKey,
|
|
54
|
+
subscribeDashboardFilters = true,
|
|
55
|
+
contextRecord,
|
|
35
56
|
}) => {
|
|
36
57
|
const t = useT();
|
|
37
58
|
const app = useApp();
|
|
@@ -39,6 +60,9 @@ export const ElasticReportBlock: React.FC<ElasticReportBlockProps> = ({
|
|
|
39
60
|
const apiRef = useRef<ApiClientLike>(api);
|
|
40
61
|
apiRef.current = api;
|
|
41
62
|
|
|
63
|
+
const activeScopeKey = subscribeDashboardFilters && scopeKey ? scopeKey : null;
|
|
64
|
+
const sharedState = useDashboardScope(activeScopeKey);
|
|
65
|
+
|
|
42
66
|
const [report, setReport] = useState<EsReportRecord | null>(null);
|
|
43
67
|
const [dataView, setDataView] = useState<EsDataViewRecord | null>(null);
|
|
44
68
|
const [fields, setFields] = useState<EsFieldCapability[]>([]);
|
|
@@ -46,17 +70,61 @@ export const ElasticReportBlock: React.FC<ElasticReportBlockProps> = ({
|
|
|
46
70
|
const [loading, setLoading] = useState(false);
|
|
47
71
|
const [error, setError] = useState<string | null>(null);
|
|
48
72
|
const reqCounter = useRef(0);
|
|
73
|
+
const latestLocalOverridesRef = useRef<ReportRuntimeOverrides | undefined>(undefined);
|
|
74
|
+
const browserTimeZone = useMemo(() => detectBrowserTimeZone(), []);
|
|
75
|
+
|
|
76
|
+
const contextFilters = useMemo(
|
|
77
|
+
() => buildRecordContextFilters(report?.spec.contextBindings, contextRecord),
|
|
78
|
+
[contextRecord, report?.spec.contextBindings],
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const composeRuntimeOverrides = useCallback(
|
|
82
|
+
(localOverrides?: ReportRuntimeOverrides): ReportRuntimeOverrides => {
|
|
83
|
+
const localQuery =
|
|
84
|
+
localOverrides?.query !== undefined ? localOverrides.query.trim() : (report?.spec.query ?? '').trim();
|
|
85
|
+
const globalQuery = (sharedState.query ?? '').trim();
|
|
86
|
+
let combinedQuery = localQuery;
|
|
87
|
+
if (globalQuery && localQuery) {
|
|
88
|
+
combinedQuery = `(${localQuery}) AND (${globalQuery})`;
|
|
89
|
+
} else if (globalQuery) {
|
|
90
|
+
combinedQuery = globalQuery;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
...localOverrides,
|
|
95
|
+
...(localOverrides?.query !== undefined || globalQuery ? { query: combinedQuery } : {}),
|
|
96
|
+
...(sharedState.timeRange
|
|
97
|
+
? { timeRange: sharedState.timeRange }
|
|
98
|
+
: localOverrides?.timeRange
|
|
99
|
+
? { timeRange: localOverrides.timeRange }
|
|
100
|
+
: {}),
|
|
101
|
+
...(sharedState.filters.length > 0 ? { sharedFilters: sharedState.filters } : {}),
|
|
102
|
+
...(contextFilters.length > 0 ? { contextFilters } : {}),
|
|
103
|
+
...(browserTimeZone ? { timeZone: browserTimeZone } : {}),
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
[
|
|
107
|
+
browserTimeZone,
|
|
108
|
+
contextFilters,
|
|
109
|
+
report?.spec.query,
|
|
110
|
+
sharedState.filters,
|
|
111
|
+
sharedState.query,
|
|
112
|
+
sharedState.timeRange,
|
|
113
|
+
],
|
|
114
|
+
);
|
|
49
115
|
|
|
50
116
|
const runWithOverrides = useCallback(
|
|
51
|
-
async (
|
|
117
|
+
async (localOverrides?: ReportRuntimeOverrides) => {
|
|
52
118
|
if (reportId === undefined || reportId === null || String(reportId).trim() === '') {
|
|
53
119
|
return;
|
|
54
120
|
}
|
|
121
|
+
latestLocalOverridesRef.current = localOverrides;
|
|
122
|
+
const merged = composeRuntimeOverrides(localOverrides);
|
|
55
123
|
const seq = ++reqCounter.current;
|
|
56
124
|
setLoading(true);
|
|
57
125
|
setError(null);
|
|
58
126
|
try {
|
|
59
|
-
const res = await executeSavedReport(apiRef.current, reportId,
|
|
127
|
+
const res = await executeSavedReport(apiRef.current, reportId, merged);
|
|
60
128
|
if (seq !== reqCounter.current) return;
|
|
61
129
|
setResult(res.result);
|
|
62
130
|
} catch (err) {
|
|
@@ -68,7 +136,7 @@ export const ElasticReportBlock: React.FC<ElasticReportBlockProps> = ({
|
|
|
68
136
|
}
|
|
69
137
|
}
|
|
70
138
|
},
|
|
71
|
-
[reportId],
|
|
139
|
+
[composeRuntimeOverrides, reportId],
|
|
72
140
|
);
|
|
73
141
|
|
|
74
142
|
useEffect(() => {
|
|
@@ -86,12 +154,16 @@ export const ElasticReportBlock: React.FC<ElasticReportBlockProps> = ({
|
|
|
86
154
|
setLoading(true);
|
|
87
155
|
setError(null);
|
|
88
156
|
try {
|
|
157
|
+
const initialOverrides: ReportRuntimeOverrides = {
|
|
158
|
+
...(browserTimeZone ? { timeZone: browserTimeZone } : {}),
|
|
159
|
+
};
|
|
89
160
|
const [meta, execution] = await Promise.all([
|
|
90
161
|
fetchReportMeta(apiRef.current, reportId),
|
|
91
|
-
executeSavedReport(apiRef.current, reportId),
|
|
162
|
+
executeSavedReport(apiRef.current, reportId, initialOverrides),
|
|
92
163
|
]);
|
|
93
164
|
if (cancelled) return;
|
|
94
|
-
|
|
165
|
+
const resolvedReport = execution.report ?? meta.report;
|
|
166
|
+
setReport(resolvedReport);
|
|
95
167
|
setDataView(execution.dataView ?? meta.dataView);
|
|
96
168
|
setFields(meta.fields ?? []);
|
|
97
169
|
setResult(execution.result);
|
|
@@ -110,11 +182,73 @@ export const ElasticReportBlock: React.FC<ElasticReportBlockProps> = ({
|
|
|
110
182
|
return () => {
|
|
111
183
|
cancelled = true;
|
|
112
184
|
};
|
|
113
|
-
}, [reportId]);
|
|
185
|
+
}, [browserTimeZone, reportId]);
|
|
186
|
+
|
|
187
|
+
// Re-run whenever shared dashboard filter bar / cross-filter / record context updates after initial mount
|
|
188
|
+
const appliedSharedStampRef = useRef<string>('');
|
|
189
|
+
useEffect(() => {
|
|
190
|
+
if (!report) return;
|
|
191
|
+
const stamp = JSON.stringify({
|
|
192
|
+
updatedAt: sharedState.updatedAt,
|
|
193
|
+
refreshTick: sharedState.refreshTick,
|
|
194
|
+
ctxFilters: contextFilters,
|
|
195
|
+
});
|
|
196
|
+
if (appliedSharedStampRef.current === '') {
|
|
197
|
+
appliedSharedStampRef.current = stamp;
|
|
198
|
+
if (sharedState.updatedAt === 0 && contextFilters.length === 0) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
} else if (appliedSharedStampRef.current === stamp) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
appliedSharedStampRef.current = stamp;
|
|
205
|
+
runWithOverrides(latestLocalOverridesRef.current);
|
|
206
|
+
}, [contextFilters, report, runWithOverrides, sharedState.refreshTick, sharedState.updatedAt]);
|
|
207
|
+
|
|
208
|
+
const handleCrossFilter = useCallback(
|
|
209
|
+
(item: ReportFilterItem) => {
|
|
210
|
+
if (activeScopeKey) {
|
|
211
|
+
upsertDashboardFilter(activeScopeKey, item);
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
[activeScopeKey],
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
const handleInspectTopValues = useCallback(
|
|
218
|
+
async (field: string, localOverrides?: ReportRuntimeOverrides): Promise<FieldTopValuesResult> => {
|
|
219
|
+
if (reportId === undefined || reportId === null) {
|
|
220
|
+
return { field, resolvedField: field, totalDocs: 0, buckets: [] };
|
|
221
|
+
}
|
|
222
|
+
return fetchFieldTopValues(apiRef.current, {
|
|
223
|
+
reportId,
|
|
224
|
+
field,
|
|
225
|
+
size: 8,
|
|
226
|
+
overrides: composeRuntimeOverrides(localOverrides),
|
|
227
|
+
});
|
|
228
|
+
},
|
|
229
|
+
[composeRuntimeOverrides, reportId],
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const handleExport = useCallback(
|
|
233
|
+
async (format: ExportFormat, columns?: string[], localOverrides?: ReportRuntimeOverrides) => {
|
|
234
|
+
if (reportId === undefined || reportId === null) return;
|
|
235
|
+
await downloadReportExport(apiRef.current, {
|
|
236
|
+
reportId,
|
|
237
|
+
reportTitle: report?.title,
|
|
238
|
+
format,
|
|
239
|
+
columns,
|
|
240
|
+
overrides: composeRuntimeOverrides(localOverrides),
|
|
241
|
+
});
|
|
242
|
+
},
|
|
243
|
+
[composeRuntimeOverrides, report?.title, reportId],
|
|
244
|
+
);
|
|
114
245
|
|
|
115
246
|
if (reportId === undefined || reportId === null || String(reportId).trim() === '') {
|
|
116
247
|
return (
|
|
117
|
-
<Card
|
|
248
|
+
<Card
|
|
249
|
+
size={compact ? 'small' : 'default'}
|
|
250
|
+
style={{ width: '100%', maxWidth: '100%', minWidth: 0, boxSizing: 'border-box', overflow: 'hidden' }}
|
|
251
|
+
>
|
|
118
252
|
<Empty description={t('Select a saved Elastic Report in block settings to display data')} />
|
|
119
253
|
</Card>
|
|
120
254
|
);
|
|
@@ -124,8 +258,17 @@ export const ElasticReportBlock: React.FC<ElasticReportBlockProps> = ({
|
|
|
124
258
|
<Card
|
|
125
259
|
size={compact ? 'small' : 'default'}
|
|
126
260
|
title={report?.title}
|
|
261
|
+
style={{ width: '100%', maxWidth: '100%', minWidth: 0, boxSizing: 'border-box', overflow: 'hidden' }}
|
|
262
|
+
styles={{ body: { maxWidth: '100%', minWidth: 0, overflowX: 'hidden' } }}
|
|
127
263
|
extra={
|
|
128
|
-
|
|
264
|
+
<span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, paddingInlineEnd: 2 }}>
|
|
265
|
+
{sharedState.filters.length > 0 && (
|
|
266
|
+
<Tag color="cyan">
|
|
267
|
+
{t('Dashboard filter')}: {sharedState.filters.length}
|
|
268
|
+
</Tag>
|
|
269
|
+
)}
|
|
270
|
+
{dataView?.indexPattern && <span style={{ fontSize: 12, opacity: 0.7 }}>{dataView.indexPattern}</span>}
|
|
271
|
+
</span>
|
|
129
272
|
}
|
|
130
273
|
>
|
|
131
274
|
{report ? (
|
|
@@ -142,6 +285,9 @@ export const ElasticReportBlock: React.FC<ElasticReportBlockProps> = ({
|
|
|
142
285
|
onExecute={(overrides) => {
|
|
143
286
|
runWithOverrides(overrides);
|
|
144
287
|
}}
|
|
288
|
+
onCrossFilter={activeScopeKey ? handleCrossFilter : undefined}
|
|
289
|
+
onInspectTopValues={handleInspectTopValues}
|
|
290
|
+
onExport={report.spec.allowExport !== false ? handleExport : undefined}
|
|
145
291
|
/>
|
|
146
292
|
) : (
|
|
147
293
|
<ElasticReportRenderer
|