@arcticdata/dsh-datarc-assistant 26.39.0 → 26.39.1

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 (149) hide show
  1. package/README.md +45 -10
  2. package/api-operations.js +404 -0
  3. package/client.js +452 -87
  4. package/cordis.patch.yml +4 -4
  5. package/entry/SKILL.md +34 -0
  6. package/icon.png +0 -0
  7. package/index.js +35 -47
  8. package/locale/en.json +6 -6
  9. package/locale/zh.json +6 -6
  10. package/native-api.mjs +326 -0
  11. package/native-contracts.js +195 -0
  12. package/native-execution.js +184 -0
  13. package/native-runner.mjs +107 -0
  14. package/native-state.js +67 -0
  15. package/native-tools.js +241 -0
  16. package/operations.js +1260 -0
  17. package/package.json +65 -51
  18. package/{skills → runtime}/datarc-access-permissions/SKILL.md +30 -30
  19. package/{skills → runtime}/datarc-access-permissions/references/api.md +31 -31
  20. package/{skills → runtime}/datarc-access-permissions/references/write-order.md +16 -16
  21. package/{skills → runtime}/datarc-analysis-report/SKILL.md +125 -125
  22. package/{skills → runtime}/datarc-analysis-report/agents/openai.yaml +6 -6
  23. package/{skills → runtime}/datarc-analysis-report/references/analyst-frameworks.md +215 -215
  24. package/{skills → runtime}/datarc-analysis-report/references/api-contracts.md +478 -478
  25. package/{skills → runtime}/datarc-analysis-report/references/direct-card-fallback-design.md +66 -66
  26. package/{skills → runtime}/datarc-analysis-report/references/human-in-the-loop.md +142 -142
  27. package/{skills → runtime}/datarc-analysis-report/references/loop-personalization-policy.md +138 -138
  28. package/{skills → runtime}/datarc-analysis-report/references/manifest-example.json +779 -779
  29. package/{skills → runtime}/datarc-analysis-report/references/narrative-schema.md +160 -160
  30. package/{skills → runtime}/datarc-analysis-report/references/question-syntax.md +151 -151
  31. package/{skills → runtime}/datarc-analysis-report/scripts/run_analysis_report.py +2225 -2225
  32. package/{skills → runtime}/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -1914
  33. package/{skills → runtime}/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -5295
  34. package/{skills → runtime}/datarc-data-search/SKILL.md +243 -243
  35. package/{skills → runtime}/datarc-data-search/scripts/calculate.py +116 -116
  36. package/{skills → runtime}/datarc-data-search/scripts/data-search.mjs +628 -628
  37. package/{skills/datarc-dataseek-search → runtime/datarc-data-search}/scripts/datarc-client.mjs +464 -464
  38. package/{skills → runtime}/datarc-data-search/scripts/search-result.mjs +211 -211
  39. package/{skills → runtime}/datarc-dataseek-batch-evaluation/SKILL.md +59 -59
  40. package/{skills → runtime}/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -36
  41. package/{skills → runtime}/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -30
  42. package/{skills/datarc-data-search → runtime/datarc-dataseek-batch-evaluation}/scripts/datarc-client.mjs +464 -464
  43. package/{skills → runtime}/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -1020
  44. package/{skills → runtime}/datarc-dataseek-search/SKILL.md +96 -96
  45. package/{skills/datarc-dataseek-batch-evaluation → runtime/datarc-dataseek-search}/scripts/datarc-client.mjs +464 -464
  46. package/{skills → runtime}/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -257
  47. package/{skills → runtime}/datarc-dataseek-semantic-debugging/SKILL.md +44 -44
  48. package/{skills → runtime}/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -4
  49. package/{skills → runtime}/datarc-dataseek-semantic-debugging/evals/evals.json +165 -165
  50. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/dataset.md +77 -77
  51. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -93
  52. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/interventions.md +64 -64
  53. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -47
  54. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/rollback.md +11 -11
  55. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -95
  56. package/{skills → runtime}/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -29
  57. package/{skills → runtime}/datarc-dataseek-testset-generation/SKILL.md +47 -47
  58. package/{skills → runtime}/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -464
  59. package/{skills → runtime}/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -526
  60. package/{skills → runtime}/datarc-datasource/SKILL.md +97 -97
  61. package/{skills → runtime}/datarc-oauth/SKILL.md +50 -50
  62. package/{skills → runtime}/datarc-oauth/scripts/auth.mjs +192 -192
  63. package/{skills → runtime}/datarc-oauth/scripts/credentials.mjs +237 -237
  64. package/{skills → runtime}/datarc-report-add-card/SKILL.md +90 -90
  65. package/{skills → runtime}/datarc-report-add-card/agents/openai.yaml +4 -4
  66. package/runtime/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
  67. package/{skills → runtime}/datarc-report-add-card/scripts/report-card.mjs +439 -439
  68. package/{skills → runtime}/datarc-semantic-alias/SKILL.md +20 -20
  69. package/{skills → runtime}/datarc-semantic-alias/scripts/alias-plan.mjs +422 -422
  70. package/{skills → runtime}/datarc-semantic-alias/scripts/alias.mjs +11 -11
  71. package/{skills → runtime}/datarc-semantic-calibration/SKILL.md +20 -20
  72. package/{skills → runtime}/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -683
  73. package/{skills → runtime}/datarc-semantic-calibration/scripts/calibration.mjs +11 -11
  74. package/{skills → runtime}/datarc-semantic-core/SKILL.md +12 -12
  75. package/{skills → runtime}/datarc-semantic-core/scripts/artifact-journal.mjs +853 -830
  76. package/{skills → runtime}/datarc-semantic-core/scripts/canonical.mjs +48 -48
  77. package/{skills → runtime}/datarc-semantic-core/scripts/case-model.mjs +608 -608
  78. package/{skills → runtime}/datarc-semantic-core/scripts/cli.mjs +180 -180
  79. package/{skills → runtime}/datarc-semantic-core/scripts/datarc-client.mjs +180 -180
  80. package/{skills → runtime}/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -57
  81. package/{skills → runtime}/datarc-semantic-core/scripts/interventions.mjs +693 -693
  82. package/{skills → runtime}/datarc-semantic-core/scripts/live-resolution.mjs +305 -305
  83. package/{skills → runtime}/datarc-semantic-core/scripts/model-scope.mjs +49 -49
  84. package/{skills → runtime}/datarc-semantic-core/scripts/reproduction.mjs +330 -330
  85. package/{skills → runtime}/datarc-semantic-core/scripts/semantic-cli.mjs +799 -740
  86. package/{skills → runtime}/datarc-semantic-core/scripts/shared.mjs +312 -312
  87. package/{skills → runtime}/datarc-semantic-core/scripts/verification.mjs +1111 -1014
  88. package/{skills → runtime}/datarc-semantic-dataset/SKILL.md +20 -20
  89. package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -749
  90. package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -813
  91. package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset.mjs +11 -11
  92. package/{skills → runtime}/datarc-semantic-knowledge/SKILL.md +38 -38
  93. package/{skills → runtime}/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -61
  94. package/{skills → runtime}/datarc-semantic-knowledge/references/knowledge-generation.json +16 -16
  95. package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -347
  96. package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -848
  97. package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -31
  98. package/{skills → runtime}/datarc-semantic-prompt/SKILL.md +20 -20
  99. package/{skills → runtime}/datarc-semantic-prompt/scripts/prompt-plan.mjs +598 -447
  100. package/{skills → runtime}/datarc-semantic-prompt/scripts/prompt.mjs +15 -15
  101. package/{skills → runtime}/datarc-semantic-synonym/SKILL.md +20 -20
  102. package/{skills → runtime}/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -688
  103. package/{skills → runtime}/datarc-semantic-synonym/scripts/synonym.mjs +11 -11
  104. package/{skills → runtime}/datarc-viz-analysis-report/SKILL.md +108 -108
  105. package/{skills → runtime}/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -8
  106. package/{skills → runtime}/datarc-viz-analysis-report/assets/echarts.min.js +45 -45
  107. package/{skills → runtime}/datarc-viz-analysis-report/references/api-contracts.md +163 -163
  108. package/{skills → runtime}/datarc-viz-analysis-report/references/derivation-log.md +157 -157
  109. package/{skills → runtime}/datarc-viz-analysis-report/references/evidence-framework.md +76 -76
  110. package/{skills → runtime}/datarc-viz-analysis-report/references/html-template.md +92 -92
  111. package/{skills → runtime}/datarc-viz-analysis-report/references/provenance-format.md +56 -56
  112. package/{skills → runtime}/datarc-viz-analysis-report/references/question-syntax.md +151 -151
  113. package/{skills → runtime}/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -1708
  114. package/{skills → runtime}/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -964
  115. package/{skills → runtime}/datarc-workspace-configuration/SKILL.md +59 -59
  116. package/{skills → runtime}/datarc-workspace-configuration/references/artifacts.md +75 -75
  117. package/{skills → runtime}/datarc-workspace-configuration/references/verification.md +20 -20
  118. package/{skills → runtime}/datarc-workspace-configuration/scripts/configuration.mjs +722 -662
  119. package/{skills → runtime}/datarc-workspace-context/SKILL.md +43 -43
  120. package/{skills → runtime}/datarc-workspace-context/agents/openai.yaml +4 -4
  121. package/{skills → runtime}/datarc-workspace-context/scripts/datarc-client.mjs +31 -31
  122. package/{skills → runtime}/datarc-workspace-context/scripts/query-display.mjs +114 -114
  123. package/{skills → runtime}/datarc-workspace-context/scripts/workspace-context.mjs +58 -58
  124. package/{skills → runtime}/datarc-workspace-custom-tables/SKILL.md +89 -89
  125. package/{skills → runtime}/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -165
  126. package/{skills → runtime}/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -601
  127. package/{skills → runtime}/datarc-workspace-keywords/SKILL.md +197 -197
  128. package/{skills → runtime}/datarc-workspace-keywords/agents/openai.yaml +4 -4
  129. package/{skills → runtime}/datarc-workspace-keywords/references/api-fields.md +79 -79
  130. package/{skills → runtime}/datarc-workspace-keywords/references/derived-measures.md +177 -177
  131. package/{skills → runtime}/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -145
  132. package/{skills → runtime}/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -2421
  133. package/{skills → runtime}/datarc-workspace-membership/SKILL.md +29 -29
  134. package/{skills → runtime}/datarc-workspace-membership/references/api.md +19 -19
  135. package/{skills → runtime}/datarc-workspace-modeling/SKILL.md +209 -209
  136. package/{skills → runtime}/datarc-workspace-modeling/agents/openai.yaml +4 -4
  137. package/{skills → runtime}/datarc-workspace-modeling/references/join-preview.md +38 -38
  138. package/{skills → runtime}/datarc-workspace-modeling/references/model-plan.md +300 -300
  139. package/{skills → runtime}/datarc-workspace-modeling/references/model-tags.md +95 -95
  140. package/{skills → runtime}/datarc-workspace-modeling/references/modeling-contract.json +29 -29
  141. package/{skills → runtime}/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -2423
  142. package/{skills → runtime}/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -137
  143. package/{skills → runtime}/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -238
  144. package/{skills → runtime}/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -1261
  145. package/skills-provider.js +51 -0
  146. package/workflow-index.json +434 -0
  147. package/icon.svg +0 -4
  148. package/skills/datarc-report-add-card/scripts/datarc-client.mjs +0 -464
  149. package/skills-index.json +0 -194
@@ -1,177 +1,177 @@
1
- # 衍生指标
2
-
3
- 衍生指标是工作区级指标:公式引用已有指标,查询时先在各模型聚合基础指标,再融合结果,最后逐行执行公式。它不绑定 `model`、`field`、`sql`、`calcType` 或 `type`。
4
-
5
- ## 目录
6
-
7
- - [创建时机](#创建时机)
8
- - [输入](#输入)
9
- - [两个指标分类轴](#两个指标分类轴)
10
- - [查重与更新](#查重与更新)
11
- - [跨模型预检](#跨模型预检)
12
- - [命令流程](#命令流程)
13
- - [查询验收](#查询验收)
14
-
15
- ## 创建时机
16
-
17
- 建模阶段只记录计算意图。满足以下条件后才生成衍生指标计划:
18
-
19
- 1. 公式中的基础指标已经创建;
20
- 2. 每个基础指标至少进入一个 ready 模型;
21
- 3. 所有模型已回读,最新指标目录已重新获取;
22
- 4. 已确认没有一个基础指标可以直接满足需求。
23
-
24
- 尚未满足时保持 `wait_models_ready`,不提前创建一个 `used=false` 的指标来代替建模。
25
-
26
- ## 输入
27
-
28
- ```json
29
- {
30
- "names": ["退款率"],
31
- "formula": "[退款额] / [营业额]",
32
- "format": "percentage",
33
- "cumulative": false,
34
- "description": "退款额占营业额比例"
35
- }
36
- ```
37
-
38
- 公式最长 1024 字符,只包含:
39
-
40
- - 数字;
41
- - `[指标显示名]`;
42
- - `+`、`-`、`*`、`/`;
43
- - 圆括号和空格。
44
-
45
- 至少引用一个已有指标。不使用 SQL、函数、条件表达式或幂运算。
46
-
47
- 公式可以引用已有衍生指标。脚本递归展开其 `formula_dependency`,模型覆盖和回读以基础叶子指标 ID 为准;同时保留公式直接引用的指标,用于判断这些指标自身声明的普通/累计语义。批量创建有依赖关系的衍生指标时按拓扑顺序逐个创建和回读;循环引用直接失败。
48
-
49
- `cumulative` 必须显式为布尔值。`derived.json` 只能包含工作区指标字段;模型覆盖、粒度和查询验收信息放在独立的 `verification-plan.json`,不能混入指标请求体。
50
-
51
- ## 两个指标分类轴
52
-
53
- 不要把“基础、衍生、普通、累计”当成四个互斥类型:
54
-
55
- | 分类轴 | 取值 |
56
- | -------- | ----------- |
57
- | 计算来源 | 基础 / 衍生 |
58
- | 时间语义 | 普通 / 累计 |
59
-
60
- `formula` 决定计算来源,`cumulative` 决定时间语义。
61
-
62
- - 直接引用全部普通:默认建议 `cumulative=false`。
63
- - 直接引用全部累计:默认建议 `cumulative=true`。
64
- - 直接引用普通和累计混合:先明确时间口径,再让用户显式确认 `cumulative`,并执行真实查询验收。
65
-
66
- 后端允许混合公式,但不会替业务判断输出指标的累计语义。
67
-
68
- 嵌套衍生指标有两层源码语义:每个衍生指标保留自己的 `cumulative`;执行模型覆盖时,`formula_dependency` 又会递归展开为基础叶子。脚本因此分别报告直接引用的 `timeSemantics` 和叶子的 `leafTimeSemantics`。二者不同,或任一包含累计指标时,都必须执行真实查询验收。
69
-
70
- ## 查重与更新
71
-
72
- `upsert-derived` 对所有别名做大小写无关的精确匹配:
73
-
74
- | 结果 | 动作 |
75
- | ------------------------ | -------------------------------------------- |
76
- | 无同名资源 | 名称校验后计划 create |
77
- | 同名同公式、显式字段兼容 | reuse,不写接口 |
78
- | 同名衍生指标但定义不同 | 计划 update,正式写入要求 `--confirm UPDATE` |
79
- | 同名基础指标 | 类型冲突,不原地转换 |
80
- | 多个名称命中不同资源 | 歧义,停止确认 |
81
-
82
- 更新时脚本清除 `used`、`models`、`isValid`、`formula_snapshot`、`formula_dependency` 等服务端派生字段。新增别名才重新做名称校验,避免旧别名把资源自身识别为冲突。
83
-
84
- 完整替换名称、删除旧别名或新名称与旧名称没有交集时,用 `--id <现有指标id>` 定位。公式依赖变更前检查直接和传递的 `dependentMeasures`;不通过新建近义指标绕过更新确认。
85
-
86
- ## 跨模型预检
87
-
88
- `check-formula` 只校验语法和引用。`upsert-derived` 继续返回:
89
-
90
- - `dependencies`:递归展开的基础指标 ID;
91
- - `dependencyModels`:每个基础指标所在的 ready 模型;
92
- - `formulaReferences`:公式直接引用的指标及其普通/累计分类;
93
- - `sourceModels`:覆盖全部依赖的最小模型组合;
94
- - `sharedModels`:能在单模型内覆盖全部依赖的候选;
95
- - `requiresDataBlend`;
96
- - `enableDataBlend`、`maxBlendModelNum`、`maxBlendRowNum`;
97
- - `timeSemantics`、`leafTimeSemantics`、`queryVerificationRequired`、`blockers`。
98
-
99
- 跨模型创建的硬 blocker:
100
-
101
- - 任一基础依赖未进入 ready 模型;
102
- - 工作区未开启 `enableDataBlend`;
103
- - 最小模型组合超过 `maxBlendModelNum`。
104
-
105
- `maxBlendModelNum` 未配置时后端默认 3,`maxBlendRowNum` 未配置时默认 100000。实际查询任一子查询超过行数上限仍会失败。
106
-
107
- 脚本不能仅从指标目录证明以下条件:
108
-
109
- - 目标时间维度及粒度是否在来源模型间兼容;
110
- - 目标普通维度是否能作为共同融合键;
111
- - 默认过滤是否产生相同数据口径;
112
- - 最终用户的数据权限是否覆盖全部来源模型;
113
- - 代表性查询是否低于行数限制。
114
-
115
- 因此 `requiresDataBlend=true` 时必须单独创建 `verification-plan.json`:
116
-
117
- ```json
118
- {
119
- "intendedGrain": ["业务日期", "门店"],
120
- "sharedTimeSeries": [3],
121
- "sharedDimensions": [4],
122
- "sourceModels": ["sales-model", "refund-model"],
123
- "compatibility": "needs-query-verification"
124
- }
125
- ```
126
-
127
- 该对象不是 `derived.json` 的一部分,也不会发送到指标 API。
128
-
129
- 没有共同维度的标量计算、普通与累计混合、直接引用与基础叶子的时间语义不一致,以及默认时间轴不同的场景,都需要单独说明口径,不能把公式校验成功写成“跨模型可算”。
130
-
131
- ## 命令流程
132
-
133
- ```bash
134
- node scripts/workspace-keywords.mjs upsert-derived \
135
- --workspace-id 12 \
136
- --input derived.json \
137
- --dry-run
138
- ```
139
-
140
- 检查 dry-run 的 `action`、`dependencies`、`coverage` 和 `blockers`。无 blocker 后执行:
141
-
142
- ```bash
143
- node scripts/workspace-keywords.mjs upsert-derived \
144
- --workspace-id 12 \
145
- --input derived.json
146
- ```
147
-
148
- 同名异公式更新:
149
-
150
- ```bash
151
- node scripts/workspace-keywords.mjs upsert-derived \
152
- --workspace-id 12 \
153
- --id 9 \
154
- --input derived.json \
155
- --confirm UPDATE
156
- ```
157
-
158
- 写入后脚本验证:
159
-
160
- - `isValid=true`;
161
- - `formula_dependency` 与预期基础依赖一致;
162
- - 公式与显式输入字段一致;
163
- - `models` 为空;
164
- - `used` 只反映基础叶子已进入 ready 模型;
165
- - `queryability` 为 `BLOCKED`、`NEEDS_QUERY_VERIFICATION` 或 `READY_FOR_QUERY_VALIDATION`,不会仅凭保存成功返回“可查询”。
166
-
167
- ## 查询验收
168
-
169
- 使用 `datarc-data-search`,至少执行一个与目标用途相同的时间粒度和分组维度查询:
170
-
171
- 1. 分别查询各基础指标,确认模型、过滤和聚合结果;
172
- 2. 查询衍生指标,确认融合键和结果行数;
173
- 3. 手算一组数据对比公式结果;
174
- 4. 普通/累计混合时分别验证区间查询和趋势查询;
175
- 5. 记录 `maxBlendRowNum`、权限或共同粒度造成的限制。
176
-
177
- 完成条件:公式回读有效、依赖一致、代表性查询正确;否则明确 `queryability=BLOCKED/NEEDS_QUERY_VERIFICATION` 和具体 blocker,不能宣称已可查询。
1
+ # 衍生指标
2
+
3
+ 衍生指标是工作区级指标:公式引用已有指标,查询时先在各模型聚合基础指标,再融合结果,最后逐行执行公式。它不绑定 `model`、`field`、`sql`、`calcType` 或 `type`。
4
+
5
+ ## 目录
6
+
7
+ - [创建时机](#创建时机)
8
+ - [输入](#输入)
9
+ - [两个指标分类轴](#两个指标分类轴)
10
+ - [查重与更新](#查重与更新)
11
+ - [跨模型预检](#跨模型预检)
12
+ - [命令流程](#命令流程)
13
+ - [查询验收](#查询验收)
14
+
15
+ ## 创建时机
16
+
17
+ 建模阶段只记录计算意图。满足以下条件后才生成衍生指标计划:
18
+
19
+ 1. 公式中的基础指标已经创建;
20
+ 2. 每个基础指标至少进入一个 ready 模型;
21
+ 3. 所有模型已回读,最新指标目录已重新获取;
22
+ 4. 已确认没有一个基础指标可以直接满足需求。
23
+
24
+ 尚未满足时保持 `wait_models_ready`,不提前创建一个 `used=false` 的指标来代替建模。
25
+
26
+ ## 输入
27
+
28
+ ```json
29
+ {
30
+ "names": ["退款率"],
31
+ "formula": "[退款额] / [营业额]",
32
+ "format": "percentage",
33
+ "cumulative": false,
34
+ "description": "退款额占营业额比例"
35
+ }
36
+ ```
37
+
38
+ 公式最长 1024 字符,只包含:
39
+
40
+ - 数字;
41
+ - `[指标显示名]`;
42
+ - `+`、`-`、`*`、`/`;
43
+ - 圆括号和空格。
44
+
45
+ 至少引用一个已有指标。不使用 SQL、函数、条件表达式或幂运算。
46
+
47
+ 公式可以引用已有衍生指标。脚本递归展开其 `formula_dependency`,模型覆盖和回读以基础叶子指标 ID 为准;同时保留公式直接引用的指标,用于判断这些指标自身声明的普通/累计语义。批量创建有依赖关系的衍生指标时按拓扑顺序逐个创建和回读;循环引用直接失败。
48
+
49
+ `cumulative` 必须显式为布尔值。`derived.json` 只能包含工作区指标字段;模型覆盖、粒度和查询验收信息放在独立的 `verification-plan.json`,不能混入指标请求体。
50
+
51
+ ## 两个指标分类轴
52
+
53
+ 不要把“基础、衍生、普通、累计”当成四个互斥类型:
54
+
55
+ | 分类轴 | 取值 |
56
+ | -------- | ----------- |
57
+ | 计算来源 | 基础 / 衍生 |
58
+ | 时间语义 | 普通 / 累计 |
59
+
60
+ `formula` 决定计算来源,`cumulative` 决定时间语义。
61
+
62
+ - 直接引用全部普通:默认建议 `cumulative=false`。
63
+ - 直接引用全部累计:默认建议 `cumulative=true`。
64
+ - 直接引用普通和累计混合:先明确时间口径,再让用户显式确认 `cumulative`,并执行真实查询验收。
65
+
66
+ 后端允许混合公式,但不会替业务判断输出指标的累计语义。
67
+
68
+ 嵌套衍生指标有两层源码语义:每个衍生指标保留自己的 `cumulative`;执行模型覆盖时,`formula_dependency` 又会递归展开为基础叶子。脚本因此分别报告直接引用的 `timeSemantics` 和叶子的 `leafTimeSemantics`。二者不同,或任一包含累计指标时,都必须执行真实查询验收。
69
+
70
+ ## 查重与更新
71
+
72
+ `upsert-derived` 对所有别名做大小写无关的精确匹配:
73
+
74
+ | 结果 | 动作 |
75
+ | ------------------------ | -------------------------------------------- |
76
+ | 无同名资源 | 名称校验后计划 create |
77
+ | 同名同公式、显式字段兼容 | reuse,不写接口 |
78
+ | 同名衍生指标但定义不同 | 计划 update,正式写入要求 `--confirm UPDATE` |
79
+ | 同名基础指标 | 类型冲突,不原地转换 |
80
+ | 多个名称命中不同资源 | 歧义,停止确认 |
81
+
82
+ 更新时脚本清除 `used`、`models`、`isValid`、`formula_snapshot`、`formula_dependency` 等服务端派生字段。新增别名才重新做名称校验,避免旧别名把资源自身识别为冲突。
83
+
84
+ 完整替换名称、删除旧别名或新名称与旧名称没有交集时,用 `--id <现有指标id>` 定位。公式依赖变更前检查直接和传递的 `dependentMeasures`;不通过新建近义指标绕过更新确认。
85
+
86
+ ## 跨模型预检
87
+
88
+ `check-formula` 只校验语法和引用。`upsert-derived` 继续返回:
89
+
90
+ - `dependencies`:递归展开的基础指标 ID;
91
+ - `dependencyModels`:每个基础指标所在的 ready 模型;
92
+ - `formulaReferences`:公式直接引用的指标及其普通/累计分类;
93
+ - `sourceModels`:覆盖全部依赖的最小模型组合;
94
+ - `sharedModels`:能在单模型内覆盖全部依赖的候选;
95
+ - `requiresDataBlend`;
96
+ - `enableDataBlend`、`maxBlendModelNum`、`maxBlendRowNum`;
97
+ - `timeSemantics`、`leafTimeSemantics`、`queryVerificationRequired`、`blockers`。
98
+
99
+ 跨模型创建的硬 blocker:
100
+
101
+ - 任一基础依赖未进入 ready 模型;
102
+ - 工作区未开启 `enableDataBlend`;
103
+ - 最小模型组合超过 `maxBlendModelNum`。
104
+
105
+ `maxBlendModelNum` 未配置时后端默认 3,`maxBlendRowNum` 未配置时默认 100000。实际查询任一子查询超过行数上限仍会失败。
106
+
107
+ 脚本不能仅从指标目录证明以下条件:
108
+
109
+ - 目标时间维度及粒度是否在来源模型间兼容;
110
+ - 目标普通维度是否能作为共同融合键;
111
+ - 默认过滤是否产生相同数据口径;
112
+ - 最终用户的数据权限是否覆盖全部来源模型;
113
+ - 代表性查询是否低于行数限制。
114
+
115
+ 因此 `requiresDataBlend=true` 时必须单独创建 `verification-plan.json`:
116
+
117
+ ```json
118
+ {
119
+ "intendedGrain": ["业务日期", "门店"],
120
+ "sharedTimeSeries": [3],
121
+ "sharedDimensions": [4],
122
+ "sourceModels": ["sales-model", "refund-model"],
123
+ "compatibility": "needs-query-verification"
124
+ }
125
+ ```
126
+
127
+ 该对象不是 `derived.json` 的一部分,也不会发送到指标 API。
128
+
129
+ 没有共同维度的标量计算、普通与累计混合、直接引用与基础叶子的时间语义不一致,以及默认时间轴不同的场景,都需要单独说明口径,不能把公式校验成功写成“跨模型可算”。
130
+
131
+ ## 命令流程
132
+
133
+ ```bash
134
+ node scripts/workspace-keywords.mjs upsert-derived \
135
+ --workspace-id 12 \
136
+ --input derived.json \
137
+ --dry-run
138
+ ```
139
+
140
+ 检查 dry-run 的 `action`、`dependencies`、`coverage` 和 `blockers`。无 blocker 后执行:
141
+
142
+ ```bash
143
+ node scripts/workspace-keywords.mjs upsert-derived \
144
+ --workspace-id 12 \
145
+ --input derived.json
146
+ ```
147
+
148
+ 同名异公式更新:
149
+
150
+ ```bash
151
+ node scripts/workspace-keywords.mjs upsert-derived \
152
+ --workspace-id 12 \
153
+ --id 9 \
154
+ --input derived.json \
155
+ --confirm UPDATE
156
+ ```
157
+
158
+ 写入后脚本验证:
159
+
160
+ - `isValid=true`;
161
+ - `formula_dependency` 与预期基础依赖一致;
162
+ - 公式与显式输入字段一致;
163
+ - `models` 为空;
164
+ - `used` 只反映基础叶子已进入 ready 模型;
165
+ - `queryability` 为 `BLOCKED`、`NEEDS_QUERY_VERIFICATION` 或 `READY_FOR_QUERY_VALIDATION`,不会仅凭保存成功返回“可查询”。
166
+
167
+ ## 查询验收
168
+
169
+ 使用 `datarc-data-search`,至少执行一个与目标用途相同的时间粒度和分组维度查询:
170
+
171
+ 1. 分别查询各基础指标,确认模型、过滤和聚合结果;
172
+ 2. 查询衍生指标,确认融合键和结果行数;
173
+ 3. 手算一组数据对比公式结果;
174
+ 4. 普通/累计混合时分别验证区间查询和趋势查询;
175
+ 5. 记录 `maxBlendRowNum`、权限或共同粒度造成的限制。
176
+
177
+ 完成条件:公式回读有效、依赖一致、代表性查询正确;否则明确 `queryability=BLOCKED/NEEDS_QUERY_VERIFICATION` 和具体 blocker,不能宣称已可查询。
@@ -1,145 +1,145 @@
1
- import { resolveCredentials } from "../../datarc-oauth/scripts/credentials.mjs";
2
- import { readFile } from "node:fs/promises";
3
-
4
- export class DatarcError extends Error {
5
- constructor(message, code = "DATARC_ERROR", details) {
6
- super(message);
7
- this.name = "DatarcError";
8
- this.code = code;
9
- this.details = details;
10
- }
11
- }
12
-
13
- export function parseArgs(argv) {
14
- const result = { _: [] };
15
- for (let index = 0; index < argv.length; index += 1) {
16
- const value = argv[index];
17
- if (!value.startsWith("--")) {
18
- result._.push(value);
19
- continue;
20
- }
21
- const key = value.slice(2);
22
- const next = argv[index + 1];
23
- if (next === undefined || next.startsWith("--")) result[key] = true;
24
- else {
25
- result[key] =
26
- result[key] === undefined ? next : [].concat(result[key], next);
27
- index += 1;
28
- }
29
- }
30
- return result;
31
- }
32
-
33
- export function required(args, key) {
34
- if (args[key] === undefined || args[key] === true || args[key] === "")
35
- throw new DatarcError(`缺少参数 --${key}`, "INVALID_ARGUMENT");
36
- return args[key];
37
- }
38
-
39
- export async function readJson(path) {
40
- const text = path === "-" ? await readStdin() : await readFile(path, "utf8");
41
- try {
42
- return JSON.parse(text);
43
- } catch {
44
- throw new DatarcError("输入不是有效 JSON", "INVALID_JSON");
45
- }
46
- }
47
-
48
- export function createClient(options = {}) {
49
- const { endpoint, token } = resolveCredentials(options);
50
- const fetchImpl = options.fetchImpl ?? globalThis.fetch;
51
- if (!endpoint)
52
- throw new DatarcError("未配置 DATARC_ENDPOINT", "MISSING_ENDPOINT");
53
- if (!token) throw new DatarcError("未配置 DATARC_TOKEN", "MISSING_TOKEN");
54
-
55
- async function request(method, path, { query, body } = {}) {
56
- const url = new URL(`${endpoint}${path}`);
57
- for (const [key, value] of Object.entries(query ?? {}))
58
- for (const item of [].concat(value ?? []))
59
- if (item !== "") url.searchParams.append(key, item);
60
- let response;
61
- try {
62
- response = await fetchImpl(url, {
63
- method,
64
- headers: {
65
- Authorization: token,
66
- ...(body === undefined ? {} : { "Content-Type": "application/json" }),
67
- },
68
- ...(body === undefined ? {} : { body: JSON.stringify(body) }),
69
- });
70
- } catch (error) {
71
- throw new DatarcError(`网络请求失败:${error.message}`, "NETWORK_ERROR");
72
- }
73
- const text = await response.text();
74
- let payload;
75
- try {
76
- payload = text ? JSON.parse(text) : {};
77
- } catch {
78
- throw new DatarcError(
79
- `服务返回非 JSON(HTTP ${response.status})`,
80
- "INVALID_RESPONSE",
81
- );
82
- }
83
- if (!response.ok || (payload.status && payload.status !== "SUCCESS"))
84
- throw new DatarcError(
85
- payload.message || `请求失败(HTTP ${response.status})`,
86
- apiErrorCode(response.status),
87
- {
88
- httpStatus: response.status,
89
- status: payload.status,
90
- data: payload.data,
91
- },
92
- );
93
- return payload.data;
94
- }
95
- return { request };
96
- }
97
-
98
- export function encryptSql(value) {
99
- const first = Buffer.from(String(value), "utf8").toString("base64url");
100
- const second = Buffer.from([...first].reverse().join(""), "utf8").toString(
101
- "base64url",
102
- );
103
- return [...second].reverse().join("");
104
- }
105
-
106
- export function decryptSql(value) {
107
- const second = [...String(value)].reverse().join("");
108
- const firstReversed = Buffer.from(second, "base64url").toString("utf8");
109
- const first = [...firstReversed].reverse().join("");
110
- return Buffer.from(first, "base64url").toString("utf8");
111
- }
112
-
113
- export function errorJson(error) {
114
- return {
115
- code: error?.code ?? "UNEXPECTED_ERROR",
116
- message: error?.message ?? String(error),
117
- ...(error?.details === undefined ? {} : { details: error.details }),
118
- };
119
- }
120
-
121
- export async function runCli(main) {
122
- try {
123
- process.stdout.write(
124
- `${JSON.stringify({ ok: true, data: await main() }, null, 2)}\n`,
125
- );
126
- } catch (error) {
127
- process.stdout.write(
128
- `${JSON.stringify({ ok: false, error: errorJson(error) }, null, 2)}\n`,
129
- );
130
- process.exitCode = 1;
131
- }
132
- }
133
-
134
- function apiErrorCode(status) {
135
- if (status === 401) return "AUTHENTICATION_FAILED";
136
- if (status === 403) return "PERMISSION_DENIED";
137
- if (status === 429) return "RATE_LIMITED";
138
- return "API_ERROR";
139
- }
140
-
141
- async function readStdin() {
142
- const chunks = [];
143
- for await (const chunk of process.stdin) chunks.push(chunk);
144
- return Buffer.concat(chunks).toString("utf8");
145
- }
1
+ import { resolveCredentials } from "../../datarc-oauth/scripts/credentials.mjs";
2
+ import { readFile } from "node:fs/promises";
3
+
4
+ export class DatarcError extends Error {
5
+ constructor(message, code = "DATARC_ERROR", details) {
6
+ super(message);
7
+ this.name = "DatarcError";
8
+ this.code = code;
9
+ this.details = details;
10
+ }
11
+ }
12
+
13
+ export function parseArgs(argv) {
14
+ const result = { _: [] };
15
+ for (let index = 0; index < argv.length; index += 1) {
16
+ const value = argv[index];
17
+ if (!value.startsWith("--")) {
18
+ result._.push(value);
19
+ continue;
20
+ }
21
+ const key = value.slice(2);
22
+ const next = argv[index + 1];
23
+ if (next === undefined || next.startsWith("--")) result[key] = true;
24
+ else {
25
+ result[key] =
26
+ result[key] === undefined ? next : [].concat(result[key], next);
27
+ index += 1;
28
+ }
29
+ }
30
+ return result;
31
+ }
32
+
33
+ export function required(args, key) {
34
+ if (args[key] === undefined || args[key] === true || args[key] === "")
35
+ throw new DatarcError(`缺少参数 --${key}`, "INVALID_ARGUMENT");
36
+ return args[key];
37
+ }
38
+
39
+ export async function readJson(path) {
40
+ const text = path === "-" ? await readStdin() : await readFile(path, "utf8");
41
+ try {
42
+ return JSON.parse(text);
43
+ } catch {
44
+ throw new DatarcError("输入不是有效 JSON", "INVALID_JSON");
45
+ }
46
+ }
47
+
48
+ export function createClient(options = {}) {
49
+ const { endpoint, token } = resolveCredentials(options);
50
+ const fetchImpl = options.fetchImpl ?? globalThis.fetch;
51
+ if (!endpoint)
52
+ throw new DatarcError("未配置 DATARC_ENDPOINT", "MISSING_ENDPOINT");
53
+ if (!token) throw new DatarcError("未配置 DATARC_TOKEN", "MISSING_TOKEN");
54
+
55
+ async function request(method, path, { query, body } = {}) {
56
+ const url = new URL(`${endpoint}${path}`);
57
+ for (const [key, value] of Object.entries(query ?? {}))
58
+ for (const item of [].concat(value ?? []))
59
+ if (item !== "") url.searchParams.append(key, item);
60
+ let response;
61
+ try {
62
+ response = await fetchImpl(url, {
63
+ method,
64
+ headers: {
65
+ Authorization: token,
66
+ ...(body === undefined ? {} : { "Content-Type": "application/json" }),
67
+ },
68
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),
69
+ });
70
+ } catch (error) {
71
+ throw new DatarcError(`网络请求失败:${error.message}`, "NETWORK_ERROR");
72
+ }
73
+ const text = await response.text();
74
+ let payload;
75
+ try {
76
+ payload = text ? JSON.parse(text) : {};
77
+ } catch {
78
+ throw new DatarcError(
79
+ `服务返回非 JSON(HTTP ${response.status})`,
80
+ "INVALID_RESPONSE",
81
+ );
82
+ }
83
+ if (!response.ok || (payload.status && payload.status !== "SUCCESS"))
84
+ throw new DatarcError(
85
+ payload.message || `请求失败(HTTP ${response.status})`,
86
+ apiErrorCode(response.status),
87
+ {
88
+ httpStatus: response.status,
89
+ status: payload.status,
90
+ data: payload.data,
91
+ },
92
+ );
93
+ return payload.data;
94
+ }
95
+ return { request };
96
+ }
97
+
98
+ export function encryptSql(value) {
99
+ const first = Buffer.from(String(value), "utf8").toString("base64url");
100
+ const second = Buffer.from([...first].reverse().join(""), "utf8").toString(
101
+ "base64url",
102
+ );
103
+ return [...second].reverse().join("");
104
+ }
105
+
106
+ export function decryptSql(value) {
107
+ const second = [...String(value)].reverse().join("");
108
+ const firstReversed = Buffer.from(second, "base64url").toString("utf8");
109
+ const first = [...firstReversed].reverse().join("");
110
+ return Buffer.from(first, "base64url").toString("utf8");
111
+ }
112
+
113
+ export function errorJson(error) {
114
+ return {
115
+ code: error?.code ?? "UNEXPECTED_ERROR",
116
+ message: error?.message ?? String(error),
117
+ ...(error?.details === undefined ? {} : { details: error.details }),
118
+ };
119
+ }
120
+
121
+ export async function runCli(main) {
122
+ try {
123
+ process.stdout.write(
124
+ `${JSON.stringify({ ok: true, data: await main() }, null, 2)}\n`,
125
+ );
126
+ } catch (error) {
127
+ process.stdout.write(
128
+ `${JSON.stringify({ ok: false, error: errorJson(error) }, null, 2)}\n`,
129
+ );
130
+ process.exitCode = 1;
131
+ }
132
+ }
133
+
134
+ function apiErrorCode(status) {
135
+ if (status === 401) return "AUTHENTICATION_FAILED";
136
+ if (status === 403) return "PERMISSION_DENIED";
137
+ if (status === 429) return "RATE_LIMITED";
138
+ return "API_ERROR";
139
+ }
140
+
141
+ async function readStdin() {
142
+ const chunks = [];
143
+ for await (const chunk of process.stdin) chunks.push(chunk);
144
+ return Buffer.concat(chunks).toString("utf8");
145
+ }