mirror of https://github.com/hykilpikonna/AquaDX
[+] Add play count to trend
parent
5b2687ae83
commit
3b6517090c
|
@ -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 }
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue