kcg-ai-monitoring/frontend/src/services/kpi.ts
htlee 4e6ac8645a feat: S5 프론트 실데이터 전환 — EventList/Statistics/EnforcementHistory/Dashboard KPI
이벤트 목록 (EventList):
- eventStore를 GET /api/events 호출로 전환
- 서버 필터링 (level/status/category), 페이지네이션
- 상태 배지 (NEW/ACK/IN_PROGRESS/RESOLVED/FALSE_POSITIVE)
- getEventStats() 기반 KPI 카드

단속 이력 (EnforcementHistory):
- 신규 services/enforcement.ts (GET/POST /enforcement/records, /plans)
- enforcementStore를 API 기반으로 전환
- KPI 카드 (총단속/처벌/AI일치/오탐) 클라이언트 계산

통계 (Statistics):
- kpi.ts를 GET /api/stats/kpi, /stats/monthly 실제 호출로 전환
- toMonthlyTrend/toViolationTypes 변환 헬퍼 추가
- BarChart/AreaChart 기존 구조 유지

대시보드 KPI:
- kpiStore를 API 기반으로 전환 (getKpiMetrics + getMonthlyStats)
- Dashboard KPI_UI_MAP에 kpiKey 기반 매핑 추가

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 12:14:53 +09:00

100 lines
3.1 KiB
TypeScript

/**
* KPI/통계 API 서비스
* - 실제 백엔드 API 호출 (GET /api/stats/kpi, /api/stats/monthly)
* - 하위 호환용 변환 헬퍼 제공
*/
import type { KpiMetric, MonthlyTrend, ViolationType } from '@data/mock/kpi';
const API_BASE = import.meta.env.VITE_API_URL ?? '/api';
// ─── 백엔드 API 응답 타입 ───────────────────
export interface PredictionKpi {
kpiKey: string;
kpiLabel: string;
value: number;
trend: string | null; // 'up', 'down', 'flat'
deltaPct: number | null;
updatedAt: string;
}
export interface PredictionStatsMonthly {
statMonth: string; // '2026-04-01' (DATE -> ISO string)
totalDetections: number;
totalEnforcements: number;
byCategory: Record<string, number> | null;
byZone: Record<string, number> | null;
byRiskLevel: Record<string, number> | null;
byGearType: Record<string, number> | null;
byViolationType: Record<string, number> | null;
eventCount: number;
criticalEventCount: number;
falsePositiveCount: number;
aiAccuracyPct: number | null;
}
// ─── API 호출 ───────────────────
export async function getKpiMetrics(): Promise<PredictionKpi[]> {
const res = await fetch(`${API_BASE}/stats/kpi`, { credentials: 'include' });
if (!res.ok) throw new Error(`API error: ${res.status}`);
return res.json();
}
export async function getMonthlyStats(
from: string,
to: string,
): Promise<PredictionStatsMonthly[]> {
const res = await fetch(`${API_BASE}/stats/monthly?from=${from}&to=${to}`, {
credentials: 'include',
});
if (!res.ok) throw new Error(`API error: ${res.status}`);
return res.json();
}
// ─── 하위 호환 변환 헬퍼 ───────────────────
/** PredictionKpi -> 기존 KpiMetric 형태로 변환 (Dashboard에서 사용) */
export function toKpiMetric(kpi: PredictionKpi): KpiMetric {
return {
id: kpi.kpiKey,
label: kpi.kpiLabel,
value: kpi.value,
prev: kpi.deltaPct
? Math.round(kpi.value / (1 + kpi.deltaPct / 100))
: undefined,
};
}
/** PredictionStatsMonthly -> MonthlyTrend 변환 */
export function toMonthlyTrend(stat: PredictionStatsMonthly): MonthlyTrend {
return {
month: stat.statMonth.substring(0, 7), // '2026-04-01' -> '2026-04'
enforce: stat.totalEnforcements ?? 0,
detect: stat.totalDetections ?? 0,
accuracy: stat.aiAccuracyPct ?? 0,
};
}
/** MonthlyStats의 byViolationType -> ViolationType[] 변환 (기간 합산) */
export function toViolationTypes(
stats: PredictionStatsMonthly[],
): ViolationType[] {
const totals: Record<string, number> = {};
stats.forEach((s) => {
if (s.byViolationType) {
Object.entries(s.byViolationType).forEach(([k, v]) => {
totals[k] = (totals[k] ?? 0) + (v as number);
});
}
});
const sum = Object.values(totals).reduce((a, b) => a + b, 0);
return Object.entries(totals)
.map(([type, count]) => ({
type,
count,
pct: sum > 0 ? Math.round((count / sum) * 100) : 0,
}))
.sort((a, b) => b.count - a.count);
}