import axios from 'axios'; import { API_BASE_URL } from './apiConfig'; // 기간 정보 타입 export interface PeriodInfo { id: number; period: number; isDefault: boolean; enabled: boolean; displayOrder: number; } // 지표별 기간 응답 타입 export interface IndicatorPeriodsResponse { indicatorType: string; indicatorDisplayName: string; periods: PeriodInfo[]; defaultPeriod: number | null; } // 기간 요청 타입 export interface PeriodRequest { indicatorType: string; period: number; isDefault?: boolean; displayOrder?: number; enabled?: boolean; } // 기간 설정 DTO export interface IndicatorPeriodSettingDto { id: number; indicatorType: string; period: number; isDefault: boolean; displayOrder: number; enabled: boolean; } // 기간이 필요 없는 지표 목록 export const PERIOD_NOT_REQUIRED_INDICATORS = [ 'MACD', 'MACD_HISTOGRAM' ]; /** * 기간이 필요한 지표인지 확인 */ export const isPeriodRequired = (indicatorType: string): boolean => { return !PERIOD_NOT_REQUIRED_INDICATORS.includes(indicatorType); }; /** * 모든 지표의 기간 설정 조회 */ export const getAllIndicatorPeriods = async (): Promise => { const response = await axios.get(`${API_BASE_URL}/indicator-periods`); return response.data; }; /** * 특정 지표의 기간 설정 조회 */ export const getIndicatorPeriods = async (indicatorType: string): Promise => { const response = await axios.get(`${API_BASE_URL}/indicator-periods/${indicatorType}`); return response.data; }; /** * 특정 지표의 활성화된 기간 목록 조회 (드롭다운용) */ export const getEnabledPeriods = async (indicatorType: string): Promise => { const response = await axios.get(`${API_BASE_URL}/indicator-periods/${indicatorType}/enabled`); return response.data; }; /** * 모든 지표의 활성화된 기간 맵 조회 */ export const getAllEnabledPeriodsMap = async (): Promise> => { const response = await axios.get(`${API_BASE_URL}/indicator-periods/map`); return response.data; }; /** * 기간 추가 */ export const addPeriod = async (request: PeriodRequest): Promise => { const response = await axios.post(`${API_BASE_URL}/indicator-periods`, request); return response.data; }; /** * 기간 수정 */ export const updatePeriod = async (id: number, request: Partial): Promise => { const response = await axios.put(`${API_BASE_URL}/indicator-periods/${id}`, request); return response.data; }; /** * 기간 삭제 */ export const deletePeriod = async (id: number): Promise => { await axios.delete(`${API_BASE_URL}/indicator-periods/${id}`); }; /** * 지표 타입 표시명 가져오기 - 업비트 표준 16개 */ export const getIndicatorDisplayName = (indicatorType: string): string => { const displayNames: Record = { VOLUME: '거래량', MACD: 'MACD', STOCHASTIC: 'Stochastic Slow', RSI: 'RSI', ADX: 'ADX', CCI: 'CCI', TRIX: 'TRIX', DISPARITY: '이격도', NEW_PSYCHOLOGICAL: '신심리도', INVEST_PSYCHOLOGICAL: '투자심리도', WILLIAMS_R: 'Williams %R', BWI: '볼린저밴드 %B', OBV: 'OBV', VOLUME_OSC: 'VolumeOSC', VR: 'VR', DMI: 'DMI', }; return displayNames[indicatorType] || indicatorType; }; // 캐시된 기간 맵 (성능 최적화) let cachedPeriodsMap: Record | null = null; let cacheTimestamp: number = 0; const CACHE_DURATION = 5 * 60 * 1000; // 5분 /** * 캐시된 기간 맵 조회 (성능 최적화) */ export const getCachedPeriodsMap = async (): Promise> => { const now = Date.now(); if (cachedPeriodsMap && (now - cacheTimestamp) < CACHE_DURATION) { return cachedPeriodsMap; } cachedPeriodsMap = await getAllEnabledPeriodsMap(); cacheTimestamp = now; return cachedPeriodsMap; }; /** * 캐시 무효화 */ export const invalidatePeriodsCache = (): void => { cachedPeriodsMap = null; cacheTimestamp = 0; };