From d73c0bdf732e88657775a853bb73fd69a322520c Mon Sep 17 00:00:00 2001 From: William Woods Date: Mon, 3 Apr 2023 04:31:24 +0000 Subject: [PATCH] Set to main --- .../clients/geoduck/UserLocationFetcher.scala | 61 ++++++++++++------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/follow-recommendations-service/common/src/main/scala/com/twitter/follow_recommendations/common/clients/geoduck/UserLocationFetcher.scala b/follow-recommendations-service/common/src/main/scala/com/twitter/follow_recommendations/common/clients/geoduck/UserLocationFetcher.scala index d83208033..706ae5143 100644 --- a/follow-recommendations-service/common/src/main/scala/com/twitter/follow_recommendations/common/clients/geoduck/UserLocationFetcher.scala +++ b/follow-recommendations-service/common/src/main/scala/com/twitter/follow_recommendations/common/clients/geoduck/UserLocationFetcher.scala @@ -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.stitch.Stitch -import javax.inject.{Inject, Singleton} +import javax.inject.Inject +import javax.inject.Singleton @Singleton class UserLocationFetcher @Inject() ( locationServiceClient: LocationServiceClient, reverseGeocodeClient: ReverseGeocodeClient, statsReceiver: StatsReceiver) { - private val stats = statsReceiver.scope("user_location_fetcher") - def getGeohashAndCountryCode(userId: Option[Long], ipAddress: Option[String]): Stitch[Option[GeohashAndCountryCode]] = { - val totalRequestsCounter = stats.counter("requests").incr() + private val stats: StatsReceiver = 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") - val lscLocationStitch = Stitch.collect(userId.map(locationServiceClient.getGeohashAndCountryCode)).rescue { - case _: Exception => - stats.counter("location_service_exception").incr() - Stitch.None - } + def getGeohashAndCountryCode( + userId: Option[Long], + ipAddress: Option[String] + ): 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 { - case _: Exception => - stats.counter("reverse_geocode_exception").incr() - Stitch.None - } + val ipLocationStitch = Stitch + .collect { + ipAddress.map(reverseGeocodeClient.getGeohashAndCountryCode) + }.rescue { + case _: Exception => + reverseGeocodeExceptionCounter.incr() + Stitch.None + } Stitch.join(lscLocationStitch, ipLocationStitch).map { - case (lscLocation, ipLocation) => - (lscLocation.flatMap(_.geohash).orElse(ipLocation.flatMap(_.geohash)), - lscLocation.flatMap(_.countryCode).orElse(ipLocation.flatMap(_.countryCode))) match { - case (Some(geohash), Some(countryCode)) => - Some(GeohashAndCountryCode(geohash, countryCode)) - case _ => - stats.counter("empty").incr() + case (lscLocation, ipLocation) => { + val geohash = lscLocation.flatMap(_.geohash).orElse(ipLocation.flatMap(_.geohash)) + val countryCode = + lscLocation.flatMap(_.countryCode).orElse(ipLocation.flatMap(_.countryCode)) + (geohash, countryCode) match { + case (None, None) => + emptyResponsesCounter.incr() None + case _ => Some(GeohashAndCountryCode(geohash, countryCode)) + } } } }