Skip to content

Commit

Permalink
[+] Add play count to trend
Browse files Browse the repository at this point in the history
  • Loading branch information
hykilpikonna committed Feb 10, 2024
1 parent 5b2687a commit 3b65170
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ class Maimai2New(
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")
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()

// Assume it's sorted by date, map the values O(n)
val map = d.associate { it.playDate to it.afterRating }
// Precompute the play counts for each date in O(n)
val playCounts = d.groupingBy { it.playDate }.eachCount()

// Is sorting here necessary?
return map.map { TrendOut(it.key, it.value) }.sortedBy { it.date }
// Use the precomputed play counts
return d.distinctBy { it.playDate }
.map { TrendOut(it.playDate, it.afterRating, playCounts[it.playDate] ?: 0) }
.sortedBy { it.date }
}
}

0 comments on commit 3b65170

Please sign in to comment.