|
| 1 | +package org.simple.clinic.home.overdue |
| 2 | + |
| 3 | +import org.simple.clinic.feature.Feature |
| 4 | +import org.simple.clinic.feature.Features |
| 5 | +import org.simple.clinic.returnscore.LikelyToReturnIfCalledScoreType |
| 6 | +import org.simple.clinic.returnscore.ReturnScore |
| 7 | +import java.util.UUID |
| 8 | +import javax.inject.Inject |
| 9 | +import kotlin.math.max |
| 10 | +import kotlin.random.Random |
| 11 | + |
| 12 | +class OverdueAppointmentSorter @Inject constructor( |
| 13 | + private val returnScoreDao: ReturnScore.RoomDao, |
| 14 | + private val features: Features, |
| 15 | + private val random: Random = Random.Default |
| 16 | +) { |
| 17 | + |
| 18 | + fun sort(overdueAppointments: List<OverdueAppointment>): List<SortedOverdueAppointment> { |
| 19 | + |
| 20 | + if (!features.isEnabled(Feature.SortOverdueBasedOnReturnScore)) { |
| 21 | + return overdueAppointments.map { |
| 22 | + SortedOverdueAppointment( |
| 23 | + appointment = it, |
| 24 | + score = 0f, |
| 25 | + bucket = OverdueBucket.REMAINING |
| 26 | + ) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + val scores = returnScoreDao.getAllImmediate() |
| 31 | + .filter { it.scoreType == LikelyToReturnIfCalledScoreType } |
| 32 | + |
| 33 | + val scoreMap: Map<UUID, Float> = scores.associate { |
| 34 | + it.patientUuid to it.scoreValue |
| 35 | + } |
| 36 | + |
| 37 | + val withScores = overdueAppointments.map { appointment -> |
| 38 | + val score = scoreMap[appointment.appointment.patientUuid] ?: 0f |
| 39 | + appointment to score |
| 40 | + } |
| 41 | + |
| 42 | + val sorted = withScores.sortedByDescending { it.second } |
| 43 | + |
| 44 | + val total = sorted.size |
| 45 | + if (total == 0) return emptyList() |
| 46 | + |
| 47 | + val top20End = max((total * 0.2).toInt(), 1) |
| 48 | + val next30End = max((total * 0.5).toInt(), top20End) |
| 49 | + |
| 50 | + val top20 = sorted.take(top20End) |
| 51 | + val next30 = sorted.subList(top20End, next30End) |
| 52 | + val rest = sorted.drop(next30End) |
| 53 | + |
| 54 | + val topPickCount = max((top20.size * 0.5).toInt(), 1) |
| 55 | + val nextPickCount = max((next30.size * 0.5).toInt(), 1) |
| 56 | + |
| 57 | + val topPicked = top20.shuffled(random).take(topPickCount) |
| 58 | + val nextPicked = next30.shuffled(random).take(nextPickCount) |
| 59 | + |
| 60 | + val selectedAppointments = (topPicked + nextPicked) |
| 61 | + .map { it.first } |
| 62 | + .toSet() |
| 63 | + |
| 64 | + val topRemaining = top20.filterNot { it.first in selectedAppointments } |
| 65 | + val nextRemaining = next30.filterNot { it.first in selectedAppointments } |
| 66 | + |
| 67 | + fun mapToSorted( |
| 68 | + list: List<Pair<OverdueAppointment, Float>>, |
| 69 | + bucket: OverdueBucket |
| 70 | + ) = list.map { (appointment, score) -> |
| 71 | + SortedOverdueAppointment( |
| 72 | + appointment = appointment, |
| 73 | + score = score, |
| 74 | + bucket = bucket |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + return buildList { |
| 79 | + addAll(mapToSorted(topPicked, OverdueBucket.TOP_20)) |
| 80 | + addAll(mapToSorted(nextPicked, OverdueBucket.NEXT_30)) |
| 81 | + addAll(mapToSorted(topRemaining, OverdueBucket.TOP_20)) |
| 82 | + addAll(mapToSorted(nextRemaining, OverdueBucket.NEXT_30)) |
| 83 | + addAll(mapToSorted(rest, OverdueBucket.REMAINING)) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments