fix: hydrate id export metrics and remove new tier columns

This commit is contained in:
2026-05-18 19:50:14 +08:00
parent 38da39589f
commit 37f7e0b5e6
4 changed files with 114 additions and 11 deletions
@@ -30,7 +30,6 @@ const GENDER_LABELS = ["男性", "女性"];
const AGE_LABELS = ["18-23", "24-30", "31-40", "41-50", "50+"];
const CITY_TIER_LABELS = [
"一线城市",
"新一线城市",
"二线城市",
"三线城市",
"四线城市",
+52 -4
View File
@@ -296,6 +296,7 @@ export function createMarketController(options: CreateMarketControllerOptions) {
`识别 ${parsed.ids.length + parsed.duplicates.length + parsed.invalidTokens.length} 个,去重后 ${parsed.ids.length} 个,非法 ${parsed.invalidTokens.length}`
);
const backendMetricsByAuthorId = await loadBackendMetricsMap(parsed.ids);
const rows: AudienceProfileExportRow[] = [];
for (let index = 0; index < parsed.ids.length; index += 1) {
const authorId = parsed.ids[index];
@@ -303,7 +304,12 @@ export function createMarketController(options: CreateMarketControllerOptions) {
toolbar,
`按ID画像导出中 ${index + 1}/${parsed.ids.length}...`
);
rows.push(await loadAudienceProfileRowById(authorId));
rows.push(
await loadAudienceProfileRowById(
authorId,
backendMetricsByAuthorId.get(authorId)
)
);
}
options.onCsvReady?.(
@@ -761,12 +767,20 @@ export function createMarketController(options: CreateMarketControllerOptions) {
}
async function loadAudienceProfileRowById(
authorId: string
authorId: string,
backendMetrics?: BackendMetrics
): Promise<AudienceProfileExportRow> {
const baseRecord = await loadAuthorBaseInfoSafe(authorId);
const [baseRecord, metricsResult] = await Promise.all([
loadAuthorBaseInfoSafe(authorId),
loadAuthorMetricsSafe(authorId)
]);
const recordForRequests = {
...baseRecord,
authorName: baseRecord.authorName || authorId
authorName: baseRecord.authorName || authorId,
...(metricsResult.success ? { rates: metricsResult.rates } : {}),
...(backendMetrics
? { backendMetrics, backendMetricsStatus: "success" as const }
: {})
};
const [profiles, businessAbility] = await Promise.all([
loadAudienceProfileSet(recordForRequests),
@@ -814,6 +828,40 @@ export function createMarketController(options: CreateMarketControllerOptions) {
}
}
async function loadAuthorMetricsSafe(
authorId: string
): Promise<MarketApiResult> {
try {
return await loadAuthorMetrics(authorId);
} catch {
return {
reason: "request-failed",
success: false
};
}
}
async function loadBackendMetricsMap(
authorIds: string[]
): Promise<Map<string, BackendMetrics>> {
const metricsMap = new Map<string, BackendMetrics>();
if (!searchBackendMetrics || authorIds.length === 0) {
return metricsMap;
}
try {
const rows = await searchBackendMetrics(authorIds);
rows.forEach((row) => {
const { starId, ...backendMetrics } = row;
metricsMap.set(starId, backendMetrics);
});
} catch {
return metricsMap;
}
return metricsMap;
}
function collectAudienceProfileRowFailures(
baseRecord: MarketRecord,
profiles: AudienceProfileExportRow["profiles"],