From 683970395b431f901df9ea5a72c63be542474e2d Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Thu, 17 May 2018 02:25:20 +0800 Subject: [PATCH] WIP: account stats implementation --- .../content/database/dao/AccountDailyStats.kt | 38 +++++++++++-------- .../task/worker/AccountDailyStatWorker.kt | 5 +-- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/content/database/dao/AccountDailyStats.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/content/database/dao/AccountDailyStats.kt index 1a3c24736..05362d546 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/content/database/dao/AccountDailyStats.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/content/database/dao/AccountDailyStats.kt @@ -81,22 +81,21 @@ abstract class AccountDailyStats { val firstNonNullIndex = indexOfFirst { it != null } if (firstNonNullIndex < 0) throw DataNotReadyException() - val diffs = LongArray(size - firstNonNullIndex) item@{ index -> - val statIndex = index + firstNonNullIndex - if (statIndex == 0) return@item 0 + val diffs = LongArray(size - firstNonNullIndex - 1) item@{ index -> + val statIndex = index + firstNonNullIndex + 1 return@item numberAt(statIndex, AccountStats::statusesCount) - numberAt(statIndex - 1, AccountStats::statusesCount) } - var maxDiff = diffs.max()!! - if (maxDiff <= 0L) { - maxDiff = Math.abs(diffs.min()!!) + var positiveRange = diffs.max()!! + if (positiveRange <= 0L) { + positiveRange = Math.abs(diffs.min()!!) } - if (maxDiff <= 0L) { - maxDiff = 10 + if (positiveRange <= 0L) { + positiveRange = 10 } - val values = FloatArray(size - firstNonNullIndex) item@{ index -> - return@item diffs[index] / maxDiff.toFloat() + val values = FloatArray(diffs.size) item@{ index -> + return@item diffs[index] / positiveRange.toFloat() } val periodSum = lastNumber - firstNumber @@ -104,20 +103,27 @@ abstract class AccountDailyStats { } private fun Array.followersSummary(): AccountStats.DisplaySummary { + val maxNumber = maxBy { it?.followersCount ?: Long.MIN_VALUE }!!.followersCount - val firstNumber = numberAt(0, AccountStats::followersCount) + val firstNonNullIndex = indexOfFirst { it != null } + if (firstNonNullIndex < 0) throw DataNotReadyException() + + val firstNumber = numberAt(firstNonNullIndex, AccountStats::followersCount) val lastNumber = numberAt(lastIndex, AccountStats::followersCount) - val maxNumber = maxBy { it?.followersCount ?: Long.MIN_VALUE }!!.followersCount - val valuesCount = size - val values = indices.map { index -> + var positiveRange = maxNumber - firstNumber + if (positiveRange <= 0L) { + positiveRange = 10 + } + + val values = (firstNonNullIndex..lastIndex).map { index -> val number = numberAt(index, AccountStats::followersCount) - return@map (number - firstNumber) / (maxNumber - firstNumber).toFloat() + return@map (number - firstNumber) / positiveRange.toFloat() }.toFloatArray() val growth = lastNumber - firstNumber return AccountStats.DisplaySummary(firstNumber, growth.sign, Math.abs(growth).toString(), - valuesCount, values) + size, values) } private fun Array.numberAt(index: Int, selector: (AccountStats) -> Long): Long { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/worker/AccountDailyStatWorker.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/worker/AccountDailyStatWorker.kt index 48e40410f..0439cc765 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/worker/AccountDailyStatWorker.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/worker/AccountDailyStatWorker.kt @@ -22,10 +22,7 @@ class AccountDailyStatWorker : Worker() { val am = AccountManager.get(applicationContext) val dao = TwidereDatabase.get(applicationContext).accountDailyStats() val date = Date() - val existingStats = dao.list(date) - val accounts = am.getAllDetails(true).filterNot { account -> - existingStats.any { it.accountKey == account.key } - } + val accounts = am.getAllDetails(true) val stats = accounts.mapNotNull { val user = try { when (it.type) {