WIP: account stats implementation

This commit is contained in:
Mariotaku Lee 2018-05-17 02:25:20 +08:00
parent 8094bbec4e
commit 683970395b
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
2 changed files with 23 additions and 20 deletions

View File

@ -81,22 +81,21 @@ abstract class AccountDailyStats {
val firstNonNullIndex = indexOfFirst { it != null } val firstNonNullIndex = indexOfFirst { it != null }
if (firstNonNullIndex < 0) throw DataNotReadyException() if (firstNonNullIndex < 0) throw DataNotReadyException()
val diffs = LongArray(size - firstNonNullIndex) item@{ index -> val diffs = LongArray(size - firstNonNullIndex - 1) item@{ index ->
val statIndex = index + firstNonNullIndex val statIndex = index + firstNonNullIndex + 1
if (statIndex == 0) return@item 0
return@item numberAt(statIndex, AccountStats::statusesCount) - numberAt(statIndex - 1, return@item numberAt(statIndex, AccountStats::statusesCount) - numberAt(statIndex - 1,
AccountStats::statusesCount) AccountStats::statusesCount)
} }
var maxDiff = diffs.max()!! var positiveRange = diffs.max()!!
if (maxDiff <= 0L) { if (positiveRange <= 0L) {
maxDiff = Math.abs(diffs.min()!!) positiveRange = Math.abs(diffs.min()!!)
} }
if (maxDiff <= 0L) { if (positiveRange <= 0L) {
maxDiff = 10 positiveRange = 10
} }
val values = FloatArray(size - firstNonNullIndex) item@{ index -> val values = FloatArray(diffs.size) item@{ index ->
return@item diffs[index] / maxDiff.toFloat() return@item diffs[index] / positiveRange.toFloat()
} }
val periodSum = lastNumber - firstNumber val periodSum = lastNumber - firstNumber
@ -104,20 +103,27 @@ abstract class AccountDailyStats {
} }
private fun Array<AccountStats?>.followersSummary(): AccountStats.DisplaySummary { private fun Array<AccountStats?>.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 lastNumber = numberAt(lastIndex, AccountStats::followersCount)
val maxNumber = maxBy { it?.followersCount ?: Long.MIN_VALUE }!!.followersCount var positiveRange = maxNumber - firstNumber
val valuesCount = size if (positiveRange <= 0L) {
val values = indices.map { index -> positiveRange = 10
}
val values = (firstNonNullIndex..lastIndex).map { index ->
val number = numberAt(index, AccountStats::followersCount) val number = numberAt(index, AccountStats::followersCount)
return@map (number - firstNumber) / (maxNumber - firstNumber).toFloat() return@map (number - firstNumber) / positiveRange.toFloat()
}.toFloatArray() }.toFloatArray()
val growth = lastNumber - firstNumber val growth = lastNumber - firstNumber
return AccountStats.DisplaySummary(firstNumber, growth.sign, Math.abs(growth).toString(), return AccountStats.DisplaySummary(firstNumber, growth.sign, Math.abs(growth).toString(),
valuesCount, values) size, values)
} }
private fun Array<AccountStats?>.numberAt(index: Int, selector: (AccountStats) -> Long): Long { private fun Array<AccountStats?>.numberAt(index: Int, selector: (AccountStats) -> Long): Long {

View File

@ -22,10 +22,7 @@ class AccountDailyStatWorker : Worker() {
val am = AccountManager.get(applicationContext) val am = AccountManager.get(applicationContext)
val dao = TwidereDatabase.get(applicationContext).accountDailyStats() val dao = TwidereDatabase.get(applicationContext).accountDailyStats()
val date = Date() val date = Date()
val existingStats = dao.list(date) val accounts = am.getAllDetails(true)
val accounts = am.getAllDetails(true).filterNot { account ->
existingStats.any { it.accountKey == account.key }
}
val stats = accounts.mapNotNull { val stats = accounts.mapNotNull {
val user = try { val user = try {
when (it.type) { when (it.type) {