[+] Add play count to trend

pull/10/head
Azalea 2024-02-10 05:50:28 -05:00
parent 5b2687ae83
commit 3b6517090c
1 changed files with 8 additions and 6 deletions

View File

@ -14,17 +14,19 @@ class Maimai2New(
private val userPlaylogRepository: UserPlaylogRepository private val userPlaylogRepository: UserPlaylogRepository
) )
{ {
data class TrendOut(val date: String, val rating: Int) data class TrendOut(val date: String, val rating: Int, val plays: Int)
@GetMapping("trend") @GetMapping("trend")
fun trend(@RequestParam userId: Long): List<TrendOut> { fun trend(@RequestParam userId: Long): List<TrendOut> {
// O(n log n) sort TODO: It might be possible to optimize this // O(n log n) sort
val d = userPlaylogRepository.findByUser_Card_ExtId(userId).sortedBy { it.playDate }.toList() val d = userPlaylogRepository.findByUser_Card_ExtId(userId).sortedBy { it.playDate }.toList()
// Assume it's sorted by date, map the values O(n) // Precompute the play counts for each date in O(n)
val map = d.associate { it.playDate to it.afterRating } val playCounts = d.groupingBy { it.playDate }.eachCount()
// Is sorting here necessary? // Use the precomputed play counts
return map.map { TrendOut(it.key, it.value) }.sortedBy { it.date } return d.distinctBy { it.playDate }
.map { TrendOut(it.playDate, it.afterRating, playCounts[it.playDate] ?: 0) }
.sortedBy { it.date }
} }
} }