From 2bd9acb8d244e4f484a85e2d3ef8c47155de4d42 Mon Sep 17 00:00:00 2001 From: Dmitry Marchuk Date: Tue, 29 Aug 2023 13:31:08 +0300 Subject: [PATCH] Fix periodic fetcher #dockerpush #latest --- build.gradle.kts | 2 +- gradle.properties | 3 +-- .../common/misc/PeriodicFetcherFactory.kt | 1 + .../monobudget/mono/MonoAccountsService.kt | 27 +++++++++---------- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 94c0d9a..fd3c193 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -38,7 +38,7 @@ repositories { dependencies { implementation(kotlin("stdlib")) implementation(kotlin("reflect")) - implementation("io.github.smaugfm:monobank:0.0.1") + implementation("io.github.smaugfm:monobank:0.0.2") implementation("io.github.smaugfm:lunchmoney:1.0.2") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.6.4") implementation("io.insert-koin:koin-core:$koin") diff --git a/gradle.properties b/gradle.properties index 5038c85..896ff0d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,4 @@ -version=2.0.0 -kotlin.code.style=official +version=1.0.1 org.gradle.parallel=true org.gradle.caching=true org.gradle.configureondemand=true diff --git a/src/main/kotlin/io/github/smaugfm/monobudget/common/misc/PeriodicFetcherFactory.kt b/src/main/kotlin/io/github/smaugfm/monobudget/common/misc/PeriodicFetcherFactory.kt index b5cdaff..c3ace54 100644 --- a/src/main/kotlin/io/github/smaugfm/monobudget/common/misc/PeriodicFetcherFactory.kt +++ b/src/main/kotlin/io/github/smaugfm/monobudget/common/misc/PeriodicFetcherFactory.kt @@ -37,6 +37,7 @@ class PeriodicFetcherFactory(private val scope: CoroutineScope) { fetch() } catch (e: Throwable) { log.error(e) { "Error fetching $name: " } + delay(interval) continue } if (data === initial) { diff --git a/src/main/kotlin/io/github/smaugfm/monobudget/mono/MonoAccountsService.kt b/src/main/kotlin/io/github/smaugfm/monobudget/mono/MonoAccountsService.kt index e0a3784..1cc7a6f 100644 --- a/src/main/kotlin/io/github/smaugfm/monobudget/mono/MonoAccountsService.kt +++ b/src/main/kotlin/io/github/smaugfm/monobudget/mono/MonoAccountsService.kt @@ -10,6 +10,7 @@ import kotlinx.coroutines.reactor.awaitSingle import kotlinx.serialization.ExperimentalSerializationApi import mu.KotlinLogging import org.koin.core.annotation.Single +import reactor.core.publisher.Flux private val log = KotlinLogging.logger { } @@ -20,27 +21,23 @@ class MonoAccountsService( private val settings: MultipleAccountSettings ) : BankAccountService() { + private val otherAccounts = settings.settings.filterIsInstance() + .map { Account(it.accountId, it.alias, it.currency) } + private val monoApis = settings.settings.filterIsInstance() + .map { MonoApi(it.token) to it.accountId } + private val fetcher = fetcherFactory.create("Monobank accounts") { - settings.settings.map { - when (it) { - is OtherAccountSettings -> - Account(it.accountId, it.alias, it.currency) - - is MonoAccountSettings -> { - val api = MonoApi(it.token) - - val accountId = it.accountId - val monoAccount = - api.api.getClientInformation().awaitSingle() - .accounts.firstOrNull { account -> account.id == accountId }!! + Flux.concat(monoApis.map { (api, accountId) -> + api.api.getClientInformation() + .map { info -> info.accounts.first { a -> a.id == accountId } } + .map { Account( accountId, settings.byId[accountId]!!.alias, - monoAccount.currencyCode + it!!.currencyCode ) } - } - } + }).collectList().awaitSingle() + otherAccounts } override suspend fun getAccounts() = fetcher.getData().filter { it.id in settings.accountIds }