Set to main

This commit is contained in:
William Woods 2023-04-03 04:31:24 +00:00
parent ef04e156fd
commit d73c0bdf73

View File

@ -1,39 +1,58 @@
package com.twitter.follow_recommendations.common.clients.geoduck
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, Singleton} import javax.inject.Inject
import javax.inject.Singleton
@Singleton @Singleton
class UserLocationFetcher @Inject() ( class UserLocationFetcher @Inject() (
locationServiceClient: LocationServiceClient, locationServiceClient: LocationServiceClient,
reverseGeocodeClient: ReverseGeocodeClient, reverseGeocodeClient: ReverseGeocodeClient,
statsReceiver: StatsReceiver) { statsReceiver: StatsReceiver) {
private val stats = statsReceiver.scope("user_location_fetcher")
def getGeohashAndCountryCode(userId: Option[Long], ipAddress: Option[String]): Stitch[Option[GeohashAndCountryCode]] = { private val stats: StatsReceiver = statsReceiver.scope("user_location_fetcher")
val totalRequestsCounter = stats.counter("requests").incr() 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")
val lscLocationStitch = Stitch.collect(userId.map(locationServiceClient.getGeohashAndCountryCode)).rescue { def getGeohashAndCountryCode(
case _: Exception => userId: Option[Long],
stats.counter("location_service_exception").incr() ipAddress: Option[String]
Stitch.None ): Stitch[Option[GeohashAndCountryCode]] = {
} totalRequestsCounter.incr()
val lscLocationStitch = Stitch
.collect {
userId.map(locationServiceClient.getGeohashAndCountryCode)
}.rescue {
case _: Exception =>
locationServiceExceptionCounter.incr()
Stitch.None
}
val ipLocationStitch = Stitch.collect(ipAddress.map(reverseGeocodeClient.getGeohashAndCountryCode)).rescue { val ipLocationStitch = Stitch
case _: Exception => .collect {
stats.counter("reverse_geocode_exception").incr() ipAddress.map(reverseGeocodeClient.getGeohashAndCountryCode)
Stitch.None }.rescue {
} case _: Exception =>
reverseGeocodeExceptionCounter.incr()
Stitch.None
}
Stitch.join(lscLocationStitch, ipLocationStitch).map { Stitch.join(lscLocationStitch, ipLocationStitch).map {
case (lscLocation, ipLocation) => case (lscLocation, ipLocation) => {
(lscLocation.flatMap(_.geohash).orElse(ipLocation.flatMap(_.geohash)), val geohash = lscLocation.flatMap(_.geohash).orElse(ipLocation.flatMap(_.geohash))
lscLocation.flatMap(_.countryCode).orElse(ipLocation.flatMap(_.countryCode))) match { val countryCode =
case (Some(geohash), Some(countryCode)) => lscLocation.flatMap(_.countryCode).orElse(ipLocation.flatMap(_.countryCode))
Some(GeohashAndCountryCode(geohash, countryCode)) (geohash, countryCode) match {
case _ => case (None, None) =>
stats.counter("empty").incr() emptyResponsesCounter.incr()
None None
case _ => Some(GeohashAndCountryCode(geohash, countryCode))
}
} }
} }
} }