Skip to content

Commit

Permalink
♻️ refactor: 패키지 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn committed Jul 9, 2024
1 parent bfa8717 commit c4e7372
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.waither.weatherservice.batch;

import static com.waither.weatherservice.utills.DateTimeUtils.*;

import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -24,7 +26,7 @@ public class DailyWeatherTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
LocalDateTime baseTime = LocalDateTime.now();
String[] dateTime = weatherService.convertLocalDateTimeToString(baseTime).split("_");
String[] dateTime = convertLocalDateTimeToString(baseTime).split("_");
List<Region> regionList = weatherService.getRegionList();
regionList.stream()
.forEach(region -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.waither.weatherservice.batch;

import static com.waither.weatherservice.utills.DateTimeUtils.*;

import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -24,7 +26,7 @@ public class ExpectedWeatherTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
LocalDateTime baseTime = LocalDateTime.now().minusHours(1);
String[] dateTime = weatherService.convertLocalDateTimeToString(baseTime).split("_");
String[] dateTime = convertLocalDateTimeToString(baseTime).split("_");
List<Region> regionList = weatherService.getRegionList();
regionList.stream()
.forEach(region -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@Slf4j
@Component
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OpenApiUtil {
public class OpenApiUtils {

@Value("${openapi.forecast.key}")
private String forecastKey;
Expand Down Expand Up @@ -145,11 +145,6 @@ public List<MsgOpenApiResponse.Item> callAdvisoryApi(String location, String tod
}
}

public String convertLocalDateToString(LocalDate localDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
return localDate.format(formatter);
}

public List<AirKoreaOpenApiResponse.Items> callAirKorea(String searchDate) throws URISyntaxException {
int pageNo = 1;
int numOfRows = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.waither.weatherservice.service;

import static com.waither.weatherservice.utills.DateTimeUtils.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;
Expand All @@ -18,12 +18,12 @@
import com.waither.weatherservice.entity.Region;
import com.waither.weatherservice.entity.WeatherAdvisory;
import com.waither.weatherservice.exception.WeatherExceptionHandler;
import com.waither.weatherservice.gps.GpsTransfer;
import com.waither.weatherservice.utills.GpsTransferUtils;
import com.waither.weatherservice.kafka.KafkaMessage;
import com.waither.weatherservice.kafka.Producer;
import com.waither.weatherservice.openapi.ForeCastOpenApiResponse;
import com.waither.weatherservice.openapi.MsgOpenApiResponse;
import com.waither.weatherservice.openapi.OpenApiUtil;
import com.waither.weatherservice.openapi.OpenApiUtils;
import com.waither.weatherservice.repository.DailyWeatherRepository;
import com.waither.weatherservice.repository.ExpectedWeatherRepository;
import com.waither.weatherservice.repository.RegionRepository;
Expand All @@ -39,7 +39,7 @@
@RequiredArgsConstructor
public class WeatherService {

private final OpenApiUtil openApiUtil;
private final OpenApiUtils openApiUtil;
private final DailyWeatherRepository dailyWeatherRepository;
private final ExpectedWeatherRepository expectedWeatherRepository;
private final WeatherAdvisoryRepository weatherAdvisoryRepository;
Expand Down Expand Up @@ -134,9 +134,9 @@ public void createDailyWeather(

public void createWeatherAdvisory(double latitude, double longitude) throws URISyntaxException, IOException {
LocalDate now = LocalDate.now();
String today = openApiUtil.convertLocalDateToString(now);
String today = convertLocalDateToString(now);

String location = GpsTransfer.convertGpsToRegionCode(latitude, longitude);
String location = GpsTransferUtils.convertGpsToRegionCode(latitude, longitude);

List<MsgOpenApiResponse.Item> items = openApiUtil.callAdvisoryApi(location, today);

Expand Down Expand Up @@ -207,38 +207,6 @@ public MainWeatherResponse getMainWeather(double latitude, double longitude) {
return weatherMainResponse;
}

public String convertLocalDateTimeToString(LocalDateTime time) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = time.format(formatter);

String[] lst = formattedDateTime.split(" ");
String baseDate = lst[0].replace("-", "");

String[] temp = lst[1].split(":");
String baseTime = temp[0] + "00";

return baseDate + "_" + baseTime;
}

public LocalDateTime convertLocalDateTimeToDailyWeatherTime(LocalDateTime time) {

// DailyWeather 정보는 3시간마다
List<Integer> scheduledHours = Arrays.asList(0, 3, 6, 9, 12, 15, 18, 21);

int currentHour = time.getHour();
int adjustedHour = scheduledHours.stream()
.filter(hour -> hour <= currentHour)
.reduce((first, second) -> second)
.orElse(scheduledHours.get(scheduledHours.size() - 1)); // 이전 날의 마지막 스케줄 시간(21시) 반환

// 현재 시간이 첫 스케줄 시간(0시)보다 작을 경우, 전날의 마지막 스케줄 시간으로 설정
if (currentHour < scheduledHours.get(0)) {
time = time.minusDays(1);
}

return time.withHour(adjustedHour).withMinute(0).withSecond(0).withNano(0);
}

public List<Region> getRegionList() {
return regionRepository.findAll();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.waither.weatherservice.utills;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DateTimeUtils {

public static LocalDateTime convertLocalDateTimeToDailyWeatherTime(LocalDateTime time) {

// DailyWeather 정보는 3시간마다
List<Integer> scheduledHours = Arrays.asList(0, 3, 6, 9, 12, 15, 18, 21);

int currentHour = time.getHour();
int adjustedHour = scheduledHours.stream()
.filter(hour -> hour <= currentHour)
.reduce((first, second) -> second)
.orElse(scheduledHours.get(scheduledHours.size() - 1)); // 이전 날의 마지막 스케줄 시간(21시) 반환

// 현재 시간이 첫 스케줄 시간(0시)보다 작을 경우, 전날의 마지막 스케줄 시간으로 설정
if (currentHour < scheduledHours.get(0)) {
time = time.minusDays(1);
}

return time.withHour(adjustedHour).withMinute(0).withSecond(0).withNano(0);
}

public static String convertLocalDateTimeToString(LocalDateTime time) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = time.format(formatter);

String[] lst = formattedDateTime.split(" ");
String baseDate = lst[0].replace("-", "");

String[] temp = lst[1].split(":");
String baseTime = temp[0] + "00";

return baseDate + "_" + baseTime;
}

public static String convertLocalDateToString(LocalDate localDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
return localDate.format(formatter);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.waither.weatherservice.gps;
package com.waither.weatherservice.utills;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -15,8 +15,7 @@
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
// @Component
public class GpsTransfer {
public class GpsTransferUtils {

// 격자 간격 (단위: km)
private static final double GRID_INTERVAL = 5.0;
Expand Down Expand Up @@ -110,7 +109,7 @@ public static LatXLngY convertGridToGps(double x, double y) {
public static String convertGpsToRegionCode(double lat, double lon) {
String regionCode = null;
try {
InputStream inputStream = GpsTransfer.class.getResourceAsStream("/api/Region.xlsx");
InputStream inputStream = GpsTransferUtils.class.getResourceAsStream("/api/Region.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0); // 시트 인덱스, 첫 번째 시트를 가져옴

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.waither.weatherservice.gps;
package com.waither.weatherservice.utills;

import lombok.Builder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.waither.weatherservice.gps.GpsTransfer;
import com.waither.weatherservice.gps.LatXLngY;
import com.waither.weatherservice.utills.GpsTransferUtils;
import com.waither.weatherservice.utills.LatXLngY;

@SpringBootTest
class GpsTransferTest {

@Autowired
GpsTransfer gpsTransfer;
GpsTransferUtils gpsTransfer;

LatXLngY expected;

Expand Down

0 comments on commit c4e7372

Please sign in to comment.