the-algorithm/pushservice/src/main/scala/com/twitter/frigate/pushservice/refresh_handler/RFPHStatsRecorder.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

78 lines
2.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.pushservice.model.PushTypes.PushCandidate
import com.twitter.frigate.thriftscala.CommonRecommendationType
class RFPHStatsRecorder(implicit statsReceiver: StatsReceiver) {
private val selectedCandidateScoreStats: StatsReceiver =
statsReceiver.scope("score_of_sent_candidate_times_10000")
private val emptyScoreStats: StatsReceiver =
statsReceiver.scope("score_of_sent_candidate_empty")
def trackPredictionScoreStats(candidate: PushCandidate): Unit = {
candidate.mrWeightedOpenOrNtabClickRankingProbability.foreach {
case Some(s) =>
selectedCandidateScoreStats
.stat("weighted_open_or_ntab_click_ranking")
.add((s * 10000).toFloat)
case None =>
emptyScoreStats.counter("weighted_open_or_ntab_click_ranking").incr()
}
candidate.mrWeightedOpenOrNtabClickFilteringProbability.foreach {
case Some(s) =>
selectedCandidateScoreStats
.stat("weighted_open_or_ntab_click_filtering")
.add((s * 10000).toFloat)
case None =>
emptyScoreStats.counter("weighted_open_or_ntab_click_filtering").incr()
}
candidate.mrWeightedOpenOrNtabClickRankingProbability.foreach {
case Some(s) =>
selectedCandidateScoreStats
.scope(candidate.commonRecType.toString)
.stat("weighted_open_or_ntab_click_ranking")
.add((s * 10000).toFloat)
case None =>
emptyScoreStats
.scope(candidate.commonRecType.toString)
.counter("weighted_open_or_ntab_click_ranking")
.incr()
}
}
def refreshRequestExceptionStats(
exception: Throwable,
bStats: StatsReceiver
): Unit = {
bStats.counter("failures").incr()
bStats.scope("failures").counter(exception.getClass.getCanonicalName).incr()
}
def loggedOutRequestExceptionStats(
exception: Throwable,
bStats: StatsReceiver
): Unit = {
bStats.counter("logged_out_failures").incr()
bStats.scope("failures").counter(exception.getClass.getCanonicalName).incr()
}
def rankDistributionStats(
candidatesDetails: Seq[CandidateDetails[PushCandidate]],
numRecsPerTypeStat: (CommonRecommendationType => Stat)
): Unit = {
candidatesDetails
.groupBy { c =>
c.candidate.commonRecType
}
.mapValues { s =>
s.size
}
.foreach { case (crt, numRecs) => numRecsPerTypeStat(crt).add(numRecs) }
}
}