Skip to content

Commit

Permalink
✨ Regenerate the database more often with calls directly to fetchFromUSP
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoColman committed Mar 5, 2024
1 parent ff4e318 commit 334ba55
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
9 changes: 8 additions & 1 deletion restaurants/src/main/kotlin/RestaurantJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package app.jopiter.restaurants

import app.jopiter.restaurants.model.Restaurant
import app.jopiter.restaurants.repository.RestaurantItemRepository
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import java.time.LocalDate.now
Expand All @@ -31,6 +32,12 @@ class RestaurantJob(
private val repository: RestaurantItemRepository
) {

private val logger = LoggerFactory.getLogger(this::class.java)

@Scheduled(fixedRate = 15_000)
fun run() = Restaurant.values().forEach { repository.get(it.id, setOf(now())) }
fun run() = Restaurant.entries.forEach {
logger.info("Starting scheduled execution to fetch $it")
repository.fetchFromUsp(it.id)
logger.info("Finished scheduled execution to fetch $it")
}
}
2 changes: 1 addition & 1 deletion restaurants/src/main/kotlin/model/Restaurant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ enum class Restaurant(val id: Int, val restaurantName: String) {
Eel2(23, "EEL - Área II"),;

companion object {
fun getById(id: Int) = values().first { it.id == id }
fun getById(id: Int) = entries.first { it.id == id }
}
}
20 changes: 6 additions & 14 deletions restaurants/src/main/kotlin/repository/RestaurantItemRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,14 @@ class RestaurantItemRepository(
fun get(restaurantId: Int, dates: Set<LocalDate>) = dates.flatMap { fetch(restaurantId, it) }.toSet()

private fun fetch(restaurantId: Int, date: LocalDate) =
fetchFromPostgres(restaurantId, date).ifEmpty { fetchFromUsp(restaurantId, date) }

private fun fetchFromUsp(restaurantId: Int, date: LocalDate): Set<RestaurantItem> {
val items = if (YearWeek.from(date) == YearWeek.now()) {
uspRestaurantItemRepository.fetch(restaurantId)
} else emptySet()

val requested= items.filter { it.date == date }
fetchFromPostgres(restaurantId, date)

fun fetchFromUsp(restaurantId: Int): Set<RestaurantItem> {
val items = uspRestaurantItemRepository.fetch(restaurantId)
postgresRestaurantItemRepository.put(items)

return requested.toSet()
return items
}

private fun fetchFromPostgres(
restaurantId: Int,
date: LocalDate
) = postgresRestaurantItemRepository.get(restaurantId, date)
private fun fetchFromPostgres(restaurantId: Int, date: LocalDate) =
postgresRestaurantItemRepository.get(restaurantId, date)
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ class PostgresRestaurantItemRepository(
}
}

fun get(restaurantId: Int, date: LocalDate): Set<RestaurantItem> = restaurantItems
.filter { it.date eq date }
.filter { it.restaurantId eq restaurantId }
.map { it.toItem() }
.toSet()
fun get(restaurantId: Int, date: LocalDate, period: Period? = null): Set<RestaurantItem> {
val query = restaurantItems.filter { it.date eq date }.filter { it.restaurantId eq restaurantId }
if(period != null) query.filter { it.period eq period }

fun get(restaurantId: Int, date: LocalDate, period: Period): Set<RestaurantItem> = restaurantItems
.filter { it.date eq date }
.filter { it.restaurantId eq restaurantId }
.filter { it.period eq period }
.map { it.toItem() }
.toSet()
return query.map { it.toItem() }.toSet()
}
}

object RestaurantItems : Table<RestaurantItemEntity>("restaurant_item") {
Expand Down

0 comments on commit 334ba55

Please sign in to comment.