@dazitech/cli 3.0.7 → 3.0.9
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/README.md +1 -1
- package/dist/clis/dazi-app.js +1 -1
- package/dist/clis/dazi-flow.js +1 -1
- package/dist/clis/dazi-onto.js +73 -22
- package/dist/clis/dazi.js +266 -171
- package/dist/docs/flow/flow-project-guide.md +1 -1
- package/dist/docs/guides/quickstart.md +18 -4
- package/dist/docs/guides/troubleshooting.md +12 -1
- package/dist/docs/guides/workspace-v3.md +43 -23
- package/dist/docs/index.json +28 -3
- package/dist/docs/onto/action-guide.md +3 -3
- package/dist/docs/onto/dazi_script_sdk_reference.md +244 -174
- package/dist/docs/onto/dazi_script_seed_data_guide.md +158 -155
- package/dist/docs/onto/function-guide.md +82 -27
- package/dist/docs/onto/space-management.md +3 -1
- package/dist/docs/onto//346/234/254/344/275/223/345/210/206/347/261/273/350/247/204/345/210/222/344/270/216SDK/346/211/251/345/261/225/346/226/271/346/241/210.md +168 -0
- package/dist/docs/onto//346/234/254/344/275/223/345/221/275/345/220/215/350/247/204/350/214/203_/347/211/251/347/220/206/350/241/250Cube/344/270/216/345/257/271/350/261/241.md +402 -0
- package/dist/docs/onto//346/234/254/344/275/223/350/204/232/346/234/254/347/274/226/345/206/231/346/214/207/345/215/227.md +200 -34
- package/dist/docs/onto//346/234/254/344/275/223/350/247/204/345/210/222/346/214/207/345/215/227.md +188 -38
- package/dist/docs/onto//350/204/232/346/234/254/350/277/220/350/241/214/347/272/240/351/224/231_/345/225/206/345/212/241/346/210/220/346/234/254/346/226/271/346/241/210/345/274/200/345/217/221/350/277/207/347/250/213.md +213 -0
- package/dist/docs/onto//350/247/204/345/210/222/347/244/272/344/276/213_/344/272/247/345/223/201/351/224/200/345/224/256/346/234/254/344/275/223/350/247/204/345/210/222/346/226/271/346/241/210.md +620 -0
- package/dist/docs/onto//350/247/204/345/210/222/347/244/272/344/276/213_/345/210/251/346/266/246/345/210/206/346/236/220/346/234/254/344/275/223/346/226/271/346/241/210.md +680 -541
- package/dist/examples/index.json +208 -22
- package/dist/examples/onto/README.md +51 -0
- package/dist/examples/onto/_templates/ontology_function_template.py +50 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_account_breakdown.py +62 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_budget_vs_actual.py +69 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_cost_center_profit.py +64 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_get_summary.py +61 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_mom_analysis.py +82 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_top_accounts.py +61 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/profit_fn_yoy_analysis.py +79 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/save_test_arguments.ps1 +38 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.account_breakdown.json +1 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.budget_vs_actual.json +1 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.cost_center_profit.json +1 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.get_summary.json +1 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.mom_analysis.json +1 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.top_accounts.json +1 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/functions/test_arguments/profit.fn.yoy_analysis.json +1 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/setup/profit_ontology_init.py +679 -0
- package/dist/examples/onto//345/210/251/346/266/246/347/244/272/344/276/213/setup/profit_seed_data.py +216 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_channel_mix.py +89 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_customer_segmentation.py +121 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_get_summary.py +78 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_mom_analysis.py +89 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_region_breakdown.py +84 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_top_products.py +98 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/sales_fn_yoy_analysis.py +87 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/save_test_arguments.ps1 +38 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.channel_mix.json +5 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.customer_segmentation.json +5 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.get_summary.json +5 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.mom_analysis.json +5 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.region_breakdown.json +5 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.top_products.json +5 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/functions/test_arguments/sales.fn.yoy_analysis.json +5 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/setup/sales_ontology_init.py +539 -0
- package/dist/examples/onto//351/224/200/345/224/256/347/244/272/344/276/213/setup/sales_seed_data.py +163 -0
- package/dist/prompts/index.json +2 -2
- package/dist/prompts/onto/action-design.md +4 -1
- package/dist/prompts/onto/function-design.md +46 -19
- package/dist/prompts/onto/rule-seed.md +5 -1
- package/dist/prompts/onto/script-publish-run.md +87 -25
- package/package.json +1 -1
- package/dist/examples/onto/function/profit_fn_customer_segmentation.py +0 -117
- package/dist/examples/onto/function/profit_fn_mom_analysis.py +0 -89
- package/dist/examples/onto/function/profit_fn_top_products.py +0 -89
- package/dist/examples/onto/function/profit_fn_yoy_analysis.py +0 -89
- package/dist/examples/onto/setup/profit_ontology_init.py +0 -388
|
@@ -1,155 +1,158 @@
|
|
|
1
|
-
# DaziScript 灌数脚本编写指南
|
|
2
|
-
|
|
3
|
-
**文档 ID**: `onto/dazi-script-seed-data-guide`
|
|
4
|
-
**适用**: dazi-vscode v3 + ClickHouse 数据空间
|
|
5
|
-
|
|
6
|
-
> **用途**:约定工作区内「一次性 / 可重复执行」的造数、补数、历史回填脚本的写法,供人与 LLM 统一遵循。
|
|
7
|
-
> SDK API 见 **[dazi_script_sdk_reference](./dazi_script_sdk_reference.md)**;目录与发布见 **[本体脚本编写指南](./本体脚本编写指南.md)**。
|
|
8
|
-
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
## 1. 什么是灌数脚本
|
|
12
|
-
|
|
13
|
-
**灌数脚本**指在**已存在表结构**(或同脚本内先 DDL)的前提下,向当前空间写入**演示数据、回归数据、历史样本**的 DaziScript。
|
|
14
|
-
|
|
15
|
-
### 1.1 放置位置(v3)
|
|
16
|
-
|
|
17
|
-
| 类型 | 推荐路径 |
|
|
18
|
-
| -------------- | ------------------------------------------------------------------------------------------------------ |
|
|
19
|
-
| **项目内开发** |
|
|
20
|
-
| **参考示例** |
|
|
21
|
-
|
|
22
|
-
- `space_id` 以
|
|
23
|
-
- 侧栏 **帮助 → 📎 示例 → 下载所有示例**,或:`dazi examples sync` → `资源/examples/`。
|
|
24
|
-
- 须定义 **`main()`**,**不要**写 `if __name__ == "__main__":`。
|
|
25
|
-
|
|
26
|
-
> 不再使用 `spaces/<space_id>/editorial/scripts/setup
|
|
27
|
-
|
|
28
|
-
---
|
|
29
|
-
|
|
30
|
-
## 2. 硬约束(违反易导致联调失败)
|
|
31
|
-
|
|
32
|
-
### 2.1 ClickHouse:`INSERT ... VALUES` 与注释
|
|
33
|
-
|
|
34
|
-
- **`VALUES` 与各元组之间禁止 SQL 行注释 `--`**。常见报错:`CANNOT_PARSE_INPUT_ASSERTION_FAILED`、`Code: 27`。
|
|
35
|
-
- **允许**:在**整条** `INSERT` **之前**写注释;或拆成多条 `INSERT`。
|
|
36
|
-
- **推荐**:大批量灌数用 **`s.sql.insert_rows(table, rows)`**(`rows` 为 `list[dict]`)。
|
|
37
|
-
|
|
38
|
-
### 2.2 幂等与可重复执行
|
|
39
|
-
|
|
40
|
-
- 先 `query_one` / `count()` 判断已有数据,满足阈值则**跳过**或**仅补差额**。
|
|
41
|
-
- 「清空再灌」须**显式开关**(如 `--force`),**默认**不破坏已有数据。
|
|
42
|
-
|
|
43
|
-
### 2.3 数据质量
|
|
44
|
-
|
|
45
|
-
- 枚举字段勿含多余空格;`Date` / `DateTime` 与表定义、SDK 规整规则一致。
|
|
46
|
-
|
|
47
|
-
### 2.4 输出与验收
|
|
48
|
-
|
|
49
|
-
- 关键步骤 `output.print`;结束可用 `output.success(...)`。
|
|
50
|
-
- 机器解析约定:`__JSON_SUMMARY__` + JSON(与回归脚本一致)。
|
|
51
|
-
|
|
52
|
-
---
|
|
53
|
-
|
|
54
|
-
## 3. 推荐模式对照
|
|
55
|
-
|
|
56
|
-
| 场景 | 推荐做法 |
|
|
57
|
-
| ------------- | ------------------------------------------------------------------ |
|
|
58
|
-
| 少量固定行 | 单条 `INSERT ... VALUES`,元组间**无**行间 `--`;或拆多条 `INSERT` |
|
|
59
|
-
| 多批次、多行 | **`s.sql.insert_rows`** + Python 生成 `list[dict]` |
|
|
60
|
-
| 需读 SQL 文件 | 注释写在 **INSERT 块上方**(Python 侧),不要塞进 `VALUES` 中间 |
|
|
61
|
-
|
|
62
|
-
---
|
|
63
|
-
|
|
64
|
-
## 4. 示例:`insert_rows`(推荐)
|
|
65
|
-
|
|
66
|
-
将 `space_id
|
|
67
|
-
|
|
68
|
-
```python
|
|
69
|
-
"""示例:按空间灌入演示数据(幂等 + insert_rows)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
space_id
|
|
73
|
-
"""
|
|
74
|
-
|
|
75
|
-
import json
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
def main():
|
|
79
|
-
space_id = "space__your_id_here" # 与 README 一致
|
|
80
|
-
table = "your_fact_table"
|
|
81
|
-
s = space.get(space_id)
|
|
82
|
-
|
|
83
|
-
try:
|
|
84
|
-
n = int(s.sql.query_one(f"SELECT count() FROM {table}") or 0)
|
|
85
|
-
except Exception:
|
|
86
|
-
n = 0
|
|
87
|
-
if n > 0:
|
|
88
|
-
output.print(f"{table} 已有 {n} 行,跳过灌数")
|
|
89
|
-
return
|
|
90
|
-
|
|
91
|
-
rows = [
|
|
92
|
-
{"id": "row_001", "name": "样例A", "metric_value": 100.5, "biz_date": "2025-01-10"},
|
|
93
|
-
{"id": "row_002", "name": "样例B", "metric_value": 200.0, "biz_date": "2025-01-11"},
|
|
94
|
-
]
|
|
95
|
-
|
|
96
|
-
inserted = s.sql.insert_rows(table, rows)
|
|
97
|
-
output.print(f"已插入 {inserted} 行")
|
|
98
|
-
output.success("灌数完成")
|
|
99
|
-
output.print("__JSON_SUMMARY__" + json.dumps(
|
|
100
|
-
{"ok": True, "table": table, "inserted": inserted},
|
|
101
|
-
ensure_ascii=True,
|
|
102
|
-
default=str,
|
|
103
|
-
))
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
---
|
|
107
|
-
|
|
108
|
-
## 5. 示例:单条 `execute` + `VALUES`
|
|
109
|
-
|
|
110
|
-
```python
|
|
111
|
-
def main():
|
|
112
|
-
s = space.get("space__your_id_here")
|
|
113
|
-
if int(s.sql.query_one("SELECT count() FROM your_fact_table WHERE id = 'row_001'") or 0) > 0:
|
|
114
|
-
output.print("已存在,跳过")
|
|
115
|
-
return
|
|
116
|
-
|
|
117
|
-
s.sql.execute(
|
|
118
|
-
"""
|
|
119
|
-
INSERT INTO your_fact_table VALUES
|
|
120
|
-
('row_001', '样例A', 100.5, toDate('2025-01-10')),
|
|
121
|
-
('row_002', '样例B', 200.0, toDate('2025-01-11'))
|
|
122
|
-
"""
|
|
123
|
-
)
|
|
124
|
-
output.success("灌数完成")
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
---
|
|
128
|
-
|
|
129
|
-
## 6. 发布与执行(dazi-vscode)
|
|
130
|
-
|
|
131
|
-
在工作区根目录(`--space`
|
|
132
|
-
|
|
133
|
-
```bash
|
|
134
|
-
# 预检
|
|
135
|
-
dazi onto script publish-preview
|
|
136
|
-
|
|
137
|
-
# 发布到平台(data_script / 初始化脚本)
|
|
138
|
-
dazi onto script publish
|
|
139
|
-
|
|
140
|
-
# 若已入库且已知 script-id,可在平台侧执行;或通过 Onto 侧栏运行
|
|
141
|
-
dazi onto script run --script-id <script-id> --space <space-id> --params '{}'
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
- [
|
|
1
|
+
# DaziScript 灌数脚本编写指南
|
|
2
|
+
|
|
3
|
+
**文档 ID**: `onto/dazi-script-seed-data-guide`
|
|
4
|
+
**适用**: dazi-vscode v3 + ClickHouse 数据空间
|
|
5
|
+
|
|
6
|
+
> **用途**:约定工作区内「一次性 / 可重复执行」的造数、补数、历史回填脚本的写法,供人与 LLM 统一遵循。
|
|
7
|
+
> SDK API 见 **[dazi_script_sdk_reference](./dazi_script_sdk_reference.md)**;目录与发布见 **[本体脚本编写指南](./本体脚本编写指南.md)**。
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## 1. 什么是灌数脚本
|
|
12
|
+
|
|
13
|
+
**灌数脚本**指在**已存在表结构**(或同脚本内先 DDL)的前提下,向当前空间写入**演示数据、回归数据、历史样本**的 DaziScript。
|
|
14
|
+
|
|
15
|
+
### 1.1 放置位置(v3)
|
|
16
|
+
|
|
17
|
+
| 类型 | 推荐路径 |
|
|
18
|
+
| -------------- | ------------------------------------------------------------------------------------------------------ |
|
|
19
|
+
| **项目内开发** | `<工作区根>/项目/<业务名>/本体/ontos/<实现名>/setup/<名称>.py` |
|
|
20
|
+
| **参考示例** | **`资源/examples/onto/销售示例/setup/`**(init + seed)、**`资源/examples/onto/利润示例/setup/`**(init 模板) |
|
|
21
|
+
|
|
22
|
+
- `space_id` 以 **`项目/<业务名>/本体/ontos/<实现名>/README.md`** 为准(扩展「新建本体实现」时已绑定)。
|
|
23
|
+
- 侧栏 **帮助 → 📎 示例 → 下载所有示例**,或:`dazi examples sync` → `资源/examples/`。
|
|
24
|
+
- 须定义 **`main()`**,**不要**写 `if __name__ == "__main__":`。
|
|
25
|
+
|
|
26
|
+
> 不再使用 `spaces/<space_id>/editorial/scripts/setup/`、`项目/onto_<名称>/脚本/` 作为 v3 本地约定路径。
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 2. 硬约束(违反易导致联调失败)
|
|
31
|
+
|
|
32
|
+
### 2.1 ClickHouse:`INSERT ... VALUES` 与注释
|
|
33
|
+
|
|
34
|
+
- **`VALUES` 与各元组之间禁止 SQL 行注释 `--`**。常见报错:`CANNOT_PARSE_INPUT_ASSERTION_FAILED`、`Code: 27`。
|
|
35
|
+
- **允许**:在**整条** `INSERT` **之前**写注释;或拆成多条 `INSERT`。
|
|
36
|
+
- **推荐**:大批量灌数用 **`s.sql.insert_rows(table, rows)`**(`rows` 为 `list[dict]`)。
|
|
37
|
+
|
|
38
|
+
### 2.2 幂等与可重复执行
|
|
39
|
+
|
|
40
|
+
- 先 `query_one` / `count()` 判断已有数据,满足阈值则**跳过**或**仅补差额**。
|
|
41
|
+
- 「清空再灌」须**显式开关**(如 `--force`),**默认**不破坏已有数据。
|
|
42
|
+
|
|
43
|
+
### 2.3 数据质量
|
|
44
|
+
|
|
45
|
+
- 枚举字段勿含多余空格;`Date` / `DateTime` 与表定义、SDK 规整规则一致。
|
|
46
|
+
|
|
47
|
+
### 2.4 输出与验收
|
|
48
|
+
|
|
49
|
+
- 关键步骤 `output.print`;结束可用 `output.success(...)`。
|
|
50
|
+
- 机器解析约定:`__JSON_SUMMARY__` + JSON(与回归脚本一致)。
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 3. 推荐模式对照
|
|
55
|
+
|
|
56
|
+
| 场景 | 推荐做法 |
|
|
57
|
+
| ------------- | ------------------------------------------------------------------ |
|
|
58
|
+
| 少量固定行 | 单条 `INSERT ... VALUES`,元组间**无**行间 `--`;或拆多条 `INSERT` |
|
|
59
|
+
| 多批次、多行 | **`s.sql.insert_rows`** + Python 生成 `list[dict]` |
|
|
60
|
+
| 需读 SQL 文件 | 注释写在 **INSERT 块上方**(Python 侧),不要塞进 `VALUES` 中间 |
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## 4. 示例:`insert_rows`(推荐)
|
|
65
|
+
|
|
66
|
+
将 `space_id`、表名、列名替换为实现单元 README 与规划文档中的真实值。表须已存在(或在本脚本前文 DDL 创建)。
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
"""示例:按空间灌入演示数据(幂等 + insert_rows)
|
|
70
|
+
|
|
71
|
+
放置:项目/<业务名>/本体/ontos/<实现名>/setup/demo_seed.py
|
|
72
|
+
space_id:见实现单元 README.md
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
import json
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def main():
|
|
79
|
+
space_id = "space__your_id_here" # 与 README 一致
|
|
80
|
+
table = "your_fact_table"
|
|
81
|
+
s = space.get(space_id)
|
|
82
|
+
|
|
83
|
+
try:
|
|
84
|
+
n = int(s.sql.query_one(f"SELECT count() FROM {table}") or 0)
|
|
85
|
+
except Exception:
|
|
86
|
+
n = 0
|
|
87
|
+
if n > 0:
|
|
88
|
+
output.print(f"{table} 已有 {n} 行,跳过灌数")
|
|
89
|
+
return
|
|
90
|
+
|
|
91
|
+
rows = [
|
|
92
|
+
{"id": "row_001", "name": "样例A", "metric_value": 100.5, "biz_date": "2025-01-10"},
|
|
93
|
+
{"id": "row_002", "name": "样例B", "metric_value": 200.0, "biz_date": "2025-01-11"},
|
|
94
|
+
]
|
|
95
|
+
|
|
96
|
+
inserted = s.sql.insert_rows(table, rows)
|
|
97
|
+
output.print(f"已插入 {inserted} 行")
|
|
98
|
+
output.success("灌数完成")
|
|
99
|
+
output.print("__JSON_SUMMARY__" + json.dumps(
|
|
100
|
+
{"ok": True, "table": table, "inserted": inserted},
|
|
101
|
+
ensure_ascii=True,
|
|
102
|
+
default=str,
|
|
103
|
+
))
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## 5. 示例:单条 `execute` + `VALUES`
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
def main():
|
|
112
|
+
s = space.get("space__your_id_here")
|
|
113
|
+
if int(s.sql.query_one("SELECT count() FROM your_fact_table WHERE id = 'row_001'") or 0) > 0:
|
|
114
|
+
output.print("已存在,跳过")
|
|
115
|
+
return
|
|
116
|
+
|
|
117
|
+
s.sql.execute(
|
|
118
|
+
"""
|
|
119
|
+
INSERT INTO your_fact_table VALUES
|
|
120
|
+
('row_001', '样例A', 100.5, toDate('2025-01-10')),
|
|
121
|
+
('row_002', '样例B', 200.0, toDate('2025-01-11'))
|
|
122
|
+
"""
|
|
123
|
+
)
|
|
124
|
+
output.success("灌数完成")
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## 6. 发布与执行(dazi-vscode)
|
|
130
|
+
|
|
131
|
+
在工作区根目录(`--space` 与实现单元 README 一致):
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
# 预检
|
|
135
|
+
dazi onto script publish-preview 项目/<业务名>/本体/ontos/<实现名>/setup/demo_seed.py --space <space-id>
|
|
136
|
+
|
|
137
|
+
# 发布到平台(data_script / 初始化脚本)
|
|
138
|
+
dazi onto script publish 项目/<业务名>/本体/ontos/<实现名>/setup/demo_seed.py --space <space-id>
|
|
139
|
+
|
|
140
|
+
# 若已入库且已知 script-id,可在平台侧执行;或通过 Onto 侧栏运行
|
|
141
|
+
dazi onto script run --script-id <script-id> --space <space-id> --params '{}'
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
**参考完整初始化+灌数**:
|
|
145
|
+
|
|
146
|
+
- **推荐(已落地)**:`资源/examples/onto/销售示例/setup/sales_ontology_init.py`、`sales_seed_data.py`(见 [规划示例_产品销售本体规划方案.md](./规划示例_产品销售本体规划方案.md))
|
|
147
|
+
- **通用模板**:复制 `资源/examples/onto/利润示例/setup/profit_ontology_init.py` 到 `项目/<业务名>/本体/ontos/<实现名>/setup/` 后按空间改 `space_id` 与表名,再发布执行。
|
|
148
|
+
|
|
149
|
+
> **已废弃**:`dazi-agent run --file "spaces/.../editorial/..."` — 请改用上表 `dazi onto script publish` / `function run`。
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## 7. 相关文档
|
|
154
|
+
|
|
155
|
+
- [DaziScript SDK 参考](./dazi_script_sdk_reference.md)
|
|
156
|
+
- [本体脚本编写指南](./本体脚本编写指南.md)
|
|
157
|
+
- [本体脚本编写指南](./本体脚本编写指南.md)
|
|
158
|
+
- [本体规划指南](./本体规划指南.md)
|
|
@@ -2,67 +2,122 @@
|
|
|
2
2
|
|
|
3
3
|
**文档 ID**: `onto/function-guide`
|
|
4
4
|
|
|
5
|
+
> **执行脚本前必读**:[`脚本运行纠错_商务成本方案开发过程.md`](./脚本运行纠错_商务成本方案开发过程.md)(CLI、SQL、`output.print_json` 等常见误用)。
|
|
6
|
+
|
|
5
7
|
## 函数生命周期
|
|
6
8
|
|
|
7
9
|
```
|
|
8
|
-
|
|
10
|
+
编写 .py → publish-preview(可选)→ publish(--register-function-id 入库)
|
|
11
|
+
→ function run 验证 → save-test-arguments(写入 test_arguments,侧栏预填)
|
|
12
|
+
→ 后续改代码:update-code
|
|
9
13
|
```
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
工作路径:`项目/<业务名>/本体/ontos/<实现名>/functions/`;本地 JSON:`functions/test_arguments/<function_id>.json`。
|
|
12
16
|
|
|
13
|
-
|
|
14
|
-
# 发布新函数(路径指向本体项目 脚本/ 目录)
|
|
15
|
-
dazi onto function publish 项目/onto_<项目名>/脚本/my_func.py \
|
|
16
|
-
--space <space-id> \
|
|
17
|
-
--function-id my_function \
|
|
18
|
-
--display-name "我的函数" \
|
|
19
|
-
--entry main
|
|
20
|
-
```
|
|
17
|
+
## 新建函数
|
|
21
18
|
|
|
22
|
-
|
|
19
|
+
**推荐**:复制 `资源/examples/onto/_templates/ontology_function_template.py` 或 `销售示例/functions/sales_fn_*.py`,再改业务逻辑。
|
|
23
20
|
|
|
24
21
|
```bash
|
|
25
|
-
|
|
22
|
+
# 发布新函数(路径指向本体实现单元 functions/ 目录)
|
|
23
|
+
dazi onto script publish 项目/<业务名>/本体/ontos/<实现名>/functions/my_func.py \
|
|
26
24
|
--space <space-id> \
|
|
27
|
-
--register-function-id
|
|
25
|
+
--register-function-id domain.fn.my_analysis
|
|
28
26
|
```
|
|
29
27
|
|
|
30
|
-
`<space-id>` 取自
|
|
28
|
+
`<space-id>` 取自 **`项目/<业务名>/本体/ontos/<实现名>/README.md`**。
|
|
31
29
|
|
|
32
30
|
## 更新已有函数代码
|
|
33
31
|
|
|
34
32
|
```bash
|
|
35
|
-
# 用工作副本快速更新(不做完整 publish 流程)
|
|
36
33
|
dazi onto function update-code <function-id> \
|
|
37
34
|
--space <space-id> \
|
|
38
35
|
--stem my_func
|
|
39
36
|
```
|
|
40
37
|
|
|
41
|
-
`--stem` 为脚本文件名(不含扩展名),须与
|
|
38
|
+
`--stem` 为脚本文件名(不含扩展名),须与 `functions/` 下文件一致。
|
|
42
39
|
|
|
43
40
|
## 运行函数
|
|
44
41
|
|
|
45
42
|
```bash
|
|
46
43
|
dazi onto function run <function-id> \
|
|
47
44
|
--space <space-id> \
|
|
48
|
-
--params '{
|
|
45
|
+
--params '{}'
|
|
49
46
|
```
|
|
50
47
|
|
|
51
|
-
|
|
48
|
+
> **`function run` 不支持 `--arguments-json-file`**。详见 [脚本运行纠错](./脚本运行纠错_商务成本方案开发过程.md#4-cli-参数认知错误)。
|
|
49
|
+
|
|
50
|
+
PowerShell 下复杂 JSON 建议用环境变量 `DAZI_PARAMS` 或侧栏运行(使用已保存的 `test_arguments`)。详见 [本体脚本编写指南](./本体脚本编写指南.md#函数测试参数test_arguments发布后必做)。
|
|
51
|
+
|
|
52
|
+
## 保存测试参数(test_arguments)
|
|
53
|
+
|
|
54
|
+
函数 **`run` 验证通过后**,须将默认入参写入函数定义,侧栏 **Onto → 运行函数** 才会预填。
|
|
55
|
+
|
|
56
|
+
**本地文件**:`functions/test_arguments/<function_id>.json`(格式见 [本体脚本编写指南](./本体脚本编写指南.md))。
|
|
52
57
|
|
|
53
58
|
```bash
|
|
54
|
-
dazi onto function save-test-arguments <
|
|
59
|
+
dazi onto function save-test-arguments <ofn_internal_id> \
|
|
55
60
|
--space <space-id> \
|
|
56
|
-
--
|
|
61
|
+
--arguments-json-file 项目/<业务名>/本体/ontos/<实现名>/functions/test_arguments/<function_id>.json
|
|
57
62
|
```
|
|
58
63
|
|
|
59
|
-
|
|
64
|
+
> **注意**:须用平台**内部 id** `ofn_xxx`(`dazi onto function list` 返回),不是 `function_id` 字符串。
|
|
65
|
+
|
|
66
|
+
## 函数脚本结构(标准模板)
|
|
67
|
+
|
|
68
|
+
本体函数与 setup/seed 脚本不同:**`main()` 无参**,平台注入 `ctx`、`space`、`onto`;**必须 return** `function_result`,**禁止** `output.print_json()`。
|
|
60
69
|
|
|
61
70
|
```python
|
|
62
|
-
|
|
63
|
-
""
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
71
|
+
TEST_ARGUMENTS = {
|
|
72
|
+
"v": 1,
|
|
73
|
+
"arguments": {"start_date": "2025-01-01", "end_date": "2026-06-30"},
|
|
74
|
+
"object_type_code": "SalesAnalysis",
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _ontology_fn_body(p):
|
|
79
|
+
params = dict(p.get_params() or {})
|
|
80
|
+
start_date = params.get("start_date", "")
|
|
81
|
+
end_date = params.get("end_date", "")
|
|
82
|
+
|
|
83
|
+
rows = p.sql.query(f"SELECT sum(amount) AS total FROM fact WHERE date >= '{start_date}'")
|
|
84
|
+
row = rows[0] if rows else {}
|
|
85
|
+
total = float(row.get("total", 0) or 0)
|
|
86
|
+
|
|
87
|
+
return p.function_result(
|
|
88
|
+
columns=["total"],
|
|
89
|
+
data=[{"total": total}],
|
|
90
|
+
row_count=1,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def main():
|
|
95
|
+
s = space.get(ctx.space_id or "")
|
|
96
|
+
_Ports = type(
|
|
97
|
+
"_Ports",
|
|
98
|
+
(),
|
|
99
|
+
{
|
|
100
|
+
"get_params": lambda self: dict(ctx.params or {}),
|
|
101
|
+
"function_result": lambda self, **kw: onto.function_result(**kw),
|
|
102
|
+
},
|
|
103
|
+
)
|
|
104
|
+
p = _Ports()
|
|
105
|
+
p.sql = s.sql
|
|
106
|
+
return _ontology_fn_body(p)
|
|
68
107
|
```
|
|
108
|
+
|
|
109
|
+
| 要点 | 说明 |
|
|
110
|
+
| ---- | ---- |
|
|
111
|
+
| 入口 | **`def main():`**(无 `params` 形参) |
|
|
112
|
+
| 入参 | `ctx.params` → `p.get_params()` |
|
|
113
|
+
| 空间 / SQL | `s = space.get(ctx.space_id)` → `p.sql = s.sql` |
|
|
114
|
+
| 出参 | **`return p.function_result(columns=..., data=..., row_count=...)`** |
|
|
115
|
+
| 禁止 | `output.print_json`、`main(params: dict)`、裸 `return {"k": v}` |
|
|
116
|
+
|
|
117
|
+
可复制模板:`资源/examples/onto/_templates/ontology_function_template.py` · 参考实现:`销售示例/functions/sales_fn_get_summary.py`。
|
|
118
|
+
|
|
119
|
+
## 相关文档
|
|
120
|
+
|
|
121
|
+
- [脚本运行纠错(商务成本方案实录)](./脚本运行纠错_商务成本方案开发过程.md)
|
|
122
|
+
- [本体脚本编写指南](./本体脚本编写指南.md)
|
|
123
|
+
- [DaziScript SDK 参考](./dazi_script_sdk_reference.md)
|
|
@@ -23,7 +23,7 @@ dazi onto space list
|
|
|
23
23
|
dazi onto space init --space-id <space-id>
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
本地目录结构(**历史/遗留布局**,由 CLI `space init` 生成):
|
|
27
27
|
|
|
28
28
|
```
|
|
29
29
|
onto/<space-id>/
|
|
@@ -37,6 +37,8 @@ onto/<space-id>/
|
|
|
37
37
|
.dazi-space.json
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
+
> **推荐工作区结构(v3)**:日常开发与文档约定使用 **`项目/<业务名>/本体/ontos/<实现名>/`** 实现单元布局(`README.md` 为 `space_id` 权威来源,`plans/`、`setup/`、`functions/`、`快速启动_<实现名>.md`)。`dazi onto space init` 生成的 **`onto/<space-id>/`** 仅用于 CLI 拉取快照、同步脚本等**对照/迁移**场景,**不应**作为新建本体项目的主开发路径。详见 **[本体规划指南](./本体规划指南.md)**、**[本体脚本编写指南](./本体脚本编写指南.md)**。
|
|
41
|
+
|
|
40
42
|
## 拉取快照
|
|
41
43
|
|
|
42
44
|
将远端本体数据同步到本地:
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# 349 · 本体分类规划与 SDK 扩展方案
|
|
2
|
+
|
|
3
|
+
**文档 ID**:`onto/category-taxonomy-sdk`
|
|
4
|
+
|
|
5
|
+
> **文档编号**:349
|
|
6
|
+
> **类型**:规范 + 实施方案(对齐 §347 · `ads_categories` 平台分类)
|
|
7
|
+
> **同步路径**:`dazi docs sync` → `资源/docs/onto/本体分类规划与SDK扩展方案.md`
|
|
8
|
+
> **前置依据**:[本体命名规范 §347](./本体命名规范_物理表Cube与对象.md)、[本体规划指南](./本体规划指南.md)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 1. 目标
|
|
13
|
+
|
|
14
|
+
将 **347 语义分类**(规划表「类别/分类」列)与 **`ads_categories` 平台分类**(侧栏分组、桥表挂载)打通,支持 init 脚本幂等注册。
|
|
15
|
+
|
|
16
|
+
**约束**:本体规划阶段分类 **全部平级**(挂空间默认根下,不建多层树)。
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## 2. 两类分类,一套语言
|
|
21
|
+
|
|
22
|
+
| 维度 | 347 语义分类 | ads_categories 平台分类 |
|
|
23
|
+
|------|-------------|------------------------|
|
|
24
|
+
| 作用 | 规划自检、命名约束 | 侧栏展示、资源分组 |
|
|
25
|
+
| 对齐 | 平台 `category_name` **=** 347 标准中文名 | 禁止自定义别名 |
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 3. 347 标准分类字典(6 类资源)
|
|
30
|
+
|
|
31
|
+
### 3.1 物理表(`category_kind=dataspace_table`)
|
|
32
|
+
|
|
33
|
+
| 347 类别 | 前缀 | 平台分类名 |
|
|
34
|
+
|----------|------|------------|
|
|
35
|
+
| 时间维 | `dim_date` | 时间维 |
|
|
36
|
+
| 维度表 | `dim_` | 维度表 |
|
|
37
|
+
| 事实表 | `fact_` | 事实表 |
|
|
38
|
+
| 桥表 | `bridge_` | 桥表 |
|
|
39
|
+
| 映射表 | `map_` | 映射表 |
|
|
40
|
+
| 配置表 | `cfg_` | 配置表 |
|
|
41
|
+
| 汇总表 | `agg_` | 汇总表 |
|
|
42
|
+
| 临时表 | `tmp_` | 临时表 |
|
|
43
|
+
|
|
44
|
+
### 3.2 Cube(`category_kind=cube`)
|
|
45
|
+
|
|
46
|
+
| 347 类别 | 平台分类名 |
|
|
47
|
+
|----------|------------|
|
|
48
|
+
| Process | 流程型 |
|
|
49
|
+
| Subject | 主体型 |
|
|
50
|
+
| Comparison | 对比型 |
|
|
51
|
+
| Snapshot | 快照型 |
|
|
52
|
+
|
|
53
|
+
> 时间增强不单独建 Cube(347 §4.2),不设平台分类。
|
|
54
|
+
|
|
55
|
+
### 3.3 本体对象(`category_kind=ontology_object_type`)
|
|
56
|
+
|
|
57
|
+
| 347 分类 | 平台分类名 |
|
|
58
|
+
|----------|------------|
|
|
59
|
+
| Master | 主数据 |
|
|
60
|
+
| Transaction | 事务 |
|
|
61
|
+
| Analytical | 分析 |
|
|
62
|
+
| Reference | 参考 |
|
|
63
|
+
| Configuration | 配置 |
|
|
64
|
+
|
|
65
|
+
### 3.4 表间关系(`category_kind=relation`,347 扩展)
|
|
66
|
+
|
|
67
|
+
| 关系类别 | 平台分类名 |
|
|
68
|
+
|----------|------------|
|
|
69
|
+
| 时间关联 | 时间关联 |
|
|
70
|
+
| 主数据关联 | 主数据关联 |
|
|
71
|
+
| 层级自关联 | 层级自关联 |
|
|
72
|
+
| 预实关联 | 预实关联 |
|
|
73
|
+
| 桥接关联 | 桥接关联 |
|
|
74
|
+
|
|
75
|
+
### 3.5 本体链接(`category_kind=ontology_link_type`,347 扩展)
|
|
76
|
+
|
|
77
|
+
| 链接类别 | 平台分类名 |
|
|
78
|
+
|----------|------------|
|
|
79
|
+
| 归属/包含 | 归属关系 |
|
|
80
|
+
| 分析归因 | 分析归因 |
|
|
81
|
+
| 层级/结构 | 层级关系 |
|
|
82
|
+
| 对比/映射 | 对比关系 |
|
|
83
|
+
|
|
84
|
+
### 3.6 本体函数(`category_kind=ontology_function`,347 扩展)
|
|
85
|
+
|
|
86
|
+
| 函数类别 | 平台分类名 |
|
|
87
|
+
|----------|------------|
|
|
88
|
+
| 总览 | 总览分析 |
|
|
89
|
+
| 趋势 | 趋势分析 |
|
|
90
|
+
| 结构 | 结构分析 |
|
|
91
|
+
| 预实 | 预实分析 |
|
|
92
|
+
| 组织 | 组织分析 |
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 4. SDK 设计
|
|
97
|
+
|
|
98
|
+
### 4.1 模块
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
s.categories # CategoryManager
|
|
102
|
+
s.categories.apply_registry(CATEGORY_REGISTRY)
|
|
103
|
+
s.categories.ensure_347(kind="table", category="维度表")
|
|
104
|
+
s.categories.assign_table("维度表", "dim_account")
|
|
105
|
+
s.categories.auto_assign_tables(["dim_account", "fact_gl_journal_entry"])
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 4.2 init 脚本
|
|
109
|
+
|
|
110
|
+
**方式 A — 批量**(推荐):顶部 `CATEGORY_REGISTRY` 与规划 **附录 B** 对齐;资源注册完成后:
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
cat_counts = s.categories.apply_registry(CATEGORY_REGISTRY, skip_missing=True)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**方式 B — 内联**(P2):各注册 API 支持 `category_347=`,注册后即时挂载(与方式 A 幂等可并存):
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
s.tables.register_with_meta("dim_product", ..., category_347="维度表")
|
|
120
|
+
s.register_cube("SalesCube", ..., category_347="流程型")
|
|
121
|
+
s.onto.define_object_type("Product", "产品", category_347="主数据")
|
|
122
|
+
s.tables.add_relationship(..., category_347="时间关联")
|
|
123
|
+
s.onto.define_link_type(..., category_347="归属关系")
|
|
124
|
+
s.onto.register_function("sales.fn.get_summary", adapter, category_347="总览分析")
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
函数类资源若 init 未 `register_function`,批量方式须 `skip_missing=True`。
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## 5. 规划与 init 约定
|
|
132
|
+
|
|
133
|
+
| 产出 | 位置 | 说明 |
|
|
134
|
+
| ---- | ---- | ---- |
|
|
135
|
+
| 规划附录 B | `plans/<主题>.md` | 6 类资源分类对照表 |
|
|
136
|
+
| `CATEGORY_REGISTRY` | `setup/*_ontology_init.py` 顶部 | 与附录 B 逐行一致 |
|
|
137
|
+
| 分类步骤 | init 末尾 | `[10/10] apply_registry` |
|
|
138
|
+
|
|
139
|
+
参考:[规划示例_产品销售](./规划示例_产品销售本体规划方案.md#附录-b347-分类挂载对照表category_registry)、[规划示例_利润分析](./规划示例_利润分析本体方案.md#附录-b347-分类挂载对照表category_registry)。
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## 6. 验收标准
|
|
144
|
+
|
|
145
|
+
1. 平台侧栏分类名与 347 标准表一致
|
|
146
|
+
2. `dim_*` / `fact_*` 自动归入维度表/事实表
|
|
147
|
+
3. init 重跑幂等,桥表不重复
|
|
148
|
+
4. `CATEGORY_REGISTRY` 与规划附录 B 逐行一致
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## 7. 相关文档
|
|
153
|
+
|
|
154
|
+
| 文档 | 路径(`dazi docs sync` 后) |
|
|
155
|
+
| ---- | --------------------------- |
|
|
156
|
+
| 347 命名规范 | `资源/docs/onto/本体命名规范_物理表Cube与对象.md` |
|
|
157
|
+
| 本体规划指南 | `资源/docs/onto/本体规划指南.md` |
|
|
158
|
+
| SDK 参考 §5.6 | `资源/docs/onto/dazi_script_sdk_reference.md` |
|
|
159
|
+
| 脚本编写指南 | `资源/docs/onto/本体脚本编写指南.md` |
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
**变更记录**
|
|
164
|
+
|
|
165
|
+
| 日期 | 说明 |
|
|
166
|
+
| ---- | ---- |
|
|
167
|
+
| 2026-06-06 | 首版:对齐 347 分类方案 + SDK 设计 |
|
|
168
|
+
| 2026-06-06 | 改为 `dazi-vscode` 完整可同步正文(客户环境不依赖 `dazi/docs/`) |
|