Skip to content

Commit

Permalink
Renames, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
smaugfm committed Nov 28, 2023
1 parent b0ef630 commit 7d697ca
Show file tree
Hide file tree
Showing 60 changed files with 340 additions and 385 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ Previously this app supported only YNAB as this was my preferred financial manag
Last commit where YNAB support was working reliably (and I was personally using it)
is [3a7da7af](https://github.com/smaugfm/monobudget/commit/3a7da7afd85bffa310f54a322c46d626d24f488c) (May 2022)

PRs are feature requests are welcome!
PRs and feature requests are welcome!
14 changes: 7 additions & 7 deletions src/main/kotlin/io/github/smaugfm/monobudget/Application.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.github.smaugfm.monobudget

import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smaugfm.monobudget.common.exception.BudgetBackendError
import io.github.smaugfm.monobudget.common.lifecycle.StatementItemProcessor
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingEventDelivery
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingScopeComponent
import io.github.smaugfm.monobudget.common.exception.BudgetBackendException
import io.github.smaugfm.monobudget.common.startup.ApplicationStartupVerifier
import io.github.smaugfm.monobudget.common.statement.StatementSource
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementEvents
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementItemProcessor
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingScopeComponent
import io.github.smaugfm.monobudget.common.telegram.TelegramApi
import io.github.smaugfm.monobudget.common.telegram.TelegramCallbackHandler
import io.github.smaugfm.monobudget.common.util.injectAll
import io.github.smaugfm.monobudget.common.verify.ApplicationStartupVerifier
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
Expand All @@ -28,7 +28,7 @@ class Application<TTransaction, TNewTransaction> :
private val statementSources by injectAll<StatementSource>()
private val startupVerifiers by injectAll<ApplicationStartupVerifier>()
private val telegramCallbackHandler by inject<TelegramCallbackHandler<TTransaction>>()
private val statementEvents by inject<StatementProcessingEventDelivery>()
private val statementEvents by inject<StatementEvents>()

suspend fun run() {
runStartupChecks()
Expand All @@ -48,7 +48,7 @@ class Application<TTransaction, TNewTransaction> :
scope.get<StatementItemProcessor<TTransaction, TNewTransaction>>()
.process()
statementEvents.onStatementEnd(ctx)
} catch (e: BudgetBackendError) {
} catch (e: BudgetBackendException) {
statementEvents.onStatementRetry(ctx, e)
} catch (e: Throwable) {
statementEvents.onStatementError(ctx, e)
Expand Down
21 changes: 0 additions & 21 deletions src/main/kotlin/io/github/smaugfm/monobudget/Main.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.smaugfm.monobudget

import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.resilience4j.core.IntervalFunction
import io.github.resilience4j.kotlin.retry.RetryConfig
import io.github.resilience4j.reactor.retry.RetryOperator
import io.github.resilience4j.retry.Retry
import io.github.smaugfm.lunchmoney.api.LunchmoneyApi
import io.github.smaugfm.lunchmoney.model.LunchmoneyInsertTransaction
import io.github.smaugfm.lunchmoney.model.LunchmoneyTransaction
Expand Down Expand Up @@ -34,7 +31,6 @@ import org.koin.dsl.module
import org.koin.ksp.generated.*
import java.net.URI
import java.nio.file.Paths
import java.time.Duration

private val log = KotlinLogging.logger {}

Expand Down Expand Up @@ -104,7 +100,6 @@ private fun runtimeModule(
single { settings.bot }
single { settings.accounts }
single { settings.retry }
single { apiRetry() }
settings.accounts.settings.filterIsInstance<MonoAccountSettings>()
.forEach { s -> single(StringQualifier(s.alias)) { MonoApi(s.token, s.accountId, s.alias) } }
settings.transfer.forEach { s ->
Expand All @@ -113,22 +108,6 @@ private fun runtimeModule(
single { coroutineScope }
}

@Suppress("MagicNumber")
private fun apiRetry(): Retry =
Retry.of(
"apiRetry",
RetryConfig {
maxAttempts(3)
failAfterMaxAttempts(true)
intervalFunction(
IntervalFunction.ofExponentialBackoff(
Duration.ofSeconds(1),
2.0,
),
)
},
)

private fun lunchmoneyModule(budgetBackend: Lunchmoney) =
module {
single {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package io.github.smaugfm.monobudget.common.account
import io.github.smaugfm.monobudget.common.model.financial.StatementItem
import kotlinx.coroutines.CompletableDeferred

sealed class MaybeTransferStatement<TTransaction> {
sealed class MaybeTransfer<TTransaction> {
abstract val statement: StatementItem

data class Transfer<TTransaction>(
override val statement: StatementItem,
private val processed: TTransaction,
) : MaybeTransferStatement<TTransaction>() {
) : MaybeTransfer<TTransaction>() {
@Suppress("UNCHECKED_CAST")
fun <T : Any> processed(): T {
return processed as T
Expand All @@ -19,7 +19,7 @@ sealed class MaybeTransferStatement<TTransaction> {
class NotTransfer<TTransaction>(
override val statement: StatementItem,
private val processedDeferred: CompletableDeferred<TTransaction>,
) : MaybeTransferStatement<TTransaction>() {
) : MaybeTransfer<TTransaction>() {
@Volatile
private var ran = false

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.smaugfm.monobudget.common.account

import io.github.smaugfm.monobudget.common.misc.ConcurrentExpiringMap
import io.github.smaugfm.monobudget.common.model.financial.StatementItem
import io.github.smaugfm.monobudget.common.util.misc.ConcurrentExpiringMap
import kotlinx.coroutines.Deferred
import kotlin.time.Duration.Companion.minutes

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.smaugfm.monobudget.common.account

import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.misc.ConcurrentExpiringMap
import io.github.smaugfm.monobudget.common.model.financial.StatementItem
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.util.misc.ConcurrentExpiringMap
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Deferred

Expand All @@ -14,7 +14,7 @@ abstract class TransferDetector<TTransaction>(
private val ctx: StatementProcessingContext,
private val cache: ConcurrentExpiringMap<StatementItem, Deferred<TTransaction>>,
) {
suspend fun checkTransfer(): MaybeTransferStatement<TTransaction> =
suspend fun checkForTransfer(): MaybeTransfer<TTransaction> =
ctx.getOrPut("transfer") {
val existingTransfer =
cache.entries.firstOrNull { (recentStatementItem) ->
Expand All @@ -27,12 +27,12 @@ abstract class TransferDetector<TTransaction>(
"Current: ${ctx.item}\n" +
"Recent transfer: $existingTransfer"
}
MaybeTransferStatement.Transfer(ctx.item, existingTransfer)
MaybeTransfer.Transfer(ctx.item, existingTransfer)
} else {
val deferred = CompletableDeferred<TTransaction>()
cache.add(ctx.item, deferred)

MaybeTransferStatement.NotTransfer(ctx.item, deferred)
MaybeTransfer.NotTransfer(ctx.item, deferred)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.smaugfm.monobudget.common.category

import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smaugfm.monobudget.common.misc.MCC
import io.github.smaugfm.monobudget.common.model.financial.Amount
import io.github.smaugfm.monobudget.common.model.settings.MccOverrideSettings
import io.github.smaugfm.monobudget.common.util.MCCRegistry
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

Expand Down Expand Up @@ -33,7 +33,7 @@ abstract class CategoryService : KoinComponent {
mccOverride.mccToCategoryName[mcc] ?: categoryNameByMccGroup(mcc)

private fun categoryNameByMccGroup(mcc: Int): String? {
val mccObj = MCC.map[mcc]
val mccObj = MCCRegistry.map[mcc]
if (mccObj == null) {
log.warn { "Unknown MCC code $mcc" }
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.smaugfm.monobudget.common.exception

class BudgetBackendError(
class BudgetBackendException(
cause: Throwable,
val userMessage: String,
) : Exception(cause)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.smaugfm.monobudget.common.retry

import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingContext
import java.util.UUID
import kotlin.time.Duration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jsonMapper
import com.fasterxml.jackson.module.kotlin.kotlinModule
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingContext
import kotlinx.datetime.Instant
import java.nio.file.Path
import java.nio.file.StandardOpenOption
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.github.smaugfm.monobudget.common.retry

import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smaugfm.monobudget.common.exception.BudgetBackendError
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingEventListener
import io.github.smaugfm.monobudget.common.exception.BudgetBackendException
import io.github.smaugfm.monobudget.common.model.settings.RetrySettings
import io.github.smaugfm.monobudget.common.statement.StatementSource
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingEventListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -31,7 +31,7 @@ class RetryStatementSource(

override suspend fun handleRetry(
ctx: StatementProcessingContext,
e: BudgetBackendError,
e: BudgetBackendException,
) {
val request =
repository.addRetryRequest(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.smaugfm.monobudget.common.retry

import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingContext
import kotlin.time.Duration

interface StatementRetryRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.smaugfm.monobudget.common.retry

import com.fasterxml.jackson.annotation.JsonIgnore
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingContext
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.time.Duration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.smaugfm.monobudget.common.verify
package io.github.smaugfm.monobudget.common.startup

interface ApplicationStartupVerifier {
suspend fun verify()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.smaugfm.monobudget.common.verify
package io.github.smaugfm.monobudget.common.startup

import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smaugfm.lunchmoney.api.LunchmoneyApi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.smaugfm.monobudget.common.statement

import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingEventListener
import io.github.smaugfm.monobudget.common.model.financial.OtherBankStatementItem
import io.github.smaugfm.monobudget.common.model.financial.StatementItem
import io.github.smaugfm.monobudget.common.model.settings.OtherBanksTransferSettings
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingEventListener
import org.koin.core.annotation.Single

@Single
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.smaugfm.monobudget.common.statement

import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.model.financial.OtherBankStatementItem
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingContext
import kotlinx.coroutines.flow.MutableSharedFlow
import org.koin.core.annotation.Single

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.smaugfm.monobudget.common.statement

import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingContext
import kotlinx.coroutines.flow.Flow

interface StatementSource {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.smaugfm.monobudget.common.lifecycle
package io.github.smaugfm.monobudget.common.statement.lifecycle

import com.elbekd.bot.types.CallbackQuery
import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smaugfm.monobudget.common.exception.BudgetBackendError
import io.github.smaugfm.monobudget.common.exception.BudgetBackendException
import io.github.smaugfm.monobudget.common.model.callback.CallbackType
import io.github.smaugfm.monobudget.common.util.injectAll
import org.koin.core.annotation.Single
Expand All @@ -11,7 +11,7 @@ import org.koin.core.component.KoinComponent
private val log = KotlinLogging.logger {}

@Single
class StatementProcessingEventDelivery : KoinComponent {
class StatementEvents : KoinComponent {
private val newStatementListeners
by injectAll<StatementProcessingEventListener.New>()
private val statementEndListeners
Expand Down Expand Up @@ -57,7 +57,7 @@ class StatementProcessingEventDelivery : KoinComponent {

suspend fun onStatementRetry(
ctx: StatementProcessingContext,
e: BudgetBackendError,
e: BudgetBackendException,
) {
retryScheduledEventListeners.forEach { l ->
l.handleRetry(ctx, e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.smaugfm.monobudget.common.lifecycle
package io.github.smaugfm.monobudget.common.statement.lifecycle

import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smaugfm.monobudget.common.account.BankAccountService
Expand All @@ -25,7 +25,7 @@ abstract class StatementItemProcessor<TTransaction, TNewTransaction>(

private suspend fun processStatement() {
val maybeTransfer =
transferDetector.checkTransfer()
transferDetector.checkForTransfer()

val transaction = transactionFactory.create(maybeTransfer)
val message = messageFormatter.format(ctx.item, transaction)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.smaugfm.monobudget.common.lifecycle
package io.github.smaugfm.monobudget.common.statement.lifecycle

import io.github.smaugfm.monobudget.common.model.financial.StatementItem

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.smaugfm.monobudget.common.lifecycle
package io.github.smaugfm.monobudget.common.statement.lifecycle

import com.elbekd.bot.types.CallbackQuery
import io.github.smaugfm.monobudget.common.exception.BudgetBackendError
import io.github.smaugfm.monobudget.common.exception.BudgetBackendException
import io.github.smaugfm.monobudget.common.model.callback.CallbackType

sealed interface StatementProcessingEventListener {
Expand All @@ -23,7 +23,7 @@ sealed interface StatementProcessingEventListener {
interface Retry : StatementProcessingEventListener {
suspend fun handleRetry(
ctx: StatementProcessingContext,
e: BudgetBackendError,
e: BudgetBackendException,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.smaugfm.monobudget.common.lifecycle
package io.github.smaugfm.monobudget.common.statement.lifecycle

import org.koin.core.component.KoinScopeComponent
import org.koin.core.component.createScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.elbekd.bot.types.Message
import com.elbekd.bot.types.ParseMode
import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smaugfm.monobudget.common.category.CategoryService
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingEventDelivery
import io.github.smaugfm.monobudget.common.model.callback.ActionCallbackType
import io.github.smaugfm.monobudget.common.model.callback.ActionCallbackType.ChooseCategory
import io.github.smaugfm.monobudget.common.model.callback.CallbackType
Expand All @@ -18,6 +17,7 @@ import io.github.smaugfm.monobudget.common.model.callback.TransactionUpdateType.
import io.github.smaugfm.monobudget.common.model.callback.TransactionUpdateType.Uncategorize
import io.github.smaugfm.monobudget.common.model.callback.TransactionUpdateType.UpdateCategory
import io.github.smaugfm.monobudget.common.model.settings.MultipleAccountSettings
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementEvents
import io.github.smaugfm.monobudget.common.transaction.TransactionMessageFormatter
import io.github.smaugfm.monobudget.common.transaction.TransactionMessageFormatter.Companion.extractPayee
import io.github.smaugfm.monobudget.common.transaction.TransactionMessageFormatter.Companion.extractTransactionId
Expand All @@ -34,7 +34,7 @@ abstract class TelegramCallbackHandler<TTransaction> : KoinComponent {
private val formatter: TransactionMessageFormatter<TTransaction> by inject()
private val monoSettings: MultipleAccountSettings by inject()
private val telegramChatIds = monoSettings.telegramChatIds
private val statementEvents by inject<StatementProcessingEventDelivery>()
private val statementEvents by inject<StatementEvents>()

suspend fun handle(callbackQuery: CallbackQuery) {
var callbackType: CallbackType? = null
Expand Down
Loading

0 comments on commit 7d697ca

Please sign in to comment.