503 lines
17 KiB
JavaScript
503 lines
17 KiB
JavaScript
export const DEFAULT_COMPUTE_PARTICIPANTS = ["CN", "US", "EUROPE", "APAC", "OTHER"];
|
|
|
|
export const COMPUTE_MODE_OPTIONS = [
|
|
{
|
|
mode: "global_share",
|
|
shortZh: "总算力份额",
|
|
shortEn: "Total Share",
|
|
labelZh: "全球总算力份额",
|
|
labelEn: "Global Total Share",
|
|
unit: "%",
|
|
},
|
|
{
|
|
mode: "intelligent_share",
|
|
shortZh: "智能算力",
|
|
shortEn: "AI Compute",
|
|
labelZh: "全球智能算力竞争份额",
|
|
labelEn: "Global AI Compute Share",
|
|
unit: "%",
|
|
},
|
|
{
|
|
mode: "bilateral_rpi",
|
|
shortZh: "双边RPI",
|
|
shortEn: "CN/US RPI",
|
|
labelZh: "中美相对平价指数 (US=100%)",
|
|
labelEn: "Relative Parity Index (US=100%)",
|
|
unit: "%",
|
|
},
|
|
{
|
|
mode: "absolute",
|
|
shortZh: "绝对规模",
|
|
shortEn: "Absolute",
|
|
labelZh: "算力设备总规模",
|
|
labelEn: "Total Compute Capacity",
|
|
unit: "EFlop/s (FP32)",
|
|
},
|
|
{
|
|
mode: "frontier_cluster_share",
|
|
shortZh: "前沿集群样本",
|
|
shortEn: "Frontier Clusters",
|
|
labelZh: "公开前沿集群算力占比",
|
|
labelEn: "Public Frontier Cluster Share",
|
|
unit: "%",
|
|
},
|
|
];
|
|
|
|
const SHARE_METER_MODES = new Set([
|
|
"global_share",
|
|
"intelligent_share",
|
|
"bilateral_share",
|
|
"frontier_cluster_share",
|
|
"chip_inflow_share",
|
|
]);
|
|
|
|
const kindOf = (entry) => entry?.kind === "region" || entry?.kind === "group"
|
|
? "region"
|
|
: "country";
|
|
|
|
export function getComputeModeMeta(mode) {
|
|
const match = COMPUTE_MODE_OPTIONS.find((item) => item.mode === mode);
|
|
if (match) return match;
|
|
if (mode === "bilateral_share") {
|
|
return {
|
|
mode: "bilateral_share",
|
|
shortZh: "双边占比",
|
|
shortEn: "Bilateral Share",
|
|
labelZh: "中美双边合计份额占比",
|
|
labelEn: "CN-US Bilateral Combined Share",
|
|
unit: "%",
|
|
};
|
|
}
|
|
if (mode === "chip_inflow_share") {
|
|
return {
|
|
mode: "chip_inflow_share",
|
|
shortZh: "高端芯片流向",
|
|
shortEn: "Chip Inflows",
|
|
labelZh: "公开高端算力芯片新增流向占比",
|
|
labelEn: "Tracked Frontier Chip Inflow Share",
|
|
unit: "%",
|
|
};
|
|
}
|
|
return COMPUTE_MODE_OPTIONS[0];
|
|
}
|
|
|
|
export function memberCodes(entry) {
|
|
const members = Array.isArray(entry?.members) ? entry.members : [];
|
|
return new Set((members.length ? members : [entry?.code]).filter(Boolean));
|
|
}
|
|
|
|
export function selectComputeParticipant(current, nextCode, catalog, maximum = 6) {
|
|
if (!nextCode || current.includes(nextCode)) return { codes: current, replaced: [] };
|
|
const next = catalog.find((entry) => entry.code === nextCode);
|
|
if (!next) return { codes: current, replaced: [] };
|
|
|
|
const byCode = new Map(catalog.map((entry) => [entry.code, entry]));
|
|
const nextMembers = memberCodes(next);
|
|
const overlaps = current.filter((code) => {
|
|
const selected = byCode.get(code);
|
|
if (!selected) return false;
|
|
if (kindOf(next) === "country") {
|
|
return kindOf(selected) === "region" && memberCodes(selected).has(next.code);
|
|
}
|
|
return kindOf(selected) === "country" && nextMembers.has(selected.code);
|
|
});
|
|
|
|
const insertionIndex = overlaps.length
|
|
? Math.min(...overlaps.map((code) => current.indexOf(code)))
|
|
: current.length;
|
|
const withoutOverlaps = current.filter((code) => !overlaps.includes(code));
|
|
if (withoutOverlaps.length >= maximum) {
|
|
return { codes: current, replaced: [], limitReached: true };
|
|
}
|
|
const codes = [...withoutOverlaps];
|
|
codes.splice(Math.min(insertionIndex, codes.length), 0, nextCode);
|
|
return { codes, replaced: overlaps };
|
|
}
|
|
|
|
export function summarizeComputeTrend(points, mode) {
|
|
const comparable = (Array.isArray(points) ? points : [])
|
|
.filter((point) => Number.isFinite(point?.year) && Number.isFinite(point?.value));
|
|
if (comparable.length < 2) return null;
|
|
const previous = comparable.at(-2);
|
|
const current = comparable.at(-1);
|
|
return {
|
|
fromYear: previous.year,
|
|
toYear: current.year,
|
|
change: Number((current.value - previous.value).toFixed(2)),
|
|
unit: mode === "absolute" ? "EFlop/s (FP32)" : "percentage_points",
|
|
};
|
|
}
|
|
|
|
export function computeShareMeterValue(value, mode) {
|
|
const numericValue = Number(value);
|
|
if (value === null || value === "" || !Number.isFinite(numericValue) || numericValue < 0) {
|
|
return null;
|
|
}
|
|
if (SHARE_METER_MODES.has(mode)) {
|
|
return numericValue <= 100 ? numericValue : null;
|
|
}
|
|
if (mode === "bilateral_rpi") {
|
|
return numericValue <= 200 ? Math.min(100, numericValue) : null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function resolveObservationWindow(points, focusYear = null) {
|
|
const rows = Array.isArray(points) ? points : [];
|
|
const observed = rows.filter(
|
|
(point) => Number.isFinite(point?.year) && point?.value !== null && Number.isFinite(Number(point?.value)),
|
|
);
|
|
if (!observed.length) {
|
|
return {
|
|
activePoint: null,
|
|
latestObserved: null,
|
|
isAfterLatestReference: false,
|
|
latestReferenceLabel: null,
|
|
};
|
|
}
|
|
const latestObserved = observed.at(-1);
|
|
if (!Number.isFinite(focusYear)) {
|
|
return {
|
|
activePoint: latestObserved,
|
|
latestObserved,
|
|
isAfterLatestReference: false,
|
|
latestReferenceLabel: latestObserved.periodLabel || String(latestObserved.year),
|
|
};
|
|
}
|
|
const exact = observed.find((point) => point.year === focusYear) || null;
|
|
if (exact) {
|
|
return {
|
|
activePoint: exact,
|
|
latestObserved,
|
|
isAfterLatestReference: false,
|
|
latestReferenceLabel: exact.periodLabel || String(exact.year),
|
|
};
|
|
}
|
|
const isAfterLatestReference = focusYear > latestObserved.year;
|
|
return {
|
|
activePoint: latestObserved,
|
|
latestObserved,
|
|
isAfterLatestReference,
|
|
latestReferenceLabel: latestObserved.periodLabel || String(latestObserved.year),
|
|
};
|
|
}
|
|
|
|
export function buildBilateralDuelSummary(history, focusYear = null) {
|
|
const rpi = history?.bilateral_rpi;
|
|
if (!rpi || typeof rpi !== "object") return null;
|
|
|
|
const mode = history?.mode || "global_share";
|
|
let snapshot = null;
|
|
if (mode === "intelligent_share") {
|
|
snapshot = rpi.latest_intelligent || rpi.latest_total;
|
|
} else if (mode === "frontier_cluster_share") {
|
|
snapshot = rpi.latest_frontier_cluster || rpi.latest_total;
|
|
} else if (mode === "chip_inflow_share") {
|
|
snapshot = rpi.latest_chip_inflow || rpi.latest_total;
|
|
} else if (Array.isArray(rpi.total_series) && Number.isFinite(focusYear)) {
|
|
snapshot = rpi.total_series.find((row) => row.year === focusYear) || rpi.latest_total;
|
|
} else {
|
|
snapshot = rpi.latest_total || rpi.latest_intelligent;
|
|
}
|
|
|
|
const cnValue = Number(snapshot?.cn_value);
|
|
const usValue = Number(snapshot?.us_value);
|
|
const rpiPercent = Number(snapshot?.rpi_percent);
|
|
if (!Number.isFinite(cnValue) || !Number.isFinite(usValue) || cnValue < 0 || usValue <= 0) {
|
|
return null;
|
|
}
|
|
const combined = cnValue + usValue;
|
|
if (combined <= 0) return null;
|
|
const cnBarWidthPct = Number(((cnValue / combined) * 100).toFixed(1));
|
|
const usBarWidthPct = Number((100 - cnBarWidthPct).toFixed(1));
|
|
|
|
return {
|
|
year: snapshot.year ?? null,
|
|
periodLabel: snapshot.period_label || (snapshot.year ? String(snapshot.year) : ""),
|
|
cnValue,
|
|
usValue,
|
|
rpiPercent: Number.isFinite(rpiPercent) ? rpiPercent : Number(((cnValue / usValue) * 100).toFixed(1)),
|
|
gapPp: Number.isFinite(Number(snapshot.gap_pp)) ? Number(snapshot.gap_pp) : Number((cnValue - usValue).toFixed(1)),
|
|
yoyCnPp: Number.isFinite(Number(snapshot.yoy_cn_pp)) ? Number(snapshot.yoy_cn_pp) : null,
|
|
yoyUsPp: Number.isFinite(Number(snapshot.yoy_us_pp)) ? Number(snapshot.yoy_us_pp) : null,
|
|
cnBarWidthPct,
|
|
usBarWidthPct,
|
|
evidenceId: snapshot.evidence_id || null,
|
|
};
|
|
}
|
|
|
|
export function buildDenominatorAttributionSummary(history, code) {
|
|
const entry = history?.denominator_attribution?.[code];
|
|
if (!entry || !Number.isFinite(Number(entry.latest_value))) return null;
|
|
return {
|
|
latestYear: entry.latest_year ?? null,
|
|
latestPeriod: entry.latest_period || String(entry.latest_year || ""),
|
|
latestValue: Number(entry.latest_value),
|
|
latestGlobalTotal: Number.isFinite(Number(entry.latest_global_total)) ? Number(entry.latest_global_total) : null,
|
|
fromPeriod: entry.from_period || (entry.from_year ? String(entry.from_year) : null),
|
|
toPeriod: entry.to_period || (entry.to_period ? String(entry.to_period) : null),
|
|
countryFrom: Number.isFinite(Number(entry.country_from)) ? Number(entry.country_from) : null,
|
|
countryTo: Number.isFinite(Number(entry.country_to)) ? Number(entry.country_to) : null,
|
|
countryGrowthPct: Number.isFinite(Number(entry.country_growth_pct)) ? Number(entry.country_growth_pct) : null,
|
|
globalFrom: Number.isFinite(Number(entry.global_from)) ? Number(entry.global_from) : null,
|
|
globalTo: Number.isFinite(Number(entry.global_to)) ? Number(entry.global_to) : null,
|
|
globalGrowthPct: Number.isFinite(Number(entry.global_growth_pct)) ? Number(entry.global_growth_pct) : null,
|
|
dilutedByGlobalExpansion: Boolean(entry.diluted_by_global_expansion),
|
|
unit: entry.unit || "EFlop/s (FP32)",
|
|
evidenceIds: Array.isArray(entry.evidence_ids) ? entry.evidence_ids : [],
|
|
};
|
|
}
|
|
|
|
export function formatStructureSegments(structures, code) {
|
|
if (!structures || !code || typeof structures !== "object") return null;
|
|
const row = structures[code];
|
|
if (!row || !Number.isFinite(Number(row.intelligent_share)) || !Number.isFinite(Number(row.general_and_super_share))) {
|
|
return null;
|
|
}
|
|
const intelligentShare = Number(row.intelligent_share);
|
|
const generalAndSuperShare = Number(row.general_and_super_share);
|
|
if (intelligentShare <= 0 && generalAndSuperShare <= 0) return null;
|
|
|
|
const globalRow = structures.GLOBAL;
|
|
const globalIntelligentShare = Number.isFinite(Number(globalRow?.intelligent_share))
|
|
? Number(globalRow.intelligent_share)
|
|
: null;
|
|
|
|
return {
|
|
scope: code,
|
|
year: row.year ?? null,
|
|
periodLabel: row.period_label || (row.year ? String(row.year) : ""),
|
|
intelligentShare,
|
|
generalAndSuperShare,
|
|
globalIntelligentShare,
|
|
note: row.note || "",
|
|
noteEn: row.note_en || "",
|
|
sourceId: row.source_id || null,
|
|
};
|
|
}
|
|
|
|
function buildSegmentedPath(points) {
|
|
const segments = [];
|
|
let current = [];
|
|
for (const pt of points) {
|
|
if (pt && Number.isFinite(pt.x) && Number.isFinite(pt.y)) {
|
|
current.push(pt);
|
|
} else if (current.length) {
|
|
segments.push(current);
|
|
current = [];
|
|
}
|
|
}
|
|
if (current.length) segments.push(current);
|
|
return segments
|
|
.filter((seg) => seg.length >= 1)
|
|
.map((seg) => seg.map((pt, idx) => `${idx === 0 ? "M" : "L"}${pt.x.toFixed(1)},${pt.y.toFixed(1)}`).join(" "))
|
|
.join(" ");
|
|
}
|
|
|
|
export function buildDivergenceChartGeometry(divergenceSeries, milestones = [], options = {}) {
|
|
const width = options.width ?? 228;
|
|
const height = options.height ?? 102;
|
|
const padLeft = options.padLeft ?? 24;
|
|
const padRight = options.padRight ?? 12;
|
|
const padTop = options.padTop ?? 12;
|
|
const padBottom = options.padBottom ?? 22;
|
|
|
|
const rawRows = (Array.isArray(divergenceSeries) ? divergenceSeries : [])
|
|
.filter((row) => Number.isFinite(row?.year))
|
|
.sort((a, b) => a.year - b.year);
|
|
|
|
const observedRows = rawRows.filter((row) => {
|
|
const vals = row?.values || {};
|
|
return Object.values(vals).some((val) => val !== null && Number.isFinite(Number(val)));
|
|
});
|
|
|
|
const rows = observedRows.length ? observedRows : rawRows;
|
|
if (!rows.length) return null;
|
|
|
|
const allValues = [];
|
|
for (const row of rows) {
|
|
for (const val of Object.values(row?.values || {})) {
|
|
if (val !== null && Number.isFinite(Number(val))) {
|
|
allValues.push(Number(val));
|
|
}
|
|
}
|
|
}
|
|
if (!allValues.length) return null;
|
|
|
|
const yMin = 0;
|
|
const yMax = Math.max(50, Math.ceil(Math.max(...allValues) / 10) * 10);
|
|
const plotW = Math.max(24, width - padLeft - padRight);
|
|
const plotH = Math.max(24, height - padTop - padBottom);
|
|
const xForIndex = (index) => (
|
|
rows.length === 1
|
|
? padLeft + plotW / 2
|
|
: padLeft + (index / (rows.length - 1)) * plotW
|
|
);
|
|
const yForValue = (val) => padTop + (1 - (val - yMin) / Math.max(1, yMax - yMin)) * plotH;
|
|
|
|
const yearIndex = new Map(rows.map((row, idx) => [row.year, idx]));
|
|
|
|
const usSlots = rows.map((row, idx) => {
|
|
const raw = row?.values?.US;
|
|
if (raw === null || raw === undefined || !Number.isFinite(Number(raw))) return null;
|
|
const value = Number(raw);
|
|
return {
|
|
year: row.year,
|
|
periodLabel: row.period_label || String(row.year),
|
|
value,
|
|
x: xForIndex(idx),
|
|
y: yForValue(value),
|
|
evidenceId: row.evidence_id || null,
|
|
};
|
|
});
|
|
|
|
const cnSlots = rows.map((row, idx) => {
|
|
const raw = row?.values?.CN;
|
|
if (raw === null || raw === undefined || !Number.isFinite(Number(raw))) return null;
|
|
const value = Number(raw);
|
|
return {
|
|
year: row.year,
|
|
periodLabel: row.period_label || String(row.year),
|
|
value,
|
|
x: xForIndex(idx),
|
|
y: yForValue(value),
|
|
evidenceId: row.evidence_id || null,
|
|
};
|
|
});
|
|
|
|
// Build corridor polygon spans wherever consecutive observed years have both US & CN
|
|
const corridorPolygons = [];
|
|
let activeSpan = [];
|
|
for (let i = 0; i < rows.length; i += 1) {
|
|
if (usSlots[i] && cnSlots[i]) {
|
|
activeSpan.push({ us: usSlots[i], cn: cnSlots[i] });
|
|
} else if (activeSpan.length) {
|
|
if (activeSpan.length >= 2) corridorPolygons.push(activeSpan);
|
|
activeSpan = [];
|
|
}
|
|
}
|
|
if (activeSpan.length >= 2) corridorPolygons.push(activeSpan);
|
|
|
|
const corridorPaths = corridorPolygons.map((span) => {
|
|
const upper = span.map((pair, idx) => `${idx === 0 ? "M" : "L"}${pair.us.x.toFixed(1)},${pair.us.y.toFixed(1)}`).join(" ");
|
|
const lower = [...span]
|
|
.reverse()
|
|
.map((pair) => `L${pair.cn.x.toFixed(1)},${pair.cn.y.toFixed(1)}`)
|
|
.join(" ");
|
|
return `${upper} ${lower} Z`;
|
|
});
|
|
|
|
const minYear = rows[0].year;
|
|
const maxYear = rows.at(-1).year;
|
|
const milestoneAnchors = (Array.isArray(milestones) ? milestones : [])
|
|
.filter((ms) => Number.isFinite(ms?.year) && ms.year >= minYear && ms.year <= maxYear + 1)
|
|
.map((ms) => {
|
|
let x = padLeft + plotW;
|
|
if (yearIndex.has(ms.year)) {
|
|
x = xForIndex(yearIndex.get(ms.year));
|
|
} else if (maxYear > minYear) {
|
|
const clamped = Math.min(maxYear, Math.max(minYear, ms.year));
|
|
x = padLeft + ((clamped - minYear) / (maxYear - minYear)) * plotW;
|
|
}
|
|
return {
|
|
id: ms.id,
|
|
year: ms.year,
|
|
month: ms.month ?? null,
|
|
x: Number(x.toFixed(1)),
|
|
y: Number((padTop + plotH + 1).toFixed(1)),
|
|
title: ms.title || "",
|
|
titleEn: ms.title_en || ms.title || "",
|
|
summary: ms.summary || "",
|
|
summaryEn: ms.summary_en || ms.summary || "",
|
|
sourceId: ms.source_id || null,
|
|
};
|
|
});
|
|
|
|
return {
|
|
width,
|
|
height,
|
|
padLeft,
|
|
padRight,
|
|
padTop,
|
|
padBottom,
|
|
yMin,
|
|
yMax,
|
|
years: rows.map((row, idx) => ({
|
|
year: row.year,
|
|
x: Number(xForIndex(idx).toFixed(1)),
|
|
})),
|
|
usPath: buildSegmentedPath(usSlots),
|
|
cnPath: buildSegmentedPath(cnSlots),
|
|
usPoints: usSlots.filter(Boolean),
|
|
cnPoints: cnSlots.filter(Boolean),
|
|
corridorPaths,
|
|
milestones: milestoneAnchors,
|
|
};
|
|
}
|
|
|
|
export function buildInnovationConvergenceGeometry(innovationRows, innovationSummary = null, options = {}) {
|
|
const width = options.width ?? 176;
|
|
const height = options.height ?? 102;
|
|
const padLeft = options.padLeft ?? 22;
|
|
const padRight = options.padRight ?? 12;
|
|
const padTop = options.padTop ?? 14;
|
|
const padBottom = options.padBottom ?? 22;
|
|
|
|
const observed = (Array.isArray(innovationRows) ? innovationRows : [])
|
|
.filter((row) => Number.isFinite(row?.year) && row?.value !== null && Number.isFinite(Number(row?.value)))
|
|
.sort((a, b) => a.year - b.year)
|
|
.map((row) => ({
|
|
year: row.year,
|
|
value: Number(row.value),
|
|
evidenceId: row.evidence_id || null,
|
|
}));
|
|
|
|
if (!observed.length) return null;
|
|
|
|
const values = observed.map((item) => item.value);
|
|
const yMin = Math.max(0, Math.floor(Math.min(...values) - 4));
|
|
const yMax = Math.ceil(Math.max(...values) + 3);
|
|
const plotW = Math.max(24, width - padLeft - padRight);
|
|
const plotH = Math.max(24, height - padTop - padBottom);
|
|
const yBottom = padTop + plotH;
|
|
|
|
const points = observed.map((item, idx) => {
|
|
const x = observed.length === 1
|
|
? padLeft + plotW / 2
|
|
: padLeft + (idx / (observed.length - 1)) * plotW;
|
|
const y = padTop + (1 - (item.value - yMin) / Math.max(1, yMax - yMin)) * plotH;
|
|
return {
|
|
...item,
|
|
x: Number(x.toFixed(1)),
|
|
y: Number(y.toFixed(1)),
|
|
};
|
|
});
|
|
|
|
const linePath = points
|
|
.map((pt, idx) => `${idx === 0 ? "M" : "L"}${pt.x.toFixed(1)},${pt.y.toFixed(1)}`)
|
|
.join(" ");
|
|
const areaPath = points.length >= 2
|
|
? `${linePath} L${points.at(-1).x.toFixed(1)},${yBottom.toFixed(1)} L${points[0].x.toFixed(1)},${yBottom.toFixed(1)} Z`
|
|
: "";
|
|
|
|
const computedDelta = points.length >= 2
|
|
? Number((points.at(-1).value - points[0].value).toFixed(2))
|
|
: null;
|
|
|
|
return {
|
|
width,
|
|
height,
|
|
padLeft,
|
|
padTop,
|
|
yBottom,
|
|
yMin,
|
|
yMax,
|
|
points,
|
|
linePath,
|
|
areaPath,
|
|
delta: Number.isFinite(Number(innovationSummary?.delta))
|
|
? Number(innovationSummary.delta)
|
|
: computedDelta,
|
|
};
|
|
}
|