Refactored code for performance and readability

This commit is contained in:
William Woods 2023-04-03 07:35:00 +00:00
parent ec83d01dca
commit ab82629574
1 changed files with 36 additions and 41 deletions

View File

@ -7,56 +7,51 @@ import com.twitter.geoduck.common.thriftscala.TransactionLocation
import com.twitter.geoduck.common.thriftscala.UserLocationRequest import com.twitter.geoduck.common.thriftscala.UserLocationRequest
import com.twitter.geoduck.thriftscala.LocationService import com.twitter.geoduck.thriftscala.LocationService
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 LocationServiceClient @Inject() (locationService: LocationService.MethodPerEndpoint) { class LocationServiceClient @Inject() (locationService: LocationService.MethodPerEndpoint) {
def getGeohashAndCountryCode(userId: Long): Stitch[GeohashAndCountryCode] = { def getGeohashAndCountryCode(userId: Long): Stitch[GeohashAndCountryCode] = {
Stitch val request = UserLocationRequest(
.callFuture { Seq(userId),
locationService Some(PlaceQuery(allPlaceTypes = Some(true))),
.userLocation( simpleReverseGeocode = true
UserLocationRequest( )
Seq(userId),
Some(PlaceQuery(allPlaceTypes = Some(true))), for {
simpleReverseGeocode = true)) transactionLocation <- locationService.userLocation(request).map(_.found(userId))
.map(_.found.get(userId)).map { transactionLocationOpt => geohashOpt <- getGeohashFromTransactionLocation(transactionLocation)
val geohashOpt = transactionLocationOpt.flatMap(getGeohashFromTransactionLocation) countryCodeOpt = transactionLocation.simpleRgcResult.flatMap(_.countryCodeAlpha2)
val countryCodeOpt = } yield GeohashAndCountryCode(geohashOpt, countryCodeOpt)
transactionLocationOpt.flatMap(_.simpleRgcResult.flatMap(_.countryCodeAlpha2))
GeohashAndCountryCode(geohashOpt, countryCodeOpt)
}
}
} }
private[this] def getGeohashFromTransactionLocation( private[this] def getGeohashFromTransactionLocation(
transactionLocation: TransactionLocation transactionLocation: TransactionLocation
): Option[String] = { ): Stitch[Option[String]] = Stitch.from {
transactionLocation.geohash.flatMap { geohash => transactionLocation.geohash match {
val geohashPrefixLength = transactionLocation.locationSource match { case Some(geohash) =>
// if location source is logical, keep the first 4 chars in geohash val geohashPrefixLength = transactionLocation.locationSource match {
case Some(LocationSource.Logical) => Some(4) case Some(LocationSource.Logical) => Some(4)
// if location source is physical, keep the prefix according to accuracy case Some(LocationSource.Physical) =>
// accuracy is the accuracy of GPS readings in the unit of meter transactionLocation.coordinate.flatMap { coordinate =>
case Some(LocationSource.Physical) => coordinate.accuracy match {
transactionLocation.coordinate.flatMap { coordinate => case Some(accuracy) if accuracy < 50 => Some(7)
coordinate.accuracy match { case Some(accuracy) if accuracy < 200 => Some(6)
case Some(accuracy) if (accuracy < 50) => Some(7) case Some(accuracy) if accuracy < 1000 => Some(5)
case Some(accuracy) if (accuracy < 200) => Some(6) case Some(accuracy) if accuracy < 50000 => Some(4)
case Some(accuracy) if (accuracy < 1000) => Some(5) case Some(accuracy) if accuracy < 100000 => Some(3)
case Some(accuracy) if (accuracy < 50000) => Some(4) case _ => None
case Some(accuracy) if (accuracy < 100000) => Some(3) }
case _ => None
} }
} case Some(LocationSource.Model) => Some(4)
case Some(LocationSource.Model) => Some(4) case _ => None
case _ => None }
}
geohashPrefixLength match { geohashPrefixLength match {
case Some(l: Int) => geohash.stringGeohash.map(_.take(l)) case Some(l: Int) => Stitch.value(Some(geohash.stringGeohash.take(l)))
case _ => None case _ => Stitch.value(None)
} }
case _ => Stitch.value(None)
} }
} }
} }