260128_03_ENH: 샘플데이터 랜덤 적용, lg 보급률 하단 카드 + 게이지 테이블 적용

This commit is contained in:
2026-01-28 17:01:40 +09:00
parent 4c00cfa946
commit d7cb9259cd
12 changed files with 395 additions and 134 deletions

View File

@@ -9,17 +9,42 @@ import {PRODUCT_KEY_MAP} from "@/components/lge-dashboard";
const PRODUCT_KEYS = Object.keys(PRODUCT_KEY_MAP) as ProductKey[];
const randInt = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1)) + min;
const DUMMY_SEED = Number(process.env.NEXT_PUBLIC_DUMMY_SEED ?? 12345);
// 문자열 → 32bit 해시
const hash32 = (s: string) => {
let h = 2166136261;
for (let i = 0; i < s.length; i++) {
h ^= s.charCodeAt(i);
h = Math.imul(h, 16777619);
}
return h >>> 0;
};
// 결정적 PRNG
const mulberry32 = (a: number) => {
return () => {
let t = (a += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
};
const randInt = (r: () => number, min: number, max: number) =>
Math.floor(r() * (max - min + 1)) + min;
type RandRange = { min: number | null; max: number | null };
const makeProductDist = (range?: RandRange): Record<ProductKey, number> => {
const makeProductDist = (seedKey: string, range?: RandRange): Record<ProductKey, number> => {
const min = range?.min ?? 1;
const max = range?.max ?? 60;
const seed = (hash32(seedKey) ^ DUMMY_SEED) >>> 0;
const r = mulberry32(seed);
const out = {} as Record<ProductKey, number>;
for (const k of PRODUCT_KEYS) out[k] = randInt(min, max);
for (const k of PRODUCT_KEYS) out[k] = randInt(r, min, max);
return out;
};
@@ -33,7 +58,7 @@ const productRow = (
year,
rowLabel,
total,
...makeProductDist(range),
...makeProductDist(`${year}|${rowLabel}|${range.min ?? ""}|${range.max ?? ""}`, range),
...(extra ?? {}),
});
@@ -69,71 +94,119 @@ const calcCagrRow = (row24: ServerProductRow, row26: ServerProductRow): ServerPr
return out;
};
// 배너(41키) 그룹별 합이 100 되도록 고정 분포
const BANNER_DIST_DEFAULT: Record<BannerKey, number> = {
// 라이프스테이지(6)
single: 20,
noKids: 20,
preschoolKids: 20,
schoolKids: 20,
adultKids: 10,
independentSenior: 10,
const splitTo100 = (r: () => number, n: number, minEach: number, maxEach: number): number[] => {
// 불가능한 범위 방어
const minSum = n * minEach;
const maxSum = n * maxEach;
if (minSum > 100) throw new Error(`splitTo100 invalid: minEach too large (n=${n}, minEach=${minEach})`);
if (maxSum < 100) throw new Error(`splitTo100 invalid: maxEach too small (n=${n}, maxEach=${maxEach})`);
// 가구원수(4)
hh1: 25,
hh2: 35,
hh3: 20,
hh4Plus: 20,
// 1) 최소값으로 깔기
const out = Array(n).fill(minEach);
const remain = 100 - minSum;
// 연령(5)
age20s: 15,
age30s: 25,
age40s: 30,
age50s: 20,
age60s: 10,
// 2) 각 슬롯이 추가로 받을 수 있는 상한
const caps = Array(n).fill(maxEach - minEach);
// 점유형태(5)
ownerOccupied: 50,
jeonse: 25,
depositAndMonthly: 10,
monthlyNoDeposit: 10,
otherTenure: 5,
// 3) remain을 cap 내에서 랜덤 분배
// (가중치 기반으로 일단 분배 → cap 초과를 다시 재분배)
const w = Array.from({ length: n }, () => randInt(r, 1, 100));
const wSum = w.reduce((a, b) => a + b, 0);
// 주거형태(6)
apartment: 60,
house: 15,
officetel: 10,
villa: 10,
studio: 3,
otherHousingType: 2,
const add = w.map((wi) => Math.floor((wi / wSum) * remain));
const used = add.reduce((a, b) => a + b, 0);
const left = remain - used;
// 평형(5)
under19Pyeong: 10,
pyeong20s: 20,
pyeong30s: 35,
pyeong40s: 20,
pyeong50Plus: 15,
// 남은 left를 가중치 큰 순으로 1씩
const order = w
.map((wi, i) => ({ wi, i }))
.sort((a, b) => b.wi - a.wi)
.map((x) => x.i);
// 권역(6)
regionCapital: 45,
regionGyeongnamBusanUlsan: 15,
regionGyeongbukDaegu: 10,
regionJeollaGwangju: 10,
regionChungcheongDaejeonSejong: 10,
regionGangwonJeju: 10,
for (let k = 0; k < left; k++) add[order[k % n]] += 1;
// 소득(4)
incomeUnder299: 20,
income300to599: 40,
income600to899: 25,
income900Plus: 15,
// cap 적용(초과분 회수)
let overflow = 0;
for (let i = 0; i < n; i++) {
if (add[i] > caps[i]) {
overflow += add[i] - caps[i];
add[i] = caps[i];
}
}
// 회수한 overflow를 cap 남은 곳에 다시 채우기(랜덤)
while (overflow > 0) {
const candidates = [];
for (let i = 0; i < n; i++) {
const room = caps[i] - add[i];
if (room > 0) candidates.push({ i, room });
}
if (candidates.length === 0) break;
const pick = candidates[randInt(r, 0, candidates.length - 1)];
add[pick.i] += 1;
overflow -= 1;
}
// 최종 합 100 보장
for (let i = 0; i < n; i++) out[i] += add[i];
const sum = out.reduce((a, b) => a + b, 0);
if (sum !== 100) {
// 마지막 보정(이론상 거의 안 탐)
out[n - 1] += 100 - sum;
}
return out;
};
const bannerRow = (year: number, rowLabel: string, total: number, extra?: Record<string, number>): ServerBannerRow => ({
/** ✅ 그룹 키 정의(각 그룹 합 100) */
const BANNER_GROUPS: Record<string, BannerKey[]> = {
lifeStage: ["single", "noKids", "preschoolKids", "schoolKids", "adultKids", "independentSenior"],
household: ["hh1", "hh2", "hh3", "hh4Plus"],
age: ["age20s", "age30s", "age40s", "age50s", "age60s"],
tenure: ["ownerOccupied", "jeonse", "depositAndMonthly", "monthlyNoDeposit", "otherTenure"],
housing: ["apartment", "house", "officetel", "villa", "studio", "otherHousingType"],
pyeong: ["under19Pyeong", "pyeong20s", "pyeong30s", "pyeong40s", "pyeong50Plus"],
region: [
"regionCapital",
"regionGyeongnamBusanUlsan",
"regionGyeongbukDaegu",
"regionJeollaGwangju",
"regionChungcheongDaejeonSejong",
"regionGangwonJeju",
],
income: ["incomeUnder299", "income300to599", "income600to899", "income900Plus"],
};
/** ✅ 기존 상수 대신: 호출할 때마다 “결정적 랜덤” 분포 생성 */
export const makeBannerDistDefault = (seedKey: string, range?: RandRange): Record<BannerKey, number> => {
const seed = (hash32(seedKey) ^ DUMMY_SEED) >>> 0;
const r = mulberry32(seed);
const minEach = range?.min ?? 1;
const maxEach = range?.max ?? 60;
const out = {} as Record<BannerKey, number>;
(Object.keys(BANNER_GROUPS) as Array<keyof typeof BANNER_GROUPS>).forEach((groupName) => {
const keys = BANNER_GROUPS[groupName];
const parts = splitTo100(r, keys.length, minEach, maxEach); // ✅ min/max 적용
for (let i = 0; i < keys.length; i++) out[keys[i]] = parts[i];
});
return out;
};
export const bannerRow = (
year: number,
rowLabel: string,
total: number,
range: RandRange = { min: null, max: null },
extra?: Partial<Record<BannerKey, number>>,
): ServerBannerRow => ({
year,
rowLabel,
total,
...BANNER_DIST_DEFAULT,
...makeBannerDistDefault(`${year}|${rowLabel}`, range), // ✅ min/max 적용
...(extra ?? {}),
});
@@ -197,21 +270,36 @@ export const apiOwnShareOfTotalPurchase: ServerProductRow[] = [
];
export const apiLifeStageByProduct: ServerBannerRow[] = [
bannerRow(26, "냉장고", 1200, { single: 18, noKids: 22, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "세탁기", 1100, { single: 18, noKids: 22, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "에어컨", 900, { single: 22, noKids: 18, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "TV", 950, { single: 18, noKids: 22, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "청소기", 1000, { single: 22, noKids: 18, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "김치 냉장고", 700, { single: 22, noKids: 18, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "공기 청정기", 800, { single: 22, noKids: 18, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "가습기", 650, { single: 22, noKids: 18, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "제습기", 600, { single: 18, noKids: 22, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "건조기", 500, { single: 18, noKids: 22, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "식기세척기", 450, { single: 22, noKids: 18, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "의류 관리기", 320, { single: 18, noKids: 22, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "로봇 청소기", 420, { single: 22, noKids: 18, preschoolKids: 20, schoolKids: 20, adultKids: 10, independentSenior: 10 }),
bannerRow(26, "냉장고", 1200),
bannerRow(26, "세탁기", 1100),
bannerRow(26, "에어컨", 900),
bannerRow(26, "TV", 950),
bannerRow(26, "청소기", 1000),
bannerRow(26, "김치 냉장고", 700),
bannerRow(26, "공기 청정기", 800),
bannerRow(26, "가습기", 650),
bannerRow(26, "제습기", 600),
bannerRow(26, "건조기", 500),
bannerRow(26, "식기세척기", 450),
bannerRow(26, "의류 관리기", 320),
bannerRow(26, "로봇 청소기", 420),
];
export const apiLifeStageByProductForGauge: ServerBannerRow[] = [
bannerRow(26, "냉장고", 1200, {min:10, max:99}),
bannerRow(26, "세탁기", 1100, {min:10, max:99}),
bannerRow(26, "에어컨", 900, {min:10, max:99}),
bannerRow(26, "TV", 950, {min:10, max:99}),
bannerRow(26, "청소기", 1000, {min:10, max:99}),
bannerRow(26, "김치 냉장고", 700, {min:10, max:99}),
bannerRow(26, "공기 청정기", 800, {min:10, max:99}),
bannerRow(26, "가습기", 650, {min:10, max:99}),
bannerRow(26, "제습기", 600, {min:10, max:99}),
bannerRow(26, "건조기", 500, {min:10, max:99}),
bannerRow(26, "식기세척기", 450, {min:10, max:99}),
bannerRow(26, "의류 관리기", 320, {min:10, max:99}),
bannerRow(26, "로봇 청소기", 420, {min:10, max:99}),
];
/** ---------------------------
* API #6
* year rowLabel total banner