Skip to content

Commit

Permalink
Merge pull request EveryUniv#107 from EveryUniv/dev
Browse files Browse the repository at this point in the history
fix: BusStation 캐싱 시 정류장 이름 입력 없이 모든 데이터 가져오도록 수정
  • Loading branch information
gutanbug authored Feb 7, 2024
2 parents 59c5455 + c6955d7 commit 351d29f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@ public class BusController {
* 예상 버스 도착 시간을 조회합니다.
* 캐싱이 적용됩니다. 데이터는 최신이 아닐 수 있으며, 데이터 가져온 시점은 capturedAt을 통해 알 수 있습니다.
*
* @param stationName 버스 정류장 이름. 가능한 값: 단국대정문, 곰상
* @return 버스 도착 예상 시간 목록
*/
@GetMapping
public ResponseBusArrivalDto listBusArrivalTime(@NotBlank @RequestParam String stationName) {
BusStation station = BusStation.of(stationName);
if (station == null) {
throw new InvalidBusStationException();
}
return service.listBusArrival(station);
public ResponseBusArrivalDto listBusArrivalTime() {
return service.listBusArrival();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Set;

@Getter
Expand Down Expand Up @@ -36,4 +37,8 @@ public static BusStation of(String name) {
}
return null;
}

public static List<BusStation> getBusStations() {
return List.of(BusStation.values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public class ResponseBusArrivalDto {
@Schema(description = "도착 시간이 계산된 시각", example = "2022-03-01 11:31:11")
@JsonFormat(pattern = DATE_TIME_FORMAT_PATTERN, timezone = "Asia/Seoul")
private final Instant capturedAt;
private final List<BusArrivalDto> busArrivalList;
private final List<BusArrivalDto> busArrivalEntranceList;
private final List<BusArrivalDto> busArrivalPlazaList;
}
43 changes: 30 additions & 13 deletions src/main/java/com/dku/council/domain/bus/service/BusService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -24,23 +25,39 @@ public class BusService {
private final OpenApiBusService openApiBusService;
private final BusArrivalRepository memoryRepository;

public ResponseBusArrivalDto listBusArrival(BusStation station) {
public ResponseBusArrivalDto listBusArrival() {
Instant now = Instant.now(clock);
String stationName = station.name();
Instant capturedAt = Instant.now();
List<BusArrivalDto> busArrivalEntranceList = new ArrayList<>();
List<BusArrivalDto> busArrivalPlazaList = new ArrayList<>();

List<BusStation> busStations = BusStation.getBusStations();

CachedBusArrivals cached = memoryRepository.getArrivals(stationName, now)
.orElseGet(() -> {
List<BusArrival> arrivals = openApiBusService.retrieveBusArrival(station);
return memoryRepository.cacheArrivals(stationName, arrivals, now);
});
for (BusStation busStation : busStations) {
CachedBusArrivals cached = memoryRepository.getArrivals(busStation.name(), now)
.orElseGet(() -> {
List<BusArrival> arrivals = openApiBusService.retrieveBusArrival(busStation);
return memoryRepository.cacheArrivals(busStation.name(), arrivals, now);
});

Duration diff = Duration.between(cached.getCapturedAt(), now);
if (cached != null) {
capturedAt = cached.getCapturedAt();
Duration diff = Duration.between(cached.getCapturedAt(), now);

List<BusArrivalDto> busArrivalDtos = cached.getArrivals().stream()
.map(arrival -> interpolation(arrival, diff))
.map(BusArrivalDto::new)
.collect(Collectors.toList());
return new ResponseBusArrivalDto(cached.getCapturedAt(), busArrivalDtos);
if (busStation.equals(BusStation.DKU_GATE)) {
busArrivalEntranceList = cached.getArrivals().stream()
.map(arrival -> interpolation(arrival, diff))
.map(BusArrivalDto::new)
.collect(Collectors.toList());
} else{
busArrivalPlazaList = cached.getArrivals().stream()
.map(arrival -> interpolation(arrival, diff))
.map(BusArrivalDto::new)
.collect(Collectors.toList());
}
}
}
return new ResponseBusArrivalDto(capturedAt, busArrivalEntranceList, busArrivalPlazaList);
}

private static BusArrival interpolation(BusArrival arrival, Duration diff) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ void listBusArrivalNoCached() {
when(memoryRepository.cacheArrivals(eq(station.name()), any(), any())).thenReturn(cached);

// when
ResponseBusArrivalDto dto = service.listBusArrival(BusStation.DKU_GATE);
ResponseBusArrivalDto dto = service.listBusArrival();

// then
verify(openApiBusService).retrieveBusArrival(station);
assertThat(dto.getCapturedAt().getEpochSecond()).isEqualTo(now.getEpochSecond());
assertThat(dto.getBusArrivalList().size()).isEqualTo(5);
assertThat(dto.getBusArrivalEntranceList().size()).isEqualTo(5);
}

@Test
Expand All @@ -74,11 +74,11 @@ void listBusArrivalCached() {
when(memoryRepository.getArrivals(station.name(), now)).thenReturn(Optional.of(cached));

// when
ResponseBusArrivalDto dto = service.listBusArrival(BusStation.DKU_GATE);
ResponseBusArrivalDto dto = service.listBusArrival();

// then
assertThat(dto.getCapturedAt().getEpochSecond()).isEqualTo(now.getEpochSecond());
assertThat(dto.getBusArrivalList().size()).isEqualTo(5);
assertThat(dto.getBusArrivalEntranceList().size()).isEqualTo(5);
}

@Test
Expand All @@ -95,14 +95,14 @@ void listBusArrivalCachedWithInterpolation() {
when(memoryRepository.getArrivals(station.name(), now)).thenReturn(Optional.of(cached));

// when
ResponseBusArrivalDto dto = service.listBusArrival(BusStation.DKU_GATE);
ResponseBusArrivalDto dto = service.listBusArrival();

// then
assertThat(dto.getCapturedAt().getEpochSecond()).isEqualTo(now.getEpochSecond() - 10);
assertThat(dto.getBusArrivalList().size()).isEqualTo(2);
assertThat(dto.getBusArrivalList().get(0).getPredictTime1()).isEqualTo(arrivals.get(0).getPredictTimeSec1() - 10);
assertThat(dto.getBusArrivalList().get(0).getPredictTime2()).isEqualTo(arrivals.get(0).getPredictTimeSec2() - 10);
assertThat(dto.getBusArrivalList().get(1).getPredictTime1()).isEqualTo(0);
assertThat(dto.getBusArrivalList().get(1).getPredictTime2()).isEqualTo(0);
assertThat(dto.getBusArrivalEntranceList().size()).isEqualTo(2);
assertThat(dto.getBusArrivalEntranceList().get(0).getPredictTime1()).isEqualTo(arrivals.get(0).getPredictTimeSec1() - 10);
assertThat(dto.getBusArrivalEntranceList().get(0).getPredictTime2()).isEqualTo(arrivals.get(0).getPredictTimeSec2() - 10);
assertThat(dto.getBusArrivalEntranceList().get(1).getPredictTime1()).isEqualTo(0);
assertThat(dto.getBusArrivalEntranceList().get(1).getPredictTime2()).isEqualTo(0);
}
}

0 comments on commit 351d29f

Please sign in to comment.