@arcticdata/dsh-datarc-skills 0.0.0-stage → 26.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -2
- package/cordis.patch.yml +4 -0
- package/icon.svg +4 -0
- package/index.js +35 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +37 -4
- package/skills/datarc-access-permissions/SKILL.md +30 -0
- package/skills/datarc-access-permissions/references/api.md +31 -0
- package/skills/datarc-access-permissions/references/write-order.md +16 -0
- package/skills/datarc-analysis-report/SKILL.md +125 -0
- package/skills/datarc-analysis-report/agents/openai.yaml +6 -0
- package/skills/datarc-analysis-report/references/analyst-frameworks.md +215 -0
- package/skills/datarc-analysis-report/references/api-contracts.md +478 -0
- package/skills/datarc-analysis-report/references/direct-card-fallback-design.md +66 -0
- package/skills/datarc-analysis-report/references/human-in-the-loop.md +142 -0
- package/skills/datarc-analysis-report/references/loop-personalization-policy.md +138 -0
- package/skills/datarc-analysis-report/references/manifest-example.json +779 -0
- package/skills/datarc-analysis-report/references/narrative-schema.md +160 -0
- package/skills/datarc-analysis-report/references/question-syntax.md +151 -0
- package/skills/datarc-analysis-report/scripts/run_analysis_report.py +2225 -0
- package/skills/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -0
- package/skills/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -0
- package/skills/datarc-data-search/SKILL.md +243 -0
- package/skills/datarc-data-search/scripts/calculate.py +116 -0
- package/skills/datarc-data-search/scripts/data-search.mjs +628 -0
- package/skills/datarc-data-search/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-data-search/scripts/search-result.mjs +211 -0
- package/skills/datarc-dataseek-batch-evaluation/SKILL.md +59 -0
- package/skills/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -0
- package/skills/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -0
- package/skills/datarc-dataseek-batch-evaluation/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -0
- package/skills/datarc-dataseek-search/SKILL.md +96 -0
- package/skills/datarc-dataseek-search/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -0
- package/skills/datarc-dataseek-semantic-debugging/SKILL.md +44 -0
- package/skills/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -0
- package/skills/datarc-dataseek-semantic-debugging/evals/evals.json +165 -0
- package/skills/datarc-dataseek-semantic-debugging/references/dataset.md +77 -0
- package/skills/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -0
- package/skills/datarc-dataseek-semantic-debugging/references/interventions.md +64 -0
- package/skills/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -0
- package/skills/datarc-dataseek-semantic-debugging/references/rollback.md +11 -0
- package/skills/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -0
- package/skills/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -0
- package/skills/datarc-dataseek-testset-generation/SKILL.md +47 -0
- package/skills/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -0
- package/skills/datarc-datasource/SKILL.md +97 -0
- package/skills/datarc-oauth/SKILL.md +47 -0
- package/skills/datarc-oauth/scripts/auth.mjs +130 -0
- package/skills/datarc-oauth/scripts/credentials.mjs +237 -0
- package/skills/datarc-report-add-card/SKILL.md +90 -0
- package/skills/datarc-report-add-card/agents/openai.yaml +4 -0
- package/skills/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-report-add-card/scripts/report-card.mjs +439 -0
- package/skills/datarc-semantic-alias/SKILL.md +20 -0
- package/skills/datarc-semantic-alias/scripts/alias-plan.mjs +422 -0
- package/skills/datarc-semantic-alias/scripts/alias.mjs +11 -0
- package/skills/datarc-semantic-calibration/SKILL.md +20 -0
- package/skills/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -0
- package/skills/datarc-semantic-calibration/scripts/calibration.mjs +11 -0
- package/skills/datarc-semantic-core/SKILL.md +12 -0
- package/skills/datarc-semantic-core/scripts/artifact-journal.mjs +830 -0
- package/skills/datarc-semantic-core/scripts/canonical.mjs +48 -0
- package/skills/datarc-semantic-core/scripts/case-model.mjs +608 -0
- package/skills/datarc-semantic-core/scripts/cli.mjs +180 -0
- package/skills/datarc-semantic-core/scripts/datarc-client.mjs +180 -0
- package/skills/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -0
- package/skills/datarc-semantic-core/scripts/interventions.mjs +693 -0
- package/skills/datarc-semantic-core/scripts/live-resolution.mjs +305 -0
- package/skills/datarc-semantic-core/scripts/model-scope.mjs +49 -0
- package/skills/datarc-semantic-core/scripts/reproduction.mjs +330 -0
- package/skills/datarc-semantic-core/scripts/semantic-cli.mjs +740 -0
- package/skills/datarc-semantic-core/scripts/shared.mjs +312 -0
- package/skills/datarc-semantic-core/scripts/verification.mjs +1014 -0
- package/skills/datarc-semantic-dataset/SKILL.md +20 -0
- package/skills/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -0
- package/skills/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -0
- package/skills/datarc-semantic-dataset/scripts/dataset.mjs +11 -0
- package/skills/datarc-semantic-knowledge/SKILL.md +38 -0
- package/skills/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -0
- package/skills/datarc-semantic-knowledge/references/knowledge-generation.json +16 -0
- package/skills/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -0
- package/skills/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -0
- package/skills/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -0
- package/skills/datarc-semantic-prompt/SKILL.md +20 -0
- package/skills/datarc-semantic-prompt/scripts/prompt-plan.mjs +447 -0
- package/skills/datarc-semantic-prompt/scripts/prompt.mjs +15 -0
- package/skills/datarc-semantic-synonym/SKILL.md +20 -0
- package/skills/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -0
- package/skills/datarc-semantic-synonym/scripts/synonym.mjs +11 -0
- package/skills/datarc-viz-analysis-report/SKILL.md +108 -0
- package/skills/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -0
- package/skills/datarc-viz-analysis-report/assets/echarts.min.js +45 -0
- package/skills/datarc-viz-analysis-report/references/api-contracts.md +163 -0
- package/skills/datarc-viz-analysis-report/references/derivation-log.md +157 -0
- package/skills/datarc-viz-analysis-report/references/evidence-framework.md +76 -0
- package/skills/datarc-viz-analysis-report/references/html-template.md +92 -0
- package/skills/datarc-viz-analysis-report/references/provenance-format.md +56 -0
- package/skills/datarc-viz-analysis-report/references/question-syntax.md +151 -0
- package/skills/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -0
- package/skills/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -0
- package/skills/datarc-workspace-configuration/SKILL.md +59 -0
- package/skills/datarc-workspace-configuration/references/artifacts.md +75 -0
- package/skills/datarc-workspace-configuration/references/verification.md +20 -0
- package/skills/datarc-workspace-configuration/scripts/configuration.mjs +662 -0
- package/skills/datarc-workspace-context/SKILL.md +43 -0
- package/skills/datarc-workspace-context/agents/openai.yaml +4 -0
- package/skills/datarc-workspace-context/scripts/datarc-client.mjs +31 -0
- package/skills/datarc-workspace-context/scripts/query-display.mjs +114 -0
- package/skills/datarc-workspace-context/scripts/workspace-context.mjs +58 -0
- package/skills/datarc-workspace-custom-tables/SKILL.md +89 -0
- package/skills/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -0
- package/skills/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -0
- package/skills/datarc-workspace-keywords/SKILL.md +197 -0
- package/skills/datarc-workspace-keywords/agents/openai.yaml +4 -0
- package/skills/datarc-workspace-keywords/references/api-fields.md +79 -0
- package/skills/datarc-workspace-keywords/references/derived-measures.md +177 -0
- package/skills/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -0
- package/skills/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -0
- package/skills/datarc-workspace-membership/SKILL.md +29 -0
- package/skills/datarc-workspace-membership/references/api.md +19 -0
- package/skills/datarc-workspace-modeling/SKILL.md +209 -0
- package/skills/datarc-workspace-modeling/agents/openai.yaml +4 -0
- package/skills/datarc-workspace-modeling/references/join-preview.md +38 -0
- package/skills/datarc-workspace-modeling/references/model-plan.md +300 -0
- package/skills/datarc-workspace-modeling/references/model-tags.md +95 -0
- package/skills/datarc-workspace-modeling/references/modeling-contract.json +29 -0
- package/skills/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -0
- package/skills/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -0
- package/skills/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -0
- package/skills/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -0
- package/skills-index.json +194 -0
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import {
|
|
5
|
+
createClient,
|
|
6
|
+
DatarcError,
|
|
7
|
+
decryptSql,
|
|
8
|
+
encryptSql,
|
|
9
|
+
parseArgs,
|
|
10
|
+
readJson,
|
|
11
|
+
required,
|
|
12
|
+
runCli,
|
|
13
|
+
} from "./datarc-client.mjs";
|
|
14
|
+
|
|
15
|
+
const HELP = `node scripts/workspace-custom-tables.mjs <command> --workspace-id <id>
|
|
16
|
+
list [--query text] [--offset 0] [--limit 50]
|
|
17
|
+
get|columns|preview|verify --table-uid <uid>
|
|
18
|
+
column-preview --table-uid <uid> --column <name>
|
|
19
|
+
validate --input <json-file|->
|
|
20
|
+
create --input <json-file|-> [--dry-run]
|
|
21
|
+
update|rename --table-uid <uid> --input <json-file|-> --dry-run
|
|
22
|
+
update|rename --table-uid <uid> --input <json-file|-> --if-match <fingerprint> [--allow-referenced]
|
|
23
|
+
Input: create {tableName,sql}; update/validate {sql}; rename {tableName}.
|
|
24
|
+
SQL is plain text in the input file. --max-wait-ms defaults to 300000;
|
|
25
|
+
--poll-interval-ms defaults to 2000. No automatic write retries or rollback.`;
|
|
26
|
+
|
|
27
|
+
const COMMANDS = {
|
|
28
|
+
list: { allowedArgs: ["query", "offset", "limit"] },
|
|
29
|
+
get: { allowedArgs: ["table-uid"] },
|
|
30
|
+
columns: { allowedArgs: ["table-uid"] },
|
|
31
|
+
preview: { allowedArgs: ["table-uid"] },
|
|
32
|
+
"column-preview": { allowedArgs: ["table-uid", "column"] },
|
|
33
|
+
verify: { allowedArgs: ["table-uid"] },
|
|
34
|
+
validate: { allowedArgs: ["input"], fields: ["sql"] },
|
|
35
|
+
create: {
|
|
36
|
+
method: "POST",
|
|
37
|
+
allowedArgs: ["input", "dry-run"],
|
|
38
|
+
fields: ["tableName", "sql"],
|
|
39
|
+
},
|
|
40
|
+
update: {
|
|
41
|
+
method: "PUT",
|
|
42
|
+
allowedArgs: [
|
|
43
|
+
"input",
|
|
44
|
+
"table-uid",
|
|
45
|
+
"dry-run",
|
|
46
|
+
"if-match",
|
|
47
|
+
"allow-referenced",
|
|
48
|
+
],
|
|
49
|
+
fields: ["sql"],
|
|
50
|
+
},
|
|
51
|
+
rename: {
|
|
52
|
+
method: "PATCH",
|
|
53
|
+
allowedArgs: [
|
|
54
|
+
"input",
|
|
55
|
+
"table-uid",
|
|
56
|
+
"dry-run",
|
|
57
|
+
"if-match",
|
|
58
|
+
"allow-referenced",
|
|
59
|
+
],
|
|
60
|
+
fields: ["tableName"],
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export async function execute(argv, options = {}) {
|
|
65
|
+
const args = parseArgs(argv);
|
|
66
|
+
if (args.help || args._[0] === "help") return { help: HELP };
|
|
67
|
+
const command = args._[0];
|
|
68
|
+
const config = Object.hasOwn(COMMANDS, command) ? COMMANDS[command] : null;
|
|
69
|
+
if (!config || args._.length !== 1)
|
|
70
|
+
throw new DatarcError(HELP, "INVALID_ARGUMENT");
|
|
71
|
+
const allowed = new Set([
|
|
72
|
+
"_",
|
|
73
|
+
"workspace-id",
|
|
74
|
+
"max-wait-ms",
|
|
75
|
+
"poll-interval-ms",
|
|
76
|
+
...config.allowedArgs,
|
|
77
|
+
]);
|
|
78
|
+
for (const [key, value] of Object.entries(args)) {
|
|
79
|
+
if (!allowed.has(key) || (key !== "_" && Array.isArray(value)))
|
|
80
|
+
throw new DatarcError(
|
|
81
|
+
`未知或重复参数 --${key};请运行 --help 核对参数并移除重复项`,
|
|
82
|
+
"INVALID_ARGUMENT",
|
|
83
|
+
);
|
|
84
|
+
if (["dry-run", "allow-referenced"].includes(key) && value !== true)
|
|
85
|
+
throw new DatarcError(
|
|
86
|
+
`--${key} 不接受参数值;请仅保留该开关,不附加值`,
|
|
87
|
+
"INVALID_ARGUMENT",
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
const workspaceId = integer(
|
|
91
|
+
required(args, "workspace-id"),
|
|
92
|
+
1,
|
|
93
|
+
Number.MAX_SAFE_INTEGER,
|
|
94
|
+
);
|
|
95
|
+
const now = options.now ?? Date.now;
|
|
96
|
+
const deadline = now() + integer(args["max-wait-ms"] ?? 300000, 1, 1800000);
|
|
97
|
+
const interval = integer(args["poll-interval-ms"] ?? 2000, 1, 30000);
|
|
98
|
+
const sleep =
|
|
99
|
+
options.sleep ??
|
|
100
|
+
((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
101
|
+
const client = options.client ?? createClient(options);
|
|
102
|
+
const prefix = `/api/v2/admin-projects/${workspaceId}`;
|
|
103
|
+
const collection = `${prefix}/custom-tables/`;
|
|
104
|
+
async function request(method, path, payload = {}) {
|
|
105
|
+
if (now() >= deadline)
|
|
106
|
+
throw new DatarcError(
|
|
107
|
+
"操作等待超时;请先读取当前配置和任务状态,确认后调整 --max-wait-ms,避免重复写入",
|
|
108
|
+
"TIMEOUT",
|
|
109
|
+
);
|
|
110
|
+
return client.request(method, path, {
|
|
111
|
+
...payload,
|
|
112
|
+
timeoutMs: Math.min(70000, deadline - now()),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
const workspace = await request("GET", `${prefix}/`);
|
|
116
|
+
if (workspace?.allowCustomTable !== true)
|
|
117
|
+
throw new DatarcError(
|
|
118
|
+
"工作区未配置支持自定义表的数据源,或当前服务未提供该能力;请确认数据源类型或联系工作区管理员后重试",
|
|
119
|
+
"CUSTOM_TABLE_UNSUPPORTED",
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
if (command === "list") {
|
|
123
|
+
const rows = await request("GET", collection, {
|
|
124
|
+
query: {
|
|
125
|
+
q: args.query === undefined ? "" : required(args, "query"),
|
|
126
|
+
offset: integer(args.offset ?? 0, 0, Number.MAX_SAFE_INTEGER),
|
|
127
|
+
limit: integer(args.limit ?? 50, 1, 1000),
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
assertArray(rows);
|
|
131
|
+
return rows.map(summary);
|
|
132
|
+
}
|
|
133
|
+
const uid = ["list", "validate", "create"].includes(command)
|
|
134
|
+
? null
|
|
135
|
+
: normalizeUid(required(args, "table-uid"));
|
|
136
|
+
const path = uid ? `${collection}${uid}/` : null;
|
|
137
|
+
const current = uid ? await request("GET", path) : null;
|
|
138
|
+
if (
|
|
139
|
+
uid &&
|
|
140
|
+
(current?.uid !== uid ||
|
|
141
|
+
typeof current.sql !== "string" ||
|
|
142
|
+
typeof current.tableName !== "string")
|
|
143
|
+
)
|
|
144
|
+
throw new DatarcError(
|
|
145
|
+
"自定义表回读身份或内容不匹配;请用 list 核对工作区和表 UID,再重新 get;持续异常请联系管理员",
|
|
146
|
+
"INVALID_RESPONSE",
|
|
147
|
+
);
|
|
148
|
+
const impact = uid ? references(workspace.settings, uid) : null;
|
|
149
|
+
if (command === "get")
|
|
150
|
+
return {
|
|
151
|
+
...summary(current),
|
|
152
|
+
editable: { tableName: current.tableName, sql: decryptSql(current.sql) },
|
|
153
|
+
impact,
|
|
154
|
+
fingerprint: fingerprint(current, impact),
|
|
155
|
+
};
|
|
156
|
+
if (command === "columns")
|
|
157
|
+
return {
|
|
158
|
+
columns: await request("GET", `${path}columns/`),
|
|
159
|
+
verified: false,
|
|
160
|
+
};
|
|
161
|
+
if (command === "preview") return request("GET", `${path}data/`);
|
|
162
|
+
if (command === "column-preview")
|
|
163
|
+
return request("GET", `${path}column-preview/`, {
|
|
164
|
+
query: { columnName: required(args, "column") },
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
async function trial(sql) {
|
|
168
|
+
const task = await request("POST", `${prefix}/async-tasks/`, {
|
|
169
|
+
body: { taskType: 10, params: { sql: encryptSql(sql) } },
|
|
170
|
+
});
|
|
171
|
+
const taskUid = normalizeUid(task?.uid);
|
|
172
|
+
const taskPath = `${prefix}/async-tasks/${taskUid}/`;
|
|
173
|
+
try {
|
|
174
|
+
while (now() < deadline) {
|
|
175
|
+
const detail = await request("GET", taskPath);
|
|
176
|
+
if (Number(detail.status) === 2) {
|
|
177
|
+
const result = detail.result;
|
|
178
|
+
if (
|
|
179
|
+
result?.status !== "SUCCESS" ||
|
|
180
|
+
!Array.isArray(result.data) ||
|
|
181
|
+
!result.data.length
|
|
182
|
+
)
|
|
183
|
+
throw new DatarcError(
|
|
184
|
+
"SQL 试跑未返回有效字段;请检查 SELECT 输出字段后重新 validate,持续异常请联系管理员检查任务结果",
|
|
185
|
+
"INVALID_TASK_RESULT",
|
|
186
|
+
);
|
|
187
|
+
const names = new Set();
|
|
188
|
+
for (const column of result.data) {
|
|
189
|
+
if (
|
|
190
|
+
!column?.name ||
|
|
191
|
+
column.type === undefined ||
|
|
192
|
+
!Array.isArray(column.data) ||
|
|
193
|
+
names.has(column.name)
|
|
194
|
+
)
|
|
195
|
+
throw new DatarcError(
|
|
196
|
+
"SQL 试跑字段无效或重名;请为输出字段设置唯一别名后重新 validate,持续异常请联系管理员检查字段类型",
|
|
197
|
+
"INVALID_TASK_RESULT",
|
|
198
|
+
);
|
|
199
|
+
names.add(column.name);
|
|
200
|
+
}
|
|
201
|
+
return result.data;
|
|
202
|
+
}
|
|
203
|
+
if (![0, 1].includes(Number(detail.status)))
|
|
204
|
+
throw new DatarcError(
|
|
205
|
+
`${client.redact(detail.result?.message || "SQL 试跑失败、取消或过期")};请检查 SQL、数据源连接及任务状态后重新 validate`,
|
|
206
|
+
"SQL_VALIDATION_FAILED",
|
|
207
|
+
{ taskUid, status: detail.status },
|
|
208
|
+
);
|
|
209
|
+
await sleep(Math.min(interval, Math.max(0, deadline - now())));
|
|
210
|
+
}
|
|
211
|
+
throw new DatarcError(
|
|
212
|
+
"SQL 试跑等待超时;请先检查任务状态和 SQL 性能,确认后台任务结束后调整 --max-wait-ms 并重新 validate",
|
|
213
|
+
"TIMEOUT",
|
|
214
|
+
);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
if (
|
|
217
|
+
!["SQL_VALIDATION_FAILED", "INVALID_TASK_RESULT"].includes(error.code)
|
|
218
|
+
) {
|
|
219
|
+
let cancelRequested = false;
|
|
220
|
+
try {
|
|
221
|
+
await client.request("DELETE", taskPath, { timeoutMs: 10000 });
|
|
222
|
+
cancelRequested = true;
|
|
223
|
+
} catch {
|
|
224
|
+
/* Preserve the original failure and disclose pending work. */
|
|
225
|
+
}
|
|
226
|
+
error.details = {
|
|
227
|
+
...error.details,
|
|
228
|
+
taskUid,
|
|
229
|
+
cancelRequested,
|
|
230
|
+
backgroundMayStillRun: true,
|
|
231
|
+
nextAction: cancelRequested
|
|
232
|
+
? "已请求取消,但后台可能仍在运行;请查询任务状态,确认结束后再重试"
|
|
233
|
+
: "取消请求未成功,后台可能仍在运行;请查询任务状态或联系管理员取消,确认结束后再重试",
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
throw error;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function verify(table, expectedColumns) {
|
|
241
|
+
const targetPath = `${collection}${table.uid}/`;
|
|
242
|
+
while (now() < deadline) {
|
|
243
|
+
const fresh = await request("GET", targetPath);
|
|
244
|
+
if (!unchanged(table, fresh))
|
|
245
|
+
throw new DatarcError(
|
|
246
|
+
"自定义表在验证期间已变化,请重新读取",
|
|
247
|
+
"CONCURRENT_CHANGE",
|
|
248
|
+
);
|
|
249
|
+
const columns = await request("GET", `${targetPath}columns/`);
|
|
250
|
+
if (columnsMatch(columns, expectedColumns)) {
|
|
251
|
+
const preview = await request("GET", `${targetPath}data/`);
|
|
252
|
+
if (
|
|
253
|
+
!Array.isArray(preview?.data) ||
|
|
254
|
+
!Array.isArray(preview.data[0]) ||
|
|
255
|
+
preview.data[0].length !== expectedColumns.length ||
|
|
256
|
+
preview.data[0].some(
|
|
257
|
+
(name, index) =>
|
|
258
|
+
unquoteColumn(name) !== expectedColumns[index].name,
|
|
259
|
+
) ||
|
|
260
|
+
preview.data.some(
|
|
261
|
+
(row) =>
|
|
262
|
+
!Array.isArray(row) || row.length !== expectedColumns.length,
|
|
263
|
+
)
|
|
264
|
+
)
|
|
265
|
+
throw new DatarcError(
|
|
266
|
+
"自定义表预览结构与 SQL 试跑不一致;请重新 get 核对 SQL 后运行 verify,持续异常请联系管理员检查字段解析",
|
|
267
|
+
"PREVIEW_MISMATCH",
|
|
268
|
+
);
|
|
269
|
+
const last = await request("GET", targetPath);
|
|
270
|
+
if (!unchanged(table, last))
|
|
271
|
+
throw new DatarcError(
|
|
272
|
+
"预览期间自定义表发生变化,请重新验证",
|
|
273
|
+
"CONCURRENT_CHANGE",
|
|
274
|
+
);
|
|
275
|
+
return {
|
|
276
|
+
...summary(last),
|
|
277
|
+
verified: true,
|
|
278
|
+
columns,
|
|
279
|
+
previewRowCount: preview.data.length - 1,
|
|
280
|
+
modelSource: { tableUid: table.uid },
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
await sleep(Math.min(interval, Math.max(0, deadline - now())));
|
|
284
|
+
}
|
|
285
|
+
throw new DatarcError(
|
|
286
|
+
"SQL 已保存,但字段尚未与试跑结果一致;后台解析可能仍在运行,请稍后 verify",
|
|
287
|
+
"SCHEMA_NOT_READY",
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (command === "verify")
|
|
292
|
+
return verify(current, await trial(decryptSql(current.sql)));
|
|
293
|
+
const input = validateInput(command, await readJson(required(args, "input")));
|
|
294
|
+
if (command === "validate")
|
|
295
|
+
return { validated: true, columns: await trial(input.sql) };
|
|
296
|
+
if (command === "create" || command === "rename") {
|
|
297
|
+
const matches = await request("GET", collection, {
|
|
298
|
+
query: { q: input.tableName, count: 1 },
|
|
299
|
+
});
|
|
300
|
+
if (!Number.isInteger(matches) || matches < 0)
|
|
301
|
+
throw new DatarcError(
|
|
302
|
+
"名称检查返回异常;请重新 list 核对目录,持续异常请联系管理员检查服务响应",
|
|
303
|
+
"INVALID_RESPONSE",
|
|
304
|
+
);
|
|
305
|
+
// Search is substring-based; page through every candidate before comparing exact names.
|
|
306
|
+
const seen = new Set();
|
|
307
|
+
for (let offset = 0; offset < matches; offset += 100) {
|
|
308
|
+
const rows = await request("GET", collection, {
|
|
309
|
+
query: { q: input.tableName, offset, limit: 100 },
|
|
310
|
+
});
|
|
311
|
+
assertArray(rows);
|
|
312
|
+
if (!rows.length)
|
|
313
|
+
throw new DatarcError(
|
|
314
|
+
"名称检查分页不完整,请重试读取",
|
|
315
|
+
"INCOMPLETE_CATALOG",
|
|
316
|
+
);
|
|
317
|
+
for (const row of rows) {
|
|
318
|
+
if (!row.uid || seen.has(row.uid))
|
|
319
|
+
throw new DatarcError(
|
|
320
|
+
"名称目录发生变化,请重新读取",
|
|
321
|
+
"INCOMPLETE_CATALOG",
|
|
322
|
+
);
|
|
323
|
+
seen.add(row.uid);
|
|
324
|
+
if (row.tableName === input.tableName && row.uid !== uid)
|
|
325
|
+
throw new DatarcError(
|
|
326
|
+
"已有同名自定义表,请选择修改已有表或另取名称",
|
|
327
|
+
"DUPLICATE_NAME",
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (seen.size !== matches)
|
|
332
|
+
throw new DatarcError(
|
|
333
|
+
"名称检查数量不一致;请重新 list 核对目录,再重新 dry-run",
|
|
334
|
+
"INCOMPLETE_CATALOG",
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
const baseline = current ? fingerprint(current, impact) : null;
|
|
338
|
+
const plan = {
|
|
339
|
+
command,
|
|
340
|
+
workspaceId,
|
|
341
|
+
tableUid: uid,
|
|
342
|
+
input,
|
|
343
|
+
impact,
|
|
344
|
+
...(baseline ? { fingerprint: baseline } : {}),
|
|
345
|
+
};
|
|
346
|
+
if (args["dry-run"]) return { ...plan, saved: false, sqlValidated: false };
|
|
347
|
+
if (current) {
|
|
348
|
+
if (required(args, "if-match") !== baseline)
|
|
349
|
+
throw new DatarcError(
|
|
350
|
+
"表配置或引用关系已变化,请重新 dry-run",
|
|
351
|
+
"CONCURRENT_CHANGE",
|
|
352
|
+
);
|
|
353
|
+
if (impact.length && !args["allow-referenced"])
|
|
354
|
+
throw new DatarcError(
|
|
355
|
+
"该表存在模型或维度表引用,请确认影响后使用 --allow-referenced",
|
|
356
|
+
"REFERENCED_TABLE",
|
|
357
|
+
{ impact },
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
const expected = command === "rename" ? null : await trial(input.sql);
|
|
361
|
+
if (current) {
|
|
362
|
+
const freshWorkspace = await request("GET", `${prefix}/`);
|
|
363
|
+
const fresh = await request("GET", path);
|
|
364
|
+
if (
|
|
365
|
+
freshWorkspace?.allowCustomTable !== true ||
|
|
366
|
+
fingerprint(fresh, references(freshWorkspace.settings, uid)) !== baseline
|
|
367
|
+
)
|
|
368
|
+
throw new DatarcError(
|
|
369
|
+
"SQL 试跑期间配置或引用已变化,请重新规划",
|
|
370
|
+
"CONCURRENT_CHANGE",
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
let saved;
|
|
374
|
+
try {
|
|
375
|
+
saved = await request(config.method, path ?? collection, {
|
|
376
|
+
body: {
|
|
377
|
+
...(input.tableName ? { tableName: input.tableName } : {}),
|
|
378
|
+
...(input.sql ? { sql: encryptSql(input.sql) } : {}),
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
} catch (error) {
|
|
382
|
+
error.details = {
|
|
383
|
+
...error.details,
|
|
384
|
+
writeOutcome: "unknown",
|
|
385
|
+
tableUid: uid,
|
|
386
|
+
tableName: input.tableName ?? current?.tableName,
|
|
387
|
+
nextAction: "先读取现状核对;不要直接重试写入",
|
|
388
|
+
};
|
|
389
|
+
throw error;
|
|
390
|
+
}
|
|
391
|
+
try {
|
|
392
|
+
normalizeUid(saved?.uid);
|
|
393
|
+
if (uid && saved.uid !== uid)
|
|
394
|
+
throw new DatarcError(
|
|
395
|
+
"保存返回了不同的表身份;请用 list 和 get 核对实际写入的表,联系管理员检查响应,不要直接重试写入",
|
|
396
|
+
"READBACK_MISMATCH",
|
|
397
|
+
);
|
|
398
|
+
const readback = await request("GET", `${collection}${saved.uid}/`);
|
|
399
|
+
if (
|
|
400
|
+
readback.uid !== saved.uid ||
|
|
401
|
+
readback.tableName !== (input.tableName ?? current.tableName) ||
|
|
402
|
+
normalizeSql(decryptSql(readback.sql)) !==
|
|
403
|
+
(input.sql ?? normalizeSql(decryptSql(current.sql)))
|
|
404
|
+
)
|
|
405
|
+
throw new DatarcError(
|
|
406
|
+
"保存后的表配置与本次请求不一致;请用 get 核对实际配置及并发修改,再重新规划,不要直接重试写入",
|
|
407
|
+
"READBACK_MISMATCH",
|
|
408
|
+
);
|
|
409
|
+
if (command === "rename")
|
|
410
|
+
return {
|
|
411
|
+
...summary(readback),
|
|
412
|
+
saved: true,
|
|
413
|
+
verified: false,
|
|
414
|
+
verification: "name-only",
|
|
415
|
+
impact,
|
|
416
|
+
};
|
|
417
|
+
return { ...(await verify(readback, expected)), saved: true, impact };
|
|
418
|
+
} catch (error) {
|
|
419
|
+
error.details = {
|
|
420
|
+
...error.details,
|
|
421
|
+
saved: true,
|
|
422
|
+
tableUid: saved?.uid ?? uid,
|
|
423
|
+
nextAction: "配置已写入;请读取并 verify,不要重复创建或自动回滚",
|
|
424
|
+
};
|
|
425
|
+
throw error;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
export function references(settings, uid) {
|
|
430
|
+
if (
|
|
431
|
+
!settings ||
|
|
432
|
+
!Array.isArray(settings.models) ||
|
|
433
|
+
!Array.isArray(settings.dimensionTables) ||
|
|
434
|
+
!Array.isArray(settings.drafts)
|
|
435
|
+
)
|
|
436
|
+
throw new DatarcError(
|
|
437
|
+
"工作区配置目录不完整,无法确认引用范围;请联系管理员检查工作区配置接口,恢复完整目录后重新 dry-run",
|
|
438
|
+
"INCOMPLETE_CATALOG",
|
|
439
|
+
);
|
|
440
|
+
const dimensions = settings.dimensionTables.filter(
|
|
441
|
+
(item) => item.tableUid === uid,
|
|
442
|
+
);
|
|
443
|
+
const ids = new Set(dimensions.map((item) => item.uid));
|
|
444
|
+
const result = dimensions.map((item) => ({
|
|
445
|
+
kind: "dimension-table",
|
|
446
|
+
uid: item.uid,
|
|
447
|
+
name: item.mark ?? "",
|
|
448
|
+
configHash: hash(item),
|
|
449
|
+
}));
|
|
450
|
+
for (const [kind, models] of [
|
|
451
|
+
["model", settings.models],
|
|
452
|
+
["draft", settings.drafts],
|
|
453
|
+
]) {
|
|
454
|
+
for (const model of models) {
|
|
455
|
+
if (usesTable(model, uid, ids))
|
|
456
|
+
result.push({
|
|
457
|
+
kind,
|
|
458
|
+
uid: model.uid,
|
|
459
|
+
name: model.name ?? "",
|
|
460
|
+
ready: model.ready === true,
|
|
461
|
+
disabled: model.disable === true,
|
|
462
|
+
configHash: hash(model),
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return result.sort((a, b) =>
|
|
467
|
+
`${a.kind}:${a.uid}`.localeCompare(`${b.kind}:${b.uid}`),
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export function fingerprint(table, impact) {
|
|
472
|
+
return createHash("sha256")
|
|
473
|
+
.update(
|
|
474
|
+
JSON.stringify({
|
|
475
|
+
uid: table.uid,
|
|
476
|
+
tableName: table.tableName,
|
|
477
|
+
sql: table.sql,
|
|
478
|
+
updatedAt: table.updatedAt,
|
|
479
|
+
impact,
|
|
480
|
+
}),
|
|
481
|
+
)
|
|
482
|
+
.digest("hex");
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function hash(value) {
|
|
486
|
+
return createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function usesTable(value, uid, dimensionUids) {
|
|
490
|
+
if (!value || typeof value !== "object") return false;
|
|
491
|
+
if (value.tableUid === uid || dimensionUids.has(value.uid)) return true;
|
|
492
|
+
return Object.values(value).some((item) =>
|
|
493
|
+
usesTable(item, uid, dimensionUids),
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function unchanged(table, fresh) {
|
|
498
|
+
return (
|
|
499
|
+
fresh?.uid === table.uid &&
|
|
500
|
+
fresh.sql === table.sql &&
|
|
501
|
+
fresh.tableName === table.tableName
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function columnsMatch(columns, expected) {
|
|
506
|
+
return (
|
|
507
|
+
Array.isArray(columns) &&
|
|
508
|
+
columns.length === expected.length &&
|
|
509
|
+
columns.every(
|
|
510
|
+
(column, index) =>
|
|
511
|
+
column.columnName === expected[index].name &&
|
|
512
|
+
String(column.columnType?.baseType) === String(expected[index].type),
|
|
513
|
+
)
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function unquoteColumn(value) {
|
|
518
|
+
const text = String(value);
|
|
519
|
+
return (text.startsWith('"') && text.endsWith('"')) ||
|
|
520
|
+
(text.startsWith("`") && text.endsWith("`")) ||
|
|
521
|
+
(text.startsWith("[") && text.endsWith("]"))
|
|
522
|
+
? text.slice(1, -1)
|
|
523
|
+
: text;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function validateInput(command, input) {
|
|
527
|
+
const fields = COMMANDS[command].fields;
|
|
528
|
+
if (
|
|
529
|
+
!input ||
|
|
530
|
+
Array.isArray(input) ||
|
|
531
|
+
typeof input !== "object" ||
|
|
532
|
+
Object.keys(input).some((key) => !fields.includes(key))
|
|
533
|
+
)
|
|
534
|
+
throw new DatarcError(
|
|
535
|
+
`输入只允许 ${fields.join("、")};请将输入改为仅包含这些字段的 JSON 对象`,
|
|
536
|
+
"INVALID_INPUT",
|
|
537
|
+
);
|
|
538
|
+
const result = {};
|
|
539
|
+
for (const key of fields) {
|
|
540
|
+
if (typeof input[key] !== "string" || !input[key].trim())
|
|
541
|
+
throw new DatarcError(
|
|
542
|
+
`${key} 必须为非空字符串;请补齐输入文件中的该字段`,
|
|
543
|
+
"INVALID_INPUT",
|
|
544
|
+
);
|
|
545
|
+
result[key] = key === "sql" ? normalizeSql(input[key]) : input[key].trim();
|
|
546
|
+
if (!result[key] || (key === "tableName" && result[key].length > 256))
|
|
547
|
+
throw new DatarcError(
|
|
548
|
+
`${key} 内容无效;请提供有效 SQL,表名需为 1–256 个字符`,
|
|
549
|
+
"INVALID_INPUT",
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
return result;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function normalizeSql(sql) {
|
|
556
|
+
return sql.trim().replace(/^[ ;]+|[ ;]+$/g, "");
|
|
557
|
+
}
|
|
558
|
+
function integer(value, min, max) {
|
|
559
|
+
if (
|
|
560
|
+
typeof value === "boolean" ||
|
|
561
|
+
!/^\d+$/.test(String(value)) ||
|
|
562
|
+
!Number.isSafeInteger(Number(value)) ||
|
|
563
|
+
Number(value) < min ||
|
|
564
|
+
Number(value) > max
|
|
565
|
+
)
|
|
566
|
+
throw new DatarcError(
|
|
567
|
+
`整数参数超出有效范围;请将参数改为 ${min}–${max} 范围内的整数`,
|
|
568
|
+
"INVALID_ARGUMENT",
|
|
569
|
+
);
|
|
570
|
+
return Number(value);
|
|
571
|
+
}
|
|
572
|
+
function normalizeUid(value) {
|
|
573
|
+
if (
|
|
574
|
+
typeof value !== "string" ||
|
|
575
|
+
!/^(?:[a-f0-9]{32}|[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$/i.test(
|
|
576
|
+
value,
|
|
577
|
+
)
|
|
578
|
+
)
|
|
579
|
+
throw new DatarcError(
|
|
580
|
+
"需要有效的表或任务 UUID;请从 list 或任务创建响应获取 32 位十六进制或标准带横线 UUID,持续异常请联系管理员",
|
|
581
|
+
"INVALID_ARGUMENT",
|
|
582
|
+
);
|
|
583
|
+
return value.replaceAll("-", "").toLowerCase();
|
|
584
|
+
}
|
|
585
|
+
function summary(table) {
|
|
586
|
+
return {
|
|
587
|
+
uid: table.uid,
|
|
588
|
+
tableName: table.tableName,
|
|
589
|
+
updatedAt: table.updatedAt,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
function assertArray(value) {
|
|
593
|
+
if (!Array.isArray(value))
|
|
594
|
+
throw new DatarcError(
|
|
595
|
+
"目录响应不是列表;请重新读取目录,持续异常请联系管理员检查服务响应",
|
|
596
|
+
"INVALID_RESPONSE",
|
|
597
|
+
);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
|
|
601
|
+
await runCli(() => execute(process.argv.slice(2)));
|