forked from linkong/planet
68 lines
2.4 KiB
JavaScript
68 lines
2.4 KiB
JavaScript
export const DEFAULT_COMPUTE_PARTICIPANTS = ["CN", "US", "EUROPE", "APAC", "OTHER"];
|
|
|
|
const kindOf = (entry) => entry?.kind === "region" || entry?.kind === "group"
|
|
? "region"
|
|
: "country";
|
|
|
|
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: current.value - previous.value,
|
|
unit: mode === "absolute" ? "EFlop/s (FP32)" : "percentage_points",
|
|
};
|
|
}
|
|
|
|
export function computeShareMeterValue(value, mode) {
|
|
const numericValue = Number(value);
|
|
if (
|
|
!["global_share", "bilateral_share"].includes(mode) ||
|
|
value === null ||
|
|
value === "" ||
|
|
!Number.isFinite(numericValue) ||
|
|
numericValue < 0 ||
|
|
numericValue > 100
|
|
) {
|
|
return null;
|
|
}
|
|
return numericValue;
|
|
}
|