fix: stabilize auth identity, record operations, and workflow diagnostics

This commit is contained in:
gitea committed 2026-10-15 14:54:10 +08:00
1 parent c74fdda7da
commit 7ec369f77e
36 files changed
+1021 -337

No files matched your search

+43 -33
View File
@@ -5,7 +5,7 @@ import {
buildKeywordFilter,
calculateEntityRecordStats,
fetchAllEntityRecords,
filterRecordsByPredicate,
compileRecordPredicate,
iterateAllEntityRecords,
normalizeDatalinkRecord,
parseExpectClause,
@@ -14,11 +14,17 @@ import {
} from "../../../domains/datalink/services.js";
import type { LedgerContinuityReport } from "../../../domains/datalink/types.js";
import { booleanOption, numberOption, stringOption } from "../../parser.js";
import { printJson, printTable, printKeyValue } from "../../printer.js";
import { printJson, printTable, printKeyValue, redactSensitiveText } from "../../printer.js";
import { usageError } from "../../errors.js";
import { open } from "node:fs/promises";
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
import { cell, resolveAppAndEntity, splitFields } from "./shared.js";
export async function handleRecords(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const isStream = booleanOption(parsed.options, ["ndjson", "stream"], false);
if (isStream && !runtime.outputOptions?.outputFile && !runtime.writeStdout) {
throw usageError("当前调用入口不支持直接 NDJSON 流,请使用 --output-file <path> 保存,或在终端运行该命令。");
}
const isStatsSubcommand = parsed.args[0] === "stats" || parsed.action === "stats";
const appIndex = parsed.args[0] === "stats" ? 1 : 0;
const entityIndex = parsed.args[0] === "stats" ? 2 : 1;
@@ -28,9 +34,9 @@ export async function handleRecords(parsed: CliParsedArgs, runtime: CliRuntime):
const keyword = stringOption(parsed.options, ["keyword", "q", "query"]);
const filterBy = stringOption(parsed.options, ["filter", "filter-by", "filterBy"]) || (keyword ? buildKeywordFilter(keyword) : undefined);
const whereClause = stringOption(parsed.options, ["where"]);
const predicate = whereClause ? compileRecordPredicate(whereClause, resolved.entity) : undefined;
const sumOption = stringOption(parsed.options, ["sum", "aggregate"]);
const isProfileStats = isStatsSubcommand || booleanOption(parsed.options, ["stats", "profile"], false);
const isStream = booleanOption(parsed.options, ["ndjson", "stream"], false);
const isRaw = booleanOption(parsed.options, ["raw"], false);
const isFlat = booleanOption(parsed.options, ["flat", "normalize", "normalized"], false);
const isAll = booleanOption(parsed.options, ["all", "a"], false);
@@ -44,33 +50,41 @@ export async function handleRecords(parsed: CliParsedArgs, runtime: CliRuntime):
// 1. 高性能流式下发模式 (--ndjson / --stream,内存开销 O(pageSize),专为海量数据外部脚本管道设计)
if (isStream) {
let streamCount = 0;
const maxLimit = isAll ? Infinity : count;
let bytes = 0;
const maxLimit = isAll ? Infinity : count ?? 20;
const outputFile = runtime.outputOptions?.outputFile;
const file = outputFile ? await open(outputFile, "w", 0o600) : undefined;
for await (const rawRecord of iterateAllEntityRecords(
resolved.clients.ekbClient,
resolved.corpId,
resolved.entity.id,
{ filterBy, pageSize }
)) {
if (whereClause) {
const matched = filterRecordsByPredicate([rawRecord], whereClause, resolved.entity);
if (matched.length === 0) continue;
try {
if (maxLimit > 0) {
for await (const rawRecord of iterateAllEntityRecords(
resolved.clients.ekbClient,
resolved.corpId,
resolved.entity.id,
{ filterBy, pageSize }
)) {
if (predicate && !predicate(rawRecord)) continue;
let outputRecord: Record<string, unknown> = isRaw
? (rawRecord as Record<string, unknown>)
: (normalizeDatalinkRecord(rawRecord, resolved.entity) as unknown as Record<string, unknown>);
if (fields.length) {
outputRecord = selectRecordFields(outputRecord, fields, resolved.entity.fields);
}
const chunk = redactSensitiveText(JSON.stringify(outputRecord)) + "\n";
if (file) await file.writeFile(chunk);
else await runtime.writeStdout!(chunk);
bytes += Buffer.byteLength(chunk);
streamCount++;
if (maxLimit !== undefined && streamCount >= maxLimit) break;
}
}
let outputRecord: Record<string, unknown> = isRaw
? (rawRecord as Record<string, unknown>)
: (normalizeDatalinkRecord(rawRecord, resolved.entity) as unknown as Record<string, unknown>);
if (fields.length) {
outputRecord = selectRecordFields(outputRecord, fields, resolved.entity.fields);
}
process.stdout.write(JSON.stringify(outputRecord) + "\n");
streamCount++;
if (maxLimit !== undefined && streamCount >= maxLimit) break;
} finally {
await file?.close();
}
return { stdout: "", stderr: "", exitCode: 0 };
return { stdout: outputFile ? printJson({ outputFile, bytes, records: streamCount }) : "", stderr: "", exitCode: 0 };
}
// 2. 宏观画像与统计模式 (--stats 或 platform records stats <应用> <对象>)
@@ -82,9 +96,7 @@ export async function handleRecords(parsed: CliParsedArgs, runtime: CliRuntime):
{ filterBy, pageSize }
);
let records = page.data;
if (whereClause) {
records = filterRecordsByPredicate(records, whereClause, resolved.entity);
}
if (predicate) records = records.filter(predicate);
const stats = calculateEntityRecordStats(records, resolved.entity, page.total);
if (runtime.jsonMode) {
@@ -118,9 +130,7 @@ export async function handleRecords(parsed: CliParsedArgs, runtime: CliRuntime):
: await searchEntityRecords(resolved.clients.ekbClient, resolved.corpId, resolved.entity.id, { start, count, filterBy });
let data = page.data;
if (whereClause) {
data = filterRecordsByPredicate(data, whereClause, resolved.entity);
}
if (predicate) data = data.filter(predicate);
// 3. 数学聚合核算模式 (--sum="租赁费用,日折旧费")
if (sumOption) {