the-algorithm/pushservice/src/main/scala/com/twitter/frigate/pushservice/refresh_handler/RFPHRestrictStep.scala
twitter-team b389c3d302 Open-sourcing pushservice
Pushservice is the main recommendation service we use to surface recommendations to our users via notifications. It fetches candidates from various sources, ranks them in order of relevance, and applies filters to determine the best one to send.
2023-05-19 16:27:07 -05:00

35 lines
1.6 KiB
Scala

package com.twitter.frigate.pushservice.refresh_handler
import com.twitter.finagle.stats.Stat
import com.twitter.finagle.stats.StatsReceiver
import com.twitter.frigate.common.base.CandidateDetails
import com.twitter.frigate.common.base.TargetUser
import com.twitter.frigate.common.candidate.TargetABDecider
import com.twitter.frigate.pushservice.model.PushTypes.PushCandidate
import com.twitter.frigate.pushservice.params.PushFeatureSwitchParams
import com.twitter.frigate.pushservice.target.TargetScoringDetails
class RFPHRestrictStep()(implicit stats: StatsReceiver) {
private val statsReceiver: StatsReceiver = stats.scope("RefreshForPushHandler")
private val restrictStepStats: StatsReceiver = statsReceiver.scope("restrict")
private val restrictStepNumCandidatesDroppedStat: Stat =
restrictStepStats.stat("candidates_dropped")
/**
* Limit the number of candidates that enter the Take step
*/
def restrict(
target: TargetUser with TargetABDecider with TargetScoringDetails,
candidates: Seq[CandidateDetails[PushCandidate]]
): (Seq[CandidateDetails[PushCandidate]], Seq[CandidateDetails[PushCandidate]]) = {
if (target.params(PushFeatureSwitchParams.EnableRestrictStep)) {
val restrictSizeParam = PushFeatureSwitchParams.RestrictStepSize
val (newCandidates, filteredCandidates) = candidates.splitAt(target.params(restrictSizeParam))
val numDropped = candidates.length - newCandidates.length
restrictStepNumCandidatesDroppedStat.add(numDropped)
(newCandidates, filteredCandidates)
} else (candidates, Seq.empty)
}
}