ratings auto clearup

This commit is contained in:
InsanusMokrassar 2024-11-06 20:48:21 +06:00
parent 569f15330c
commit aeb3bf1f3d
4 changed files with 46 additions and 2 deletions

View File

@ -2,6 +2,10 @@
## 0.8.0 ## 0.8.0
* Dependencies update
* `Ratings`:
* Add autoclearing of ratings without target posts each half hours
## 0.7.0 ## 0.7.0
* Dependencies update * Dependencies update

View File

@ -11,6 +11,7 @@ kotlin {
dependencies { dependencies {
api project(":plaguposter.common") api project(":plaguposter.common")
api project(":plaguposter.posts") api project(":plaguposter.posts")
api libs.krontab
} }
} }
} }

View File

@ -0,0 +1,18 @@
package dev.inmo.plaguposter.ratings.models
import dev.inmo.krontab.EveryHourScheduler
import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.buildSchedule
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable
internal data class RatingsConfig(
@SerialName("manualRecheckKrontab")
val manualRecheckKrontabTemplate: KrontabTemplate = "0 /30 *"
) {
@Transient
val manualRecheckKrontab
get() = buildSchedule(manualRecheckKrontabTemplate)
}

View File

@ -1,20 +1,29 @@
package dev.inmo.plaguposter.ratings package dev.inmo.plaguposter.ratings
import dev.inmo.krontab.utils.asTzFlowWithDelays
import dev.inmo.kslog.common.TagLogger
import dev.inmo.kslog.common.i
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.singleWithBinds import dev.inmo.micro_utils.koin.singleWithBinds
import dev.inmo.micro_utils.repos.unset import dev.inmo.micro_utils.repos.unset
import dev.inmo.plagubot.Plugin import dev.inmo.plagubot.Plugin
import dev.inmo.plagubot.config
import dev.inmo.plagubot.database import dev.inmo.plagubot.database
import dev.inmo.plagubot.registerConfig
import dev.inmo.plaguposter.common.useCache import dev.inmo.plaguposter.common.useCache
import dev.inmo.plaguposter.posts.repo.PostsRepo import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.ratings.Plugin.setupBotPlugin
import dev.inmo.plaguposter.ratings.exposed.ExposedRatingsRepo import dev.inmo.plaguposter.ratings.exposed.ExposedRatingsRepo
import dev.inmo.plaguposter.ratings.models.RatingsConfig
import dev.inmo.plaguposter.ratings.repo.* import dev.inmo.plaguposter.ratings.repo.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.serialization.json.* import kotlinx.serialization.json.*
import org.koin.core.Koin import org.koin.core.Koin
import org.koin.core.module.Module import org.koin.core.module.Module
object Plugin : Plugin { object Plugin : Plugin {
private val Log = TagLogger("RatingsPlugin")
override fun Module.setupDI(config: JsonObject) { override fun Module.setupDI(config: JsonObject) {
single { ExposedRatingsRepo(database) } single { ExposedRatingsRepo(database) }
singleWithBinds<RatingsRepo> { singleWithBinds<RatingsRepo> {
@ -26,12 +35,24 @@ object Plugin : Plugin {
base base
} }
} }
registerConfig(RatingsConfig.serializer(), "ratings") { RatingsConfig() }
} }
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
val config = koin.config<RatingsConfig>()
val scope = koin.get<CoroutineScope>()
val ratingsRepo = koin.get<RatingsRepo>() val ratingsRepo = koin.get<RatingsRepo>()
koin.get<PostsRepo>().deletedObjectsIdsFlow.subscribeSafelyWithoutExceptions(this) { val postsRepo = koin.get<PostsRepo>()
postsRepo.deletedObjectsIdsFlow.subscribeSafelyWithoutExceptions(scope) {
ratingsRepo.unset(it) ratingsRepo.unset(it)
} }
config.manualRecheckKrontab.asTzFlowWithDelays().subscribeSafelyWithoutExceptions(scope) {
Log.i { "Start clearing ratings without registered posts" }
val postsIdsToRemove = ratingsRepo.getAll().keys - postsRepo.getAll().keys
Log.i { "Posts to remove: $postsIdsToRemove" }
ratingsRepo.unset(postsIdsToRemove.toList())
}
} }
} }