From 37ec1935eadb3000c113c7348760f4d3dedc3256 Mon Sep 17 00:00:00 2001 From: William Woods Date: Mon, 3 Apr 2023 07:22:50 +0000 Subject: [PATCH] Refactored code that improves performance and readability --- .../geoduck/ReverseGeocodeClient.scala | 50 +++++-------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/follow-recommendations-service/common/src/main/scala/com/twitter/follow_recommendations/common/clients/geoduck/ReverseGeocodeClient.scala b/follow-recommendations-service/common/src/main/scala/com/twitter/follow_recommendations/common/clients/geoduck/ReverseGeocodeClient.scala index 576359128..628ad9a7f 100644 --- a/follow-recommendations-service/common/src/main/scala/com/twitter/follow_recommendations/common/clients/geoduck/ReverseGeocodeClient.scala +++ b/follow-recommendations-service/common/src/main/scala/com/twitter/follow_recommendations/common/clients/geoduck/ReverseGeocodeClient.scala @@ -1,7 +1,6 @@ package com.twitter.follow_recommendations.common.clients.geoduck import com.twitter.follow_recommendations.common.models.GeohashAndCountryCode -import com.twitter.geoduck.common.thriftscala.Location import com.twitter.geoduck.common.thriftscala.PlaceQuery import com.twitter.geoduck.common.thriftscala.ReverseGeocodeIPRequest import com.twitter.geoduck.service.thriftscala.GeoContext @@ -12,46 +11,21 @@ import javax.inject.Singleton @Singleton class ReverseGeocodeClient @Inject() (rgcService: ReverseGeocoder.MethodPerEndpoint) { - def getGeohashAndCountryCode(ipAddress: String): Stitch[GeohashAndCountryCode] = { - Stitch - .callFuture { - rgcService - .reverseGeocodeIp( - ReverseGeocodeIPRequest( - Seq(ipAddress), - PlaceQuery(None), - simpleReverseGeocode = true - ) // note: simpleReverseGeocode means that country code will be included in response - ).map { response => - response.found.get(ipAddress) match { - case Some(location) => getGeohashAndCountryCodeFromLocation(location) - case _ => GeohashAndCountryCode(None, None) - } - } - } - } - - private def getGeohashAndCountryCodeFromLocation(location: Location): GeohashAndCountryCode = { - val countryCode: Option[String] = location.simpleRgcResult.flatMap { _.countryCodeAlpha2 } - - val geohashString: Option[String] = location.geohash.flatMap { hash => - hash.stringGeohash.flatMap { hashString => - Some(ReverseGeocodeClient.truncate(hashString)) + def getGeohashAndCountryCode(ipAddress: String): Stitch[GeohashAndCountryCode] = + Stitch.callFuture { + rgcService.reverseGeocodeIp( + ReverseGeocodeIPRequest(Seq(ipAddress), PlaceQuery(None), simpleReverseGeocode = true) + ).map { response => + response.found.get(ipAddress) match { + case Some(location) => getGeohashAndCountryCodeFromLocation(location) + case _ => GeohashAndCountryCode(None, None) + } } } + private def getGeohashAndCountryCodeFromLocation(location: Location): GeohashAndCountryCode = { + val countryCode = location.simpleRgcResult.flatMap(_.countryCodeAlpha2) + val geohashString = location.geohash.flatMap(_.stringGeohash.map(_.take(4))) GeohashAndCountryCode(geohashString, countryCode) } - -} - -object ReverseGeocodeClient { - - val DefaultGeoduckIPRequestContext: GeoContext = - GeoContext(allPlaceTypes = true, includeGeohash = true, includeCountryCode = true) - - // All these geohashes are guessed by IP (Logical Location Source). - // So take the four letters to make sure it is consistent with LocationServiceClient - val GeohashLengthAfterTruncation = 4 - def truncate(geohash: String): String = geohash.take(GeohashLengthAfterTruncation) }