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.
- 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/dist/ai/skills/elastic-report/SKILLS.md +52 -0
- package/dist/client/357.0dd385384cab5dbf.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/851.35e416bfb3fb565f.js +10 -0
- package/dist/client-v2/857.a5bf9637d5fd4ef5.js +10 -0
- package/dist/client-v2/877.81b10beb7b7f4bd7.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +5 -3
- package/dist/locale/en-US.json +213 -99
- package/dist/locale/vi-VN.json +213 -99
- package/dist/locale/zh-CN.json +213 -99
- 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 +238 -14
- package/dist/server/plugin.js +156 -22
- package/dist/server/report-block-tools.js +340 -0
- package/dist/server/services/ElasticReportService.js +137 -17
- 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 +26 -2
- package/package.json +3 -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 -19
- package/src/client/workflow-instructions.tsx +44 -0
- package/src/client-v2/__tests__/elastic-report.test.tsx +879 -311
- package/src/client-v2/__tests__/kql-autocomplete.test.ts +178 -0
- package/src/client-v2/api.ts +320 -224
- package/src/client-v2/bus/DashboardFilterBus.ts +212 -0
- package/src/client-v2/components/ElasticBasicCharts.tsx +652 -399
- 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 +306 -160
- package/src/client-v2/components/ElasticReportRenderer.tsx +1257 -707
- package/src/client-v2/components/HighlightText.tsx +50 -50
- package/src/client-v2/components/ReportSelect.tsx +85 -72
- package/src/client-v2/index.tsx +14 -14
- package/src/client-v2/locale.ts +23 -23
- package/src/client-v2/models/ElasticFilterBarBlockModel.tsx +115 -0
- package/src/client-v2/models/ElasticReportBlockModel.tsx +118 -85
- package/src/client-v2/pages/ElasticReportManager.tsx +1453 -1047
- package/src/client-v2/plugin.tsx +107 -52
- package/src/client-v2/report-destination.tsx +126 -0
- package/src/client-v2/utils/kqlAutocomplete.ts +861 -0
- package/src/client-v2/workflow/instructions.tsx +149 -0
- package/src/index.ts +11 -11
- package/src/locale/en-US.json +213 -99
- package/src/locale/vi-VN.json +213 -99
- package/src/locale/zh-CN.json +213 -99
- package/src/server/__tests__/ai-blocks.integration.test.ts +292 -0
- package/src/server/__tests__/query-compiler.test.ts +620 -231
- package/src/server/__tests__/report-mcp-acl.integration.test.ts +645 -377
- package/src/server/acl-guard.ts +194 -103
- package/src/server/collections/es-report-data-views.ts +64 -58
- package/src/server/collections/es-reports.ts +80 -74
- package/src/server/index.ts +15 -15
- package/src/server/mcp/ElasticReportMcpServer.ts +391 -391
- package/src/server/mcp/ElasticReportToolCatalog.ts +667 -417
- package/src/server/plugin.ts +599 -431
- package/src/server/report-block-tools.ts +400 -0
- package/src/server/services/ElasticReportService.ts +494 -326
- package/src/server/services/ElasticsearchConnectionService.ts +728 -401
- package/src/server/services/ReportExportService.ts +282 -0
- package/src/server/services/ReportQueryCompiler.ts +1552 -1017
- package/src/server/workflow/ElasticReportQueryInstruction.ts +124 -0
- package/src/shared/constants.ts +39 -29
- package/src/shared/types.ts +272 -175
- package/ai/skills/elastic-report/SKILLS.md +0 -32
- package/dist/client-v2/851.488fdbf5bc5b6a36.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
|
@@ -0,0 +1,400 @@
|
|
|
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
|
+
/**
|
|
11
|
+
* This file is part of the NocoBase (R) project.
|
|
12
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
13
|
+
* Authors: NocoBase Team.
|
|
14
|
+
*
|
|
15
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
16
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { Context } from '@nocobase/actions';
|
|
20
|
+
import { type Repository, Transaction } from '@nocobase/database';
|
|
21
|
+
import type { Application } from '@nocobase/server';
|
|
22
|
+
import { BLOCK_ADDED_EVENT, BLOCK_SETTINGS_EVENT, PLUGIN_NAME, REPORT_DESTINATION_CONTEXT } from '../shared/constants';
|
|
23
|
+
import { assertAuthenticated, canAccessReport, getEffectiveRoles } from './acl-guard';
|
|
24
|
+
import type { ElasticReportToolDefinition } from './mcp/ElasticReportToolCatalog';
|
|
25
|
+
import { type ElasticReportService, extractCurrentUserId } from './services/ElasticReportService';
|
|
26
|
+
|
|
27
|
+
type Fields = Record<string, unknown>;
|
|
28
|
+
type FlowRepository = Repository & {
|
|
29
|
+
patch: (model: Fields, options: { transaction: Transaction }) => Promise<void>;
|
|
30
|
+
ensureRouteContent: (
|
|
31
|
+
slot: { parentId: string; subKey: 'grid'; use: string },
|
|
32
|
+
options: { transaction: Transaction },
|
|
33
|
+
) => Promise<string>;
|
|
34
|
+
upsertModel: (model: Fields, options: { transaction: Transaction }) => Promise<string>;
|
|
35
|
+
findModelById: (
|
|
36
|
+
uid: string,
|
|
37
|
+
options: { transaction: Transaction },
|
|
38
|
+
) => Promise<{
|
|
39
|
+
uid: string;
|
|
40
|
+
use?: string;
|
|
41
|
+
subModels?: {
|
|
42
|
+
items?: Array<{
|
|
43
|
+
uid: string;
|
|
44
|
+
use?: string;
|
|
45
|
+
props?: Fields;
|
|
46
|
+
stepParams?: { elasticReportBlockSettings?: { editReport?: Fields } };
|
|
47
|
+
}>;
|
|
48
|
+
};
|
|
49
|
+
} | null>;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const isObject = (value: unknown): value is Fields =>
|
|
53
|
+
value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
54
|
+
|
|
55
|
+
function fail(ctx: Partial<Context>, status: number, key: string): never {
|
|
56
|
+
throw Object.assign(new Error(ctx.t?.(key, { ns: PLUGIN_NAME }) ?? key), { status });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const defaults = { reportId: null, showToolbar: true, compact: false, subscribeDashboardFilters: true, syncGroup: '' };
|
|
60
|
+
const propertySchemas = {
|
|
61
|
+
reportId: { type: ['number', 'string'] },
|
|
62
|
+
showToolbar: { type: 'boolean' },
|
|
63
|
+
compact: { type: 'boolean' },
|
|
64
|
+
subscribeDashboardFilters: { type: 'boolean' },
|
|
65
|
+
syncGroup: { type: 'string' },
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
function isReportId(value: unknown): value is string | number {
|
|
69
|
+
return (typeof value === 'string' && !!value.trim()) || (typeof value === 'number' && Number.isSafeInteger(value));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function validField(key: string, value: unknown, expected: boolean) {
|
|
73
|
+
if (key === 'reportId') {
|
|
74
|
+
return (expected && value === null) || isReportId(value);
|
|
75
|
+
}
|
|
76
|
+
if (key === 'syncGroup') return typeof value === 'string';
|
|
77
|
+
return Object.hasOwn(defaults, key) && typeof value === 'boolean';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function sameSetting(key: string, left: unknown, right: unknown) {
|
|
81
|
+
if (key === 'reportId' && left != null && right != null) return String(left) === String(right);
|
|
82
|
+
return left === right;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function readPatch(ctx: Partial<Context>, { changes, expected }: Fields) {
|
|
86
|
+
if (!isObject(changes) || !isObject(expected)) fail(ctx, 400, 'Invalid block changes');
|
|
87
|
+
const keys = Object.keys(changes);
|
|
88
|
+
if (!keys.length || keys.length !== Object.keys(expected).length) fail(ctx, 400, 'Invalid block changes');
|
|
89
|
+
for (const key of keys) {
|
|
90
|
+
if (
|
|
91
|
+
!Object.hasOwn(expected, key) ||
|
|
92
|
+
!validField(key, changes[key], false) ||
|
|
93
|
+
!validField(key, expected[key], true)
|
|
94
|
+
) {
|
|
95
|
+
fail(ctx, 400, 'Invalid block changes');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return { changes, expected };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function selectedContext(app: Application, ctx: Partial<Context>, type: string) {
|
|
102
|
+
const values = ctx.action?.params?.values;
|
|
103
|
+
let items: unknown;
|
|
104
|
+
if (Array.isArray(values?.messages)) {
|
|
105
|
+
const userMessage = values.messages.find((item: unknown) => isObject(item) && item.role === 'user');
|
|
106
|
+
items = userMessage?.workContext;
|
|
107
|
+
} else if (typeof values?.sessionId === 'string') {
|
|
108
|
+
// ASK resumes in a fresh request. Resolve the user turn for this decision inside its owner's conversation.
|
|
109
|
+
const sessionId = values.sessionId;
|
|
110
|
+
const owned = await app.db.getRepository('aiConversations').count({
|
|
111
|
+
filter: { sessionId, userId: extractCurrentUserId(ctx) },
|
|
112
|
+
});
|
|
113
|
+
if (owned) {
|
|
114
|
+
const message = await app.db.getRepository('aiMessages').findOne({
|
|
115
|
+
filter: { sessionId, role: 'user', ...(values.messageId ? { messageId: { $lte: values.messageId } } : {}) },
|
|
116
|
+
fields: ['workContext'],
|
|
117
|
+
sort: ['-messageId'],
|
|
118
|
+
});
|
|
119
|
+
items = message?.get('workContext');
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const selected = Array.isArray(items)
|
|
123
|
+
? items.filter((item): item is Fields => isObject(item) && item.type === type)
|
|
124
|
+
: [];
|
|
125
|
+
return selected.length === 1 ? selected[0] : undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function resolvePage(
|
|
129
|
+
app: Application,
|
|
130
|
+
ctx: Partial<Context>,
|
|
131
|
+
uid: string,
|
|
132
|
+
write: boolean,
|
|
133
|
+
transaction: Transaction,
|
|
134
|
+
) {
|
|
135
|
+
const paths = await app.db.getRepository('flowModelTreePath').find({
|
|
136
|
+
filter: { descendant: uid },
|
|
137
|
+
sort: ['depth'],
|
|
138
|
+
fields: ['ancestor'],
|
|
139
|
+
transaction,
|
|
140
|
+
});
|
|
141
|
+
const ancestors = [...new Set([uid, ...paths.map((path) => path.get('ancestor') as string)])];
|
|
142
|
+
const routes = app.db.getRepository('desktopRoutes');
|
|
143
|
+
const anchors = await routes.find({
|
|
144
|
+
filter: { schemaUid: { $in: ancestors }, type: { $in: ['flowPage', 'tabs'] } },
|
|
145
|
+
transaction,
|
|
146
|
+
});
|
|
147
|
+
const anchor = ancestors.map((id) => anchors.find((route) => route.get('schemaUid') === id)).find(Boolean);
|
|
148
|
+
const page =
|
|
149
|
+
anchor?.get('type') === 'tabs'
|
|
150
|
+
? await routes.findOne({ filter: { id: anchor.get('parentId'), type: 'flowPage' }, transaction })
|
|
151
|
+
: anchor;
|
|
152
|
+
if (!page || !anchor) fail(ctx, 403, 'Block page unavailable');
|
|
153
|
+
const roles = getEffectiveRoles(ctx);
|
|
154
|
+
const routeIds = new Set([page.get('id'), anchor.get('id')].map(String));
|
|
155
|
+
if (app.db.hasCollection('uiLayouts')) {
|
|
156
|
+
// A route is visible only inside an enabled layout. The layout plugin also checks the parent menu chain.
|
|
157
|
+
let parentId = page.get('parentId');
|
|
158
|
+
while (parentId != null && !roles.includes('root')) {
|
|
159
|
+
if (routeIds.has(String(parentId))) fail(ctx, 403, 'Block page unavailable');
|
|
160
|
+
routeIds.add(String(parentId));
|
|
161
|
+
const parent = await routes.findOne({ filterByTk: parentId, fields: ['parentId'], transaction });
|
|
162
|
+
if (!parent) fail(ctx, 403, 'Block page unavailable');
|
|
163
|
+
parentId = parent.get('parentId');
|
|
164
|
+
}
|
|
165
|
+
const enabled = await app.db
|
|
166
|
+
.getRepository('uiLayouts')
|
|
167
|
+
.find({ filter: { enabled: true }, fields: ['uid'], transaction });
|
|
168
|
+
const assignments = await app.db.getRepository('desktopRoutesUiLayouts').find({
|
|
169
|
+
filter: {
|
|
170
|
+
desktopRouteId: { $in: [...routeIds] },
|
|
171
|
+
uiLayoutUid: { $in: enabled.map((layout) => layout.get('uid')) },
|
|
172
|
+
},
|
|
173
|
+
transaction,
|
|
174
|
+
});
|
|
175
|
+
const inCommonLayout = enabled.some(
|
|
176
|
+
(layout) => assignments.filter((link) => link.get('uiLayoutUid') === layout.get('uid')).length === routeIds.size,
|
|
177
|
+
);
|
|
178
|
+
if (!inCommonLayout) fail(ctx, 403, 'Block page unavailable');
|
|
179
|
+
}
|
|
180
|
+
if (!roles.includes('root')) {
|
|
181
|
+
const grants = await app.db.getRepository('rolesDesktopRoutes').find({
|
|
182
|
+
filter: { roleName: { $in: roles }, desktopRouteId: { $in: [...routeIds] } },
|
|
183
|
+
transaction,
|
|
184
|
+
});
|
|
185
|
+
for (const grant of grants) routeIds.delete(String(grant.get('desktopRouteId')));
|
|
186
|
+
if (routeIds.size) fail(ctx, 403, 'Block page unavailable');
|
|
187
|
+
}
|
|
188
|
+
const acl = app.acl.can({ roles, resource: 'flowModels', action: 'save' });
|
|
189
|
+
const canSave = !!acl && !Object.keys(acl.params ?? {}).length;
|
|
190
|
+
const shared =
|
|
191
|
+
app.db.hasCollection('flowModelTemplates') &&
|
|
192
|
+
(await app.db.getRepository('flowModelTemplates').count({
|
|
193
|
+
filter: { targetUid: { $in: ancestors } },
|
|
194
|
+
transaction,
|
|
195
|
+
}));
|
|
196
|
+
const canConfigure = canSave && !shared;
|
|
197
|
+
if (write && !canConfigure) fail(ctx, 403, 'Block editing unavailable');
|
|
198
|
+
return { pageUid: String(page.get('schemaUid')), canConfigure };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function createReportBlockTools(app: Application, reports: ElasticReportService): ElasticReportToolDefinition[] {
|
|
202
|
+
async function requirePublishedReport(id: string | number, ctx: Partial<Context>, transaction: Transaction) {
|
|
203
|
+
const report = await reports.findReport(id, transaction);
|
|
204
|
+
if (!report?.published || !report.dataView || !canAccessReport(app, ctx, report, report.dataView))
|
|
205
|
+
fail(ctx, 403, 'Report unavailable for this block');
|
|
206
|
+
return report;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function addBlock({ pageUid, tabUid, reportId }: Fields, ctx: Partial<Context>) {
|
|
210
|
+
assertAuthenticated(ctx);
|
|
211
|
+
const attachment = await selectedContext(app, ctx, REPORT_DESTINATION_CONTEXT);
|
|
212
|
+
const selected = attachment?.content;
|
|
213
|
+
if (
|
|
214
|
+
!isObject(selected) ||
|
|
215
|
+
typeof pageUid !== 'string' ||
|
|
216
|
+
!pageUid ||
|
|
217
|
+
typeof tabUid !== 'string' ||
|
|
218
|
+
!tabUid ||
|
|
219
|
+
attachment.uid !== tabUid ||
|
|
220
|
+
!isReportId(reportId) ||
|
|
221
|
+
selected.pageUid !== pageUid ||
|
|
222
|
+
selected.tabUid !== tabUid ||
|
|
223
|
+
!sameSetting('reportId', selected.reportId, reportId)
|
|
224
|
+
) {
|
|
225
|
+
fail(ctx, 400, 'Select report block destination');
|
|
226
|
+
}
|
|
227
|
+
const options =
|
|
228
|
+
app.db.sequelize.getDialect() === 'sqlite'
|
|
229
|
+
? { type: Transaction.TYPES.IMMEDIATE }
|
|
230
|
+
: { isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED };
|
|
231
|
+
return app.db.sequelize.transaction(options, async (transaction) => {
|
|
232
|
+
// Serialize retries for this tab, including its first write when there is no grid to lock yet.
|
|
233
|
+
const route = await app.db.getRepository('desktopRoutes').model.findOne({
|
|
234
|
+
where: { schemaUid: tabUid, type: 'tabs' },
|
|
235
|
+
transaction,
|
|
236
|
+
lock: true,
|
|
237
|
+
});
|
|
238
|
+
if (!route) fail(ctx, 403, 'Block page unavailable');
|
|
239
|
+
const page = await resolvePage(app, ctx, tabUid, true, transaction);
|
|
240
|
+
if (page.pageUid !== pageUid) fail(ctx, 403, 'Block page unavailable');
|
|
241
|
+
const report = await requirePublishedReport(reportId, ctx, transaction);
|
|
242
|
+
const repo = app.db.getRepository<FlowRepository>('flowModels');
|
|
243
|
+
const anchor = await repo.model.findByPk(tabUid, { transaction, lock: true });
|
|
244
|
+
if (!anchor) await repo.upsertModel({ uid: tabUid, use: 'RootPageTabModel' }, { transaction });
|
|
245
|
+
const gridUid = await repo.ensureRouteContent(
|
|
246
|
+
{ parentId: tabUid, subKey: 'grid', use: 'BlockGridModel' },
|
|
247
|
+
{ transaction },
|
|
248
|
+
);
|
|
249
|
+
const grid = await repo.findModelById(gridUid, { transaction });
|
|
250
|
+
if (grid?.use !== 'BlockGridModel') fail(ctx, 403, 'Block page unavailable');
|
|
251
|
+
await resolvePage(app, ctx, gridUid, true, transaction);
|
|
252
|
+
const existing = grid.subModels?.items?.find(
|
|
253
|
+
(item) =>
|
|
254
|
+
item.use === 'ElasticReportBlockModel' &&
|
|
255
|
+
sameSetting(
|
|
256
|
+
'reportId',
|
|
257
|
+
item.stepParams?.elasticReportBlockSettings?.editReport?.reportId ?? item.props?.reportId,
|
|
258
|
+
report.id,
|
|
259
|
+
),
|
|
260
|
+
);
|
|
261
|
+
const target = { pageUid, tabUid, gridUid, reportId: report.id };
|
|
262
|
+
if (existing) return { ...target, blockUid: existing.uid, created: false };
|
|
263
|
+
// GridModel places new items at the end in both legacy row layouts and nested v2 layouts.
|
|
264
|
+
const blockUid = await repo.upsertModel(
|
|
265
|
+
{
|
|
266
|
+
parentId: gridUid,
|
|
267
|
+
subKey: 'items',
|
|
268
|
+
subType: 'array',
|
|
269
|
+
use: 'ElasticReportBlockModel',
|
|
270
|
+
stepParams: { elasticReportBlockSettings: { editReport: { reportId: report.id } } },
|
|
271
|
+
},
|
|
272
|
+
{ transaction },
|
|
273
|
+
);
|
|
274
|
+
transaction.afterCommit(() => {
|
|
275
|
+
app.emit('ws:sendToUser', {
|
|
276
|
+
userId: extractCurrentUserId(ctx),
|
|
277
|
+
message: { type: BLOCK_ADDED_EVENT, payload: { gridUid, blockUid } },
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
return { ...target, blockUid, created: true };
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
async function run(args: Fields, ctx: Partial<Context>, write: boolean) {
|
|
284
|
+
assertAuthenticated(ctx);
|
|
285
|
+
const uid = args.blockUid;
|
|
286
|
+
if (typeof uid !== 'string' || !uid.trim()) fail(ctx, 400, 'Pick one Elastic Report block');
|
|
287
|
+
if ((await selectedContext(app, ctx, 'flow-model'))?.uid !== uid) fail(ctx, 400, 'Pick one Elastic Report block');
|
|
288
|
+
const patch = write ? readPatch(ctx, args) : undefined;
|
|
289
|
+
const transactionOptions =
|
|
290
|
+
write && app.db.sequelize.getDialect() === 'sqlite' ? { type: Transaction.TYPES.IMMEDIATE } : {};
|
|
291
|
+
return app.db.sequelize.transaction(transactionOptions, async (transaction) => {
|
|
292
|
+
const repo = app.db.getRepository<FlowRepository>('flowModels');
|
|
293
|
+
const row = await repo.model.findByPk(uid, { transaction, lock: write });
|
|
294
|
+
const raw = row?.get('options');
|
|
295
|
+
const options: unknown = typeof raw === 'string' ? JSON.parse(raw) : raw;
|
|
296
|
+
if (!isObject(options) || options.use !== 'ElasticReportBlockModel') fail(ctx, 404, 'Block page unavailable');
|
|
297
|
+
const page = await resolvePage(app, ctx, uid, write, transaction);
|
|
298
|
+
if ((write || args.pageUid !== undefined) && args.pageUid !== page.pageUid)
|
|
299
|
+
fail(ctx, 403, 'Block page unavailable');
|
|
300
|
+
const steps = isObject(options.stepParams) ? options.stepParams : {};
|
|
301
|
+
const flow = isObject(steps.elasticReportBlockSettings) ? steps.elasticReportBlockSettings : {};
|
|
302
|
+
const edit = isObject(flow.editReport) ? flow.editReport : {};
|
|
303
|
+
const stored = { ...(isObject(options.props) ? options.props : {}), ...edit };
|
|
304
|
+
const settings: Fields = {};
|
|
305
|
+
for (const [key, fallback] of Object.entries(defaults)) settings[key] = stored[key] ?? fallback;
|
|
306
|
+
if (patch) {
|
|
307
|
+
const { changes, expected } = patch;
|
|
308
|
+
if (typeof changes.reportId === 'number' || typeof changes.reportId === 'string') {
|
|
309
|
+
await requirePublishedReport(changes.reportId, ctx, transaction);
|
|
310
|
+
}
|
|
311
|
+
const pending = Object.keys(changes).filter((key) => !sameSetting(key, settings[key], changes[key]));
|
|
312
|
+
if (pending.some((key) => !sameSetting(key, settings[key], expected[key])))
|
|
313
|
+
fail(ctx, 409, 'Block settings changed');
|
|
314
|
+
if (pending.length) {
|
|
315
|
+
const legacyProps = Object.fromEntries(
|
|
316
|
+
Object.keys(defaults)
|
|
317
|
+
.filter((key) => stored[key] != null && !Object.hasOwn(edit, key))
|
|
318
|
+
.map((key) => [key, stored[key]]),
|
|
319
|
+
);
|
|
320
|
+
await repo.patch(
|
|
321
|
+
{
|
|
322
|
+
uid,
|
|
323
|
+
stepParams: {
|
|
324
|
+
...steps,
|
|
325
|
+
elasticReportBlockSettings: { ...flow, editReport: { ...legacyProps, ...edit, ...changes } },
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
{ transaction },
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
transaction.afterCommit(() => {
|
|
332
|
+
app.emit('ws:sendToUser', {
|
|
333
|
+
userId: extractCurrentUserId(ctx),
|
|
334
|
+
message: { type: BLOCK_SETTINGS_EVENT, payload: { blockUid: uid, changes } },
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
return { blockUid: uid, ...page, changes };
|
|
338
|
+
}
|
|
339
|
+
let reportTitle: string | undefined;
|
|
340
|
+
if (typeof settings.reportId === 'number' || typeof settings.reportId === 'string') {
|
|
341
|
+
const report = await reports.findReport(settings.reportId, transaction);
|
|
342
|
+
if (report?.dataView && canAccessReport(app, ctx, report, report.dataView)) reportTitle = report.title;
|
|
343
|
+
if (reportTitle === undefined) settings.reportId = null;
|
|
344
|
+
}
|
|
345
|
+
return { blockUid: uid, ...page, settings, reportTitle };
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
const target = { blockUid: { type: 'string' }, pageUid: { type: 'string' } };
|
|
349
|
+
return [
|
|
350
|
+
{
|
|
351
|
+
name: 'es_add_report_block',
|
|
352
|
+
title: PLUGIN_NAME + ':Add report block',
|
|
353
|
+
description:
|
|
354
|
+
'Append the published report selected in Report block destination to the selected page and tab. Reuse an existing block for that report in the same grid. Supply the exact IDs from the attached destination.',
|
|
355
|
+
defaultPermission: 'ASK',
|
|
356
|
+
inputSchema: {
|
|
357
|
+
type: 'object',
|
|
358
|
+
required: ['pageUid', 'tabUid', 'reportId'],
|
|
359
|
+
additionalProperties: false,
|
|
360
|
+
properties: {
|
|
361
|
+
pageUid: { type: 'string' },
|
|
362
|
+
tabUid: { type: 'string' },
|
|
363
|
+
reportId: propertySchemas.reportId,
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
execute: (args, ctx = {}) => addBlock(args, ctx),
|
|
367
|
+
},
|
|
368
|
+
{
|
|
369
|
+
name: 'es_describe_block',
|
|
370
|
+
title: PLUGIN_NAME + ':Inspect selected Elastic Report block',
|
|
371
|
+
description: 'Read display settings and the page UID of the Elastic Report block attached with Pick block.',
|
|
372
|
+
defaultPermission: 'ALLOW',
|
|
373
|
+
inputSchema: { type: 'object', required: ['blockUid'], properties: target, additionalProperties: false },
|
|
374
|
+
execute: (args, ctx = {}) => run(args, ctx, false),
|
|
375
|
+
},
|
|
376
|
+
{
|
|
377
|
+
name: 'es_configure_block',
|
|
378
|
+
title: PLUGIN_NAME + ':Configure selected Elastic Report block',
|
|
379
|
+
description:
|
|
380
|
+
'Update only the picked instance. Supply the page UID and previous values from es_describe_block. Use null as the previous reportId for an unbound block.',
|
|
381
|
+
defaultPermission: 'ASK',
|
|
382
|
+
inputSchema: {
|
|
383
|
+
type: 'object',
|
|
384
|
+
required: ['blockUid', 'pageUid', 'expected', 'changes'],
|
|
385
|
+
additionalProperties: false,
|
|
386
|
+
properties: {
|
|
387
|
+
...target,
|
|
388
|
+
changes: { type: 'object', minProperties: 1, additionalProperties: false, properties: propertySchemas },
|
|
389
|
+
expected: {
|
|
390
|
+
type: 'object',
|
|
391
|
+
minProperties: 1,
|
|
392
|
+
additionalProperties: false,
|
|
393
|
+
properties: { ...propertySchemas, reportId: { type: ['number', 'string', 'null'] } },
|
|
394
|
+
},
|
|
395
|
+
},
|
|
396
|
+
},
|
|
397
|
+
execute: (args, ctx = {}) => run(args, ctx, true),
|
|
398
|
+
},
|
|
399
|
+
];
|
|
400
|
+
}
|