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 }
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<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 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<AccountStats?>.numberAt(index: Int, selector: (AccountStats) -> Long): Long {

View File

@ -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) {