@guandata/guanmetric 0.1.10 → 0.1.12

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## @guandata/guanmetric 0.1.12 - 2026-09-01
4
+
5
+ - 新增业务字典目录树、查询、新建、原地编辑和删除能力,支持业务属性筛选、dry-run 和关联指标影响确认。
6
+ - 指标模板新增机器 schema、原子/复合最小模板、无副作用 `--check-only` 预检及稳定单元格诊断,创建前可按错误码精确修正。
7
+ - 统一指标定义与查询加速的 WebFilterData 规范化和字段元数据校验,修复业务限定筛选保存后无法回显的问题。
8
+ - 查询加速筛选条件新增兼容别名转换、空值和嵌套组合校验,无法无损转换时会在提交前明确拒绝。
9
+
10
+ ## @guandata/guanmetric 0.1.11 - 2026-08-27
11
+
12
+ - 拼错二级子命令时会明确报错并返回非零退出,不再静默忽略无效输入。
13
+ - 同步结构化错误契约,自动化流程可更准确地区分请求、认证和后端业务失败。
14
+
3
15
  ## @guandata/guanmetric 0.1.10 - 2026-08-25
4
16
 
5
17
  - 支持企业 OIDC 认证上下文,并同步升级底层请求兼容能力,改善受控运行环境中的指标管理稳定性。
package/README.md CHANGED
@@ -13,20 +13,42 @@ npm install -g --foreground-scripts @guandata/guanmetric
13
13
  ## 常用命令
14
14
 
15
15
  ```bash
16
+ guanmetric template schema --type atomic
17
+ guanmetric template init --type atomic --out atomic_metrics.xlsx
18
+ guanmetric template normalize 客户指标.xlsx --check-only --output json-v1
16
19
  guanmetric template normalize 客户指标.xlsx --out standard.xlsx --report report.json
20
+ guanmetric template explain-error report.json --json
17
21
  guanmetric project-create 经营指标 --dry-run
18
22
  guanmetric dir-create 广告分析 --project-id <project_id> --dry-run
19
23
  guanmetric create --file atomic_metric.json --dry-run
20
24
  guanmetric edit <metric_id> --file patch.json --dry-run
21
25
  guanmetric delete <metric_id>
22
26
  guanmetric public-dim create --file public_dim.json --dry-run
27
+ guanmetric business-dict list 销售 -f table
28
+ guanmetric business-dict get <business_dict_id>
29
+ guanmetric business-dict create --file business_dict.json --dry-run
30
+ guanmetric business-dict edit <business_dict_id> --set desc=新口径 --dry-run
31
+ guanmetric business-dict delete <business_dict_id>
23
32
  guanmetric accelerate create <metric_id> --ds-id <dsId> --fd-id <fdId> --aggr-type SUM --filter-file filter.json --dry-run
24
33
  guanmetric metric-tree create 销售额指标树 --metric-id <metric_id> --parent-dir-id <metric_tree_dir_id> --dry-run
25
34
  guanmetric metric-tree update-config <metric_tree_id> --file metric_tree_config.json --dry-run
26
35
  ```
27
36
 
37
+ 指标模板命令提供机器可读 schema、原子/复合指标最小模板和无副作用预检。`--check-only` 不写 Excel/report,也不创建或修改指标;失败结果通过稳定的 `TEMPLATE_*` 错误码以及 Sheet、行、列、期望值和修正示例定位。
38
+
28
39
  ## 版本更新
29
40
 
41
+ ### @guandata/guanmetric 0.1.12
42
+
43
+ - 新增业务字典目录树及增删改查,支持业务属性筛选、dry-run 和删除影响确认。
44
+ - 指标模板新增机器 schema、最小模板、`--check-only` 预检和稳定单元格诊断。
45
+ - 规范指标与查询加速筛选条件,修复业务限定保存后无法回显并强化提交前校验。
46
+
47
+ ### @guandata/guanmetric 0.1.11
48
+
49
+ - 拼错二级子命令会明确报错并非零退出。
50
+ - 结构化错误可更准确地区分请求、认证和后端业务失败。
51
+
30
52
  ### @guandata/guanmetric 0.1.10
31
53
 
32
54
  - 支持企业 OIDC 认证上下文,并升级底层请求兼容能力,改善受控环境中的指标管理稳定性。
Binary file
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guandata/guanmetric",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "description": "观远 BI 指标写操作工具 - 指标创建编辑删除、指标目录、公共维度、Excel 标准化",
5
5
  "bin": {
6
6
  "guanmetric": "bin/run.js"
@@ -37,7 +37,6 @@
37
37
  "node": ">=14"
38
38
  },
39
39
  "publishConfig": {
40
- "registry": "https://registry.npmjs.org/",
41
- "access": "public"
40
+ "registry": "https://registry.npmjs.org/"
42
41
  }
43
42
  }
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: guanmetric
3
- description: 当用户要创建、编辑、删除观远 BI 指标,创建指标主题/指标目录,创建或配置指标树(含维度拆解、指标拆解公式、复合指标公式展开),批量标准化 Excel 指标模板,或管理指标公共维度时使用本 skill。查询指标详情、搜索指标、指标数据查询仍使用 guancli。
3
+ description: 当用户要创建、编辑、删除观远 BI 指标,创建指标主题/指标目录,创建或配置指标树(含维度拆解、指标拆解公式、复合指标公式展开),批量标准化 Excel 指标模板,管理指标公共维度,或查询目录树及增删改查业务字典时使用本 skill。查询指标详情、搜索指标、指标数据查询仍使用 guancli。
4
4
  compatibility: "Requires Node.js 14+. Install via npm link --foreground-scripts (local) or npm install -g --foreground-scripts @guandata/guanmetric (from internal Nexus registry) so the AI skill refresh result is visible. CLI command: guanmetric."
5
5
  ---
6
6
 
@@ -11,17 +11,27 @@ compatibility: "Requires Node.js 14+. Install via npm link --foreground-scripts
11
11
  常用命令:
12
12
 
13
13
  ```bash
14
+ guanmetric template schema --type atomic
15
+ guanmetric template init --type atomic --out atomic_metrics.xlsx
16
+ guanmetric template init --type composite --out composite_metrics.xlsx
17
+ guanmetric template normalize 客户指标.xlsx --check-only --output json-v1 --skip-biz-attrs --skip-id-resolve
14
18
  guanmetric template normalize 客户指标.xlsx --out standard.xlsx --report report.json
19
+ guanmetric template explain-error report.json --json
15
20
  guanmetric project-create 经营指标 --dry-run
16
21
  guanmetric dir-create 广告分析 --project-id <project_id> --dry-run
17
22
  guanmetric create --file atomic_metric.json --dry-run
18
23
  guanmetric edit <metric_id> --file patch.json --dry-run
19
24
  guanmetric delete <metric_id>
20
25
  guanmetric public-dim create --file public_dim.json --dry-run
26
+ guanmetric business-dict list 销售 -f table
27
+ guanmetric business-dict get <business_dict_id>
28
+ guanmetric business-dict create --file business_dict.json --dry-run
29
+ guanmetric business-dict edit <business_dict_id> --set desc=新口径 --dry-run
30
+ guanmetric business-dict delete <business_dict_id>
21
31
  guanmetric accelerate create <metric_id> --ds-id <dsId> --fd-id <fdId> --aggr-type SUM --dim 省份 --dry-run
22
32
  guanmetric accelerate create <metric_id> --ds-id <dsId> --fd-id <fdId> --aggr-type SUM --filter-file filter.json --dry-run
23
33
  guanmetric accelerate edit <accelerate_metric_id> --file accelerate_query.json --dry-run
24
- guanmetric accelerate edit <accelerate_metric_id> --filter-json '{"type":"condition","not":false,"value":{"fdId":"fd_province","filterType":"IN","filterValue":[["浙江"]]}}' --dry-run
34
+ guanmetric accelerate edit <accelerate_metric_id> --filter-json '{"type":"condition","not":false,"value":{"dsId":"ds_sales","fdId":"fd_province","name":"省份","fdType":"STRING","metaType":"DIM","filterType":"IN","filterValue":["浙江"]}}' --dry-run
25
35
  guanmetric accelerate priority <metric_id> --priority <accelerate_metric_id>=2 --dry-run
26
36
  guanmetric accelerate delete <accelerate_metric_id> --confirm
27
37
  guanmetric metric-tree create 销售额指标树 --metric-id <metric_id> --parent-dir-id <metric_tree_dir_id> --dry-run
@@ -31,10 +41,12 @@ guanmetric fetch POST /api/metric-platform/metrics/composite-metrics --file comp
31
41
 
32
42
  ## 编辑红线:编辑 ≠ 删除重建
33
43
 
34
- 用户要求**编辑/重命名/修改**已有指标时,必须用 `guanmetric edit <metric_id>` 原地更新(默认按线上详情合并 patch,改名用 `--set name=新名`);公共维度用 `public-dim edit`;查询加速用 `accelerate edit <accelerate_metric_id>` 原地更新或 `accelerate priority <metric_id>` 调整顺序;指标树用 `metric-tree rename`、`metric-tree move` 或 `metric-tree update-config` 原地更新。**严禁**用"delete 后重新 create 一个同名指标/指标树/查询加速"来模拟编辑或改名:指标 ID 被复合指标、衍生指标、指标卡片和指标树引用,删除重建会让这些引用全部断链,指标上的权限和历史配置也会丢失且无法恢复。指标树删除重建还会改变指标树 ID,导致归因入口、目录引用和外部链接失效。`delete` 只用于用户明确要求删除该指标本身。指标主题/目录目前只支持创建,不支持改名或删除;用户要求时直接说明 CLI 暂不支持,不要新建一个新主题/目录来替代。无论通过哪个命令或接口,都不得把"编辑"拆成"删除 + 重建"的组合来绕过这条红线。只有用户明确指示"先新建替代、再删除旧的"时才允许该流程,且执行前必须列出会断链和丢失的内容并获得确认。
44
+ 用户要求**编辑/重命名/修改**已有指标时,必须用 `guanmetric edit <metric_id>` 原地更新(默认按线上详情合并 patch,改名用 `--set name=新名`);业务字典用 `business-dict edit`;公共维度用 `public-dim edit`;查询加速用 `accelerate edit <accelerate_metric_id>` 原地更新或 `accelerate priority <metric_id>` 调整顺序;指标树用 `metric-tree rename`、`metric-tree move` 或 `metric-tree update-config` 原地更新。**严禁**用"delete 后重新 create 一个同名指标/业务字典/指标树/查询加速"来模拟编辑或改名:资源 ID 被其他对象引用,删除重建会导致引用、权限或历史配置丢失。业务字典删除时,服务端会先解除正式指标和草稿的 `bizMetricId`,并把字典的业务口径和业务属性保留到指标,再逻辑删除字典;因此删除前必须先查看关联指标并取得明确确认。指标树删除重建还会改变指标树 ID,导致归因入口、目录引用和外部链接失效。`delete` 只用于用户明确要求删除资源本身。指标主题/目录目前只支持创建,不支持改名或删除;用户要求时直接说明 CLI 暂不支持,不要新建一个新主题/目录来替代。无论通过哪个命令或接口,都不得把"编辑"拆成"删除 + 重建"的组合来绕过这条红线。只有用户明确指示"先新建替代、再删除旧的"时才允许该流程,且执行前必须列出会断链和丢失的内容并获得确认。
35
45
 
36
46
  批量创建指标时,必须先生成固定结构的标准 Excel 返回给用户确认;确认前不生成最终 payload,不提交真实创建。
37
47
 
48
+ 生成或校验原子/复合指标输入前,先执行 `guanmetric template schema --type atomic|composite` 获取机器契约;没有现成输入时用 `template init --type atomic|composite` 生成带合法示例的最小模板。进入正式 normalize 前优先运行 `template normalize <file> --check-only --output json-v1`:该命令不写 Excel/report,也不创建、修改或上线指标;`valid=false` 时按 diagnostics 中的稳定 code、Sheet、row、column、expected 和 example 修正。code/column 由校验规则直接产生,不从中文 message 推断;修改展示文案不会改变机器诊断。`TEMPLATE_NEEDS_REVIEW` 不属于真实错误,只有 severity=error 才使命令非零退出。已有普通 normalize report 可用 `template explain-error <report.json> --json` 提取同一诊断契约。
49
+
38
50
  如果用户提供的是客户原始 Excel/CSV,而不是已经接近标准模板的文件,agent 必须先自行读取并理解表格语义,产出一个语义映射中间表(建议命名为 `*.ai_mapped.xlsx`),再执行 `guanmetric template normalize` 生成最终标准模板。不要直接把客户原始 Excel 丢给 CLI 后交付结果;CLI 不负责穷举客户列名或业务表述。CLI 继续负责固定模板结构、必填项标记、业务属性配置读取、自动填充说明、行状态、问题说明、高亮样式、列宽自适应、基础校验和 report 输出。
39
51
 
40
52
  如果用户没有提供 Excel/CSV,而是给出一段文字、需求描述、粘贴的半结构化表格、截图转写内容或其它非文件信息,也必须先由 agent 理解语义并整理成同样的 `*.ai_mapped.xlsx` 中间表,再执行 `guanmetric template normalize` 生成标准模板。信息不足时不要直接放弃;应尽量补出可判断的指标行,并在标准模板的 `问题说明` 中列出缺失的主题、目录、数据集、字段、聚合方式、公式、衍生自、适用维度、时间维度或业务属性,让用户在返回的标准模板中修改后重新上传。
@@ -49,16 +61,18 @@ guanmetric fetch POST /api/metric-platform/metrics/composite-metrics --file comp
49
61
 
50
62
  生成衍生指标模板时,如果 `衍生自` 指标不是已明确存在的线上指标,而是本批也要创建的指标,必须在标准模板中补充对应的原子指标或复合指标行;信息不足时先补行并在该基础指标行的 `问题说明` 中列出缺失的数据集、来源字段、聚合方式、计算公式、适用维度等。不要等到创建计划阶段才临时新增基础指标。
51
63
 
52
- `fetch` 只用于指标平台写入接口尚未封装为专用命令的场景,例如复合指标/衍生指标创建、编辑和影响校验;常规原子指标、主题、目录、公共维度优先使用专用命令。
53
- 查询加速写操作已封装为 `accelerate` 专用命令;查询加速列表查询使用 `guancli metric accelerate list <metric_id>`。查询加速筛选条件使用和指标定义一致的 `WebFilterData` 结构,可放在完整请求体的 `filter` 字段,也可通过 `--filter-json` 或 `--filter-file` 传入;`not: true` 表示对该条件或条件组取反,清空筛选条件可传 `--filter-json null`。
64
+ `fetch` 只用于指标平台写入接口尚未封装为专用命令的场景,例如复合指标/衍生指标创建、编辑和影响校验;常规原子指标、主题、目录、公共维度优先使用专用命令。指标定义写入时,`fetch` 会规范化并校验顶层 `filter`:将兼容别名和 `not: true` 转成 Web 编辑器可识别的明确筛选类型,为空值筛选补 `filterValue: []`,并从当前指标或基础指标的 `applicableDims` 补齐字段元数据;无法无损转换时拒绝提交。业务限定衍生指标还会确认每个条件的 `fdId` 都存在于基础指标 `applicableDims`。
65
+ 查询加速写操作已封装为 `accelerate` 专用命令;查询加速列表查询使用 `guancli metric accelerate list <metric_id>`。查询加速筛选条件使用和指标定义一致的 `WebFilterData` 结构,可放在完整请求体的 `filter` 字段,也可通过 `--filter-json` 或 `--filter-file` 传入;合法节点仅为 `condition/operator/composition`,禁止使用 `group/combineType/conditions`。查询加速无法从指标适用维度自动补齐另一数据集的字段元数据,因此条件必须显式提供 `dsId/fdId/name/fdType/metaType`;清空筛选条件可传 `--filter-json null`。完整契约和嵌套示例见 `metric-common-fields.md`。
54
66
  指标树创建和修改已封装为 `metric-tree` 专用命令,优先使用专用命令,不要再用裸 `fetch` 直接调用指标树写接口。指标树父目录来自 `METRIC_TREE` 目录树,可用 `guancli metric_attribution tree -f json` 查询;不要把 `guancli metric tree` 返回的 `METRIC_PLATFORM` 指标目录 ID 当作指标树目录 ID。用户给出指标拆解公式时,按 `metric-tree-mutation.md` 解析成 `metricRelationTree`;遇到复合指标时读取指标详情中的 `formula`,按规则递归展开或在计划中标注不能无损展开的原因。
55
67
 
56
68
  参考文档见本 skill 的 `references/` 目录:
57
69
 
58
70
  - `metric-excel-template.md`
59
71
  - `metric-mutation.md`
72
+ - `metric-common-fields.md`
60
73
  - `metric-atomic-mutation.md`
61
74
  - `metric-composite-mutation.md`
62
75
  - `metric-derived-mutation.md`
63
76
  - `metric-public-dimension.md`
77
+ - `metric-business-dictionary.md`
64
78
  - `metric-tree-mutation.md`
@@ -0,0 +1,72 @@
1
+ # 业务字典管理
2
+
3
+ 业务字典在后端接口和代码中也称 `BusinessMetric`,CLI 使用面向产品语义的命令名 `business-dict`,并兼容 `business-metric`、`biz-dict` 别名。
4
+
5
+ ## 查询
6
+
7
+ ```bash
8
+ guanmetric business-dict list
9
+ guanmetric business-dict list 销售 --order-by utimeDesc
10
+ guanmetric business-dict list --desc 收入 --dir-id <dir_id>
11
+ guanmetric business-dict list --associated
12
+ guanmetric business-dict list --associated=false
13
+ guanmetric business-dict list --biz-attr region=华东,华南 --biz-attr channel=直营
14
+ guanmetric business-dict list -f table
15
+ guanmetric business-dict get <business_dict_id>
16
+ guanmetric business-dict tree
17
+ ```
18
+
19
+ 搜索接口无分页,`list` 返回全部匹配项。支持的排序为 `nameAsc`、`nameDesc`、`ctimeAsc`、`ctimeDesc`、`utimeAsc`、`utimeDesc`。`--associated` 仅查已关联指标的字典,显式传 `--associated=false` 仅查未关联字典;不传时不按关联状态过滤。
20
+
21
+ 业务属性搜索中,文本类属性由服务端执行不区分大小写的包含匹配,枚举类属性执行精确匹配。禁用或不存在的属性由服务端返回业务错误;不可搜索的属性不会匹配结果,通常表现为空列表。
22
+
23
+ ## 新建
24
+
25
+ 请求体字段:
26
+
27
+ | 字段 | 必填 | 说明 |
28
+ |---|---|---|
29
+ | `name` | 是 | 名称;trim 后不能为空、同域唯一、最长 250 字符 |
30
+ | `desc` | 是 | 业务口径 |
31
+ | `dirId` | 否 | `BUSINESS_METRIC` 目录 ID;用 `business-dict tree` 查询 |
32
+ | `bizAttrs` | 否 | `[{"id":"<attr_id>","values":["<value>"]}]` |
33
+
34
+ ```bash
35
+ guanmetric business-dict create --file business_dict.json --dry-run
36
+ guanmetric business-dict create --file business_dict.json
37
+ ```
38
+
39
+ 示例请求体:
40
+
41
+ ```json
42
+ {
43
+ "name": "销售额",
44
+ "desc": "订单实付金额汇总,不含退款",
45
+ "dirId": "dir_xxx",
46
+ "bizAttrs": [
47
+ {"id": "attr_region", "values": ["华东"]}
48
+ ]
49
+ }
50
+ ```
51
+
52
+ ## 编辑
53
+
54
+ ```bash
55
+ guanmetric business-dict edit <business_dict_id> --set name=含税销售额 --dry-run
56
+ guanmetric business-dict edit <business_dict_id> --file patch.json
57
+ guanmetric business-dict edit <business_dict_id> --file full.json --replace
58
+ ```
59
+
60
+ 默认会先读取线上详情,只保留 `name`、`desc`、`dirId`、`bizAttrs` 四个可提交字段,再合并 patch 并原地更新。`--replace` 仅用于调用方已经提供完整请求体的场景。严禁通过删除后新建同名字典模拟编辑。
61
+
62
+ ## 删除
63
+
64
+ ```bash
65
+ guanmetric business-dict delete <business_dict_id>
66
+ guanmetric business-dict delete <business_dict_id> --confirm
67
+ guanmetric business-dict delete <business_dict_id> --confirm --dry-run
68
+ ```
69
+
70
+ 第一次执行只读取详情,展示是否存在 `associatedMetric`,不提交删除。追加 `--confirm` 后才调用删除接口。
71
+
72
+ 删除采用逻辑删除。如果字典已关联指标,服务端会先清空正式指标与草稿中的 `bizMetricId`,并把字典的 `desc` 和 `bizAttrs` 留存在指标上,再删除字典。该行为不同于“有依赖即阻断删除”,所以确认提示必须明确说明解除关联和保留字段的影响。
@@ -32,6 +32,80 @@
32
32
  | `filter` | 业务限定/筛选 | 可选或条件必填 | 业务限定衍生指标必填 |
33
33
  | `dynamicParameters` | 动态参数默认值 | 可选 | 表达式或最近 N 配置引用动态参数时使用 |
34
34
 
35
+ ## 筛选条件 `filter`(WebFilterData)
36
+
37
+ 原子指标筛选、复合指标筛选、衍生指标业务限定和查询加速筛选共用同一套 `WebFilterData` 协议。合法节点只有:
38
+
39
+ | `type` | `value` | 说明 |
40
+ | --- | --- | --- |
41
+ | `condition` | 字段筛选对象 | 规范化后必须包含 `dsId`、`fdId`、`name`、`fdType`、`metaType`、`filterType`;按筛选类型提供 `filterValue` |
42
+ | `operator` | `and` 或 `or` | 只能出现在 `composition.value` 的两个条件节点之间 |
43
+ | `composition` | WebFilterData 数组 | 按“条件、运算符、条件”交替排列;同一 composition 内运算符必须一致,可递归嵌套 |
44
+
45
+ CLI 接受可无损转换的 `not: true` 作为兼容输入,但提交前会将其改写成明确的反向筛选类型,例如 `IN` 改为 `NI`、`CONTAINS` 改为 `NOT_CONTAINS`;组合节点按德摩根定律展开。Web 编辑器不会可靠读取节点上的 `not` 字段,因此规范化后的请求始终使用 `not: false`。遇到没有明确反向类型的条件(例如区间 `BT`)时,CLI 会拒绝提交,避免保存后语义与回显不一致。
46
+
47
+ 单条件示例:
48
+
49
+ ```json
50
+ "filter": {
51
+ "type": "condition",
52
+ "not": false,
53
+ "value": {
54
+ "dsId": "ds_sales",
55
+ "fdId": "fd_region",
56
+ "name": "区域",
57
+ "fdType": "STRING",
58
+ "metaType": "DIM",
59
+ "filterType": "IN",
60
+ "filterValue": ["华东"]
61
+ }
62
+ }
63
+ ```
64
+
65
+ 嵌套条件示例,表示“区域属于华东,并且(城市为空或城市包含杭州)”:
66
+
67
+ ```json
68
+ "filter": {
69
+ "type": "composition",
70
+ "not": false,
71
+ "value": [
72
+ {
73
+ "type": "condition",
74
+ "not": false,
75
+ "value": { "dsId": "ds_sales", "fdId": "fd_region", "name": "区域", "fdType": "STRING", "metaType": "DIM", "filterType": "IN", "filterValue": ["华东"] }
76
+ },
77
+ { "type": "operator", "value": "and" },
78
+ {
79
+ "type": "composition",
80
+ "not": false,
81
+ "value": [
82
+ {
83
+ "type": "condition",
84
+ "not": false,
85
+ "value": { "dsId": "ds_sales", "fdId": "fd_city", "name": "城市", "fdType": "STRING", "metaType": "DIM", "filterType": "IS_NULL", "filterValue": [] }
86
+ },
87
+ { "type": "operator", "value": "or" },
88
+ {
89
+ "type": "condition",
90
+ "not": false,
91
+ "value": { "dsId": "ds_sales", "fdId": "fd_city", "name": "城市", "fdType": "STRING", "metaType": "DIM", "filterType": "CONTAINS", "filterValue": ["杭州"] }
92
+ }
93
+ ]
94
+ }
95
+ ]
96
+ }
97
+ ```
98
+
99
+ 禁止使用下面这类其它系统的分组结构:
100
+
101
+ ```json
102
+ { "type": "group", "value": { "combineType": "AND", "conditions": [] } }
103
+ ```
104
+
105
+ 后端可能宽松接收这类 JSON,但 Web 指标编辑器只识别 `condition/operator/composition`,保存后会出现业务限定不回显。条件的 `fdId` 还必须存在于当前可筛选字段中:原子指标使用数据集字段;业务限定衍生指标使用基础指标 `applicableDims`。指标编辑和业务限定写入会优先从指标详情补齐缺少的字段元数据;查询加速条件必须自行提供完整元数据。
106
+
107
+ 兼容输入会在提交前规范化:`EMPTY -> IS_NULL`、`NOT_EMPTY -> NOT_NULL`、`LIKE -> CONTAINS`、`NOT_LIKE -> NOT_CONTAINS`。`IS_NULL/NOT_NULL` 必须携带空数组 `filterValue: []`;其它普通筛选类型必须携带非空数组。不要把这些兼容别名直接写进最终请求。
108
+
35
109
  ## 数据格式 `format`
36
110
 
37
111
  `format` 是指标数值的展示格式,创建和编辑原子指标、复合指标、衍生指标时都可设置。保存指标时写入 Web 表单使用的 UI 配置,不要写转换后的 d3 `specifier`、Excel format 或预览接口返回值。
@@ -263,7 +263,7 @@
263
263
 
264
264
  ## 业务限定 `BIZ_CONDITION`
265
265
 
266
- 业务限定必须提供顶层 `filter`。标准模板使用 `业务限定` 列承载筛选条件 JSON;普通用户可通过页面配置后复制 payload,复杂筛选不建议凭空拼。
266
+ 业务限定必须提供顶层 `filter`。标准模板使用 `业务限定` 列承载筛选条件 JSON。`filter` 必须遵循 `metric-common-fields.md` 的 `WebFilterData` 契约;不要使用 `group/combineType/conditions`,该结构即使被后端保存,Web 编辑页也无法正确回显。
267
267
 
268
268
  ```json
269
269
  "deriveSetting": {
@@ -274,12 +274,21 @@
274
274
  }
275
275
  },
276
276
  "filter": {
277
- "combineType": "AND",
278
- "conditions": []
277
+ "type": "condition",
278
+ "not": false,
279
+ "value": {
280
+ "dsId": "<base_metric_ds_id>",
281
+ "fdId": "<applicable_dimension_fd_id>",
282
+ "name": "<dimension_name>",
283
+ "fdType": "STRING",
284
+ "metaType": "DIM",
285
+ "filterType": "IN",
286
+ "filterValue": ["华东"]
287
+ }
279
288
  }
280
289
  ```
281
290
 
282
- `filter` 结构复杂时,优先参考同环境已有指标详情或由 Web 配置后复制 payload,不凭空拼复杂筛选。
291
+ `filter.value.fdId` 必须来自基础指标的 `applicableDims`,CLI 会据此补齐缺少的 `dsId/name/fdType/metaType`。复杂条件按 `composition.value` 中“条件、运算符、条件”交替排列;优先参考同环境已有指标详情或由 Web 配置后复制 payload,不凭空拼复杂筛选。
283
292
 
284
293
  ## 组合衍生 `COMPOSITE`
285
294
 
@@ -406,7 +415,7 @@
406
415
 
407
416
  1. 先输出指标创建计划,按 `metric-mutation.md` 的“指标创建计划”要求展示环境、主题、目录、基础指标或组合衍生引用指标、衍生配置、时间维度、格式、业务属性和保存方式。
408
417
  2. 用户确认计划后,生成 `/tmp/derived_metric.json`。
409
- 3. 本地/人工检查 JSON、衍生自指标或公式引用指标、时间维度、衍生配置、必填业务属性。
418
+ 3. 本地/人工检查 JSON、衍生自指标或公式引用指标、时间维度、衍生配置、必填业务属性。业务限定写入时,CLI 会自动读取 `baseMetricId` 详情并校验 `filter.fdId` 是否属于基础指标 `applicableDims`。
410
419
  4. 按 `metric-common-fields.md` 向用户摘要并确认。
411
420
  5. 用户确认后,不调用原子指标 `save-precheck`;衍生指标 Web 创建没有 save-precheck。
412
421
  6. 提交真实创建:
@@ -4,6 +4,59 @@
4
4
 
5
5
  客户原始 Excel 的语义理解由 agent/大模型完成,不在 CLI 中穷举各种客户列名和业务表达。CLI 保留标准模板能力:固定 sheet/字段结构、必填项红色 `*` 标记、当前环境业务属性配置读取、默认值和 ID 自动填充、`自动填充说明`、`行状态`、`问题说明`、问题字段高亮、列宽自适应、基础字段校验和报告输出。
6
6
 
7
+ ## 机器 schema、模板生成与创建前校验
8
+
9
+ 不要从长文档或历史 Excel 猜 Sheet、列名和枚举。原子/复合指标的当前机器契约由以下命令提供:
10
+
11
+ ```bash
12
+ guanmetric template schema --type atomic
13
+ guanmetric template schema --type composite
14
+ ```
15
+
16
+ 输出 schema version `1.1`。顶层 `sources` 给出本次契约核对使用的 `guandata-core` / `guandata-web` commit、源码路径和 symbol;每列除 `name/type/required` 外,还标明:
17
+
18
+ - `role=api-input`:最终进入创建接口;`apiField/apiType/apiRequired` 对应实际请求字段。
19
+ - `role=resolver-input`:项目名、目录路径、数据集名等 CLI 辅助输入,先解析为 API 所需 ID,本身不是接口字段。
20
+ - `role=formula-helper`:指标名称或数据集字段的 token 映射,先转换为后端公式中的 `[metricId]`,本身不是接口字段。
21
+ - `sourceRefs`:该列约束所依据的 source ID;`requiredWhen`、`enum`、`format`、`referenceFormat`、`maxLength`、`pattern`、`transform`、`default` 和 `example` 按需输出。
22
+
23
+ 其中:
24
+
25
+ - 原子指标聚合方式仅支持 `SUM/CNT/CNT_DISTINCT/AVG/MAX/MIN/NUL`;来源字段+聚合方式与字段表达式二选一。
26
+ - 名称最多 50 个字符且只允许中文、英文字母、数字、下划线;英文名称最多 50 个字符且只允许英文字母、数字、下划线;业务口径最多 1000 个字符。
27
+ - 原子和复合创建请求都包含 `applicableDims`。原子模板必须填写;复合模板选择“不沿用表达式中指标的共用维度”时必须填写,沿用模式由创建 payload 发送空数组并由公式引用指标计算共有维度。
28
+ - `useExpressionMetricCommonDims` 在 core 缺省为 `true`,Web 创建请求仍将它作为必传 boolean;模板缺省“是”,创建时显式转换为 `true`。
29
+ - 适用维度包含时间类型字段时,后端要求填写一个时间维度且该字段必须属于适用维度。本地 check-only 没有数据集字段类型元数据,不会根据字段名称猜测它是否为时间类型;实际创建前仍需由数据集 schema/后端校验。
30
+ - 数据格式模板枚举只支持 `默认/数值/货币/百分比`。
31
+ - 筛选格式是 WebFilterData JSON,合法节点为 `condition/operator/composition`。
32
+ - 复合指标公式使用 token;`引用指标` 格式为 `token=指标名称或指标ID`,多个映射用分号分隔。
33
+ - 责任人填写明确用户,或使用 `当前登录用户`;保存方式为 `草稿/上线`。
34
+
35
+ 没有现成输入时直接生成最小模板:
36
+
37
+ ```bash
38
+ guanmetric template init --type atomic --out atomic_metrics.xlsx
39
+ guanmetric template init --type composite --out composite_metrics.xlsx
40
+ ```
41
+
42
+ 模板包含当前 schema 的列和一行合法示例。正式生成标准 Excel 前先执行无副作用校验:
43
+
44
+ ```bash
45
+ guanmetric template normalize metrics.xlsx \
46
+ --check-only \
47
+ --output json-v1 \
48
+ --skip-biz-attrs \
49
+ --skip-id-resolve
50
+ ```
51
+
52
+ `--check-only` 不写 xlsx/report,不创建、修改或上线指标。需要按当前 BI 环境补业务属性和解析主题/目录/数据集时去掉两个 `--skip-*`,这些步骤也只读。输出包含 `valid`、`errorCodes`、`diagnostics` 和 `summary`;每条 diagnostic 给出稳定 code、severity、Sheet、行、列、期望和修正示例。code/column 在校验发生处直接赋值,message 仅用于展示,文案变化不会改变机器分类。只有 `severity=error` 导致非零退出,`TEMPLATE_NEEDS_REVIEW` 保持成功但要求后续确认。
53
+
54
+ 普通 normalize 生成的 report 同样包含 diagnostics,可单独解释:
55
+
56
+ ```bash
57
+ guanmetric template explain-error standard_metrics.report.json --json
58
+ ```
59
+
7
60
  对于客户原始 Excel/CSV,agent 必须先读取原表并生成语义映射中间表(建议命名为 `*.ai_mapped.xlsx`),把应创建的原子、复合、同比/环比、最近 N、累计、期末值、业务限定、组合衍生分别写入标准 sheet 后,再执行 `guanmetric template normalize`。不要直接对客户原始 Excel 执行 normalize 后交付结果;如果 report 的 `notes` 提示疑似业务语义列被丢弃但复合/衍生 sheet 为空,必须回到语义映射步骤重做。
8
61
 
9
62
  如果 report 的 `notes` 提示疑似存在衍生语义但对应衍生 sheet 为空,也必须回到语义映射步骤重做。检查按类型进行:同比/环比语义对应 `同比环比衍生模板`,最近 N/昨日/本周/本月等时间窗口语义对应 `最近N衍生模板`,累计/历史累计语义对应 `累计衍生模板`,期末/末次有值语义对应 `期末值衍生模板`,业务限定/筛选限定语义对应 `业务限定衍生模板`。agent 需要重新判断这些语义是普通筛选能力、时间维度能力,还是应单独创建对应衍生指标;确认不是衍生指标时,应在交付摘要中说明判断依据。
@@ -20,6 +73,8 @@
20
73
  ## 标准化命令
21
74
 
22
75
  ```bash
76
+ guanmetric template normalize <input.xlsx> --check-only --output json-v1
77
+
23
78
  guanmetric template normalize <input.xlsx> \
24
79
  --out standard_metrics.xlsx \
25
80
  --report standard_metrics_report.json
@@ -94,7 +149,7 @@ guanmetric template normalize <input.xlsx> \
94
149
  | `沿用表达式中指标的共用维度` | 复合指标默认 `是` |
95
150
  | `适用维度` | 字段名列表 |
96
151
  | `时间维度` | 时间字段名 |
97
- | `数据格式` | 默认、数值、货币、百分比等 |
152
+ | `数据格式` | `默认`、`数值`、`货币`、`百分比` |
98
153
  | `责任人` | 业务负责人 |
99
154
  | `保存方式` | `草稿` 或 `上线`,缺省自动填 `草稿` |
100
155
  | `业务属性:<属性名>` | 当前环境启用的指标自定义业务属性 |
@@ -279,7 +334,7 @@ guanmetric fetch GET /api/metric-attr-config
279
334
  ## 标准流程
280
335
 
281
336
  1. 用户给 Excel/CSV 要批量创建指标。
282
- 2. 执行 `guanmetric template normalize` 生成标准 Excel 和 report。
337
+ 2. 先执行 `guanmetric template normalize --check-only --output json-v1`,按单元格 diagnostics 修正真实错误;确认预检通过后再执行普通 normalize 生成标准 Excel 和 report。
283
338
  3. 把标准 Excel 返回给用户确认,摘要说明:
284
339
  - 映射了哪些列。
285
340
  - 剔除了哪些冗余列。
@@ -56,10 +56,11 @@
56
56
  2. 若输入是客户 Excel/CSV,先执行:
57
57
 
58
58
  ```bash
59
+ guanmetric template normalize <input.xlsx> --check-only --output json-v1
59
60
  guanmetric template normalize <input.xlsx> --out <standard.xlsx> --report <report.json>
60
61
  ```
61
62
 
62
- 把生成的标准 Excel 返回给用户确认,并摘要列映射、剔除列、自动填充字段、ID 自动解析结果、可创建/待确认/错误行数。标准化阶段不自动创建主题、目录或指标;找不到或多候选的主题/目录写入 `问题说明`。用户确认标准 Excel 前,不生成最终创建 payload,不执行 dry-run/precheck,不真实提交。若用户修改标准 Excel 后再次传回,重复执行标准化流程,直到所有行都是 `可创建`。
63
+ check-only 不写文件也不执行指标写操作;先按 diagnostics 的 Sheet/行/列和稳定错误码修正。通过后把普通 normalize 生成的标准 Excel 返回给用户确认,并摘要列映射、剔除列、自动填充字段、ID 自动解析结果、可创建/待确认/错误行数。标准化阶段不自动创建主题、目录或指标;找不到或多候选的主题/目录写入 `问题说明`。用户确认标准 Excel 前,不生成最终创建 payload,不执行 dry-run/precheck,不真实提交。若用户修改标准 Excel 后再次传回,重复执行标准化流程,直到所有行都是 `可创建`。
63
64
  3. 信息不全时先查 ID,不凭空猜:
64
65
  - 指标主题:`guancli metric project [keyword] -f json`
65
66
  - 指标目录:`guancli metric tree -f json`