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
@@ -83,7 +83,9 @@ function createElasticReportToolCatalog(app, reportService) {
83
83
  async execute(_args, ctx) {
84
84
  (0, import_acl_guard.assertAuthenticated)(ctx);
85
85
  const dataViews = await reportService.listDataViews();
86
- return { dataViews };
86
+ return {
87
+ dataViews: dataViews.filter((dv) => (0, import_acl_guard.canAccessDataView)(app, ctx, dv))
88
+ };
87
89
  }
88
90
  },
89
91
  {
@@ -118,6 +120,11 @@ function createElasticReportToolCatalog(app, reportService) {
118
120
  description: {
119
121
  type: "string",
120
122
  description: "Optional description of the dataset."
123
+ },
124
+ allowedRoles: {
125
+ type: "array",
126
+ items: { type: "string" },
127
+ description: "Optional role names allowed to access this Data View (empty means all authenticated viewers)."
121
128
  }
122
129
  }
123
130
  },
@@ -131,7 +138,8 @@ function createElasticReportToolCatalog(app, reportService) {
131
138
  dataSourceKey: typeof args.dataSourceKey === "string" ? args.dataSourceKey : "",
132
139
  indexPattern: typeof args.indexPattern === "string" ? args.indexPattern : "",
133
140
  timeField: typeof args.timeField === "string" ? args.timeField : null,
134
- description: typeof args.description === "string" ? args.description : null
141
+ description: typeof args.description === "string" ? args.description : null,
142
+ ...args.allowedRoles !== void 0 ? { allowedRoles: (0, import_acl_guard.normalizeRoleList)(args.allowedRoles) } : {}
135
143
  },
136
144
  (0, import_ElasticReportService.extractCurrentUserId)(ctx)
137
145
  );
@@ -166,6 +174,9 @@ function createElasticReportToolCatalog(app, reportService) {
166
174
  const refresh = args.refresh === true;
167
175
  if (refresh) {
168
176
  (0, import_acl_guard.assertCanManageReports)(app, ctx);
177
+ } else {
178
+ const dv = await reportService.getDataView(dataViewId);
179
+ (0, import_acl_guard.assertCanAccessDataView)(app, ctx, dv);
169
180
  }
170
181
  const { dataView, fields } = await reportService.inspectDataViewFields(dataViewId, refresh);
171
182
  return {
@@ -239,6 +250,36 @@ function createElasticReportToolCatalog(app, reportService) {
239
250
  }
240
251
  }
241
252
  },
253
+ lockedFilters: {
254
+ type: "array",
255
+ description: "Mandatory server-enforced filters (e.g. tenant or department isolation) that viewer interactive overrides can never remove or replace.",
256
+ items: {
257
+ type: "object",
258
+ required: ["field", "operator"],
259
+ properties: {
260
+ field: { type: "string" },
261
+ operator: {
262
+ type: "string",
263
+ enum: [
264
+ "eq",
265
+ "ne",
266
+ "in",
267
+ "not_in",
268
+ "gt",
269
+ "gte",
270
+ "lt",
271
+ "lte",
272
+ "between",
273
+ "contains",
274
+ "exists",
275
+ "not_exists"
276
+ ]
277
+ },
278
+ value: {},
279
+ valueTo: {}
280
+ }
281
+ }
282
+ },
242
283
  sort: {
243
284
  type: "array",
244
285
  items: {
@@ -280,14 +321,23 @@ function createElasticReportToolCatalog(app, reportService) {
280
321
  topN: { type: "number" },
281
322
  metricType: {
282
323
  type: "string",
283
- enum: ["count", "sum", "avg", "min", "max", "cardinality"]
324
+ enum: ["count", "sum", "avg", "min", "max", "cardinality", "p50", "p95", "p99"]
284
325
  },
285
326
  metricField: { type: "string" },
286
327
  metricLabel: { type: "string" },
287
- showHistogram: { type: "boolean" }
328
+ showHistogram: { type: "boolean" },
329
+ splitSeriesField: { type: "string" },
330
+ splitSeriesTopN: { type: "number" },
331
+ stackMode: { type: "string", enum: ["none", "stacked", "percent"] },
332
+ comparePreviousPeriod: { type: "boolean" }
288
333
  }
289
334
  },
290
- allowInteractiveControls: { type: "boolean" }
335
+ fieldFormats: {
336
+ type: "object",
337
+ description: 'Optional map of fieldName -> format ("duration_ms" | "bytes" | "percent" | "number" | "status_badge" | "url").'
338
+ },
339
+ allowInteractiveControls: { type: "boolean" },
340
+ allowExport: { type: "boolean" }
291
341
  }
292
342
  }
293
343
  }
@@ -298,7 +348,8 @@ function createElasticReportToolCatalog(app, reportService) {
298
348
  const { dataView, result } = await reportService.previewReport({
299
349
  dataViewId,
300
350
  spec: isRecord(args.spec) ? args.spec : { visualization: "table" },
301
- forMcp: true
351
+ forMcp: true,
352
+ userContext: (0, import_ElasticReportService.extractServerUserContext)(ctx)
302
353
  });
303
354
  return {
304
355
  dataView: {
@@ -309,12 +360,18 @@ function createElasticReportToolCatalog(app, reportService) {
309
360
  total: result.total,
310
361
  totalRelation: result.totalRelation,
311
362
  tookMs: result.tookMs,
363
+ ...result.timedOut ? { timedOut: true } : {},
364
+ ...result.shards ? { shards: result.shards } : {},
365
+ ...result.warnings && result.warnings.length > 0 ? { warnings: result.warnings } : {},
312
366
  visualization: result.visualization,
313
367
  columns: result.columns,
314
368
  sampleRows: result.rows.slice(0, 10),
315
369
  series: result.series,
370
+ ...result.seriesKeys ? { seriesKeys: result.seriesKeys } : {},
316
371
  histogram: result.histogram,
317
372
  metricValue: result.metricValue,
373
+ ...result.previousMetricValue !== void 0 ? { previousMetricValue: result.previousMetricValue } : {},
374
+ ...result.deltaPercent !== void 0 ? { deltaPercent: result.deltaPercent } : {},
318
375
  metricLabel: result.metricLabel,
319
376
  compiledDsl: result.compiledDsl
320
377
  };
@@ -347,7 +404,12 @@ function createElasticReportToolCatalog(app, reportService) {
347
404
  },
348
405
  published: {
349
406
  type: "boolean",
350
- description: "Whether the report is published for selection in page blocks (default true)."
407
+ description: "Publish for selection in blocks. Defaults to true for new reports. Omit to retain the current state on update."
408
+ },
409
+ allowedRoles: {
410
+ type: "array",
411
+ items: { type: "string" },
412
+ description: "Optional role names allowed to view/run this report (empty means all authenticated viewers)."
351
413
  },
352
414
  spec: {
353
415
  type: "object",
@@ -361,16 +423,19 @@ function createElasticReportToolCatalog(app, reportService) {
361
423
  {
362
424
  ...args.id !== void 0 ? { id: requireId(args.id, "id") } : {},
363
425
  title: typeof args.title === "string" ? args.title : "",
364
- description: typeof args.description === "string" ? args.description : null,
426
+ ...typeof args.description === "string" ? { description: args.description } : {},
365
427
  dataViewId: requireId(args.dataViewId, "dataViewId"),
366
- published: args.published !== false,
428
+ ...typeof args.published === "boolean" ? { published: args.published } : {},
429
+ ...args.allowedRoles !== void 0 ? { allowedRoles: (0, import_acl_guard.normalizeRoleList)(args.allowedRoles) } : {},
367
430
  spec: isRecord(args.spec) ? args.spec : { visualization: "table" }
368
431
  },
369
432
  (0, import_ElasticReportService.extractCurrentUserId)(ctx)
370
433
  );
371
434
  return {
372
435
  report: saved,
373
- usageHint: "Report saved and published. Users can open any NocoBase /v/ page, click Add Block -> Elastic Report, and select this report from the dropdown."
436
+ usageHint: app.i18n.t(saved.published ? "Published report ready for blocks" : "Draft saved in Settings", {
437
+ ns: "plugin-elastic-report"
438
+ })
374
439
  };
375
440
  }
376
441
  },
@@ -397,16 +462,175 @@ function createElasticReportToolCatalog(app, reportService) {
397
462
  const isManager = (0, import_acl_guard.canManageReports)(app, ctx);
398
463
  if (args.reportId !== void 0 && args.reportId !== null && String(args.reportId).trim() !== "") {
399
464
  const report = await reportService.getReport(requireId(args.reportId, "reportId"));
400
- if (!isManager && !report.published) {
401
- throw new Error("Report not found");
402
- }
465
+ const dataView = report.dataView ?? await reportService.getDataView(report.dataViewId);
466
+ (0, import_acl_guard.assertCanAccessReport)(app, ctx, report, dataView);
403
467
  return { report };
404
468
  }
405
469
  const reports = await reportService.listReports({
406
470
  publishedOnly: !isManager,
407
471
  search: typeof args.search === "string" ? args.search : void 0
408
472
  });
409
- return { reports };
473
+ return {
474
+ reports: reports.filter((r) => (0, import_acl_guard.canAccessReport)(app, ctx, r, r.dataView))
475
+ };
476
+ }
477
+ },
478
+ {
479
+ name: "es_analyze_report",
480
+ title: "Execute & Analyze Saved Elasticsearch Report (Viewer-Safe)",
481
+ description: "Execute a published Saved Elasticsearch Report under the caller's effective NocoBase role and mandatory server lockedFilters, optionally narrowing with KQL or timeRange overrides, to summarize anomalies, top buckets, KPI deltas, or sample logs.",
482
+ defaultPermission: "ALLOW",
483
+ inputSchema: {
484
+ type: "object",
485
+ required: ["reportId"],
486
+ properties: {
487
+ reportId: {
488
+ type: ["number", "string"],
489
+ description: "ID of the Saved Report from es_list_reports."
490
+ },
491
+ query: {
492
+ type: "string",
493
+ description: "Optional KQL override when the report allows interactive controls."
494
+ },
495
+ timePreset: {
496
+ type: "string",
497
+ enum: ["last_15m", "last_1h", "last_24h", "last_7d", "last_30d", "last_90d", "all"],
498
+ description: "Optional time preset override."
499
+ }
500
+ }
501
+ },
502
+ async execute(args, ctx) {
503
+ (0, import_acl_guard.assertAuthenticated)(ctx);
504
+ const reportId = requireId(args.reportId, "reportId");
505
+ const report = await reportService.getReport(reportId);
506
+ const dataView = report.dataView ?? await reportService.getDataView(report.dataViewId);
507
+ (0, import_acl_guard.assertCanAccessReport)(app, ctx, report, dataView);
508
+ const overrides = {};
509
+ if (typeof args.query === "string") {
510
+ overrides.query = args.query;
511
+ }
512
+ if (typeof args.timePreset === "string" && args.timePreset.trim()) {
513
+ overrides.timeRange = {
514
+ preset: args.timePreset
515
+ };
516
+ }
517
+ const { result } = await reportService.runSavedReport({
518
+ reportId,
519
+ overrides: Object.keys(overrides).length > 0 ? overrides : void 0,
520
+ requirePublished: false,
521
+ forMcp: true,
522
+ userContext: (0, import_ElasticReportService.extractServerUserContext)(ctx)
523
+ });
524
+ return {
525
+ report: {
526
+ id: report.id,
527
+ title: report.title,
528
+ visualization: report.visualization,
529
+ dataViewTitle: dataView.title,
530
+ indexPattern: dataView.indexPattern
531
+ },
532
+ total: result.total,
533
+ totalRelation: result.totalRelation,
534
+ tookMs: result.tookMs,
535
+ ...result.warnings ? { warnings: result.warnings } : {},
536
+ metricLabel: result.metricLabel,
537
+ metricValue: result.metricValue,
538
+ ...result.previousMetricValue !== void 0 ? { previousMetricValue: result.previousMetricValue } : {},
539
+ ...result.deltaPercent !== void 0 ? { deltaPercent: result.deltaPercent } : {},
540
+ series: result.series.slice(0, 15),
541
+ ...result.seriesKeys ? { seriesKeys: result.seriesKeys } : {},
542
+ histogram: result.histogram.slice(0, 24),
543
+ sampleRows: result.rows.slice(0, 15).map((r) => r.source)
544
+ };
545
+ }
546
+ },
547
+ {
548
+ name: "es_create_dashboard_bundle",
549
+ title: "Create Multi-Panel Elasticsearch Dashboard Bundle",
550
+ description: "Create a coordinated set of Saved Elasticsearch Reports (e.g. KPI with previous-period delta, trend line/area chart, stacked/grouped breakdown bar chart, and Discover table) sharing common Data View, lockedFilters, and allowedRoles, ready to combine with an Elastic Filter Bar on a NocoBase page.",
551
+ defaultPermission: "ALLOW",
552
+ inputSchema: {
553
+ type: "object",
554
+ required: ["dataViewId", "panels"],
555
+ properties: {
556
+ dataViewId: {
557
+ type: ["number", "string"],
558
+ description: "Target Data View ID."
559
+ },
560
+ allowedRoles: {
561
+ type: "array",
562
+ items: { type: "string" },
563
+ description: "Optional role names applied to every report in the bundle."
564
+ },
565
+ lockedFilters: {
566
+ type: "array",
567
+ description: "Optional mandatory server-enforced filters applied to every report in the bundle.",
568
+ items: {
569
+ type: "object",
570
+ required: ["field", "operator"],
571
+ properties: {
572
+ field: { type: "string" },
573
+ operator: { type: "string" },
574
+ value: {},
575
+ valueTo: {}
576
+ }
577
+ }
578
+ },
579
+ panels: {
580
+ type: "array",
581
+ minItems: 1,
582
+ maxItems: 8,
583
+ items: {
584
+ type: "object",
585
+ required: ["title", "spec"],
586
+ properties: {
587
+ title: { type: "string" },
588
+ description: { type: "string" },
589
+ spec: { type: "object" }
590
+ }
591
+ }
592
+ }
593
+ }
594
+ },
595
+ async execute(args, ctx) {
596
+ (0, import_acl_guard.assertCanManageReports)(app, ctx);
597
+ const dataViewId = requireId(args.dataViewId, "dataViewId");
598
+ const panels = Array.isArray(args.panels) ? args.panels : [];
599
+ if (panels.length === 0 || panels.length > 8) {
600
+ throw new Error('"panels" must contain between 1 and 8 report definitions');
601
+ }
602
+ const sharedRoles = args.allowedRoles !== void 0 ? (0, import_acl_guard.normalizeRoleList)(args.allowedRoles) : void 0;
603
+ const sharedLockedFilters = Array.isArray(args.lockedFilters) ? args.lockedFilters : void 0;
604
+ const userId = (0, import_ElasticReportService.extractCurrentUserId)(ctx);
605
+ const createdReports = [];
606
+ for (const panel of panels) {
607
+ if (!isRecord(panel)) continue;
608
+ const rawSpec = isRecord(panel.spec) ? { ...panel.spec } : { visualization: "table" };
609
+ if (sharedLockedFilters && !Array.isArray(rawSpec.lockedFilters)) {
610
+ rawSpec.lockedFilters = sharedLockedFilters;
611
+ }
612
+ const saved = await reportService.saveReport(
613
+ {
614
+ title: typeof panel.title === "string" ? panel.title : "Dashboard Panel",
615
+ description: typeof panel.description === "string" ? panel.description : null,
616
+ dataViewId,
617
+ published: true,
618
+ ...sharedRoles !== void 0 ? { allowedRoles: sharedRoles } : {},
619
+ spec: rawSpec
620
+ },
621
+ userId
622
+ );
623
+ createdReports.push({
624
+ id: saved.id,
625
+ title: saved.title,
626
+ visualization: saved.visualization
627
+ });
628
+ }
629
+ return {
630
+ createdCount: createdReports.length,
631
+ reports: createdReports,
632
+ usageHint: 'Add an "Elastic Filter Bar" block at the top of a NocoBase /v/ page, then add an "Elastic Report" block for each created report so all panels cross-filter together.'
633
+ };
410
634
  }
411
635
  }
412
636
  ];