Fix resolve_offers_to_hub to use DISTANCE() instead of graph traversal
All checks were successful
Build Docker Image / build (push) Successful in 1m23s

This commit is contained in:
Ruslan Bakiev
2026-01-16 15:56:25 +07:00
parent b6f9b2d70b
commit 339db65514

View File

@@ -1075,14 +1075,59 @@ class Query(graphene.ObjectType):
return []
def resolve_offers_to_hub(self, info, hub_uuid, product_uuid, limit=10):
"""Get offers for a product with routes to hub."""
return self.resolve_find_product_routes(
info,
product_uuid=product_uuid,
to_uuid=hub_uuid,
limit_sources=limit,
limit_routes=1,
)
"""Get offers for a product with routes to hub using DISTANCE()."""
db = get_db()
nodes_col = db.collection('nodes')
# Get hub coordinates
hub = nodes_col.get(hub_uuid)
if not hub:
logger.info("Hub %s not found", hub_uuid)
return []
hub_lat = hub.get('latitude')
hub_lon = hub.get('longitude')
if hub_lat is None or hub_lon is None:
logger.info("Hub %s missing coordinates", hub_uuid)
return []
# Find offers for this product sorted by distance to hub
aql = """
FOR node IN nodes
FILTER node.node_type == 'offer'
FILTER node.product_uuid == @product_uuid
FILTER node.latitude != null AND node.longitude != null
LET dist = DISTANCE(node.latitude, node.longitude, @hub_lat, @hub_lon) / 1000
SORT dist ASC
LIMIT @limit
RETURN MERGE(node, {distance_km: dist})
"""
try:
cursor = db.aql.execute(aql, bind_vars={
'product_uuid': product_uuid,
'hub_lat': hub_lat,
'hub_lon': hub_lon,
'limit': limit
})
offers = list(cursor)
logger.info("Found %d offers for product %s near hub %s", len(offers), product_uuid, hub_uuid)
results = []
for offer in offers:
# Build route for each offer
routes = Query._build_routes(db, offer['_key'], hub_uuid, limit=1)
results.append(ProductRouteOptionType(
source_uuid=offer['_key'],
source_name=offer.get('name'),
source_lat=offer.get('latitude'),
source_lon=offer.get('longitude'),
distance_km=offer.get('distance_km'),
routes=routes,
))
return results
except Exception as e:
logger.error("Error getting offers to hub: %s", e)
return []
def resolve_delivery_to_hub(self, info, offer_uuid, hub_uuid):
"""Get delivery route from offer to hub."""