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
@@ -35,9 +35,12 @@ var import_server = require("@nocobase/server");
35
35
  var import_constants = require("../shared/constants");
36
36
  var import_acl_guard = require("./acl-guard");
37
37
  var import_ElasticReportMcpServer = require("./mcp/ElasticReportMcpServer");
38
+ var import_report_block_tools = require("./report-block-tools");
38
39
  var import_ElasticReportToolCatalog = require("./mcp/ElasticReportToolCatalog");
39
40
  var import_ElasticsearchConnectionService = require("./services/ElasticsearchConnectionService");
40
41
  var import_ElasticReportService = require("./services/ElasticReportService");
42
+ var import_ReportExportService = require("./services/ReportExportService");
43
+ var import_ElasticReportQueryInstruction = require("./workflow/ElasticReportQueryInstruction");
41
44
  function isRecord(value) {
42
45
  return typeof value === "object" && value !== null && !Array.isArray(value);
43
46
  }
@@ -61,9 +64,24 @@ function parseIdParam(value, label) {
61
64
  err.status = 400;
62
65
  throw err;
63
66
  }
67
+ function parseOverridesParam(raw) {
68
+ if (isRecord(raw)) {
69
+ return raw;
70
+ }
71
+ if (typeof raw === "string" && raw.trim()) {
72
+ try {
73
+ const parsed = JSON.parse(raw);
74
+ return isRecord(parsed) ? parsed : void 0;
75
+ } catch {
76
+ return void 0;
77
+ }
78
+ }
79
+ return void 0;
80
+ }
64
81
  class PluginElasticReportServer extends import_server.Plugin {
65
82
  connectionService;
66
83
  reportService;
84
+ exportService;
67
85
  mcpServer;
68
86
  toolsCatalog = [];
69
87
  schemaCollections = [import_constants.DATA_VIEWS_COLLECTION, import_constants.REPORTS_COLLECTION];
@@ -74,9 +92,10 @@ class PluginElasticReportServer extends import_server.Plugin {
74
92
  });
75
93
  this.connectionService = new import_ElasticsearchConnectionService.ElasticsearchConnectionService(this.app);
76
94
  this.reportService = new import_ElasticReportService.ElasticReportService(this.app, this.connectionService);
95
+ this.exportService = new import_ReportExportService.ReportExportService(this.connectionService, this.reportService);
77
96
  this.toolsCatalog = (0, import_ElasticReportToolCatalog.createElasticReportToolCatalog)(this.app, this.reportService);
78
97
  this.mcpServer = new import_ElasticReportMcpServer.ElasticReportMcpServer({
79
- version: ((_a = this.options) == null ? void 0 : _a.version) || "0.1.0",
98
+ version: ((_a = this.options) == null ? void 0 : _a.version) || "0.8.0",
80
99
  tools: this.toolsCatalog,
81
100
  logger: this.app.logger
82
101
  });
@@ -85,6 +104,7 @@ class PluginElasticReportServer extends import_server.Plugin {
85
104
  this.registerServiceResource();
86
105
  this.registerMcpEndpoint();
87
106
  this.registerAclRules();
107
+ this.registerWorkflowInstructions();
88
108
  this.registerAITools();
89
109
  await this.registerPluginMcpClientEntry();
90
110
  this.app.on("beforeStop", async () => {
@@ -92,6 +112,16 @@ class PluginElasticReportServer extends import_server.Plugin {
92
112
  await ((_a = this.connectionService) == null ? void 0 : _a.close());
93
113
  });
94
114
  }
115
+ registerWorkflowInstructions() {
116
+ const workflow = this.app.pm.get("workflow");
117
+ if (!workflow || typeof workflow.registerInstruction !== "function") {
118
+ return;
119
+ }
120
+ workflow.registerInstruction(
121
+ import_constants.WORKFLOW_INSTRUCTION_TYPE,
122
+ new import_ElasticReportQueryInstruction.ElasticReportQueryInstruction(workflow, this.reportService)
123
+ );
124
+ }
95
125
  async install(_options) {
96
126
  await this.ensureSchema();
97
127
  }
@@ -117,10 +147,32 @@ class PluginElasticReportServer extends import_server.Plugin {
117
147
  name: import_constants.SERVICE_RESOURCE,
118
148
  actions: {
119
149
  listConnections: async (ctx, next) => {
150
+ (0, import_acl_guard.assertCanManageReports)(this.app, ctx);
120
151
  ctx.body = await this.reportService.listConnections();
121
152
  await next();
122
153
  },
154
+ testConnection: async (ctx, next) => {
155
+ (0, import_acl_guard.assertCanManageReports)(this.app, ctx);
156
+ const payload = getActionPayload(ctx);
157
+ const dataSourceKey = typeof payload.dataSourceKey === "string" ? payload.dataSourceKey.trim() : "";
158
+ if (!dataSourceKey) {
159
+ ctx.throw(400, "dataSourceKey is required");
160
+ }
161
+ const indexPattern = typeof payload.indexPattern === "string" && payload.indexPattern.trim() ? payload.indexPattern.trim() : "*";
162
+ try {
163
+ ctx.body = await this.reportService.testConnection(dataSourceKey, indexPattern);
164
+ } catch (error) {
165
+ ctx.throw(400, error instanceof Error ? error.message : String(error));
166
+ }
167
+ await next();
168
+ },
169
+ listRoles: async (ctx, next) => {
170
+ (0, import_acl_guard.assertCanManageReports)(this.app, ctx);
171
+ ctx.body = await this.reportService.listAvailableRoles();
172
+ await next();
173
+ },
123
174
  listIndices: async (ctx, next) => {
175
+ (0, import_acl_guard.assertCanManageReports)(this.app, ctx);
124
176
  const payload = getActionPayload(ctx);
125
177
  const dataSourceKey = typeof payload.dataSourceKey === "string" ? payload.dataSourceKey.trim() : "";
126
178
  if (!dataSourceKey) {
@@ -138,12 +190,17 @@ class PluginElasticReportServer extends import_server.Plugin {
138
190
  const payload = getActionPayload(ctx);
139
191
  try {
140
192
  if (payload.dataViewId !== void 0 && payload.dataViewId !== null && String(payload.dataViewId).trim()) {
193
+ const dvId = parseIdParam(payload.dataViewId, "dataViewId");
141
194
  const refresh = payload.refresh === true || payload.refresh === "true";
142
- ctx.body = await this.reportService.inspectDataViewFields(
143
- parseIdParam(payload.dataViewId, "dataViewId"),
144
- refresh
145
- );
195
+ if (refresh) {
196
+ (0, import_acl_guard.assertCanManageReports)(this.app, ctx);
197
+ } else {
198
+ const dv = await this.reportService.getDataView(dvId);
199
+ (0, import_acl_guard.assertCanAccessDataView)(this.app, ctx, dv);
200
+ }
201
+ ctx.body = await this.reportService.inspectDataViewFields(dvId, refresh);
146
202
  } else {
203
+ (0, import_acl_guard.assertCanManageReports)(this.app, ctx);
147
204
  const dataSourceKey = typeof payload.dataSourceKey === "string" ? payload.dataSourceKey.trim() : "";
148
205
  const indexPattern = typeof payload.indexPattern === "string" ? payload.indexPattern.trim() : "";
149
206
  if (!dataSourceKey || !indexPattern) {
@@ -159,6 +216,7 @@ class PluginElasticReportServer extends import_server.Plugin {
159
216
  await next();
160
217
  },
161
218
  upsertDataView: async (ctx, next) => {
219
+ (0, import_acl_guard.assertCanManageReports)(this.app, ctx);
162
220
  const payload = getActionPayload(ctx);
163
221
  try {
164
222
  ctx.body = await this.reportService.upsertDataView(
@@ -168,23 +226,27 @@ class PluginElasticReportServer extends import_server.Plugin {
168
226
  dataSourceKey: typeof payload.dataSourceKey === "string" ? payload.dataSourceKey : "",
169
227
  indexPattern: typeof payload.indexPattern === "string" ? payload.indexPattern : "",
170
228
  timeField: typeof payload.timeField === "string" ? payload.timeField : null,
171
- description: typeof payload.description === "string" ? payload.description : null
229
+ description: typeof payload.description === "string" ? payload.description : null,
230
+ ...payload.allowedRoles !== void 0 ? { allowedRoles: (0, import_acl_guard.normalizeRoleList)(payload.allowedRoles) } : {}
172
231
  },
173
232
  (0, import_ElasticReportService.extractCurrentUserId)(ctx)
174
233
  );
175
234
  } catch (error) {
176
- ctx.throw(400, error instanceof Error ? error.message : String(error));
235
+ const status = (error == null ? void 0 : error.status) ?? 400;
236
+ ctx.throw(status, error instanceof Error ? error.message : String(error));
177
237
  }
178
238
  await next();
179
239
  },
180
240
  previewReport: async (ctx, next) => {
241
+ (0, import_acl_guard.assertCanManageReports)(this.app, ctx);
181
242
  const payload = getActionPayload(ctx);
182
243
  try {
183
244
  const dataViewId = parseIdParam(payload.dataViewId, "dataViewId");
184
245
  ctx.body = await this.reportService.previewReport({
185
246
  dataViewId,
186
247
  spec: payload.spec,
187
- overrides: isRecord(payload.overrides) ? payload.overrides : void 0
248
+ overrides: parseOverridesParam(payload.overrides),
249
+ userContext: (0, import_ElasticReportService.extractServerUserContext)(ctx)
188
250
  });
189
251
  } catch (error) {
190
252
  const status = (error == null ? void 0 : error.status) ?? 400;
@@ -193,6 +255,7 @@ class PluginElasticReportServer extends import_server.Plugin {
193
255
  await next();
194
256
  },
195
257
  saveReport: async (ctx, next) => {
258
+ (0, import_acl_guard.assertCanManageReports)(this.app, ctx);
196
259
  const payload = getActionPayload(ctx);
197
260
  try {
198
261
  ctx.body = await this.reportService.saveReport(
@@ -202,22 +265,25 @@ class PluginElasticReportServer extends import_server.Plugin {
202
265
  description: typeof payload.description === "string" ? payload.description : null,
203
266
  dataViewId: parseIdParam(payload.dataViewId, "dataViewId"),
204
267
  spec: payload.spec,
205
- published: payload.published !== false
268
+ published: payload.published !== false,
269
+ ...payload.allowedRoles !== void 0 ? { allowedRoles: (0, import_acl_guard.normalizeRoleList)(payload.allowedRoles) } : {}
206
270
  },
207
271
  (0, import_ElasticReportService.extractCurrentUserId)(ctx)
208
272
  );
209
273
  } catch (error) {
210
- ctx.throw(400, error instanceof Error ? error.message : String(error));
274
+ const status = (error == null ? void 0 : error.status) ?? 400;
275
+ ctx.throw(status, error instanceof Error ? error.message : String(error));
211
276
  }
212
277
  await next();
213
278
  },
214
279
  listPublishedReports: async (ctx, next) => {
215
280
  const payload = getActionPayload(ctx);
216
281
  const search = typeof payload.search === "string" ? payload.search : void 0;
217
- ctx.body = await this.reportService.listReports({
282
+ const publishedReports = await this.reportService.listReports({
218
283
  publishedOnly: true,
219
284
  search
220
285
  });
286
+ ctx.body = publishedReports.filter((report) => (0, import_acl_guard.canAccessReport)(this.app, ctx, report, report.dataView));
221
287
  await next();
222
288
  },
223
289
  getReportMeta: async (ctx, next) => {
@@ -227,11 +293,8 @@ class PluginElasticReportServer extends import_server.Plugin {
227
293
  try {
228
294
  const reportId = parseIdParam(rawId, "reportId");
229
295
  const report = await this.reportService.getReport(reportId);
230
- const isManager = (0, import_acl_guard.canManageReports)(this.app, ctx);
231
- if (!isManager && !report.published) {
232
- ctx.throw(404, "Report not found");
233
- }
234
296
  const { dataView, fields } = await this.reportService.inspectDataViewFields(report.dataViewId, false);
297
+ (0, import_acl_guard.assertCanAccessReport)(this.app, ctx, report, dataView);
235
298
  ctx.body = { report, dataView, fields };
236
299
  } catch (error) {
237
300
  const status = (error == null ? void 0 : error.status) ?? 400;
@@ -245,11 +308,48 @@ class PluginElasticReportServer extends import_server.Plugin {
245
308
  const rawId = payload.reportId ?? ((_b = (_a = ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.filterByTk);
246
309
  try {
247
310
  const reportId = parseIdParam(rawId, "reportId");
248
- const isManager = (0, import_acl_guard.canManageReports)(this.app, ctx);
311
+ const report = await this.reportService.getReport(reportId);
312
+ const dataView = report.dataView ?? await this.reportService.getDataView(report.dataViewId);
313
+ (0, import_acl_guard.assertCanAccessReport)(this.app, ctx, report, dataView);
249
314
  ctx.body = await this.reportService.runSavedReport({
250
315
  reportId,
251
- overrides: isRecord(payload.overrides) ? payload.overrides : void 0,
252
- requirePublished: !isManager
316
+ overrides: parseOverridesParam(payload.overrides),
317
+ requirePublished: false,
318
+ userContext: (0, import_ElasticReportService.extractServerUserContext)(ctx)
319
+ });
320
+ } catch (error) {
321
+ const status = (error == null ? void 0 : error.status) ?? 400;
322
+ ctx.throw(status, error instanceof Error ? error.message : String(error));
323
+ }
324
+ await next();
325
+ },
326
+ inspectTopValues: async (ctx, next) => {
327
+ const payload = getActionPayload(ctx);
328
+ try {
329
+ const field = typeof payload.field === "string" ? payload.field.trim() : "";
330
+ if (!field) {
331
+ ctx.throw(400, "field is required");
332
+ }
333
+ let dataViewId;
334
+ let reportId;
335
+ if (payload.reportId !== void 0 && payload.reportId !== null && String(payload.reportId).trim() !== "") {
336
+ reportId = parseIdParam(payload.reportId, "reportId");
337
+ const report = await this.reportService.getReport(reportId);
338
+ const dataView = report.dataView ?? await this.reportService.getDataView(report.dataViewId);
339
+ (0, import_acl_guard.assertCanAccessReport)(this.app, ctx, report, dataView);
340
+ dataViewId = report.dataViewId;
341
+ } else {
342
+ dataViewId = parseIdParam(payload.dataViewId, "dataViewId");
343
+ const dataView = await this.reportService.getDataView(dataViewId);
344
+ (0, import_acl_guard.assertCanAccessDataView)(this.app, ctx, dataView);
345
+ }
346
+ ctx.body = await this.reportService.inspectFieldTopValues({
347
+ dataViewId,
348
+ ...reportId !== void 0 ? { reportId } : {},
349
+ field,
350
+ size: typeof payload.size === "number" ? payload.size : Number(payload.size) || 10,
351
+ overrides: parseOverridesParam(payload.overrides),
352
+ userContext: (0, import_ElasticReportService.extractServerUserContext)(ctx)
253
353
  });
254
354
  } catch (error) {
255
355
  const status = (error == null ? void 0 : error.status) ?? 400;
@@ -257,6 +357,37 @@ class PluginElasticReportServer extends import_server.Plugin {
257
357
  }
258
358
  await next();
259
359
  },
360
+ exportReport: async (ctx, next) => {
361
+ var _a, _b;
362
+ const payload = getActionPayload(ctx);
363
+ const rawId = payload.reportId ?? ((_b = (_a = ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.filterByTk);
364
+ try {
365
+ const reportId = parseIdParam(rawId, "reportId");
366
+ const report = await this.reportService.getReport(reportId);
367
+ const dataView = report.dataView ?? await this.reportService.getDataView(report.dataViewId);
368
+ (0, import_acl_guard.assertCanAccessReport)(this.app, ctx, report, dataView);
369
+ const format = payload.format === "ndjson" ? "ndjson" : "csv";
370
+ const columns = Array.isArray(payload.columns) ? payload.columns.filter((c) => typeof c === "string" && Boolean(c.trim())) : typeof payload.columns === "string" && payload.columns.trim() ? payload.columns.split(",").map((c) => c.trim()).filter(Boolean) : void 0;
371
+ const maxRows = typeof payload.maxRows === "number" ? payload.maxRows : payload.maxRows !== void 0 ? Number(payload.maxRows) : void 0;
372
+ const { stream, filename, contentType } = await this.exportService.createExportStream({
373
+ reportId,
374
+ format,
375
+ overrides: parseOverridesParam(payload.overrides),
376
+ ...columns ? { columns } : {},
377
+ ...maxRows !== void 0 && Number.isFinite(maxRows) ? { maxRows } : {},
378
+ userContext: (0, import_ElasticReportService.extractServerUserContext)(ctx)
379
+ });
380
+ ctx.withoutDataWrapping = true;
381
+ ctx.status = 200;
382
+ ctx.type = contentType;
383
+ ctx.set("Content-Disposition", `attachment; filename="${filename}"`);
384
+ ctx.body = stream;
385
+ } catch (error) {
386
+ const status = (error == null ? void 0 : error.status) ?? 400;
387
+ ctx.throw(status, error instanceof Error ? error.message : String(error));
388
+ }
389
+ await next();
390
+ },
260
391
  getMcpInfo: async (ctx, next) => {
261
392
  ctx.body = {
262
393
  serverName: import_constants.MCP_SERVER_NAME,
@@ -284,7 +415,11 @@ class PluginElasticReportServer extends import_server.Plugin {
284
415
  });
285
416
  }
286
417
  registerAclRules() {
287
- this.app.acl.allow(import_constants.SERVICE_RESOURCE, ["listPublishedReports", "getReportMeta", "runReport"], "loggedIn");
418
+ this.app.acl.allow(
419
+ import_constants.SERVICE_RESOURCE,
420
+ ["listPublishedReports", "getReportMeta", "runReport", "inspectTopValues", "exportReport"],
421
+ "loggedIn"
422
+ );
288
423
  this.app.acl.allow(import_constants.MCP_RESOURCE_NAME, "*", "loggedIn");
289
424
  this.app.acl.registerSnippet({
290
425
  name: import_constants.ACL_SNIPPET,
@@ -314,7 +449,7 @@ class PluginElasticReportServer extends import_server.Plugin {
314
449
  return;
315
450
  }
316
451
  toolsManager.registerTools(
317
- this.toolsCatalog.map((tool) => ({
452
+ [...this.toolsCatalog, ...(0, import_report_block_tools.createReportBlockTools)(this.app, this.reportService)].map((tool) => ({
318
453
  scope: "CUSTOM",
319
454
  defaultPermission: tool.defaultPermission,
320
455
  execution: "backend",
@@ -370,8 +505,7 @@ class PluginElasticReportServer extends import_server.Plugin {
370
505
  await collection.model.sync();
371
506
  columns = await queryInterface.describeTable(tableName);
372
507
  }
373
- const attributes = collection.model.rawAttributes;
374
- for (const [attributeName, attribute] of Object.entries(attributes)) {
508
+ for (const [attributeName, attribute] of Object.entries(collection.model.rawAttributes)) {
375
509
  const columnName = attribute.field || attributeName;
376
510
  if (columns && columnName in columns) {
377
511
  continue;