@axiom-lattice/gateway 2.1.39 → 2.1.41

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiom-lattice/gateway",
3
- "version": "2.1.39",
3
+ "version": "2.1.41",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
@@ -21,6 +21,7 @@
21
21
  "@fastify/multipart": "^9.4.0",
22
22
  "@fastify/reply-from": "^12.5.0",
23
23
  "@fastify/sensible": "^6.0.3",
24
+ "@fastify/static": "^9.0.0",
24
25
  "@fastify/swagger": "^9.5.1",
25
26
  "@fastify/swagger-ui": "^5.2.3",
26
27
  "@fastify/websocket": "^11.0.1",
@@ -36,14 +37,14 @@
36
37
  "pino-roll": "^3.1.0",
37
38
  "redis": "^5.0.1",
38
39
  "uuid": "^9.0.1",
39
- "@axiom-lattice/core": "2.1.33",
40
+ "@axiom-lattice/core": "2.1.35",
40
41
  "@axiom-lattice/protocols": "2.1.20",
41
42
  "@axiom-lattice/queue-redis": "1.0.19"
42
43
  },
43
44
  "devDependencies": {
44
45
  "@types/jest": "^29.5.14",
45
46
  "@types/lodash": "^4.17.16",
46
- "@types/node": "^20.17.23",
47
+ "@types/node": "^20.19.13",
47
48
  "@types/uuid": "^9.0.8",
48
49
  "@types/ws": "^8.18.1",
49
50
  "@typescript-eslint/eslint-plugin": "^7.2.0",
@@ -0,0 +1,695 @@
1
+ # Data Query SDK v3.0.0 - 状态机版本
2
+
3
+ > **面向 AI 助手**: 本文档专为 AI 设计,包含完整的 API 说明和声明式配置规范
4
+ > **版本**: 3.0.0 | **更新日期**: 2025-01-15
5
+
6
+ ---
7
+
8
+ ## 快速开始
9
+
10
+ ### 1. 引入 SDK
11
+
12
+ ```html
13
+ <script src="http://localhost:4001/sdk/data-query-sdk.js"></script>
14
+ <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
15
+ <script src="https://cdn.tailwindcss.com"></script>
16
+ ```
17
+
18
+ ### 2. 声明式看板配置
19
+
20
+ ```javascript
21
+ const config = {
22
+ filters: [
23
+ {
24
+ id: 'year',
25
+ label: '年份',
26
+ dimension: 'date__year',
27
+ operator: 'EQ',
28
+ defaultValue: 'ALL',
29
+ options: [
30
+ { value: 'ALL', text: '全部' },
31
+ { value: '2024', text: '2024年' }
32
+ ],
33
+ emitsToState: 'selected_year' // 选择后写入全局状态
34
+ }
35
+ ],
36
+ widgets: [
37
+ {
38
+ id: 'yearly-pie',
39
+ type: 'pie',
40
+ title: '年度销售占比(点击下钻)',
41
+ gridClass: 'md:col-span-6',
42
+ query: {
43
+ serverKey: 'production',
44
+ datasourceId: '1',
45
+ metrics: ['sales'],
46
+ groupBy: ['date__year']
47
+ },
48
+ mapping: {
49
+ name: 'date__year',
50
+ value: 'sales'
51
+ },
52
+ emit: [{ // 点击时发射状态
53
+ event: 'click',
54
+ extractValueFrom: 'date__year',
55
+ updateState: 'drill_year'
56
+ }]
57
+ },
58
+ {
59
+ id: 'monthly-bar',
60
+ type: 'bar',
61
+ title: '月度销售趋势',
62
+ gridClass: 'md:col-span-6',
63
+ query: {
64
+ serverKey: 'production',
65
+ datasourceId: '1',
66
+ metrics: ['sales'],
67
+ groupBy: ['date__month']
68
+ },
69
+ mapping: {
70
+ xAxis: 'date__month',
71
+ yAxis: ['sales']
72
+ },
73
+ receiveFilters: [{ // 接收全局状态
74
+ fromState: 'selected_year',
75
+ mapToDimension: 'date', // 映射到原始维度字段
76
+ operator: 'BETWEEN', // 日期范围使用 BETWEEN
77
+ ignoreIfEmpty: true
78
+ }, {
79
+ fromState: 'drill_year',
80
+ mapToDimension: 'date', // 映射到原始维度字段
81
+ operator: 'BETWEEN', // 日期范围使用 BETWEEN
82
+ ignoreIfEmpty: true
83
+ }]
84
+ }
85
+ ]
86
+ };
87
+ ```
88
+
89
+ ### 3. 初始化看板
90
+
91
+ ```javascript
92
+ const sdk = new DataQuerySDK({
93
+ baseURL: 'http://localhost:4001'
94
+ });
95
+
96
+ const engine = new DashboardEngine(sdk, config);
97
+ await engine.init();
98
+ ```
99
+
100
+ ---
101
+
102
+ ## 核心概念
103
+
104
+ DashboardEngine 基于**发布-订阅**模型:
105
+
106
+ - **`emit`**: 图表或过滤器被操作时,将值写入全局状态池
107
+ - **`receiveFilters`**: 图表监听全局状态池,自动应用过滤条件
108
+
109
+ **工作流程:**
110
+ 1. 用户操作过滤器或点击图表
111
+ 2. 触发 `emit`,将值写入全局状态
112
+ 3. 订阅该状态的图表收到通知
113
+ 4. 自动刷新,应用新的过滤条件
114
+
115
+ ---
116
+
117
+ ## API 参考
118
+
119
+ ### DashboardEngine
120
+
121
+ #### constructor(sdk, config, stateManager?)
122
+
123
+ ```javascript
124
+ const engine = new DashboardEngine(sdk, config);
125
+ // 或使用自定义状态管理器
126
+ const engine = new DashboardEngine(sdk, config, new StateManager());
127
+ ```
128
+
129
+ **参数:**
130
+
131
+ | 参数 | 类型 | 必填 | 说明 |
132
+ |------|------|------|------|
133
+ | sdk | DataQuerySDK | ✅ | SDK 实例 |
134
+ | config | Object | ✅ | 看板配置 |
135
+ | stateManager | StateManager | ❌ | 自定义状态管理器 |
136
+
137
+ #### init()
138
+
139
+ 初始化看板:渲染布局、绑定事件、加载数据。
140
+
141
+ ```javascript
142
+ await engine.init();
143
+ ```
144
+
145
+ #### destroy()
146
+
147
+ 销毁看板,清理资源。
148
+
149
+ ```javascript
150
+ engine.destroy();
151
+ ```
152
+
153
+ ---
154
+
155
+ ## 配置 Schema
156
+
157
+ ### FilterConfig - 过滤器配置
158
+
159
+ ```typescript
160
+ interface FilterConfig {
161
+ id: string; // 唯一标识
162
+ label: string; // 显示标签
163
+ dimension: string; // 维度字段名
164
+ operator: 'EQ' | 'NE' | 'GT' | 'LT' | 'GTE' | 'LTE' | 'BETWEEN' | 'IN';
165
+ defaultValue: string; // 默认值
166
+ options: Array<{ value: string; text: string }>;
167
+ emitsToState: string; // 选择后写入的全局状态键
168
+ }
169
+ ```
170
+
171
+ **示例:**
172
+
173
+ ```javascript
174
+ {
175
+ id: 'region',
176
+ label: '区域',
177
+ dimension: 'region',
178
+ operator: 'EQ',
179
+ defaultValue: 'ALL',
180
+ options: [
181
+ { value: 'ALL', text: '全部' },
182
+ { value: 'north', text: '华北' },
183
+ { value: 'south', text: '华南' }
184
+ ],
185
+ emitsToState: 'selected_region'
186
+ }
187
+ ```
188
+
189
+ ---
190
+
191
+ ### WidgetConfig - 图表配置
192
+
193
+ ```typescript
194
+ interface WidgetConfig {
195
+ id: string; // 唯一标识
196
+ title: string; // 标题
197
+ type: 'bar' | 'line' | 'pie' | 'scatter' | 'kpi' | 'table';
198
+ gridClass?: string; // 布局类名(如 'md:col-span-6')
199
+
200
+ query: {
201
+ serverKey: string; // 指标服务器 key
202
+ datasourceId: string; // 数据源 ID
203
+ metrics: string[]; // 指标数组
204
+ groupBy?: string[]; // 分组维度
205
+ filters?: Filter[]; // 静态过滤器
206
+ };
207
+
208
+ mapping: WidgetMapping; // 数据映射
209
+
210
+ receiveFilters?: ReceiveFilter[]; // 接收的全局过滤器
211
+ emit?: EmitConfig[]; // 点击事件发射器
212
+ }
213
+ ```
214
+
215
+ ---
216
+
217
+ ### WidgetMapping - 数据映射
218
+
219
+ **柱状图/折线图 (bar/line):**
220
+
221
+ ```javascript
222
+ {
223
+ mapping: {
224
+ xAxis: 'date__month', // X 轴字段
225
+ yAxis: ['sales', 'profit'], // Y 轴字段数组
226
+ valueFormat: 'currency_cny' // 可选:数值格式化
227
+ }
228
+ }
229
+ ```
230
+
231
+ **饼图 (pie):**
232
+
233
+ ```javascript
234
+ {
235
+ mapping: {
236
+ name: 'region', // 分类字段
237
+ value: 'sales', // 数值字段
238
+ valueFormat: 'compact_number'
239
+ }
240
+ }
241
+ ```
242
+
243
+ **KPI 卡片 (kpi):**
244
+
245
+ ```javascript
246
+ {
247
+ mapping: {
248
+ value: 'total_sales', // 数值字段
249
+ valueFormat: 'currency_cny'
250
+ }
251
+ }
252
+ ```
253
+
254
+ **表格 (table):**
255
+
256
+ ```javascript
257
+ {
258
+ type: 'table',
259
+ title: '销售明细',
260
+ query: {
261
+ serverKey: 'production',
262
+ datasourceId: '1',
263
+ metrics: ['sales', 'profit'],
264
+ groupBy: ['product', 'region']
265
+ },
266
+ // table 类型不需要 mapping,直接显示所有列
267
+ tableConfig: {
268
+ pageSize: 10, // 每页显示行数(默认 10)
269
+ align: 'left', // 对齐方式:left/center/right
270
+ columnFormats: { // 列特定的格式化
271
+ 'sales': 'currency_cny',
272
+ 'profit': 'currency_cny'
273
+ },
274
+ defaultFormat: 'compact_number' // 默认格式化
275
+ },
276
+ emit: [{ // 点击行时发射状态
277
+ event: 'click',
278
+ extractValueFrom: 'product',
279
+ updateState: 'selected_product'
280
+ }]
281
+ }
282
+ ```
283
+
284
+ **支持的 valueFormat:**
285
+
286
+ | 格式 | 说明 | 示例 |
287
+ |------|------|------|
288
+ | `currency_cny` | 人民币 | ¥100,000 |
289
+ | `currency_usd` | 美元 | $100,000 |
290
+ | `percent` | 百分比 | 85.50% |
291
+ | `compact_number` | 紧凑数字 | 10万 |
292
+
293
+ ---
294
+
295
+ ### ReceiveFilter - 接收过滤器
296
+
297
+ 用于接收全局状态并映射为查询条件:
298
+
299
+ ```typescript
300
+ interface ReceiveFilter {
301
+ fromState: string; // 监听的全局状态键名
302
+ mapToDimension: string; // 映射到当前查询的维度
303
+ operator: string; // 操作符
304
+ ignoreIfEmpty?: boolean; // 空值时是否忽略
305
+ }
306
+ ```
307
+
308
+ **示例:**
309
+
310
+ ```javascript
311
+ {
312
+ fromState: 'selected_year', // 监听全局状态
313
+ mapToDimension: 'date', // 映射到原始维度字段(不是粒度表达式)
314
+ operator: 'BETWEEN', // 日期范围使用 BETWEEN
315
+ ignoreIfEmpty: true // 如果状态为空或 'ALL',则忽略
316
+ }
317
+ ```
318
+
319
+ **解决字段不一致问题:**
320
+
321
+ ```javascript
322
+ // Widget A - 销售数据
323
+ {
324
+ receiveFilters: [{
325
+ fromState: 'selected_region',
326
+ mapToDimension: 'region', // 映射到 'region' 字段
327
+ operator: 'EQ'
328
+ }]
329
+ }
330
+
331
+ // Widget B - 库存数据(不同字段名)
332
+ {
333
+ receiveFilters: [{
334
+ fromState: 'selected_region',
335
+ mapToDimension: 'warehouse_location', // 映射到 'warehouse_location'
336
+ operator: 'EQ'
337
+ }]
338
+ }
339
+ ```
340
+
341
+ ---
342
+
343
+ ### EmitConfig - 发射配置
344
+
345
+ 用于点击图表时发射状态:
346
+
347
+ ```typescript
348
+ interface EmitConfig {
349
+ event: 'click'; // 事件类型
350
+ extractValueFrom: string; // 从点击数据中提取的字段
351
+ updateState: string; // 写入的全局状态键名
352
+ }
353
+ ```
354
+
355
+ **示例:**
356
+
357
+ ```javascript
358
+ {
359
+ emit: [{
360
+ event: 'click',
361
+ extractValueFrom: 'date__year',
362
+ updateState: 'drill_year'
363
+ }]
364
+ }
365
+ ```
366
+
367
+ ---
368
+
369
+ ## 完整示例
370
+
371
+ ### 跨粒度联动(年度 → 月度)
372
+
373
+ ```javascript
374
+ const config = {
375
+ filters: [
376
+ {
377
+ id: 'year',
378
+ label: '年份',
379
+ dimension: 'date__year',
380
+ operator: 'EQ',
381
+ defaultValue: 'ALL',
382
+ options: [
383
+ { value: 'ALL', text: '全部年份' },
384
+ { value: '2023', text: '2023年' },
385
+ { value: '2024', text: '2024年' }
386
+ ],
387
+ emitsToState: 'selected_year'
388
+ }
389
+ ],
390
+ widgets: [
391
+ {
392
+ id: 'yearly-pie',
393
+ type: 'pie',
394
+ title: '年度销售占比(点击下钻)',
395
+ gridClass: 'md:col-span-6',
396
+ query: {
397
+ serverKey: 'sales',
398
+ datasourceId: '1',
399
+ metrics: ['sales'],
400
+ groupBy: ['date__year']
401
+ },
402
+ mapping: {
403
+ name: 'date__year',
404
+ value: 'sales',
405
+ valueFormat: 'currency_cny'
406
+ },
407
+ emit: [{
408
+ event: 'click',
409
+ extractValueFrom: 'date__year',
410
+ updateState: 'drill_year'
411
+ }]
412
+ },
413
+ {
414
+ id: 'monthly-bar',
415
+ type: 'bar',
416
+ title: '月度销售趋势',
417
+ gridClass: 'md:col-span-6',
418
+ query: {
419
+ serverKey: 'sales',
420
+ datasourceId: '1',
421
+ metrics: ['sales'],
422
+ groupBy: ['date__month']
423
+ },
424
+ mapping: {
425
+ xAxis: 'date__month',
426
+ yAxis: ['sales']
427
+ },
428
+ receiveFilters: [
429
+ {
430
+ fromState: 'selected_year',
431
+ mapToDimension: 'date', // 映射到原始维度字段
432
+ operator: 'BETWEEN', // 日期范围使用 BETWEEN
433
+ ignoreIfEmpty: true
434
+ },
435
+ {
436
+ fromState: 'drill_year',
437
+ mapToDimension: 'date', // 映射到原始维度字段
438
+ operator: 'BETWEEN', // 日期范围使用 BETWEEN
439
+ ignoreIfEmpty: true
440
+ }
441
+ ]
442
+ }
443
+ ]
444
+ };
445
+ ```
446
+
447
+ **交互效果:**
448
+ 1. 选择全局年份过滤器 → 饼图和柱状图都更新
449
+ 2. 点击饼图的某个年份 → 柱状图显示该年份的月度数据
450
+
451
+ ---
452
+
453
+ ### 多图表联动
454
+
455
+ ```javascript
456
+ const config = {
457
+ widgets: [
458
+ {
459
+ id: 'region-pie',
460
+ type: 'pie',
461
+ title: '区域分布',
462
+ query: { metrics: ['sales'], groupBy: ['region'] },
463
+ mapping: { name: 'region', value: 'sales' },
464
+ emit: [{
465
+ event: 'click',
466
+ extractValueFrom: 'region',
467
+ updateState: 'selected_region'
468
+ }]
469
+ },
470
+ {
471
+ id: 'product-bar',
472
+ type: 'bar',
473
+ title: '产品销售(按区域过滤)',
474
+ query: { metrics: ['sales'], groupBy: ['product'] },
475
+ mapping: { xAxis: 'product', yAxis: ['sales'] },
476
+ receiveFilters: [{
477
+ fromState: 'selected_region',
478
+ mapToDimension: 'region',
479
+ operator: 'EQ',
480
+ ignoreIfEmpty: true
481
+ }]
482
+ },
483
+ {
484
+ id: 'trend-line',
485
+ type: 'line',
486
+ title: '趋势(按区域过滤)',
487
+ query: { metrics: ['sales'], groupBy: ['date__month'] },
488
+ mapping: { xAxis: 'date__month', yAxis: ['sales'] },
489
+ receiveFilters: [{
490
+ fromState: 'selected_region',
491
+ mapToDimension: 'region',
492
+ operator: 'EQ',
493
+ ignoreIfEmpty: true
494
+ }]
495
+ }
496
+ ]
497
+ };
498
+ ```
499
+
500
+ ---
501
+
502
+ ## 配置验证
503
+
504
+ DashboardEngine 在初始化时会自动验证配置:
505
+
506
+ ```javascript
507
+ try {
508
+ const engine = new DashboardEngine(sdk, invalidConfig);
509
+ } catch (error) {
510
+ console.error(error.message);
511
+ // 输出:
512
+ // 配置错误:
513
+ // - widgets[0] 缺少 title
514
+ // - widgets[0].query 缺少 serverKey
515
+ // - 闭环校验失败: emit 状态 "drill_year" 没有对应的 receiveFilters 消费
516
+ }
517
+ ```
518
+
519
+ ### 手动验证配置
520
+
521
+ ```javascript
522
+ const result = validateDashboardConfig(config);
523
+
524
+ if (!result.valid) {
525
+ console.error('配置错误:');
526
+ result.errors.forEach(error => {
527
+ console.error(' -', error);
528
+ });
529
+ } else {
530
+ console.log('配置有效!');
531
+ }
532
+ ```
533
+
534
+ ---
535
+
536
+ ## DataQuerySDK
537
+
538
+ ### 构造函数
539
+
540
+ ```javascript
541
+ const sdk = new DataQuerySDK({
542
+ baseURL: 'http://localhost:4001', // Gateway 地址(必填)
543
+ timeout: 30000 // 超时时间(可选,默认 30 秒)
544
+ });
545
+ ```
546
+
547
+ > **注意:** `tenantId` 不需要手动传递!SDK 会自动从页面注入的 `window.__AI2APP_CONTEXT__` 中读取。
548
+
549
+ ### query(params)
550
+
551
+ 执行数据查询。
552
+
553
+ ```javascript
554
+ const result = await sdk.query({
555
+ serverKey: 'production_metrics',
556
+ datasourceId: '1',
557
+ metrics: ['net_sales_amt'],
558
+ groupBy: ['DocDate__month'],
559
+ filters: [{
560
+ dimension: 'DocDate',
561
+ operator: 'BETWEEN',
562
+ values: ['2025-01-01', '2025-12-31']
563
+ }]
564
+ });
565
+ ```
566
+
567
+ **返回值:**
568
+
569
+ ```javascript
570
+ {
571
+ success: true,
572
+ data: {
573
+ columns: [
574
+ { name: 'DocDate__month', type: 'date' },
575
+ { name: 'net_sales_amt', type: 'numeric' }
576
+ ],
577
+ rows: [
578
+ ['2025-01-01', 150000],
579
+ ['2025-02-01', 180000]
580
+ ],
581
+ rowsObject: [
582
+ { 'DocDate__month': '2025-01-01', 'net_sales_amt': 150000 },
583
+ { 'DocDate__month': '2025-02-01', 'net_sales_amt': 180000 }
584
+ ],
585
+ metadata: {
586
+ rowCount: 2,
587
+ executionTimeMs: 150
588
+ }
589
+ }
590
+ }
591
+ ```
592
+
593
+ ---
594
+
595
+ ## 完整 HTML 示例
596
+
597
+ ```html
598
+ <!DOCTYPE html>
599
+ <html>
600
+ <head>
601
+ <meta charset="UTF-8">
602
+ <script src="https://cdn.tailwindcss.com"></script>
603
+ <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
604
+ <script src="http://localhost:4001/sdk/data-query-sdk.js"></script>
605
+ </head>
606
+ <body class="bg-gray-50 p-8">
607
+ <div id="filters-container" class="mb-4"></div>
608
+ <div id="dashboard-grid" class="grid grid-cols-12 gap-4"></div>
609
+
610
+ <script>
611
+ const config = {
612
+ filters: [
613
+ {
614
+ id: 'year',
615
+ label: '年份',
616
+ dimension: 'date__year',
617
+ operator: 'EQ',
618
+ defaultValue: 'ALL',
619
+ options: [
620
+ { value: 'ALL', text: '全部' },
621
+ { value: '2024', text: '2024年' }
622
+ ],
623
+ emitsToState: 'selected_year'
624
+ }
625
+ ],
626
+ widgets: [
627
+ {
628
+ id: 'yearly-pie',
629
+ type: 'pie',
630
+ title: '年度销售占比',
631
+ gridClass: 'md:col-span-6',
632
+ query: {
633
+ serverKey: 'production',
634
+ datasourceId: '1',
635
+ metrics: ['sales'],
636
+ groupBy: ['date__year']
637
+ },
638
+ mapping: { name: 'date__year', value: 'sales' },
639
+ emit: [{
640
+ event: 'click',
641
+ extractValueFrom: 'date__year',
642
+ updateState: 'drill_year'
643
+ }]
644
+ },
645
+ {
646
+ id: 'monthly-bar',
647
+ type: 'bar',
648
+ title: '月度销售趋势',
649
+ gridClass: 'md:col-span-6',
650
+ query: {
651
+ serverKey: 'production',
652
+ datasourceId: '1',
653
+ metrics: ['sales'],
654
+ groupBy: ['date__month']
655
+ },
656
+ mapping: { xAxis: 'date__month', yAxis: ['sales'] },
657
+ receiveFilters: [{
658
+ fromState: 'drill_year',
659
+ mapToDimension: 'date', // 映射到原始维度字段
660
+ operator: 'BETWEEN', // 日期范围使用 BETWEEN
661
+ ignoreIfEmpty: true
662
+ }]
663
+ }
664
+ ]
665
+ };
666
+
667
+ async function init() {
668
+ const sdk = new DataQuerySDK({ baseURL: 'http://localhost:4001' });
669
+ const engine = new DashboardEngine(sdk, config);
670
+ await engine.init();
671
+ }
672
+
673
+ init().catch(console.error);
674
+ </script>
675
+ </body>
676
+ </html>
677
+ ```
678
+
679
+ ---
680
+
681
+ ## 测试
682
+
683
+ 访问测试页面查看完整测试套件:
684
+
685
+ ```
686
+ http://localhost:4001/sdk/test-dashboard.html
687
+ ```
688
+
689
+ ---
690
+
691
+ ## 版本信息
692
+
693
+ - **版本**: 3.0.0
694
+ - **更新日期**: 2025-01-15
695
+ - **文件位置**: `http://localhost:4001/sdk/data-query-sdk.js`