diff options
author | Linnea Gräf <nea@nea.moe> | 2025-05-01 21:46:32 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2025-05-01 21:46:42 +0200 |
commit | 00e37dc840f7dd9b4ec8d2aa7c17bebad08a2696 (patch) | |
tree | acd21a461b53b9e2990c9a313cc74efcee6b0c40 /server/analysis | |
parent | 0c60aa3278a9297e143ee4ad998c01b6ef2337b1 (diff) | |
download | LocalTransactionLedger-master.tar.gz LocalTransactionLedger-master.tar.bz2 LocalTransactionLedger-master.zip |
Diffstat (limited to 'server/analysis')
-rw-r--r-- | server/analysis/src/main/kotlin/moe/nea/ledger/analysis/DailyCashflow.kt | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/server/analysis/src/main/kotlin/moe/nea/ledger/analysis/DailyCashflow.kt b/server/analysis/src/main/kotlin/moe/nea/ledger/analysis/DailyCashflow.kt new file mode 100644 index 0000000..3dcb438 --- /dev/null +++ b/server/analysis/src/main/kotlin/moe/nea/ledger/analysis/DailyCashflow.kt @@ -0,0 +1,52 @@ +package moe.nea.ledger.analysis + +import com.google.auto.service.AutoService +import moe.nea.ledger.ItemChange +import moe.nea.ledger.ItemId +import moe.nea.ledger.TransactionType +import moe.nea.ledger.database.DBItemEntry +import moe.nea.ledger.database.DBLogEntry +import moe.nea.ledger.database.sql.Clause +import java.sql.Connection +import java.time.LocalDate +import kotlin.collections.component1 +import kotlin.collections.component2 + +@AutoService(Analysis::class) +class DailyCashflow : Analysis { + override val id: String + get() = "daily-cashflow" + override val name: String + get() = "Daily Cashflow" + + override fun perform( + database: Connection, + filter: AnalysisFilter + ): AnalysisResult { + val query = DBLogEntry.from(database) + .join(DBItemEntry, Clause { column(DBItemEntry.transactionId) eq column(DBLogEntry.transactionId) }) + .where(Clause { column(DBItemEntry.itemId) eq ItemId.COINS }) + .select(DBItemEntry.size, DBLogEntry.transactionId) + filter.applyTo(query) + val spentThatDay = mutableMapOf<LocalDate, Double>() + for (resultRow in query) { + val timestamp = resultRow[DBLogEntry.transactionId].getTimestamp() + val damage = resultRow[DBItemEntry.size] + val localZone = filter.timeZone() + val localDate = timestamp.atZone(localZone).toLocalDate() + spentThatDay.merge(localDate, damage) { a, b -> a + b } + } + return AnalysisResult( + listOf( + Visualization( + "Daily Cashflow", + xLabel = "Day", + yLabel = "Coins +/-", + dataPoints = spentThatDay.entries.map { (k, v) -> + DataPoint(k.atTime(12, 0).atZone(filter.timeZone()).toInstant(), v) + } + ) + ) + ) + } +}
\ No newline at end of file |