@stalfh233/omc-cli 0.8.32 → 0.8.34
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/dist/arg-vocabulary.js +223 -0
- package/dist/artifact-cmd.js +443 -0
- package/dist/batch-call.js +56 -3
- package/dist/call-command.js +70 -0
- package/dist/call-execute.js +228 -0
- package/dist/call-input.js +557 -0
- package/dist/call-output.js +240 -0
- package/dist/cli.js +13 -1359
- package/dist/command-transport.js +4 -1
- package/dist/form-support.js +14 -3
- package/dist/intent/plan-apply.js +14 -24
- package/dist/lanes/datarule.js +21 -0
- package/dist/lanes/field-contract.js +1 -1
- package/dist/lanes/field.js +128 -1
- package/dist/lanes/list-view-shapes.js +8 -1
- package/dist/lanes/list-view.js +64 -2
- package/dist/lanes/runtime-data/aggregate-support.js +209 -0
- package/dist/lanes/runtime-data/aggregate.js +679 -0
- package/dist/lanes/runtime-data/field-options.js +52 -0
- package/dist/lanes/runtime-data/lane.js +4 -0
- package/dist/lanes/runtime-data/record-delete.js +419 -0
- package/dist/lanes/runtime-data/record-read.js +984 -0
- package/dist/lanes/runtime-data/record-write.js +562 -0
- package/dist/lanes/runtime-data/registration.js +22 -0
- package/dist/lanes/runtime-data/shapes.js +594 -0
- package/dist/lanes/runtime-data/submission.js +1304 -0
- package/dist/lanes/runtime-data/write-support.js +875 -0
- package/dist/lanes/runtime-data.js +10 -5590
- package/dist/lanes/workflow/approval-ops.js +983 -0
- package/dist/lanes/workflow/instance-read.js +284 -0
- package/dist/lanes/workflow/lifecycle-write.js +1499 -0
- package/dist/lanes/workflow/permissions.js +571 -0
- package/dist/lanes/workflow/recovery.js +361 -0
- package/dist/lanes/workflow/registration.js +14 -0
- package/dist/lanes/workflow/shapes.js +811 -0
- package/dist/lanes/workflow/template-fetch.js +197 -0
- package/dist/lanes/workflow/template-merge.js +1351 -0
- package/dist/lanes/workflow/template-read.js +327 -0
- package/dist/lanes/workflow.js +11 -6312
- package/dist/model-scaffold.js +6 -2
- package/dist/plan.js +23 -1
- package/dist/read-projection.js +442 -0
- package/dist/read.js +40 -335
- package/dist/rollback.js +17 -23
- package/dist/runtime-support.js +1 -1
- package/dist/verification.js +39 -0
- package/dist/write-coordinator.js +152 -0
- package/dist/write-outcome.js +295 -0
- package/dist/write.js +34 -115
- package/package.json +3 -3
- package/skills/omc-refusals.md +1 -1
- package/skills/omc-start-here.md +2 -2
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
import { ToolInputError, defineTool } from "../../tool-types.js";
|
|
2
|
+
import { RUNTIME_LANE, RUNTIME_TOOLS } from "./lane.js";
|
|
3
|
+
import { capabilityArg, environmentArg } from "../../args.js";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { FilterDslError, injectPropertyTypes, toQueryCondition } from "../../data-list-filter.js";
|
|
6
|
+
import { AGGREGATE_CAP, AGGREGATE_FIELD_SAMPLE_LIMIT, AGGREGATE_PAGE_SIZE, aggregateDraftFilter, aggregateLeafGroup, aggregateNumeric, aggregateValue, childPageOf, localScanMode, probeGetListReadsDatabase, tryServerSum, withAggregateLeaf } from "./aggregate-support.js";
|
|
7
|
+
import { governedRead } from "../../read.js";
|
|
8
|
+
import { WireError, wireCall, wireRows } from "../../wire.js";
|
|
9
|
+
import { ENDPOINTS } from "../../endpoints.js";
|
|
10
|
+
import { choiceCodesFromFieldOptions, parseOptions } from "./field-options.js";
|
|
11
|
+
import { closestMatch } from "../../closest-match.js";
|
|
12
|
+
import { familyName } from "../../field-families.js";
|
|
13
|
+
import { extractBizObjectPage } from "./shapes.js";
|
|
14
|
+
import { scanPages } from "../runtime-paging.js";
|
|
15
|
+
import { DEFAULT_SCAN_CONCURRENCY, isThrottleClassError, mapWithConcurrency } from "../../concurrency.js";
|
|
16
|
+
export function registerDataAggregate(tools) {
|
|
17
|
+
tools.push(defineTool({
|
|
18
|
+
name: "data.aggregate",
|
|
19
|
+
search: { intents: ["分组计数", "聚合计数", "计数汇总", "按状态分组", "值域分布", "分组分布", "每类几个", "分桶计数", "求和", "合计", "总额", "平均值", "最大值", "最小值"] },
|
|
20
|
+
layer: "low",
|
|
21
|
+
kind: "read",
|
|
22
|
+
lane: RUNTIME_LANE,
|
|
23
|
+
scope: RUNTIME_TOOLS["data.aggregate"].scope,
|
|
24
|
+
description: RUNTIME_TOOLS["data.aggregate"].description,
|
|
25
|
+
inputSchema: {
|
|
26
|
+
environment: environmentArg,
|
|
27
|
+
schemaCode: z.string().min(1),
|
|
28
|
+
// 三个过滤参数与 data.list 同语义(DSL 翻译与 propertyType 注入复用同一套件)。
|
|
29
|
+
filter: z.union([z.record(z.string(), z.unknown()), z.array(z.record(z.string(), z.unknown()))]).optional().describe('单子句 {op,field,value?}(或子句数组=AND);op 同 data.list(eq/neq/gt/lt/gte/lte/like/in/notin/isnull/isnotnull 等)'),
|
|
30
|
+
filters: z.array(z.record(z.string(), z.unknown())).optional().describe("子句数组=AND"),
|
|
31
|
+
filterGroup: z.record(z.string(), z.unknown()).optional().describe('嵌套组 {op:"and"|"or",clauses:[…]}'),
|
|
32
|
+
child: z.object({
|
|
33
|
+
childSchemaCode: z.string().min(1).describe("子表编码(模型里 CHILD_TABLE 字段的 code)"),
|
|
34
|
+
bizObjectId: z.string().min(1).describe("主表记录 id(data.list 的 row.bizObjectId / data.submit 的 objectId)"),
|
|
35
|
+
pageSize: z.number().int().min(1).max(500).default(500).optional().describe("数值指标翻页的每页行数(默认 500;list_child 必须显式传 size,空/0 平台会当 Integer.MAX_VALUE)"),
|
|
36
|
+
maxPages: z.number().int().min(1).max(200).default(40).optional().describe("数值指标翻页上限(默认 40 页;打满仍未取空 → complete:false+truncated)"),
|
|
37
|
+
}).describe("子表目标:count=服务端 totalElements 精确计数(零行内联);sum/avg/min/max=分页拉目标列后本地折叠").optional(),
|
|
38
|
+
metric: z.enum(["count", "sum", "avg", "min", "max"]).describe("count=服务端 totalElements 精确计数(不读行,不需要 field);sum/avg/min/max=数值列本地折叠(需 field,翻页 cap 1000)。null 行不进聚合,avg 分母=已扫非 null 行数"),
|
|
39
|
+
field: z.string().min(1).optional().describe("metric≠count 时必填:已发布且 propertyType=2(NUMERICAL)的字段 code。未发布→aggregate-field-unresolved(带 publishedFields/didYouMean);非数值→aggregate-field-not-numerical(计数请用 metric:count)"),
|
|
40
|
+
groupBy: z.string().min(1).optional().describe("单列分组:count 值域可枚举走「值域×count」,不可枚举降级本地扫描;数值指标一律本地双列扫描(cap 1000,截断时 complete:false+capNotice)"),
|
|
41
|
+
topN: z.number().int().min(1).max(20).default(10).describe("分组桶上限,默认 10、上限 20(count 按 count 降序;数值指标按 value 降序、并列按 key)"),
|
|
42
|
+
includeDrafts: z.boolean().optional().describe("count+groupBy 默认 false(注入 sequenceStatus≠DRAFT 统一双口径);数值指标不默认排除(反映平台返回行);count 省略=平台默认(无条件含草稿、带条件排草稿);显式 false 可强制排除——口径看 filter.draftPolicy"),
|
|
43
|
+
capability: capabilityArg,
|
|
44
|
+
},
|
|
45
|
+
handler: async (ctx, args, self) => {
|
|
46
|
+
const metric = args.metric;
|
|
47
|
+
const numeric = metric !== "count";
|
|
48
|
+
const child = args.child;
|
|
49
|
+
// 子表面 fail-closed(零 wire):分组与子表是两条独立能力(子表分组另案),
|
|
50
|
+
// 而 list_child 的 filters 是扁平 List<FilterModel>,平台在没有 queryVersion
|
|
51
|
+
// 时把它们全部 AND 起来(getFiltersByQueryParam:6584-6590)——filterGroup 的
|
|
52
|
+
// OR 语义无处表达,宁可拒绝也不静默降级成 AND。
|
|
53
|
+
if (child !== undefined && args.groupBy !== undefined)
|
|
54
|
+
throw new ToolInputError({
|
|
55
|
+
status: "error",
|
|
56
|
+
blocker: "child-groupby-unsupported",
|
|
57
|
+
tool: "data.aggregate",
|
|
58
|
+
message: "child 与 groupBy 互斥:data.aggregate 的子表目标只做 count/sum/avg/min/max,不做分组(子表分组另案)。要去掉 child 做主子表分组,或去掉 groupBy 只算子表整体。",
|
|
59
|
+
});
|
|
60
|
+
if (child !== undefined && args.filterGroup !== undefined)
|
|
61
|
+
throw new ToolInputError({
|
|
62
|
+
status: "error",
|
|
63
|
+
blocker: "child-filtergroup-unsupported",
|
|
64
|
+
tool: "data.aggregate",
|
|
65
|
+
message: "child 暂不支持 filterGroup:list_child 的 filters 是扁平 List<FilterModel>,平台只在没有 queryVersion 时把它们全部 AND(getFiltersByQueryParam:6584-6590),OR 组没有 wire 形状。只支持 AND:用 filter 或 filters(多子句=AND)。",
|
|
66
|
+
});
|
|
67
|
+
// 子表目标的元数据(field.list / 过滤字段 propertyType 注入)取子表 schema;
|
|
68
|
+
// 回执的 schemaCode 仍是主表。
|
|
69
|
+
const metaSchemaCode = child?.childSchemaCode ?? args.schemaCode;
|
|
70
|
+
// 参数面 fail-closed(零 wire、零写入):metric 与 field 的组合只有两种合法形状。
|
|
71
|
+
if (metric === "count" && args.field !== undefined)
|
|
72
|
+
throw new ToolInputError({
|
|
73
|
+
status: "error",
|
|
74
|
+
blocker: "invalid-input",
|
|
75
|
+
tool: "data.aggregate",
|
|
76
|
+
message: "metric=count 只回 total(服务端 totalElements 精确计数),不接受 field——count 的草稿口径已在 filter.draftPolicy 标注。要按数值列聚合请用 metric:sum/avg/min/max + field",
|
|
77
|
+
});
|
|
78
|
+
if (numeric && (args.field === undefined || args.field.trim() === ""))
|
|
79
|
+
throw new ToolInputError({
|
|
80
|
+
status: "error",
|
|
81
|
+
blocker: "invalid-input",
|
|
82
|
+
tool: "data.aggregate",
|
|
83
|
+
message: `metric=${metric} 需要 field(已发布且 propertyType=2/NUMERICAL 的字段 code)——不给 field 无法知道聚合哪一列`,
|
|
84
|
+
nextCommand: `omc call field.list --env <env> --args '{"schemaCode":"${metaSchemaCode}","published":true}'`,
|
|
85
|
+
});
|
|
86
|
+
// count+groupBy 要统一 base 与各桶的口径,省略时默认排除草稿;count 省略时保留
|
|
87
|
+
// 平台默认(无条件含草稿),只有显式 false 才注入排除条件。数值指标(本地折叠)
|
|
88
|
+
// 不做默认注入——结果如实反映平台返回行,要排除须显式 false。
|
|
89
|
+
// 子表没有独立的流程状态(sequenceStatus 属主表),**绝不**给 list_child 注入
|
|
90
|
+
// 草稿条件——否则会静默过滤掉本应计入的子行。
|
|
91
|
+
const excludedByRequest = child === undefined && (args.includeDrafts === false || (args.includeDrafts === undefined && args.groupBy !== undefined && !numeric));
|
|
92
|
+
const includeDrafts = args.includeDrafts === true;
|
|
93
|
+
// 用户条件与草稿注入分开构造:filterGroup 会把 filters 覆盖掉(toQueryCondition
|
|
94
|
+
// 的既有语义),所以草稿条件统一按「level1 AND 追加一组」合并,任何 DSL 形状都不丢。
|
|
95
|
+
let userCondition;
|
|
96
|
+
try {
|
|
97
|
+
const asFilters = args.filters ?? (Array.isArray(args.filter) ? args.filter : undefined);
|
|
98
|
+
const asFilter = Array.isArray(args.filter) ? undefined : args.filter;
|
|
99
|
+
userCondition = toQueryCondition({
|
|
100
|
+
...(asFilter !== undefined ? { filter: asFilter } : {}),
|
|
101
|
+
...(asFilters !== undefined && asFilters.length > 0 ? { filters: asFilters } : {}),
|
|
102
|
+
...(args.filterGroup !== undefined ? { filterGroup: args.filterGroup } : {}),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (error instanceof FilterDslError)
|
|
107
|
+
return {
|
|
108
|
+
status: "refused",
|
|
109
|
+
blocker: "invalid-filter-DSL",
|
|
110
|
+
reason: error.code,
|
|
111
|
+
message: `${error.message};filter 最小形状 {"op":"eq","field":"<字段code>","value":"<值>"};多条件 filters:[…](AND)或 filterGroup:{op,clauses:[…]}`,
|
|
112
|
+
minimalValidFilter: { filter: { op: "eq", field: "<fieldCode>", value: "<value>" } },
|
|
113
|
+
};
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
const hasUserCondition = userCondition !== undefined;
|
|
117
|
+
let condition = excludedByRequest ? withAggregateLeaf(userCondition, aggregateLeafGroup("sequenceStatus", 0, "NotEq", "DRAFT")) : userCondition;
|
|
118
|
+
return governedRead(ctx, self, args, async (session) => {
|
|
119
|
+
const draftFilter = aggregateDraftFilter(excludedByRequest, includeDrafts, hasUserCondition, numeric);
|
|
120
|
+
let metaCalls = 0;
|
|
121
|
+
let published;
|
|
122
|
+
// field.list 至多一跳:同时服务 filter 的 propertyType 注入、groupBy 值域解析
|
|
123
|
+
// 与数值字段的类型校验(数值指标至少 1 次;有 filter 时同一跳兼注入)。
|
|
124
|
+
const fieldMeta = async () => {
|
|
125
|
+
if (published !== undefined)
|
|
126
|
+
return published;
|
|
127
|
+
metaCalls += 1;
|
|
128
|
+
try {
|
|
129
|
+
const rows = wireRows((await wireCall(session, { path: ENDPOINTS.fieldList, query: { schemaCode: metaSchemaCode, isPublish: "true" } })).data);
|
|
130
|
+
const map = new Map();
|
|
131
|
+
for (const row of rows) {
|
|
132
|
+
const code = String(row.code ?? row.propertyCode ?? "");
|
|
133
|
+
if (!code)
|
|
134
|
+
continue;
|
|
135
|
+
const rawName = row.name ?? row.propertyName;
|
|
136
|
+
const name = typeof rawName === "string" && rawName.length > 0 && rawName !== code ? rawName : undefined;
|
|
137
|
+
map.set(code, {
|
|
138
|
+
type: Number(row.propertyType ?? row.type),
|
|
139
|
+
choiceCodes: choiceCodesFromFieldOptions(row, parseOptions(row.options)),
|
|
140
|
+
...(name !== undefined ? { name } : {}),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
published = map;
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
published = null;
|
|
147
|
+
}
|
|
148
|
+
return published;
|
|
149
|
+
};
|
|
150
|
+
const fieldListNext = `omc call field.list --env <env> --args '{"schemaCode":"${metaSchemaCode}","published":true}'`;
|
|
151
|
+
// 数值指标:field 必须已发布且 NUMERICAL(2)——fail closed,不做类型猜测。
|
|
152
|
+
if (numeric) {
|
|
153
|
+
const target = args.field;
|
|
154
|
+
const meta = await fieldMeta();
|
|
155
|
+
if (meta === null)
|
|
156
|
+
return {
|
|
157
|
+
status: "refused",
|
|
158
|
+
blocker: "aggregate-field-unresolved",
|
|
159
|
+
reason: "metadata-unavailable",
|
|
160
|
+
schemaCode: args.schemaCode,
|
|
161
|
+
...(child !== undefined ? { childSchemaCode: child.childSchemaCode } : {}),
|
|
162
|
+
field: target,
|
|
163
|
+
message: `读不到已发布字段元数据(field.list),无法确认 ${target} 是 NUMERICAL(2)——数值聚合不做类型猜测`,
|
|
164
|
+
nextCommand: fieldListNext,
|
|
165
|
+
};
|
|
166
|
+
const targetMeta = meta.get(target);
|
|
167
|
+
if (targetMeta === undefined) {
|
|
168
|
+
const catalog = [...meta].map(([code, info]) => ({ code, ...(info.name !== undefined ? { name: info.name } : {}), type: info.type }));
|
|
169
|
+
const sample = catalog.slice(0, AGGREGATE_FIELD_SAMPLE_LIMIT);
|
|
170
|
+
const numericCodes = catalog.filter((row) => row.type === 2).map((row) => row.code);
|
|
171
|
+
// 名字最像但类型不对时改推 NUMERICAL 字段(同 data.list 的数值算子判例)。
|
|
172
|
+
const suggestion = closestMatch(target, catalog.map((row) => row.code));
|
|
173
|
+
const numericSuggestion = suggestion !== undefined && meta.get(suggestion)?.type !== 2
|
|
174
|
+
? numericCodes.find((code) => closestMatch(target, [code]) === code)
|
|
175
|
+
: undefined;
|
|
176
|
+
const didYouMean = numericSuggestion ?? suggestion;
|
|
177
|
+
return {
|
|
178
|
+
status: "refused",
|
|
179
|
+
blocker: "aggregate-field-unresolved",
|
|
180
|
+
schemaCode: args.schemaCode,
|
|
181
|
+
...(child !== undefined ? { childSchemaCode: child.childSchemaCode } : {}),
|
|
182
|
+
field: target,
|
|
183
|
+
publishedFields: sample,
|
|
184
|
+
publishedFieldsTotal: catalog.length,
|
|
185
|
+
...(didYouMean !== undefined ? { didYouMean: { [target]: didYouMean } } : {}),
|
|
186
|
+
message: `field 不在已发布元数据里:${target}(数值聚合要用已发布且 propertyType=2 的字段 code,不做猜测)` +
|
|
187
|
+
(didYouMean !== undefined ? `;是否想写 ${didYouMean}` : "") +
|
|
188
|
+
(catalog.length === 0 ? ";该模型当前没有已发布字段" : `;已发布字段共 ${catalog.length} 个,code/名称见 publishedFields(仅列前 ${sample.length} 个,其余用 field.list 查)`),
|
|
189
|
+
nextCommand: fieldListNext,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
if (targetMeta.type !== 2) {
|
|
193
|
+
const numericFields = [...meta].filter(([, info]) => info.type === 2).map(([code]) => code);
|
|
194
|
+
return {
|
|
195
|
+
status: "refused",
|
|
196
|
+
blocker: "aggregate-field-not-numerical",
|
|
197
|
+
schemaCode: args.schemaCode,
|
|
198
|
+
...(child !== undefined ? { childSchemaCode: child.childSchemaCode } : {}),
|
|
199
|
+
field: target,
|
|
200
|
+
propertyType: targetMeta.type,
|
|
201
|
+
...(numericFields.length > 0 ? { numericFields: numericFields.slice(0, AGGREGATE_FIELD_SAMPLE_LIMIT) } : {}),
|
|
202
|
+
message: `字段 ${target} 是 ${familyName(targetMeta.type)}(propertyType=${targetMeta.type}),不是 NUMERICAL(2)——sum/avg/min/max 只对数值列有意义;该字段是文本/选项,要条数用 metric:count(只回 total,不需要 field)`,
|
|
203
|
+
...(numericFields.length > 0
|
|
204
|
+
? { nextCommand: `omc call data.aggregate --env <env> --args '${JSON.stringify({ schemaCode: args.schemaCode, metric, field: numericFields[0] })}'(同模型第一个 NUMERICAL 字段)` }
|
|
205
|
+
: {}),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// Only a USER DSL filter needs published metadata; the injected draft
|
|
210
|
+
// leaf (sequenceStatus) has a platform-fixed propertyType.
|
|
211
|
+
if (hasUserCondition) {
|
|
212
|
+
const meta = await fieldMeta();
|
|
213
|
+
const injected = injectPropertyTypes(condition, (code) => {
|
|
214
|
+
const type = meta?.get(code)?.type;
|
|
215
|
+
return type !== undefined && Number.isInteger(type) ? type : undefined;
|
|
216
|
+
});
|
|
217
|
+
if (!injected.ok)
|
|
218
|
+
return {
|
|
219
|
+
status: "refused",
|
|
220
|
+
blocker: "unknown-filter-field",
|
|
221
|
+
schemaCode: args.schemaCode,
|
|
222
|
+
...(child !== undefined ? { childSchemaCode: child.childSchemaCode } : {}),
|
|
223
|
+
missing: injected.missing,
|
|
224
|
+
message: `filter 字段不在已发布元数据里:${injected.missing.join("、")}(聚合复用 data.list 的 propertyType 注入,不做猜测)`,
|
|
225
|
+
nextCommand: fieldListNext,
|
|
226
|
+
};
|
|
227
|
+
condition = injected.condition;
|
|
228
|
+
}
|
|
229
|
+
// ---- 子表目标(child)------------------------------------------------
|
|
230
|
+
// count:1 次 list_child(page:0,size:1),只取服务端 totalElements;
|
|
231
|
+
// sum/avg/min/max:columns 列投影 + 分页,本地折叠。子表没有服务端 sum
|
|
232
|
+
// (/runtime/query/sum 只认主 schema 的属性表,QueryRuntimeController.java:589-640)。
|
|
233
|
+
if (child !== undefined) {
|
|
234
|
+
const childDraftFilter = {
|
|
235
|
+
draftPolicy: "not-applicable",
|
|
236
|
+
hint: "子表没有独立的流程状态(sequenceStatus 属主表),未注入草稿条件;includeDrafts 对 child 无效",
|
|
237
|
+
};
|
|
238
|
+
// filters 是扁平 List<FilterModel>(FilterModel.java:45-55);filter/filters
|
|
239
|
+
// 的 DSL 输出是单个 AND 组,展平即平台语义——queryVersion 不传时平台把
|
|
240
|
+
// filters 全部 AND(BizObjectFacadeImpl.getFiltersByQueryParam:6584-6590)。
|
|
241
|
+
const childFilters = condition === undefined
|
|
242
|
+
? undefined
|
|
243
|
+
: condition.flat(2);
|
|
244
|
+
const listChildBody = (page, size, columns) => ({
|
|
245
|
+
schemaCode: args.schemaCode,
|
|
246
|
+
childSchemaCode: child.childSchemaCode,
|
|
247
|
+
bizObjectId: child.bizObjectId,
|
|
248
|
+
page,
|
|
249
|
+
// QueryDataVO.getSize() 在 size 空/0 时返回 Integer.MAX_VALUE
|
|
250
|
+
// (QueryDataVO.java:186-191)——size 必须显式传,否则退化成全量。
|
|
251
|
+
size,
|
|
252
|
+
...(childFilters !== undefined && childFilters.length > 0 ? { filters: childFilters } : {}),
|
|
253
|
+
...(columns !== undefined ? { columns: columns.map((propertyCode) => ({ propertyCode })) } : {}),
|
|
254
|
+
});
|
|
255
|
+
// 平台错误原样透出(message/errcode 不动),只在 hint 里补上
|
|
256
|
+
// childSchemaCode/bizObjectId,便于把「主记录不存在/子表不存在」对回目标。
|
|
257
|
+
const listChildCall = async (body) => {
|
|
258
|
+
try {
|
|
259
|
+
return await wireCall(session, { path: ENDPOINTS.runtimeFormListChild, method: "POST", body });
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
if (error instanceof WireError)
|
|
263
|
+
throw new WireError(error.kind, error.message, error.errcode, error.diagnostic, `childSchemaCode=${child.childSchemaCode}; bizObjectId=${child.bizObjectId}${error.hint !== undefined ? `;${error.hint}` : ""}`);
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
if (!numeric) {
|
|
268
|
+
const page = childPageOf((await listChildCall(listChildBody(0, 1))).data);
|
|
269
|
+
return {
|
|
270
|
+
schemaCode: args.schemaCode,
|
|
271
|
+
metric: "count",
|
|
272
|
+
value: page.total,
|
|
273
|
+
mode: "server-child",
|
|
274
|
+
rows: 0,
|
|
275
|
+
child: { childSchemaCode: child.childSchemaCode, bizObjectId: child.bizObjectId },
|
|
276
|
+
...(page.total === null ? { note: "list_child 未回 totalElements(PageVO 形状漂移)" } : {}),
|
|
277
|
+
filter: childDraftFilter,
|
|
278
|
+
wire: { calls: metaCalls + 1, plan: metaCalls > 0 ? "1 metadata + 1 list_child" : "1 list_child" },
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
const target = args.field;
|
|
282
|
+
const pageSize = child.pageSize ?? 500;
|
|
283
|
+
const maxPages = child.maxPages ?? 40;
|
|
284
|
+
const numericMetric = metric;
|
|
285
|
+
let count = 0;
|
|
286
|
+
let nonNullCount = 0;
|
|
287
|
+
let sum = 0;
|
|
288
|
+
let min = Number.POSITIVE_INFINITY;
|
|
289
|
+
let max = Number.NEGATIVE_INFINITY;
|
|
290
|
+
let pages = 0;
|
|
291
|
+
let truncated = false;
|
|
292
|
+
let readFailure;
|
|
293
|
+
for (let pageIndex = 0; pageIndex < maxPages; pageIndex += 1) {
|
|
294
|
+
let pageData;
|
|
295
|
+
try {
|
|
296
|
+
pageData = childPageOf((await listChildCall(listChildBody(pageIndex, pageSize, [target]))).data);
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
// 任何一页读取失败 → partial(complete:false),如实报已扫页数,
|
|
300
|
+
// 不把部分和当全量、也不外推。
|
|
301
|
+
readFailure = {
|
|
302
|
+
page: pageIndex,
|
|
303
|
+
...(error instanceof WireError && error.errcode !== undefined ? { code: error.errcode } : {}),
|
|
304
|
+
message: error instanceof Error ? error.message : String(error),
|
|
305
|
+
};
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
pages += 1;
|
|
309
|
+
for (const row of pageData.rows) {
|
|
310
|
+
count += 1;
|
|
311
|
+
const value = aggregateNumeric(row[target]);
|
|
312
|
+
if (value === null)
|
|
313
|
+
continue;
|
|
314
|
+
nonNullCount += 1;
|
|
315
|
+
sum += value;
|
|
316
|
+
if (value < min)
|
|
317
|
+
min = value;
|
|
318
|
+
if (value > max)
|
|
319
|
+
max = value;
|
|
320
|
+
}
|
|
321
|
+
// 不足一页 = 已取尽;正好一页且已到 maxPages = 无法证明取尽(可能还有)。
|
|
322
|
+
if (pageData.rows.length < pageSize)
|
|
323
|
+
break;
|
|
324
|
+
if (pageIndex === maxPages - 1)
|
|
325
|
+
truncated = true;
|
|
326
|
+
}
|
|
327
|
+
const complete = readFailure === undefined && !truncated;
|
|
328
|
+
const value = aggregateValue(numericMetric, sum, nonNullCount, min, max);
|
|
329
|
+
const capNotice = readFailure !== undefined
|
|
330
|
+
? `子表数值折叠:第 ${readFailure.page} 页(0 起)读取失败(${readFailure.code !== undefined ? `errcode ${readFailure.code} ` : ""}${readFailure.message}),只覆盖已扫 ${pages} 页 / ${count} 行(complete:false,非全集);已扫部分的值不是全量。`
|
|
331
|
+
: `子表数值折叠:翻到 maxPages=${maxPages} 上限仍未取空(每页 ${pageSize} 行),只覆盖已扫 ${pages} 页 / ${count} 行(complete:false,非全集)。要全集:调大 maxPages/pageSize,或用 filter 按业务键分片重跑。`;
|
|
332
|
+
return {
|
|
333
|
+
schemaCode: args.schemaCode,
|
|
334
|
+
metric,
|
|
335
|
+
field: target,
|
|
336
|
+
value,
|
|
337
|
+
rows: count,
|
|
338
|
+
nullCount: count - nonNullCount,
|
|
339
|
+
pages,
|
|
340
|
+
mode: "local-scan-child",
|
|
341
|
+
coverage: { complete, pageSize, maxPages, truncated },
|
|
342
|
+
child: { childSchemaCode: child.childSchemaCode, bizObjectId: child.bizObjectId },
|
|
343
|
+
...(value === null ? { note: "no numeric values in scanned child rows" } : {}),
|
|
344
|
+
filter: childDraftFilter,
|
|
345
|
+
...(complete ? {} : { capNotice }),
|
|
346
|
+
...(readFailure !== undefined ? { partial: true, readFailure } : {}),
|
|
347
|
+
wire: {
|
|
348
|
+
calls: metaCalls + pages + (readFailure !== undefined ? 1 : 0),
|
|
349
|
+
plan: `${metaCalls} metadata + ${pages} list_child pages${readFailure !== undefined ? " + 1 failed" : ""}`,
|
|
350
|
+
},
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
const queryBody = (size, page) => ({
|
|
354
|
+
schemaCode: args.schemaCode,
|
|
355
|
+
queryCode: args.schemaCode,
|
|
356
|
+
filters: [],
|
|
357
|
+
mobile: false,
|
|
358
|
+
page,
|
|
359
|
+
size,
|
|
360
|
+
queryVersion: 1,
|
|
361
|
+
showTotal: true,
|
|
362
|
+
...(condition !== undefined ? { queryCondition: condition } : {}),
|
|
363
|
+
});
|
|
364
|
+
const countAt = async (extraLeaf) => {
|
|
365
|
+
const body = queryBody(1, 0);
|
|
366
|
+
if (extraLeaf !== undefined)
|
|
367
|
+
body.queryCondition = withAggregateLeaf(condition, extraLeaf);
|
|
368
|
+
const response = await wireCall(session, { path: ENDPOINTS.runtimeQueryList, method: "POST", body });
|
|
369
|
+
return extractBizObjectPage(response.data).total;
|
|
370
|
+
};
|
|
371
|
+
const topN = args.topN ?? 10;
|
|
372
|
+
// 数值指标(sum/avg/min/max):平台无 group-by 面(数值有 /sum,受 GET_LIST 规则限制)→ 列投影 + size:200 翻页
|
|
373
|
+
// 本地折叠至 cap 1000。null 行不进聚合;扫不完就给 complete:false + capNotice,
|
|
374
|
+
// 绝不按扫描比例外推(与 count 的自由文本路径同一诚实口径)。
|
|
375
|
+
if (numeric) {
|
|
376
|
+
const target = args.field;
|
|
377
|
+
const groupBy = args.groupBy;
|
|
378
|
+
const emptyAcc = () => ({ count: 0, nonNullCount: 0, sum: 0, min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY });
|
|
379
|
+
const overall = emptyAcc();
|
|
380
|
+
const nullBucket = emptyAcc();
|
|
381
|
+
const buckets = new Map();
|
|
382
|
+
const numericMetric = metric;
|
|
383
|
+
// D5:首页探针同时给出 total(是否值得走 /sum)和本地扫描的首页行
|
|
384
|
+
// (回退零额外读)。只有「无 groupBy + total 已知且 > 一页」才考虑 /sum。
|
|
385
|
+
const probe = await wireCall(session, { path: ENDPOINTS.runtimeQueryList, method: "POST", body: queryBody(AGGREGATE_PAGE_SIZE, 0) });
|
|
386
|
+
const probePage = extractBizObjectPage(probe.data);
|
|
387
|
+
let strategyFallback;
|
|
388
|
+
let guardCalls = 0;
|
|
389
|
+
let sumAttempts = 0;
|
|
390
|
+
if (groupBy === undefined && probePage.totalKnown && probePage.total > AGGREGATE_PAGE_SIZE) {
|
|
391
|
+
const path = await probeGetListReadsDatabase(session, args.schemaCode);
|
|
392
|
+
guardCalls = path.wireCalls;
|
|
393
|
+
if (path.isDatabase) {
|
|
394
|
+
sumAttempts = 1;
|
|
395
|
+
const sum = await tryServerSum(session, queryBody(1, 0), target, numericMetric);
|
|
396
|
+
if (sum.ok) {
|
|
397
|
+
const total = probePage.total;
|
|
398
|
+
const nonNullCount = sum.nonNullCount;
|
|
399
|
+
return {
|
|
400
|
+
schemaCode: args.schemaCode,
|
|
401
|
+
metric,
|
|
402
|
+
field: target,
|
|
403
|
+
value: sum.value,
|
|
404
|
+
count: total,
|
|
405
|
+
nonNullCount,
|
|
406
|
+
nullCount: Math.max(0, total - nonNullCount),
|
|
407
|
+
...(sum.value === null ? { note: "no numeric values in matched rows" } : {}),
|
|
408
|
+
coverage: { mode: "server-aggregate", rowsScanned: 0, cap: null, complete: true },
|
|
409
|
+
filter: draftFilter,
|
|
410
|
+
wire: {
|
|
411
|
+
calls: metaCalls + 1 + path.wireCalls + 1,
|
|
412
|
+
plan: `1 metadata + 1 probe + ${path.wireCalls} guard + 1 /sum ALL_DATA`,
|
|
413
|
+
strategy: "server-sum",
|
|
414
|
+
},
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
strategyFallback = { strategy: "server-sum", reason: sum.reason, detail: sum.detail, guardCalls: path.wireCalls };
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
strategyFallback = { strategy: "server-sum", reason: path.reason, guardCalls: path.wireCalls };
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
// 0.6.87 D1:翻页语义统一走 scanPages(首页 total 唯一基准 / 页码序消费 /
|
|
424
|
+
// 空页停止 / 超 total 尾页裁剪 / total 缺失不当全集)。本路径只负责折叠。
|
|
425
|
+
const scan = await scanPages({
|
|
426
|
+
cap: AGGREGATE_CAP,
|
|
427
|
+
pageSize: AGGREGATE_PAGE_SIZE,
|
|
428
|
+
fetchPage: async (pageIndex) => {
|
|
429
|
+
if (pageIndex === 0)
|
|
430
|
+
return { totalKnown: probePage.totalKnown, total: probePage.total, content: probePage.content };
|
|
431
|
+
const response = await wireCall(session, { path: ENDPOINTS.runtimeQueryList, method: "POST", body: queryBody(AGGREGATE_PAGE_SIZE, pageIndex) });
|
|
432
|
+
const pageData = extractBizObjectPage(response.data);
|
|
433
|
+
return { totalKnown: pageData.totalKnown, total: pageData.total, content: pageData.content };
|
|
434
|
+
},
|
|
435
|
+
});
|
|
436
|
+
for (const row of scan.rows) {
|
|
437
|
+
// 平台 query/list 无服务端列投影面:投影在本地做(只读这两列,其余不落内存)。
|
|
438
|
+
const data = row.data && typeof row.data === "object" ? row.data : row;
|
|
439
|
+
const value = aggregateNumeric(data[target]);
|
|
440
|
+
let bucket;
|
|
441
|
+
if (groupBy !== undefined) {
|
|
442
|
+
const raw = data[groupBy];
|
|
443
|
+
if (raw === null || raw === undefined)
|
|
444
|
+
bucket = nullBucket;
|
|
445
|
+
else {
|
|
446
|
+
const key = String(raw);
|
|
447
|
+
bucket = buckets.get(key);
|
|
448
|
+
if (bucket === undefined) {
|
|
449
|
+
bucket = emptyAcc();
|
|
450
|
+
buckets.set(key, bucket);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
for (const acc of bucket === undefined ? [overall] : [overall, bucket]) {
|
|
455
|
+
acc.count += 1;
|
|
456
|
+
if (value === null)
|
|
457
|
+
continue;
|
|
458
|
+
acc.nonNullCount += 1;
|
|
459
|
+
acc.sum += value;
|
|
460
|
+
if (value < acc.min)
|
|
461
|
+
acc.min = value;
|
|
462
|
+
if (value > acc.max)
|
|
463
|
+
acc.max = value;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
const rowsScanned = scan.rows.length;
|
|
467
|
+
const total = scan.total;
|
|
468
|
+
const complete = scan.complete;
|
|
469
|
+
const scanValue = (acc) => aggregateValue(numericMetric, acc.sum, acc.nonNullCount, acc.min, acc.max);
|
|
470
|
+
const coverage = {
|
|
471
|
+
mode: localScanMode(scan.pagesConsumed),
|
|
472
|
+
rowsScanned,
|
|
473
|
+
cap: AGGREGATE_CAP,
|
|
474
|
+
complete,
|
|
475
|
+
...(complete || total === null ? {} : { missingCount: Math.max(0, total - rowsScanned) }),
|
|
476
|
+
};
|
|
477
|
+
const capNotice = total !== null
|
|
478
|
+
? `数值列本地折叠:已扫前 ${rowsScanned}/${total} 行,${metric} 只覆盖已扫部分(complete:false,非全集)。要全集:filters 按日期/业务键 gte/lt 分片重跑,或 page/size 手动翻页。`
|
|
479
|
+
: `数值列本地折叠:首页未回 totalElements(showTotal:false 或该面不回 total),已按安全上限 ${AGGREGATE_CAP} 扫到 ${rowsScanned} 行;total 未知、缺口不可算,不要当全集。要全集:filters 按日期/业务键 gte/lt 分片重跑,或 page/size 手动翻页。`;
|
|
480
|
+
const strategyNote = guardCalls + sumAttempts > 0 ? `${guardCalls} guard + ${sumAttempts} /sum + ` : "";
|
|
481
|
+
const wire = {
|
|
482
|
+
calls: metaCalls + scan.wireCalls + guardCalls + sumAttempts,
|
|
483
|
+
plan: `1 metadata + ${strategyNote}${scan.wireCalls} pages`,
|
|
484
|
+
...(scan.concurrency !== undefined ? { concurrency: scan.concurrency } : {}),
|
|
485
|
+
};
|
|
486
|
+
if (groupBy === undefined) {
|
|
487
|
+
const value = scanValue(overall);
|
|
488
|
+
return {
|
|
489
|
+
schemaCode: args.schemaCode,
|
|
490
|
+
metric,
|
|
491
|
+
field: target,
|
|
492
|
+
value,
|
|
493
|
+
count: overall.count,
|
|
494
|
+
nonNullCount: overall.nonNullCount,
|
|
495
|
+
nullCount: overall.count - overall.nonNullCount,
|
|
496
|
+
...(value === null ? { note: "no numeric values in scanned rows" } : {}),
|
|
497
|
+
coverage,
|
|
498
|
+
filter: draftFilter,
|
|
499
|
+
...(complete ? {} : { capNotice }),
|
|
500
|
+
...(strategyFallback !== undefined ? { strategyFallback } : {}),
|
|
501
|
+
wire,
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
// 降序按 value(并列按 key);value:null(整桶无数值)排在最后,不参与排名。
|
|
505
|
+
const ranked = [...buckets].map(([key, acc]) => ({ key, count: acc.count, nonNullCount: acc.nonNullCount, value: scanValue(acc) })).sort((left, right) => {
|
|
506
|
+
if (left.value === null || right.value === null) {
|
|
507
|
+
if (left.value === null && right.value === null)
|
|
508
|
+
return left.key.localeCompare(right.key);
|
|
509
|
+
return left.value === null ? 1 : -1;
|
|
510
|
+
}
|
|
511
|
+
return right.value - left.value || left.key.localeCompare(right.key);
|
|
512
|
+
});
|
|
513
|
+
const shown = ranked.slice(0, topN);
|
|
514
|
+
return {
|
|
515
|
+
schemaCode: args.schemaCode,
|
|
516
|
+
metric,
|
|
517
|
+
field: target,
|
|
518
|
+
groupBy,
|
|
519
|
+
total,
|
|
520
|
+
buckets: shown,
|
|
521
|
+
nullBucket: { count: nullBucket.count, nonNullCount: nullBucket.nonNullCount, value: scanValue(nullBucket) },
|
|
522
|
+
truncatedBuckets: ranked.length > shown.length,
|
|
523
|
+
bucketsTruncatedCount: ranked.length - shown.length,
|
|
524
|
+
coverage,
|
|
525
|
+
filter: draftFilter,
|
|
526
|
+
...(complete ? {} : { capNotice }),
|
|
527
|
+
...(strategyFallback !== undefined ? { strategyFallback } : {}),
|
|
528
|
+
wire,
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
// 1) 无 groupBy:service-total 精确计数(1 wire;带用户 filter 时 +1 元数据)。
|
|
532
|
+
if (args.groupBy === undefined) {
|
|
533
|
+
const total = await countAt();
|
|
534
|
+
return {
|
|
535
|
+
schemaCode: args.schemaCode,
|
|
536
|
+
metric: "count",
|
|
537
|
+
total,
|
|
538
|
+
coverage: { mode: "server-total", rowsScanned: 0, cap: AGGREGATE_CAP, complete: true },
|
|
539
|
+
filter: draftFilter,
|
|
540
|
+
wire: { calls: metaCalls + 1, plan: metaCalls > 0 ? "1 metadata + 1 base" : "1 base" },
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
// 2/3) groupBy:值域可枚举 → 值域×count;否则 1 列投影本地扫描。
|
|
544
|
+
const meta = await fieldMeta();
|
|
545
|
+
const groupField = meta?.get(args.groupBy);
|
|
546
|
+
const rawCodes = groupField?.choiceCodes ?? [];
|
|
547
|
+
// 0.6.87 D4:重复 choiceCodes 会让同一值域被 count 两次,U+null 之和超过
|
|
548
|
+
// base total(负 unaccounted)。按输入序去重并显式告警,不静默重复计。
|
|
549
|
+
const codes = [...new Set(rawCodes)];
|
|
550
|
+
const shapeWarnings = [];
|
|
551
|
+
if (codes.length !== rawCodes.length)
|
|
552
|
+
shapeWarnings.push(`choiceCodes-duplicate: groupBy ${args.groupBy} 的选项/字典码有 ${rawCodes.length - codes.length} 个重复(${rawCodes.length}→${codes.length}),已去重;否则同一值域会被重复计数`);
|
|
553
|
+
if (groupField && Number.isInteger(groupField.type) && codes.length > 0) {
|
|
554
|
+
// R88 S1:值域×count 扇出(base + 每值域 Eq + IsNull)彼此独立,走有界
|
|
555
|
+
// 并发(窗口 4)。fieldMeta() 已在前一行完成,绝不进并发窗口(metaCalls
|
|
556
|
+
// 双计 + session 缓存);unit 结果按输入序重组,再走既有排序/topN——与
|
|
557
|
+
// 串行逐字段一致,并发完成顺序不抖动输出。
|
|
558
|
+
const groupBy = args.groupBy;
|
|
559
|
+
const groupType = groupField.type;
|
|
560
|
+
const units = [
|
|
561
|
+
{ kind: "base" },
|
|
562
|
+
...codes.map((code) => ({ kind: "bucket", code, leaf: aggregateLeafGroup(groupBy, groupType, "Eq", code) })),
|
|
563
|
+
{ kind: "null", leaf: aggregateLeafGroup(groupBy, groupType, "IsNull", "") },
|
|
564
|
+
];
|
|
565
|
+
const leafOf = (unit) => (unit.kind === "base" ? undefined : unit.leaf);
|
|
566
|
+
const scan = await mapWithConcurrency(units, DEFAULT_SCAN_CONCURRENCY, (unit) => countAt(leafOf(unit)));
|
|
567
|
+
const values = new Array(units.length);
|
|
568
|
+
scan.outcomes.forEach((outcome, index) => {
|
|
569
|
+
if (outcome.ok)
|
|
570
|
+
values[index] = outcome.value;
|
|
571
|
+
});
|
|
572
|
+
if (scan.failed.length > 0) {
|
|
573
|
+
// 失败语义保持串行的 fail-fast:绝不返回「少一个桶」的部分结果。
|
|
574
|
+
// 非限流类(业务拒绝)直接抛最低索引(不重试、不降级,也不被限流
|
|
575
|
+
// 重试掩盖);全灭抛最低索引(确定性,等同串行);只有限流类且非
|
|
576
|
+
// 全灭时,失败项串行重放一次(并发窗口的突发限流串行往往能过),
|
|
577
|
+
// 仍失败抛最低索引。
|
|
578
|
+
const businessFailure = scan.failed.find((failure) => !isThrottleClassError(failure.error));
|
|
579
|
+
if (businessFailure !== undefined)
|
|
580
|
+
throw businessFailure.error;
|
|
581
|
+
if (scan.failed.length === units.length)
|
|
582
|
+
throw scan.failed[0].error;
|
|
583
|
+
// 串行重放是 unit 级重执行;wire.calls 仍按逻辑请求数,concurrency 直接
|
|
584
|
+
// 复用窗口内 stats(与 workflow.list / rule.references 同口径)。
|
|
585
|
+
for (const failure of scan.failed)
|
|
586
|
+
values[failure.index] = await countAt(leafOf(units[failure.index]));
|
|
587
|
+
}
|
|
588
|
+
const baseTotal = values[0];
|
|
589
|
+
const buckets = codes.map((code, index) => ({ key: code, count: values[index + 1] }));
|
|
590
|
+
const nullCount = values[units.length - 1];
|
|
591
|
+
const sums = buckets.reduce((sum, bucket) => sum + bucket.count, 0);
|
|
592
|
+
const unaccounted = baseTotal - sums - nullCount;
|
|
593
|
+
// D4:负 unaccounted 不得静默丢(旧代码只在 >0 时给键,负值连信号都没有)。
|
|
594
|
+
// 归零 + 显式 warning:桶计数是平台 COUNT 结果,不在这里改写数字本身。
|
|
595
|
+
if (unaccounted < 0)
|
|
596
|
+
shapeWarnings.push(`unaccounted-negative: 桶和 ${sums} + null ${nullCount} > base total ${baseTotal}(差 ${unaccounted}),已归零;通常是重复 choiceCodes 或平台 COUNT 漂移——不要把分桶当精确闭环`);
|
|
597
|
+
buckets.sort((left, right) => right.count - left.count || left.key.localeCompare(right.key));
|
|
598
|
+
const shown = buckets.slice(0, topN);
|
|
599
|
+
return {
|
|
600
|
+
schemaCode: args.schemaCode,
|
|
601
|
+
metric: "count",
|
|
602
|
+
groupBy: args.groupBy,
|
|
603
|
+
total: baseTotal,
|
|
604
|
+
buckets: shown,
|
|
605
|
+
nullBucket: { count: nullCount },
|
|
606
|
+
truncatedBuckets: buckets.length > shown.length,
|
|
607
|
+
bucketsTruncatedCount: buckets.length - shown.length,
|
|
608
|
+
...(unaccounted > 0 ? { unaccountedCount: unaccounted } : unaccounted < 0 ? { unaccountedCount: 0 } : {}),
|
|
609
|
+
...(shapeWarnings.length > 0 ? { shapeWarnings } : {}),
|
|
610
|
+
coverage: { mode: "server-total", rowsScanned: 0, cap: AGGREGATE_CAP, complete: true },
|
|
611
|
+
filter: draftFilter,
|
|
612
|
+
wire: {
|
|
613
|
+
calls: metaCalls + 1 + codes.length + 1,
|
|
614
|
+
plan: `1 metadata + 1 base + ${codes.length} buckets + 1 null`,
|
|
615
|
+
...(units.length > 1 ? { concurrency: scan.stats } : {}),
|
|
616
|
+
},
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
// 自由文本/高基数:翻页取前 cap 行,只投影 groupBy 列本地计数;绝不按扫描比例外推。
|
|
620
|
+
const counts = new Map();
|
|
621
|
+
let nullCount = 0;
|
|
622
|
+
// 0.6.87 D1:与数值路径共用 scanPages 的翻页/裁剪/空页/total 语义。
|
|
623
|
+
const scan = await scanPages({
|
|
624
|
+
cap: AGGREGATE_CAP,
|
|
625
|
+
pageSize: AGGREGATE_PAGE_SIZE,
|
|
626
|
+
fetchPage: async (pageIndex) => {
|
|
627
|
+
const response = await wireCall(session, { path: ENDPOINTS.runtimeQueryList, method: "POST", body: queryBody(AGGREGATE_PAGE_SIZE, pageIndex) });
|
|
628
|
+
const pageData = extractBizObjectPage(response.data);
|
|
629
|
+
return { totalKnown: pageData.totalKnown, total: pageData.total, content: pageData.content };
|
|
630
|
+
},
|
|
631
|
+
});
|
|
632
|
+
for (const row of scan.rows) {
|
|
633
|
+
const data = row.data && typeof row.data === "object" ? row.data : row;
|
|
634
|
+
const raw = data[args.groupBy];
|
|
635
|
+
if (raw === null || raw === undefined)
|
|
636
|
+
nullCount += 1;
|
|
637
|
+
else {
|
|
638
|
+
const key = String(raw);
|
|
639
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
const rowsScanned = scan.rows.length;
|
|
643
|
+
const total = scan.total;
|
|
644
|
+
const complete = scan.complete;
|
|
645
|
+
const allBuckets = [...counts].map(([key, count]) => ({ key, count })).sort((left, right) => right.count - left.count || left.key.localeCompare(right.key));
|
|
646
|
+
const shown = allBuckets.slice(0, topN);
|
|
647
|
+
return {
|
|
648
|
+
schemaCode: args.schemaCode,
|
|
649
|
+
metric: "count",
|
|
650
|
+
groupBy: args.groupBy,
|
|
651
|
+
total,
|
|
652
|
+
buckets: shown,
|
|
653
|
+
nullBucket: { count: nullCount },
|
|
654
|
+
truncatedBuckets: allBuckets.length > shown.length,
|
|
655
|
+
bucketsTruncatedCount: allBuckets.length - shown.length,
|
|
656
|
+
coverage: {
|
|
657
|
+
mode: localScanMode(scan.pagesConsumed),
|
|
658
|
+
rowsScanned,
|
|
659
|
+
cap: AGGREGATE_CAP,
|
|
660
|
+
complete,
|
|
661
|
+
...(total === null ? {} : { missingCount: Math.max(0, total - rowsScanned) }),
|
|
662
|
+
},
|
|
663
|
+
filter: draftFilter,
|
|
664
|
+
...(complete ? {} : {
|
|
665
|
+
capNotice: total !== null
|
|
666
|
+
? `值域非可枚举:已扫前 ${rowsScanned}/${total} 行,桶只覆盖已扫部分(complete:false,非全集)。要全集:filters 按日期/业务键 gte/lt 分片重跑,或 page/size 手动翻页。`
|
|
667
|
+
: `值域非可枚举:首页未回 totalElements(showTotal:false 或该面不回 total),已按安全上限 ${AGGREGATE_CAP} 扫到 ${rowsScanned} 行;total 未知、缺口不可算,不要当全集。要全集:filters 按日期/业务键 gte/lt 分片重跑,或 page/size 手动翻页。`,
|
|
668
|
+
}),
|
|
669
|
+
wire: {
|
|
670
|
+
calls: metaCalls + scan.wireCalls,
|
|
671
|
+
plan: `${metaCalls > 0 ? "1 metadata + " : ""}${scan.wireCalls} pages`,
|
|
672
|
+
...(scan.concurrency !== undefined ? { concurrency: scan.concurrency } : {}),
|
|
673
|
+
},
|
|
674
|
+
};
|
|
675
|
+
}, { target: `data.aggregate:${JSON.stringify({ schemaCode: args.schemaCode, metric: args.metric, field: args.field, filter: args.filter, filters: args.filters, filterGroup: args.filterGroup, groupBy: args.groupBy, topN: args.topN ?? 10, includeDrafts: args.includeDrafts ?? null, child: args.child ?? null })}` });
|
|
676
|
+
},
|
|
677
|
+
}));
|
|
678
|
+
}
|
|
679
|
+
//# sourceMappingURL=aggregate.js.map
|