Skip to content

Commit

Permalink
fix(api): Proper validity keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Lastaapps committed Mar 14, 2024
1 parent ce8be12 commit 859756c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import cz.lastaapps.api.core.domain.sync.runSync
import cz.lastaapps.api.core.domain.validity.ValidityChecker
import cz.lastaapps.api.core.domain.validity.ValidityKey
import cz.lastaapps.api.core.domain.validity.withCheckSince
import cz.lastaapps.api.core.domain.validity.withParams
import cz.lastaapps.core.util.extensions.localLogger
import cz.lastaapps.menza.api.agata.api.CafeteriaApi
import cz.lastaapps.menza.api.agata.data.SyncJobHash
Expand Down Expand Up @@ -127,7 +128,7 @@ internal class MenzaSubsystemRepoImpl(

override suspend fun sync(params: MenzaRepoParams, isForced: Boolean): SyncOutcome = run {
log.i { "Starting sync (f: $isForced)" }
checker.withCheckSince(ValidityKey.agataMenza(), isForced, 7.days) {
checker.withCheckSince(ValidityKey.agataMenza().withParams(params), isForced, 7.days) {
processor.runSync(subsystemJob, db, params, isForced)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import cz.lastaapps.api.core.domain.sync.runSync
import cz.lastaapps.api.core.domain.validity.ValidityChecker
import cz.lastaapps.api.core.domain.validity.ValidityKey
import cz.lastaapps.api.core.domain.validity.withCheckRecent
import cz.lastaapps.api.core.domain.validity.withParams
import cz.lastaapps.core.util.extensions.localLogger
import cz.lastaapps.menza.api.agata.api.DishApi
import cz.lastaapps.menza.api.agata.data.SyncJobHash
Expand Down Expand Up @@ -65,15 +66,18 @@ internal class TodayDishStrahovRepoImpl(
private val log = localLogger()

private val validityKey = ValidityKey.strahov()
private val isValidFlow = checker.isThisWeek(validityKey)
.onEach { log.i { "Validity changed to $it" } }

override fun getData(params: TodayRepoParams): Flow<ImmutableList<DishCategory>> =
db.strahovQueries
.get(params.language.toDB())
.asFlow()
.mapToList(Dispatchers.IO)
.combine(isValidFlow) { data, validity ->
.combine(
run {
checker.isThisWeek(validityKey.withParams(params))
.onEach { log.i { "Validity changed to $it" } }
},
) { data, validity ->
data.takeIf { validity }.orEmpty()
}
.map { it.toDomain() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import cz.lastaapps.api.core.domain.sync.runSync
import cz.lastaapps.api.core.domain.validity.ValidityChecker
import cz.lastaapps.api.core.domain.validity.ValidityKey
import cz.lastaapps.api.core.domain.validity.withCheckRecent
import cz.lastaapps.api.core.domain.validity.withParams
import cz.lastaapps.core.util.extensions.foldFlows
import cz.lastaapps.menza.api.agata.api.CafeteriaApi
import cz.lastaapps.menza.api.agata.api.DishApi
Expand Down Expand Up @@ -83,9 +84,6 @@ internal class TodayDishSubsystemRepoImpl(
private val log = Logger.withTag(this::class.simpleName + "($subsystemId)")

private val validityKey = ValidityKey.agataToday(subsystemId)
private val isValidFlow = checker.isFromToday(validityKey)
.distinctUntilChanged()
.onEach { log.i { "Validity changed to $it" } }

override fun getData(params: WeekRepoParams): Flow<ImmutableList<DishCategory>> = channelFlow {
val lang = params.language.toDB()
Expand All @@ -94,7 +92,13 @@ internal class TodayDishSubsystemRepoImpl(
db.dishQueries.getForSubsystem(subsystemId.toLong(), lang)
.asFlow()
.mapToList(Dispatchers.IO)
.combine(isValidFlow) { data, validity ->
.combine(
run {
checker.isFromToday(validityKey.withParams(params))
.distinctUntilChanged()
.onEach { log.i { "Validity changed to $it" } }
},
) { data, validity ->
data.takeIf { validity }.orEmpty()
}
.distinctUntilChanged()
Expand Down Expand Up @@ -244,7 +248,7 @@ internal class TodayDishSubsystemRepoImpl(

override suspend fun sync(params: WeekRepoParams, isForced: Boolean): SyncOutcome = run {
log.i { "Starting sync (f: $isForced)" }
checker.withCheckRecent(validityKey, isForced) {
checker.withCheckRecent(validityKey.withParams(params), isForced) {
processor.runSync(jobs, db, params, isForced = isForced)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import cz.lastaapps.api.core.domain.sync.runSync
import cz.lastaapps.api.core.domain.validity.ValidityChecker
import cz.lastaapps.api.core.domain.validity.ValidityKey
import cz.lastaapps.api.core.domain.validity.withCheckSince
import cz.lastaapps.api.core.domain.validity.withParams
import cz.lastaapps.core.domain.error.ApiError.WeekNotAvailable
import cz.lastaapps.core.util.extensions.localLogger
import cz.lastaapps.menza.api.agata.api.DishApi
Expand Down Expand Up @@ -64,15 +65,14 @@ internal class WeekDishRepoImpl(
private val log = Logger.withTag(this::class.simpleName + "($subsystemId)")

private val validityKey = ValidityKey.agataWeek(subsystemId)
private val isValidFlow = checker.isThisWeek(validityKey)
.onEach { log.i { "Validity changed to $it" } }

private val weekDishList = MutableStateFlow<ImmutableList<WeekDayDish>>(persistentListOf())

override fun getData(params: WeekRepoParams): Flow<ImmutableList<WeekDayDish>> =
combine(
weekDishList,
isValidFlow,
checker.isThisWeek(validityKey.withParams(params))
.onEach { log.i { "Validity changed to $it" } },
) { data, validity ->
data.takeIf { validity } ?: persistentListOf()
}
Expand Down Expand Up @@ -108,7 +108,7 @@ internal class WeekDishRepoImpl(
log.i { "Starting sync (f: $isForced, h: $hasSynced)" }

val myForced = isForced || !hasSynced
checker.withCheckSince(validityKey, myForced, 1.days) {
checker.withCheckSince(validityKey.withParams(params), myForced, 1.days) {
processor.runSync(syncJob, params = params, isForced = myForced).recover {
if (it is WeekNotAvailable) Unavailable else raise(it)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023, Petr Laštovička as Lasta apps, All rights reserved
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
Expand All @@ -19,9 +19,13 @@

package cz.lastaapps.api.core.domain.validity

import cz.lastaapps.api.core.domain.model.RequestParams

@JvmInline
value class ValidityKey private constructor(val name: String) {

fun withLang(lang: String) = ValidityKey(lang + '_' + name)

companion object {
fun agataToday(subsystemId: Int) = ValidityKey("agata_today_$subsystemId")
fun agataInfo(subsystemId: Int) = ValidityKey("agata_info_$subsystemId")
Expand All @@ -33,3 +37,5 @@ value class ValidityKey private constructor(val name: String) {
fun agataCtuBalance() = ValidityKey("balance_agata_ctu")
}
}

fun ValidityKey.withParams(params: RequestParams) = withLang(params.language.value)
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ val apiModule = module {
singleOf(::WalletMasterRepositoryImpl) bind WalletMasterRepository::class

factory { GetMenzaListUC(get(), get(rootName), get()) }
factory { SyncMenzaListUC(get(), get(rootName), get()) }
factory { SyncMenzaListUC(get(), get(rootName), get(), get()) }
factoryOf(::GetInfoUC)
factoryOf(::SyncInfoUC)
factoryOf(::SyncAllInfoUC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,32 @@

package cz.lastaapps.api.main.domain.usecase

import arrow.fx.coroutines.parMap
import cz.lastaapps.api.core.domain.repo.MenzaRepo
import cz.lastaapps.api.core.domain.sync.sync
import cz.lastaapps.core.domain.UCContext
import cz.lastaapps.core.domain.UseCase
import cz.lastaapps.api.core.domain.sync.getData
import cz.lastaapps.api.core.domain.sync.sync

class SyncMenzaListUC(
context: UCContext,
private val menzaRepo: MenzaRepo,
private val getRequestParamsUC: GetRequestParamsUC,
private val getImportantRequestParams: GetImportantRequestParams,
) : UseCase(context) {
suspend operator fun invoke(isForced: Boolean = false) = launch {
menzaRepo.sync(getRequestParamsUC(), isForced = isForced)
suspend operator fun invoke(isForced: Boolean = false, all: Boolean = false) = launch {
if (all) {
getImportantRequestParams().parMap {
menzaRepo.sync(it, isForced = isForced)
}.let { list ->
if (list.all { it.isRight() }) {
list.first()
} else {
list.first { it.isLeft() }
}
}

} else {
menzaRepo.sync(getRequestParamsUC(), isForced = isForced)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ internal class DownloadInitDataUC(
private val syncInfoUC: SyncAllInfoUC,
) : UseCase(context) {

private val log = localLogger()
private val log = localLogger()

suspend operator fun invoke() = launch {
suspend operator fun invoke() =
flow {
emit(DownloadProgress.INIT.right())

emit(DownloadProgress.MENZA_LIST.right())
log.i { "Starting menza download" }

syncMenzaListUC(isForced = true)
syncMenzaListUC(isForced = true, all = true)
.onLeft {
emit(it.left())
return@flow
Expand All @@ -64,5 +64,4 @@ internal class DownloadInitDataUC(
log.i { "Done" }
emit(DownloadProgress.DONE.right())
}
}
}

0 comments on commit 859756c

Please sign in to comment.