/
TimelineRankerInNetworkCandidateSource.scala
51 lines (46 loc) · 2.27 KB
/
TimelineRankerInNetworkCandidateSource.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.twitter.product_mixer.component_library.candidate_source.timeline_ranker
import com.twitter.product_mixer.core.feature.Feature
import com.twitter.product_mixer.core.feature.featuremap.FeatureMapBuilder
import com.twitter.product_mixer.core.functional_component.candidate_source.CandidateSourceWithExtractedFeatures
import com.twitter.product_mixer.core.functional_component.candidate_source.CandidatesWithSourceFeatures
import com.twitter.product_mixer.core.model.common.identifier.CandidateSourceIdentifier
import com.twitter.product_mixer.core.pipeline.PipelineQuery
import com.twitter.stitch.Stitch
import com.twitter.timelineranker.{thriftscala => t}
import javax.inject.Inject
import javax.inject.Singleton
/**
* Map of tweetId -> sourceTweet of retweets present in Timeline Ranker candidates list.
* These tweets are used only for further ranking. They are not returned to the end user.
*/
object TimelineRankerInNetworkSourceTweetsByTweetIdMapFeature
extends Feature[PipelineQuery, Map[Long, t.CandidateTweet]]
@Singleton
class TimelineRankerInNetworkCandidateSource @Inject() (
timelineRankerClient: t.TimelineRanker.MethodPerEndpoint)
extends CandidateSourceWithExtractedFeatures[t.RecapQuery, t.CandidateTweet] {
override val identifier: CandidateSourceIdentifier =
CandidateSourceIdentifier("TimelineRankerInNetwork")
override def apply(
request: t.RecapQuery
): Stitch[CandidatesWithSourceFeatures[t.CandidateTweet]] = {
Stitch
.callFuture(timelineRankerClient.getRecycledTweetCandidates(Seq(request)))
.map { response: Seq[t.GetCandidateTweetsResponse] =>
val candidates =
response.headOption.flatMap(_.candidates).getOrElse(Seq.empty).filter(_.tweet.nonEmpty)
val sourceTweetsByTweetId =
response.headOption
.flatMap(_.sourceTweets).getOrElse(Seq.empty).filter(_.tweet.nonEmpty)
.map { candidate =>
(candidate.tweet.get.id, candidate)
}.toMap
val sourceTweetsByTweetIdMapFeature = FeatureMapBuilder()
.add(TimelineRankerInNetworkSourceTweetsByTweetIdMapFeature, sourceTweetsByTweetId)
.build()
CandidatesWithSourceFeatures(
candidates = candidates,
features = sourceTweetsByTweetIdMapFeature)
}
}
}