Skip to content

Commit

Permalink
feat: 仅保留最新1000条点击记录
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Oct 17, 2023
1 parent 699ad8b commit 3efe50d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
18 changes: 18 additions & 0 deletions app/src/main/java/li/songe/gkd/data/ClickLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,23 @@ data class ClickLog(

@Query("SELECT * FROM click_log ORDER BY id DESC LIMIT 1")
fun queryLatest(): Flow<ClickLog?>


@Query(
"""
DELETE FROM click_log
WHERE (
SELECT COUNT(*)
FROM click_log
) > 1000
AND id <= (
SELECT id
FROM click_log
ORDER BY id DESC
LIMIT 1 OFFSET 1000
)
"""
)
suspend fun deleteKeepLatest(): Int
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/li/songe/gkd/service/AbState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fun isAvailableRule(rule: Rule): Boolean {
if (rule.preRules.isNotEmpty()) { // 需要提前点击某个规则
lastTriggerRuleFlow.value ?: return false
// 上一个点击的规则不在当前需要点击的列表
return rule.preRules.any { it == lastTriggerRuleFlow.value }
return rule.preRules.any { it === lastTriggerRuleFlow.value }
}
if (rule.delayTriggerTime > 0) {
if (rule.delayTriggerTime + rule.delay > System.currentTimeMillis()) {
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import li.songe.gkd.util.Singleton
import li.songe.gkd.util.increaseClickCount
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.launchWhile
import li.songe.gkd.util.recordStoreFlow
import li.songe.gkd.util.storeFlow
import li.songe.gkd.util.subsIdToRawFlow
import li.songe.gkd.util.subsItemsFlow
Expand Down Expand Up @@ -148,6 +149,9 @@ class GkdAbService : CompositionAbService({
val nodeVal = safeActiveWindow ?: continue
val target = rule.query(nodeVal) ?: continue


if (currentRules !== currentRulesFlow.value) break

// 开始延迟
if (rule.delay > 0 && rule.delayTriggerTime == 0L) {
rule.triggerDelay()
Expand Down Expand Up @@ -176,9 +180,11 @@ class GkdAbService : CompositionAbService({
)
DbSet.clickLogDao.insert(clickLog)
increaseClickCount()
if (recordStoreFlow.value.clickCount % 100 == 0) {
DbSet.clickLogDao.deleteKeepLatest()
}
}
}
if (currentRules !== currentRulesFlow.value) break
}

}
Expand Down Expand Up @@ -233,9 +239,9 @@ class GkdAbService : CompositionAbService({
topActivity to currentRules
}.debounce(300).collect { (topActivity, currentRules) ->
if (storeFlow.value.enableService) {
LogUtils.d(
topActivity, *currentRules.map { r -> r.rule.matches }.toTypedArray()
)
LogUtils.d(topActivity,
*currentRules.map { r -> "subsId:${r.subsItem.id}, subsVersion:${subsIdToRawFlow.value[r.subsItem.id]?.version} gKey=${r.group.key}, gName:${r.group.name}, ruleIndex:${r.index}, rKey:${r.key}" }
.toTypedArray())
} else {
LogUtils.d(
topActivity
Expand Down

0 comments on commit 3efe50d

Please sign in to comment.