This commit is contained in:
William Woods 2023-07-17 21:38:35 -05:00 committed by GitHub
commit 6555342918
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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