feat: rotate recommendations after a few plays
CI / chart (push) Successful in 3s
CI / test (push) Successful in 2m4s
CI / chart-push (push) Successful in 3s
CI / image (push) Failing after 6m21s

- V12 rebuilds mood_recommendations with a rotated state, play_count and
  rotated_at, plus a per-mood recommendation_rotation_plays (default 3)
- count a play when a session reports started; retire the slot at the limit
  and let TopUpService refill it, with discovery only when the pool is empty
- add POST /moods/{id}/recommendations/rotate for the All new action, which
  retires every active slot and forces discovery
- turned a rotated slot's late download completion into REJECTED so it cannot
  reappear as ready
- revive rotated slots after a 30-day cooldown to avoid draining the catalog
- expose playCount and recommendationRotationPlays in the API, chart 0.6.0
This commit is contained in:
TapTap committed 2026-11-22 17:31:28 +00:00
1 parent 44b75c4d4d
commit a3c5da559b
15 files changed
+507 -8

No files matched your search

@@ -7,6 +7,8 @@ import io.ktor.server.routing.Route
import io.ktor.server.routing.delete
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import java.time.Instant
import org.slf4j.LoggerFactory
import win.taptap.musicmanager.config.UserAccount
import win.taptap.musicmanager.config.UsersConfig
import win.taptap.musicmanager.db.MoodRecord
@@ -14,12 +16,14 @@ import win.taptap.musicmanager.db.MoodRepository
import win.taptap.musicmanager.db.PlaybackSessionRecord
import win.taptap.musicmanager.db.RadioRepository
import win.taptap.musicmanager.db.SongRepository
import win.taptap.musicmanager.discovery.TopUpService
import win.taptap.musicmanager.media.MediaGcService
import win.taptap.musicmanager.radio.RadioService
fun Route.sessionRoutes(
users: UsersConfig,
radioService: RadioService,
topUpService: TopUpService,
gc: MediaGcService,
) {
post("/moods/{id}/sessions") {
@@ -88,10 +92,30 @@ fun Route.sessionRoutes(
val user = call.requireApiUser(users)
val session = ownedSession(call.parameters["id"], user)
val request = call.receiveJson<PlaybackEventsRequest>()
val playedAt = Instant.now()
val retired = mutableListOf<String>()
val accepted = request.events.count { event ->
event.type in EVENT_TYPES &&
val recorded = event.type in EVENT_TYPES &&
event.songId.isNotBlank() &&
RadioRepository.recordEvent(session.id, user.name, event.songId, event.type)
// A play is counted when it starts, so a crash or an offline finish
// cannot make the counter lag behind what the user actually heard.
if (recorded && event.type == "started") {
MoodRepository.recordPlay(session.moodId, event.songId, playedAt)?.let {
retired += it.externalId
log.info(
"Rotated recommendation {} in mood {} after {} plays",
it.externalId,
session.moodId,
it.plays,
)
}
}
recorded
}
if (retired.isNotEmpty()) {
// Refill in the background; the response must not wait for a search.
topUpService.trigger(session.moodId)
}
call.respond(PlaybackEventsResponse(accepted))
}
@@ -138,5 +162,7 @@ private fun ownedSession(id: String?, user: UserAccount): PlaybackSessionRecord
return RadioRepository.get(id, user.name) ?: notFound("Session")
}
private val log = LoggerFactory.getLogger("win.taptap.musicmanager.api.Sessions")
private const val DEFAULT_BATCH = 15
private val EVENT_TYPES = setOf("started", "completed", "skipped")