mirror of
https://github.com/twitter/the-algorithm.git
synced 2024-12-22 18:21:51 +01:00
Merge 0917e50b70
into 72eda9a24f
This commit is contained in:
commit
cb8902669e
@ -1,11 +1,9 @@
|
|||||||
package com.twitter.follow_recommendations.common.clients.geoduck
|
package com.twitter.follow_recommendations.common.clients.geoduck
|
||||||
|
|
||||||
import com.twitter.finagle.stats.StatsReceiver
|
import com.twitter.finagle.stats.StatsReceiver
|
||||||
import com.twitter.follow_recommendations.common.models.GeohashAndCountryCode
|
import com.twitter.follow_recommendations.common.models.GeohashAndCountryCode
|
||||||
import com.twitter.stitch.Stitch
|
import com.twitter.stitch.Stitch
|
||||||
|
|
||||||
import javax.inject.Inject
|
import javax.inject.{Inject, Singleton}
|
||||||
import javax.inject.Singleton
|
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class UserLocationFetcher @Inject() (
|
class UserLocationFetcher @Inject() (
|
||||||
@ -13,46 +11,32 @@ class UserLocationFetcher @Inject() (
|
|||||||
reverseGeocodeClient: ReverseGeocodeClient,
|
reverseGeocodeClient: ReverseGeocodeClient,
|
||||||
statsReceiver: StatsReceiver) {
|
statsReceiver: StatsReceiver) {
|
||||||
|
|
||||||
private val stats: StatsReceiver = statsReceiver.scope("user_location_fetcher")
|
private val stats = statsReceiver.scope("user_location_fetcher")
|
||||||
private val totalRequestsCounter = stats.counter("requests")
|
|
||||||
private val emptyResponsesCounter = stats.counter("empty")
|
|
||||||
private val locationServiceExceptionCounter = stats.counter("location_service_exception")
|
|
||||||
private val reverseGeocodeExceptionCounter = stats.counter("reverse_geocode_exception")
|
|
||||||
|
|
||||||
def getGeohashAndCountryCode(
|
def getGeohashAndCountryCode(userId: Option[Long], ipAddress: Option[String]): Stitch[Option[GeohashAndCountryCode]] = {
|
||||||
userId: Option[Long],
|
val totalRequestsCounter = stats.counter("requests").incr()
|
||||||
ipAddress: Option[String]
|
|
||||||
): Stitch[Option[GeohashAndCountryCode]] = {
|
val lscLocationStitch = Stitch.collect(userId.map(locationServiceClient.getGeohashAndCountryCode)).rescue {
|
||||||
totalRequestsCounter.incr()
|
|
||||||
val lscLocationStitch = Stitch
|
|
||||||
.collect {
|
|
||||||
userId.map(locationServiceClient.getGeohashAndCountryCode)
|
|
||||||
}.rescue {
|
|
||||||
case _: Exception =>
|
case _: Exception =>
|
||||||
locationServiceExceptionCounter.incr()
|
stats.counter("location_service_exception").incr()
|
||||||
Stitch.None
|
Stitch.None
|
||||||
}
|
}
|
||||||
|
|
||||||
val ipLocationStitch = Stitch
|
val ipLocationStitch = Stitch.collect(ipAddress.map(reverseGeocodeClient.getGeohashAndCountryCode)).rescue {
|
||||||
.collect {
|
|
||||||
ipAddress.map(reverseGeocodeClient.getGeohashAndCountryCode)
|
|
||||||
}.rescue {
|
|
||||||
case _: Exception =>
|
case _: Exception =>
|
||||||
reverseGeocodeExceptionCounter.incr()
|
stats.counter("reverse_geocode_exception").incr()
|
||||||
Stitch.None
|
Stitch.None
|
||||||
}
|
}
|
||||||
|
|
||||||
Stitch.join(lscLocationStitch, ipLocationStitch).map {
|
Stitch.join(lscLocationStitch, ipLocationStitch).map {
|
||||||
case (lscLocation, ipLocation) => {
|
case (lscLocation, ipLocation) =>
|
||||||
val geohash = lscLocation.flatMap(_.geohash).orElse(ipLocation.flatMap(_.geohash))
|
(lscLocation.flatMap(_.geohash).orElse(ipLocation.flatMap(_.geohash)),
|
||||||
val countryCode =
|
lscLocation.flatMap(_.countryCode).orElse(ipLocation.flatMap(_.countryCode))) match {
|
||||||
lscLocation.flatMap(_.countryCode).orElse(ipLocation.flatMap(_.countryCode))
|
case (Some(geohash), Some(countryCode)) =>
|
||||||
(geohash, countryCode) match {
|
Some(GeohashAndCountryCode(geohash, countryCode))
|
||||||
case (None, None) =>
|
case _ =>
|
||||||
emptyResponsesCounter.incr()
|
stats.counter("empty").incr()
|
||||||
None
|
None
|
||||||
case _ => Some(GeohashAndCountryCode(geohash, countryCode))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user