260225_01_ENH: 실제 Data 통계 API와 연결 및 일부 페이지 축소
This commit is contained in:
@@ -1,7 +1,21 @@
|
||||
import {BannerGroupKey, ServerBannerRow} from "@/types/lg";
|
||||
import {
|
||||
BannerGroupKey,
|
||||
BannerResponse,
|
||||
BannerRow, BuildPageModeConfigResult,
|
||||
ProductResponse, ProductRow,
|
||||
StatisticsRequest
|
||||
} from "@/types/lg";
|
||||
import {ChartData} from "@/components/lge-dashboard";
|
||||
import {BANNER_GROUP, GAP_WORDING} from "@/const/lgConst";
|
||||
import {
|
||||
BANNER_GROUP,
|
||||
BANNER_KEYS,
|
||||
CATEGORY_PAGE_FIELDS,
|
||||
GAP_WORDING,
|
||||
PRODUCT_KEYS
|
||||
} from "@/const/lgConst";
|
||||
import {formatNumber} from "@/lib/utils";
|
||||
import {nextApi} from "@/lib/http-client";
|
||||
import {CategoryFilterValue} from "@/components/layout/CategoryFilter";
|
||||
|
||||
export const isSampleRow = (raw: string) => raw.replace(/\s+/g, "").includes("사례수");
|
||||
|
||||
@@ -43,6 +57,7 @@ export function formatRowLabel(
|
||||
if (convertSample && t === "사례수") return "(사례수)";
|
||||
if (convertGap && t.toLowerCase() === "gap") return GAP_WORDING;
|
||||
if (convertCagr && t.toUpperCase() === "CAGR") return "CAGR(%p)";
|
||||
if (convertYear && /^\d{4}$/.test(t)) return `${t.substring(2,4)}년`;
|
||||
if (convertYear && /^\d{2}$/.test(t)) return `${t}년`;
|
||||
if (convertRate && t.endsWith("율")) return `${t.replaceAll('(%)','')}(%)`;
|
||||
if (convertCount && t.includes("대수")) return `${t.replaceAll('(대)','')}(대)`;
|
||||
@@ -183,7 +198,7 @@ type Opts = {
|
||||
splitStackByYear?: boolean;
|
||||
};
|
||||
|
||||
export function buildYearSeriesChartData(rows: ServerBannerRow[], opts: Opts = {}): ChartData {
|
||||
export function buildYearSeriesChartData(rows: BannerRow[], opts: Opts = {}): ChartData {
|
||||
const {
|
||||
excludeRowLabels = ["사례수", "gap"],
|
||||
yearLabel = (y) => `${y}년`,
|
||||
@@ -225,7 +240,7 @@ export function buildYearSeriesChartData(rows: ServerBannerRow[], opts: Opts = {
|
||||
}
|
||||
|
||||
export function buildRowLabelSeriesChartData(
|
||||
rows: ServerBannerRow[],
|
||||
rows: BannerRow[],
|
||||
opts: Opts = {}
|
||||
): ChartData {
|
||||
const {
|
||||
@@ -268,24 +283,24 @@ export function buildRowLabelSeriesChartData(
|
||||
}
|
||||
|
||||
export const buildGapRow = (
|
||||
rows: ServerBannerRow[],
|
||||
rows: BannerRow[],
|
||||
prevYear: number,
|
||||
curYear: number,
|
||||
baseRowLabel: string,
|
||||
gapRowLabel = "gap"
|
||||
): ServerBannerRow | null => {
|
||||
): BannerRow | null => {
|
||||
const prev = rows.find(r => Number(r.year) === prevYear && r.rowLabel === baseRowLabel);
|
||||
const cur = rows.find(r => Number(r.year) === curYear && r.rowLabel === baseRowLabel);
|
||||
if (!prev || !cur) return null;
|
||||
|
||||
const gap: ServerBannerRow = {
|
||||
const gap: BannerRow = {
|
||||
year: String(curYear),
|
||||
rowLabel: gapRowLabel,
|
||||
total: String(Number(cur.total) - Number(prev.total)),
|
||||
};
|
||||
|
||||
// cur가 가진 모든 key를 돌면서 "숫자 필드"면 diff 계산
|
||||
(Object.keys(cur) as Array<keyof ServerBannerRow>).forEach((k) => {
|
||||
(Object.keys(cur) as Array<keyof BannerRow>).forEach((k) => {
|
||||
if (k === "year" || k === "rowLabel") return;
|
||||
|
||||
const vCur = cur[k];
|
||||
@@ -297,4 +312,207 @@ export const buildGapRow = (
|
||||
return gap;
|
||||
}
|
||||
|
||||
export const isBannerGroupKey = (v: string): v is BannerGroupKey => v in BANNER_GROUP;
|
||||
export const isBannerGroupKey = (v: string): v is BannerGroupKey => v in BANNER_GROUP;
|
||||
|
||||
export const fetchProductStatistics = (req: StatisticsRequest) =>
|
||||
nextApi.post<ProductResponse>("/statistics/product", req);
|
||||
|
||||
export const fetchBannerStatistics = (req: StatisticsRequest) =>
|
||||
nextApi.post<BannerResponse>("/statistics/banner", req);
|
||||
|
||||
type FilterInput =
|
||||
| Record<string, unknown>
|
||||
| Map<string, unknown>
|
||||
| null
|
||||
| undefined;
|
||||
|
||||
export function normalizeFilterCondition(
|
||||
input: FilterInput,
|
||||
upToZero: boolean = true
|
||||
): Record<string, number[]> | undefined {
|
||||
if (!input) return undefined;
|
||||
|
||||
const entries =
|
||||
input instanceof Map ? Array.from(input.entries()) : Object.entries(input);
|
||||
|
||||
const out: Record<string, number[]> = {};
|
||||
|
||||
for (const [k, v] of entries) {
|
||||
if (!k || v == null) continue;
|
||||
|
||||
const nums: number[] = [];
|
||||
|
||||
const pushNum = (x: unknown) => {
|
||||
if (x == null) return;
|
||||
|
||||
const pushIfValid = (n: number) => {
|
||||
if (!Number.isFinite(n)) return;
|
||||
if (upToZero && n === 0) return; // upToZero=true일 때만 0 제외
|
||||
nums.push(n);
|
||||
};
|
||||
|
||||
if (typeof x === "number") {
|
||||
pushIfValid(x);
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof x === "string") {
|
||||
const parts = x
|
||||
.split(/[,\|]/g)
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
for (const p of parts) {
|
||||
pushIfValid(Number(p));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (Array.isArray(v)) {
|
||||
for (const item of v) pushNum(item);
|
||||
} else {
|
||||
pushNum(v);
|
||||
}
|
||||
|
||||
if (nums.length > 0) out[k] = nums;
|
||||
}
|
||||
|
||||
return Object.keys(out).length ? out : undefined;
|
||||
}
|
||||
|
||||
export const buildCategoryFilter = (
|
||||
filterValue:CategoryFilterValue
|
||||
): Record<string, number[]> | undefined => {
|
||||
const out: Record<string, number[]> = {};
|
||||
const productCode = filterValue.product.split("#").at(-1);
|
||||
|
||||
if (productCode) {
|
||||
out["product"] = [Number(productCode)];
|
||||
}
|
||||
|
||||
return Object.keys(out).length ? out : undefined;
|
||||
}
|
||||
|
||||
export const calcGapProductRow = (after: ProductRow, before: ProductRow): ProductRow => {
|
||||
const out: ProductRow = {
|
||||
year: after?.year ?? "",
|
||||
rowLabel: "gap",
|
||||
total: String(Number(after?.total ?? "0") - Number(before?.total ?? "0")),
|
||||
};
|
||||
for (const k of PRODUCT_KEYS) {
|
||||
const a = after? after[k] ?? "0" : "0";
|
||||
const b = before? before[k] ?? "0" : "0";
|
||||
out[k] = String(Number(a) - Number(b));
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
export const calcGapBannerRow = (after: BannerRow, before: BannerRow): BannerRow => {
|
||||
const out: BannerRow = {
|
||||
year: after?.year ?? "",
|
||||
rowLabel: "gap",
|
||||
total: String(Number(after?.total ?? "0") - Number(before?.total ?? "0")),
|
||||
};
|
||||
for (const k of BANNER_KEYS) {
|
||||
const a = after? after[k] ?? "0" : "0";
|
||||
const b = before? before[k] ?? "0" : "0";
|
||||
out[k] = String(Number(a) - Number(b));
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
const round1 = (n: number) => Math.round(n * 10) / 10;
|
||||
|
||||
export const calcCagrRow = (after: ProductRow, before: ProductRow): ProductRow => {
|
||||
const years = 2; // 24 -> 26
|
||||
const out: ProductRow = {
|
||||
year: after?.year ?? "",
|
||||
rowLabel: "CAGR",
|
||||
total: "0"
|
||||
};
|
||||
|
||||
// total은 참고용으로만: (26/24) CAGR %
|
||||
out.total =
|
||||
Number(before?.total ?? "0") > 0 ? String(round1((Math.pow(Number(after?.total ?? "0") / Number(before?.total ?? "0"), 1 / years) - 1) * 100)) : "0";
|
||||
|
||||
for (const k of PRODUCT_KEYS) {
|
||||
const vBefore = before? before[k] ?? "0" : "0";
|
||||
const vAfter = after? after[k] ?? "0" : "0";
|
||||
out[k] = Number(vBefore) > 0 ? String(round1((Math.pow(Number(vAfter) / Number(vBefore), 1 / years) - 1) * 100)) : "0";
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
|
||||
export const makeTotalBasePct = (t:number|string, f:number|string):number => {
|
||||
return Number(t) > 0 ? Math.round((Number(f) / Number(t)) * 100) : 0;
|
||||
}
|
||||
|
||||
export const calcTotalBaseRow = (label:string, totalRow: ProductRow, targetRow: ProductRow): ProductRow => {
|
||||
const out: ProductRow = {
|
||||
year: targetRow?.year ?? "",
|
||||
rowLabel: `전체 ${label} 중 자사제품 비율`,
|
||||
total: String(makeTotalBasePct(totalRow.total, targetRow.total))
|
||||
};
|
||||
|
||||
for (const k of PRODUCT_KEYS) {
|
||||
const t = totalRow? totalRow[k] ?? "0" : "0";
|
||||
const f = targetRow? targetRow[k] ?? "0" : "0";
|
||||
out[k] = String(makeTotalBasePct(t, f));
|
||||
}
|
||||
return out;
|
||||
|
||||
}
|
||||
|
||||
export const withGapRow = <T>(
|
||||
rows: T[],
|
||||
calcGapRow: (last: T, secondLast: T) => T
|
||||
): T[] => {
|
||||
const last = rows.at(-1);
|
||||
const secondLast = rows.at(-2);
|
||||
if (!last || !secondLast) return rows;
|
||||
return [calcGapRow(last, secondLast), ...rows];
|
||||
}
|
||||
|
||||
export const withCagrRow = <T>(
|
||||
rows: T[],
|
||||
calcCagrRow: (last: T, first: T) => T
|
||||
): T[] => {
|
||||
const first = rows.at(0);
|
||||
const last = rows.at(-1);
|
||||
if (!first || !last) return rows;
|
||||
return [...rows, calcCagrRow(last, first)];
|
||||
}
|
||||
|
||||
export const withTotalBaseRow = <T>(
|
||||
label:string,
|
||||
totalRows: T[],
|
||||
targetRows: T[],
|
||||
calcTotalBaseRow: (label:string, total: T, target: T) => T
|
||||
): T[] => {
|
||||
const totalRow = totalRows.at(-1);
|
||||
const targetRow = targetRows.at(-1);
|
||||
|
||||
if (!totalRow || !targetRow) return targetRows;
|
||||
|
||||
const totalBaseRow = calcTotalBaseRow(label, totalRow, targetRow);
|
||||
return targetRows.length === 0 ? [totalBaseRow] : [targetRows[0], totalBaseRow, ...targetRows.slice(1)];
|
||||
}
|
||||
|
||||
const renderKey = (tpl: string, keyBase: string) => tpl.replaceAll("${keyBase}", keyBase.toLowerCase());
|
||||
|
||||
export const buildPageModeConfig = (
|
||||
filterValue: CategoryFilterValue
|
||||
):BuildPageModeConfigResult => {
|
||||
const fields = (CATEGORY_PAGE_FIELDS[filterValue.target.value] ?? [])
|
||||
.filter((f) => !!f.qidByMode[filterValue.type.value])
|
||||
.map((f) => ({
|
||||
...f,
|
||||
requestKeyResolved: renderKey(f.requestKey, filterValue.type.value),
|
||||
qid: f.qidByMode[filterValue.type.value]!,
|
||||
}));
|
||||
|
||||
const variableIdMap = Object.fromEntries(fields.map((f) => [f.requestKeyResolved, f.qid])) as Record<string, string>;
|
||||
|
||||
return { fields, variableIdMap };
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
// exportDashboardExcel.ts
|
||||
"use client";
|
||||
|
||||
import {formatRowLabel, isSampleRow} from "@/lib/lg-dashboard";
|
||||
import {formatRowLabel} from "@/lib/lg-dashboard";
|
||||
import {DataColumn} from "@/components/lge-dashboard";
|
||||
|
||||
/**
|
||||
@@ -269,8 +269,7 @@ function fitColumns(ws: any, mergeMap: Map<string, MergeInfo>, o: Required<AutoF
|
||||
});
|
||||
|
||||
for (let c = 1; c <= colCount; c++) {
|
||||
const w = clamp(widths[c] + o.padding, o.minColWidth, o.maxColWidth);
|
||||
ws.getColumn(c).width = w;
|
||||
ws.getColumn(c).width = clamp(widths[c] + o.padding, o.minColWidth, o.maxColWidth);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -768,7 +767,7 @@ export async function exportDashboardExcel<T extends Record<string, any>>(opts:
|
||||
// 데이터
|
||||
for (const row of rows) {
|
||||
const excelRow = ws.getRow(rowCursor);
|
||||
const sampleRow = isSampleRow(valueGetter(row, header.leafKeys.at(0) ?? ""));
|
||||
//const sampleRow = isSampleRow(valueGetter(row, header.leafKeys.at(0) ?? ""));
|
||||
|
||||
header.leafKeys.forEach((key, idx) => {
|
||||
const cell = excelRow.getCell(idx + 1);
|
||||
@@ -778,7 +777,7 @@ export async function exportDashboardExcel<T extends Record<string, any>>(opts:
|
||||
if (v === null || v === undefined || v === "") cell.value = null;
|
||||
else if (typeof v === "number") cell.value = v;
|
||||
else if (isColHeader) cell.value = formatRowLabel(String(v));
|
||||
else if (sampleRow) cell.value = `(${String(v)})`;
|
||||
//else if (sampleRow) cell.value = `(${String(v)})`;
|
||||
else cell.value = String(v);
|
||||
|
||||
const align =
|
||||
|
||||
@@ -145,4 +145,16 @@ export function parseNumber(
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const toNumOrNull = (v: unknown) => {
|
||||
if (v == null) return null;
|
||||
if (v === "-" || v === "") return null;
|
||||
const n = Number(v);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
};
|
||||
|
||||
export const toNumOr0 = (v: unknown) => {
|
||||
const n = Number(v);
|
||||
return Number.isFinite(n) ? n : 0;
|
||||
};
|
||||
Reference in New Issue
Block a user