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 @@ -67,7 +67,7 @@ class HomeQueryService(

val places = placeRepository.findAllWithImages()
val placesWithCongestion = places.mapNotNull { place ->
val congestion = placeRedisUtil.getRedisCongestion(place.id)
val congestion = placeRedisUtil.getTimeCongestion(place.id)
if (congestion != null) {
place to congestion
} else {
Expand All @@ -85,7 +85,7 @@ class HomeQueryService(
longitude = place.longitude?.toDouble(),
type = place.type.korean,
image = place.placeImages.firstOrNull()?.imgUrl.nullIfBlank(),
congestionLevel = congestion,
congestionLevel = congestion.toInt(),
address = place.address
)
}
Expand All @@ -99,12 +99,12 @@ class HomeQueryService(

return places.shuffled().take(5).map { place ->

val congestion = placeRedisUtil.getRedisCongestion(place.id)
val congestion = placeRedisUtil.getTimeCongestion(place.id)

HomeResponseDTO.RecommendPlace(
id = place.id,
name = place.name,
congestionLevel = congestion,
congestionLevel = congestion.toInt(),
type = place.type.korean,
image = place.placeImages.firstOrNull()?.imgUrl.nullIfBlank(),
latitude = place.latitude?.toDouble(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PlaceDetailsConverter(
name = place.name,
type = place.type.capitalEnglish,
img = placeImages.map { it.imgUrl },
congestionLevel = redisUtil.getRedisCongestion(place.id),
congestionLevel = redisUtil.getTimeCongestion(place.id).toInt(),
likeAmount = placeLikes.size,
address = place.address,
isLike = isLike,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PlaceCongestionQueryService(
type = it.type.capitalEnglish,
latitude = it.latitude,
longitude = it.longitude,
congestionLevel = placeRedisUtil.getRedisCongestion(it.id)
congestionLevel = placeRedisUtil.getTimeCongestion(it.id).toInt()
)
}

Expand Down Expand Up @@ -98,7 +98,7 @@ class PlaceCongestionQueryService(
return PlaceMapResponseDTO.PlaceDefaultInfoDto(
id = place.id,
name = place.name,
congestionLevel = placeRedisUtil.getRedisCongestion(place.id),
congestionLevel = placeRedisUtil.getTimeCongestion(place.id).toInt(),
latitude = place.latitude,
longitude = place.longitude,
address = place.address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PlaceQueryService(

// 혼잡도 조회
val placeRedisUtil = PlaceRedisUtil(redisTemplate)
val congestionMap: Map<Long, Int> = placeIdList.associateWith { placeRedisUtil.getRedisCongestion(it) }
val congestionMap: Map<Long, Int> = placeIdList.associateWith { placeRedisUtil.getTimeCongestion(it).toInt() }


// DTO 변환
Expand Down
59 changes: 31 additions & 28 deletions src/main/kotlin/busanVibe/busan/domain/place/util/PlaceRedisUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,43 @@ class PlaceRedisUtil(
private val log = LoggerFactory.getLogger("busanVibe.busan.domain.place")

// 임의로 혼잡도 생성하여 반환. 레디스 키 값으로 저장함.
fun getRedisCongestion(placeId: Long?): Int{
// fun getRedisCongestion(placeId: Long?): Int{
//
// val key = "place:congestion:$placeId"
// val randomCongestion = getRandomCongestion().toInt().toString()
//
// redisTemplate.opsForValue()
// .set(key, randomCongestion)
//
// return Integer.parseInt(randomCongestion)
// }

val key = "place:congestion:$placeId"
val randomCongestion = getRandomCongestion().toInt().toString()
// 지정 시간 혼잡도 조회
// null이면 현재시간 기준
fun getTimeCongestion(placeId: Long?, dateTime: LocalDateTime?): Float {

val dateTime = dateTime ?: LocalDateTime.now()

redisTemplate.opsForValue()
.set(key, randomCongestion)
val roundedHour = (dateTime.hour / 3) * 3
val key = "place:congestion:$placeId-${dateTime.year}-${dateTime.monthValue}-${dateTime.dayOfMonth}-$roundedHour"

val value = redisTemplate.opsForValue().get(key)

return Integer.parseInt(randomCongestion)
return (if (value != null) {
value.toFloatOrNull() ?: 0
} else {
setPlaceTimeCongestion(placeId, dateTime.withHour(roundedHour))
val newValue = redisTemplate.opsForValue().get(key)
newValue?.toFloatOrNull() ?: 0
}) as Float
}

fun getTimeCongestion(placeId: Long?):Float{
return getTimeCongestion(placeId, LocalDateTime.now())
}

// 시간 혼잡도 설정
fun setPlaceTimeCongestion(placeId: Long, dateTime: LocalDateTime) {
private fun setPlaceTimeCongestion(placeId: Long?, dateTime: LocalDateTime) {
val roundedHour = (dateTime.hour / 3) * 3
val key = "place:congestion:$placeId-${dateTime.year}-${dateTime.monthValue}-${dateTime.dayOfMonth}-$roundedHour"
val congestion = getRandomCongestion().toString()
Expand All @@ -40,27 +64,6 @@ class PlaceRedisUtil(
}
}

// 지정 시간 혼잡도 조회
// null이면 현재시간 기준
fun getTimeCongestion(placeId: Long, dateTime: LocalDateTime?): Float {

val dateTime = dateTime ?: LocalDateTime.now()

val roundedHour = (dateTime.hour / 3) * 3
val key = "place:congestion:$placeId-${dateTime.year}-${dateTime.monthValue}-${dateTime.dayOfMonth}-$roundedHour"

val value = redisTemplate.opsForValue().get(key)

return if (value != null) {
log.info("이미 존재하는 혼잡도 기록: $key, 기존 혼잡도: $value")
value.toFloatOrNull() ?: 0f
} else {
setPlaceTimeCongestion(placeId, dateTime.withHour(roundedHour))
val newValue = redisTemplate.opsForValue().get(key)
newValue?.toFloatOrNull() ?: 0f
}
}

// 혼잡도 생성 (1.0 ~ 5.0 사이의 Float)
private fun getRandomCongestion(): Float {
return (Math.random() * 4 + 1).toFloat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class SearchUtil(
latitude = place.latitude?.toDouble(),
longitude = place.longitude?.toDouble(),
address = place.address,
congestionLevel = placeRedisUtil.getRedisCongestion(place.id),
congestionLevel = placeRedisUtil.getTimeCongestion(place.id).toInt(),
isLike = place.placeLikes.any { it.user == currentUser },
startDate = null,
endDate = null,
Expand Down