Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
public class IndexDataScheduler {
private final IndexDataService indexDataService;

@Scheduled(cron = "0 0/5 9-16 * * MON-FRI", zone = "Asia/Seoul") // todo: 공휴일/휴장일 스케쥴러 처리 필요.
@Scheduled(cron = "0 0/5 9-18 * * MON-FRI", zone = "Asia/Seoul") // todo: 공휴일/휴장일 스케쥴러 처리 필요.
public void updateKospi() {
indexDataService.updateCurrentIndexData(MarketType.KOSPI);
log.info("[Scheduler] 코스피 데이터 업데이트 완료");
}

@Scheduled(cron = "30 2/5 9-16 * * MON-FRI", zone = "Asia/Seoul") // todo: 공휴일/휴장일 스케쥴러 처리 필요.
@Scheduled(cron = "0 0/5 9-18 * * MON-FRI", zone = "Asia/Seoul") // todo: 공휴일/휴장일 스케쥴러 처리 필요.
public void updateKosdaq() {
indexDataService.updateCurrentIndexData(MarketType.KOSPI);
log.info("[Scheduler] 코스닥 데이터 업데이트 완료");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
public class StockScheduler {
private final StockService stockService;

@Scheduled(cron = "0 50 23 * * MON-FRI", zone = "Asia/Seoul") // todo: 공휴일/휴장일 스케쥴러 처리 필요.
@Scheduled(cron = "5 0 18 * * MON-FRI", zone = "Asia/Seoul") // todo: 공휴일/휴장일 스케쥴러 처리 필요.
public void saveDailyStockData() {
stockService.saveDailyStockData();
log.info("[Scheduler] 일별 주가 데이터 업데이트 완료");
}

@Scheduled(cron = "0 0 22 * * MON-FRI", zone = "Asia/Seoul") // todo: 공휴일/휴장일 스케쥴러 처리 필요.
@Scheduled(cron = "0/5 0 9-18 * * MON-FRI", zone = "Asia/Seoul") // todo: 공휴일/휴장일 스케쥴러 처리 필요.
public void updateStockData() {
stockService.updateCurrentStockData();
log.info("[Scheduler] 주가 데이터 업데이트 완료");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.stockport.server.global.utils.KisParsingUtils;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -30,6 +31,7 @@ public class IndexDataServiceImpl implements IndexDataService {
private final KisIndexPriceAdaptor kisIndexPriceAdaptor;

@Override
@Transactional
public void updateCurrentIndexData(MarketType marketType) {
KisIndexCurrentPrice indexCurrentPrice = kisIndexPriceAdaptor.getIndexCurrentPrice(marketType.getCode());
IndexData currentIndexData = indexCurrentPrice.toEntity(marketType, LocalDate.now());
Expand All @@ -41,6 +43,7 @@ public void updateCurrentIndexData(MarketType marketType) {
}

@Override
@Transactional
public void updateHistoricalIndexData(MarketType marketType) {
LocalDate endDate = LocalDate.now();
LocalDate startDate = endDate.minusYears(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import com.stockport.server.global.apipayload.code.status.ErrorStatus;
import com.stockport.server.global.exception.GeneralException;
import com.stockport.server.global.feign.adaptor.KisStockPriceAdaptor;
import com.stockport.server.global.feign.dto.KisMultieStockCurrentPrice;
import com.stockport.server.global.feign.dto.KisStockCurrentPrice;
import com.stockport.server.global.feign.dto.KisStockPeriodPrice;
import com.stockport.server.global.feign.dto.wrapper.KisResponseWrapper;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
Expand Down Expand Up @@ -72,13 +74,22 @@ public List<StockQueryResponse> searchStocks(String query) {
}

@Override
@Transactional
public void updateCurrentStockData() {
List<Stock> stocks = stockRepository.findAll();
for (Stock stock : stocks) {
StockCurrentPrice newStockCurrentPrice = kisStockPriceAdaptor.getStockCurrentPrice(stock.getStockCd()).toEntity();
stock.updateCurrentPriceInfo(newStockCurrentPrice);
log.info("[stock] 현재 주가 데이터 업데이트 완료: {} 진행률 {}%", stock.getStockCd(), (stocks.indexOf(stock) + 1) * 100 / stocks.size());
for (int stockIdx = 0; stockIdx < stocks.size() / 30; stockIdx++) {
List<Stock> stockList = stocks.subList(stockIdx * 30, Math.min((stockIdx + 1) * 30, stocks.size()));
List<String> stockCdList = stockList.stream()
.map(Stock::getStockCd)
.toList();
List<StockCurrentPrice> stockCurrentPriceList = kisStockPriceAdaptor.getMultiStockCurrentPrice(stockCdList).getOutput().stream()
.map(currentPrice -> currentPrice.toEntity(LocalDate.now()))
.toList();

for (int i = 0; i < Math.min(30, stockList.size()); i++)
stockList.get(i).updateCurrentPriceInfo(stockCurrentPriceList.get(i));
}
log.info("[stock] 현재 주가 데이터 업데이트 완료");
}

@Override
Expand Down Expand Up @@ -130,6 +141,7 @@ public void updateHistoricalStockData() {
}

@Override
@Transactional
public void saveDailyStockData() {
List<Stock> stockList = stockRepository.findAll();
for (Stock stock : stockList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.stockport.server.domain.indexData.constant.MarketType;
import com.stockport.server.domain.indexData.entity.IndexData;
import com.stockport.server.domain.stock.entity.StockCurrentPrice;
import com.stockport.server.global.utils.KisParsingUtils;
import lombok.Getter;

Expand All @@ -28,15 +29,14 @@ public class KisMultieStockCurrentPrice {
@JsonProperty("prdy_ctrt")
private String changeRate; // 등락률

public IndexData toEntity(MarketType marketType, LocalDate baseDate) {
return IndexData.builder()
.marketType(marketType)
public StockCurrentPrice toEntity(LocalDate baseDate) {
return StockCurrentPrice.builder()
.baseDate(baseDate)
.closePrice(KisParsingUtils.parseDoubleSafe(currentPrice))
.openPrice(KisParsingUtils.parseDoubleSafe(openPrice))
.highPrice(KisParsingUtils.parseDoubleSafe(highPrice))
.lowPrice(KisParsingUtils.parseDoubleSafe(lowPrice))
.changeAmount(KisParsingUtils.parseDoubleSafe(changeAmount))
.currentPrice(KisParsingUtils.parseIntSafe(currentPrice))
.openPrice(KisParsingUtils.parseIntSafe(openPrice))
.highPrice(KisParsingUtils.parseIntSafe(highPrice))
.lowPrice(KisParsingUtils.parseIntSafe(lowPrice))
.changeAmount(KisParsingUtils.parseIntSafe(changeAmount))
.changeRate(KisParsingUtils.parseDoubleSafe(changeRate))
.build();
}
Expand Down
Loading