Skip to content

Commit

Permalink
Deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn authored Jul 10, 2024
2 parents f043218 + 390767d commit 322fb61
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 64 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 @@ -6,6 +6,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.waither.weatherservice.dto.request.GetWeatherRequest;
import com.waither.weatherservice.dto.request.GetWindChillRequest;
import com.waither.weatherservice.dto.response.MainWeatherResponse;
import com.waither.weatherservice.response.ApiResponse;
import com.waither.weatherservice.service.WeatherService;
Expand Down Expand Up @@ -42,4 +43,21 @@ public ApiResponse<String> convertGpsToRegionName(@ModelAttribute @Valid GetWeat
return ApiResponse.onSuccess(
weatherService.convertGpsToRegionName(getWeatherRequest.latitude(), getWeatherRequest.longitude()));
}

@Operation(summary = "체감온도 가져오기 (전 날까지만) - user-service 사용",
description = "{"
+ "\"latitude\": 37.41,"
+ "\"longitude\": 126.73,"
+ "\"baseTime\": \"2024-07-09T12:34:56\" "
+ "}")
@GetMapping("/wind-chill")
public ApiResponse<Double> getWindChill(@ModelAttribute @Valid GetWindChillRequest getWindChillRequest) {
return ApiResponse.onSuccess(
weatherService.getWindChill(
getWindChillRequest.latitude(),
getWindChillRequest.longitude(),
getWindChillRequest.baseTime()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,4 @@ public void airKoreaTest(@RequestBody AirTestRequest request) throws URISyntaxEx
public void accuweatherTest(@RequestBody AccuweatherTestRequest request) throws URISyntaxException, IOException {
weatherService.convertLocation(request.latitude(), request.longitude());
}

@GetMapping("/converㅅ")
public LocalDateTime convertTest() {

LocalDateTime specificDateTime = LocalDateTime.of(2024, 7, 5, 23, 0);
log.info("Changed : {}", specificDateTime);
return weatherService.convertLocalDateTimeToDailyWeatherTime(specificDateTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.waither.weatherservice.dto.request;

import java.time.LocalDateTime;

import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.NotNull;

public record GetWindChillRequest(
@NotNull(message = "[ERROR] 위도 입력은 필수 입니다.")
@DecimalMin(value = "33.0", message = "[ERROR] 위도는 33.0도에서 43.0도 사이여야 합니다.")
@DecimalMax(value = "43.0", message = "[ERROR] 위도는 33.0도에서 43.0도 사이여야 합니다.")
Double latitude,
@NotNull(message = "[ERROR] 경도 입력은 필수 입니다.")
@DecimalMin(value = "124.0", message = "[ERROR] 경도는 124.0도에서 132.0도 사이여야 합니다.")
@DecimalMax(value = "132.0", message = "[ERROR] 경도는 124.0도에서 132.0도 사이여야 합니다.")
Double longitude,
@NotNull(message = "[ERROR] 날짜 입력은 필수 입니다.")
LocalDateTime baseTime
)
{}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@RedisHash(value = "DailyWeather", timeToLive = 28800L) // 유효시간: 8시간
@RedisHash(value = "DailyWeather", timeToLive = 172800L) // 유효시간: 48시간
public class DailyWeather {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@RedisHash(value = "ExpectedWeather", timeToLive = 21600L) // 유효시간: 6시간
@RedisHash(value = "ExpectedWeather", timeToLive = 172800L) // 유효시간: 48시간
public class ExpectedWeather {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@RedisHash(value = "WeatherAdvisory", timeToLive = 86400L) // 유효시간: 24시간
@RedisHash(value = "WeatherAdvisory", timeToLive = 172800L) // 유효시간: 48시간
public class WeatherAdvisory {

@Id
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 @@ -130,9 +130,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 @@ -203,43 +203,34 @@ 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 List<Region> getRegionList() {
return regionRepository.findAll();
}

public LocalDateTime convertLocalDateTimeToDailyWeatherTime(LocalDateTime time) {
public String convertGpsToRegionName(double latitude, double longitude) {
return regionRepository.findRegionByLatAndLong(latitude, longitude).get(0).getRegionName();
}

// DailyWeather 정보는 3시간마다
List<Integer> scheduledHours = Arrays.asList(0, 3, 6, 9, 12, 15, 18, 21);
public double calculateWindChill(double temp, double wind) {
if (temp > 10 || wind < 4.8) {
return temp;
}
return 13.12 + 0.6215 * temp - 11.37 * Math.pow(wind, 0.16) + 0.3965 * temp * Math.pow(wind, 0.16);
}

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

// 현재 시간이 첫 스케줄 시간(0시)보다 작을 경우, 전날의 마지막 스케줄 시간으로 설정
if (currentHour < scheduledHours.get(0)) {
time = time.minusDays(1);
}
List<Region> region = regionRepository.findRegionByLatAndLong(latitude, longitude);
if (region.isEmpty())
throw new WeatherExceptionHandler(WeatherErrorCode.REGION_NOT_FOUND);

return time.withHour(adjustedHour).withMinute(0).withSecond(0).withNano(0);
}
String regionName = region.get(0).getRegionName();
LocalDateTime dailyWeatherBaseTime = convertLocalDateTimeToDailyWeatherTime(baseTime.minusHours(1));
String dailyWeatherKey = regionName + "_" + convertLocalDateTimeToString(dailyWeatherBaseTime);

public List<Region> getRegionList() {
return regionRepository.findAll();
}
DailyWeather dailyWeather = dailyWeatherRepository.findById(dailyWeatherKey)
.orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.DAILY_NOT_FOUND));

public String convertGpsToRegionName(double latitude, double longitude) {
return regionRepository.findRegionByLatAndLong(latitude, longitude).get(0).getRegionName();
return calculateWindChill(Double.valueOf(dailyWeather.getTmp()), Double.valueOf(dailyWeather.getWindDegree()));
}
}
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 322fb61

Please sign in to comment.