From 8a4caef4a1d18b8607c06990b45ef2373d41c647 Mon Sep 17 00:00:00 2001 From: Kevin Belisle Date: Wed, 30 Jun 2021 16:58:17 -0400 Subject: [PATCH] Only save new value if value changed --- .../xyz/etztech/stonks/StatisticsImporter.kt | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/xyz/etztech/stonks/StatisticsImporter.kt b/app/src/main/kotlin/xyz/etztech/stonks/StatisticsImporter.kt index 03ccc36..20aebda 100644 --- a/app/src/main/kotlin/xyz/etztech/stonks/StatisticsImporter.kt +++ b/app/src/main/kotlin/xyz/etztech/stonks/StatisticsImporter.kt @@ -4,8 +4,7 @@ import com.beust.klaxon.* import com.beust.klaxon.Klaxon import java.io.File import java.time.Instant -import org.jetbrains.exposed.sql.Database -import org.jetbrains.exposed.sql.insert +import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.`java-time`.timestamp import org.jetbrains.exposed.sql.transactions.transaction import xyz.etztech.stonks.dsl.Statistics @@ -24,14 +23,35 @@ object StatisticsImporter { val playerId = file.nameWithoutExtension transaction(database) { + val maxExpr = Statistics.value.max() + + val playerStats = emptyMap>().toMutableMap() + + Statistics.slice(Statistics.type, Statistics.name, maxExpr) + .select { Statistics.playerId.eq(playerId) } + .groupBy(Statistics.type, Statistics.name) + .forEach { + if (playerStats.containsKey(it[Statistics.type])) { + playerStats[it[Statistics.type]]?.put( + it[Statistics.name], it[maxExpr]!!) + } else { + playerStats.put( + it[Statistics.type], + mapOf(it[Statistics.name] to it[maxExpr]!!) + .toMutableMap()) + } + } + statsFile?.stats?.forEach { type, stats -> stats.forEach { name, value -> - Statistics.insert { - it[Statistics.playerId] = playerId - it[Statistics.type] = type - it[Statistics.name] = name - it[Statistics.timestamp] = Instant.now() as Instant - it[Statistics.value] = value + if (playerStats.get(type)?.get(name) != value) { + Statistics.insert { + it[Statistics.playerId] = playerId + it[Statistics.type] = type + it[Statistics.name] = name + it[Statistics.timestamp] = Instant.now() as Instant + it[Statistics.value] = value + } } } }