-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] : 차트 관련 요청 시, 최신화 후 반환하는 기능 추가
- Loading branch information
Showing
10 changed files
with
158 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
backend/src/main/java/org/dgu/backend/service/CandleInfoUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.dgu.backend.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.dgu.backend.domain.Candle; | ||
import org.dgu.backend.domain.CandleInfo; | ||
import org.dgu.backend.domain.Market; | ||
import org.dgu.backend.repository.CandleInfoRepository; | ||
import org.dgu.backend.repository.CandleRepository; | ||
import org.dgu.backend.repository.MarketRepository; | ||
import org.dgu.backend.util.CandleDataCollector; | ||
import org.dgu.backend.util.CandleUtil; | ||
import org.dgu.backend.util.DateUtil; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Objects; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CandleInfoUpdater { | ||
private final MarketRepository marketRepository; | ||
private final CandleRepository candleRepository; | ||
private final CandleInfoRepository candleInfoRepository; | ||
private final CandleDataCollector candleDataCollector; | ||
private final CandleUtil candleUtil; | ||
private final DateUtil dateUtil; | ||
|
||
// 현재 시각을 기준으로 캔들 정보를 최신화하는 메서드 | ||
public void ensureCandleInfoUpToDate(String koreanName, String candleName) { | ||
Market market = marketRepository.findByKoreanName(koreanName); | ||
Candle candle = candleRepository.findByCandleName(candleName); | ||
|
||
// 가장 최근 캔들 차트 | ||
CandleInfo latestCandleInfo = candleInfoRepository.findTopByMarketAndCandleOrderByTimestampDesc(market, candle); | ||
int candleInterval = candleUtil.calculateCandleInterval(candleName); | ||
LocalDateTime startDate; | ||
if (Objects.isNull(latestCandleInfo)) { | ||
startDate = dateUtil.convertToLocalDateTime("2019-01-01T00:00:00"); | ||
} else { | ||
startDate = latestCandleInfo.getDateTime(); | ||
if (startDate.plusMinutes(candleInterval).isAfter(LocalDateTime.now())) { | ||
return; | ||
} | ||
} | ||
|
||
candleDataCollector.collectCandleData(koreanName, candleName, startDate, LocalDateTime.now()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
backend/src/main/java/org/dgu/backend/util/CandleUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.dgu.backend.util; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.dgu.backend.domain.CandleInfo; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CandleUtil { | ||
private static final List<String> SEVEN_DAY_CANDLES = Arrays.asList("minutes1", "minutes3", "minutes5", "minutes10", "minutes15", "minutes30"); | ||
private static final List<String> SIX_MONTH_CANDLES = Arrays.asList("minutes60", "minutes240"); | ||
|
||
// 캔들을 분 기준으로 변환하는 메서드 | ||
public int calculateCandleInterval(String candleName) { | ||
switch (candleName) { | ||
case "days": | ||
return 1440; // 1일(24시간 * 60분) | ||
case "weeks": | ||
return 10080; // 1주(7일 * 24시간 * 60분) | ||
case "months": | ||
return 43200; // 1개월(30일 * 24시간 * 60분) | ||
default: // minutesN 형식의 캔들 타입 처리 | ||
return Integer.parseInt(candleName.replace("minutes", "")); | ||
} | ||
} | ||
|
||
// 캔들 종류에 따라 시작 기간을 계산해 반환하는 메서드 | ||
public LocalDateTime getStartDateByCandleName(String candleName) { | ||
LocalDateTime now = LocalDateTime.now(); | ||
if (SEVEN_DAY_CANDLES.contains(candleName)) { | ||
return now.minusDays(7); | ||
} else if (SIX_MONTH_CANDLES.contains(candleName)) { | ||
return now.minusMonths(6); | ||
} else { | ||
return LocalDateTime.of(2019, 1, 1, 0, 0); | ||
} | ||
} | ||
|
||
// 캔들 차트에서 중복 데이터를 제거하는 메서드 | ||
public List<CandleInfo> removeDuplicatedCandles(List<CandleInfo> candles) { | ||
Set<LocalDateTime> uniqueDates = new HashSet<>(); | ||
return candles.stream() | ||
.filter(candle -> uniqueDates.add(candle.getDateTime())) | ||
.collect(Collectors.toList()); | ||
} | ||
} |