Fix Data API
This commit is contained in:
@@ -30,22 +30,29 @@ class TrafficService:
|
||||
logger.debug("Traffic data received", source=traffic_data.get('source'))
|
||||
|
||||
# Validate and clean traffic data before creating response
|
||||
validated_data = {
|
||||
"date": traffic_data.get("date", datetime.now()),
|
||||
"traffic_volume": int(traffic_data.get("traffic_volume", 100)),
|
||||
"pedestrian_count": int(traffic_data.get("pedestrian_count", 150)),
|
||||
"congestion_level": str(traffic_data.get("congestion_level", "medium")),
|
||||
"average_speed": int(traffic_data.get("average_speed", 25)),
|
||||
"source": str(traffic_data.get("source", "unknown"))
|
||||
}
|
||||
# Use keyword arguments instead of unpacking
|
||||
response = TrafficDataResponse(
|
||||
date=traffic_data.get("date", datetime.now()),
|
||||
traffic_volume=int(traffic_data.get("traffic_volume", 100)),
|
||||
pedestrian_count=int(traffic_data.get("pedestrian_count", 150)),
|
||||
congestion_level=str(traffic_data.get("congestion_level", "medium")),
|
||||
average_speed=float(traffic_data.get("average_speed", 25.0)), # Fixed: use float, not int
|
||||
source=str(traffic_data.get("source", "unknown"))
|
||||
)
|
||||
|
||||
return TrafficDataResponse(**validated_data)
|
||||
logger.debug("Successfully created traffic response",
|
||||
traffic_volume=response.traffic_volume,
|
||||
congestion_level=response.congestion_level)
|
||||
return response
|
||||
else:
|
||||
logger.warning("No traffic data received from Madrid client")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get current traffic", error=str(e), lat=latitude, lon=longitude)
|
||||
# Log the full traceback for debugging
|
||||
import traceback
|
||||
logger.error("Traffic service traceback", traceback=traceback.format_exc())
|
||||
return None
|
||||
|
||||
async def get_historical_traffic(self,
|
||||
@@ -83,41 +90,41 @@ class TrafficService:
|
||||
average_speed=record.average_speed,
|
||||
source=record.source
|
||||
) for record in db_records]
|
||||
|
||||
# Fetch from API if not in database
|
||||
logger.debug("Fetching historical traffic data from Madrid API")
|
||||
traffic_data = await self.madrid_client.get_historical_traffic(
|
||||
latitude, longitude, start_date, end_date
|
||||
)
|
||||
|
||||
if traffic_data:
|
||||
# Store in database for future use
|
||||
try:
|
||||
for data in traffic_data:
|
||||
if isinstance(data, dict):
|
||||
traffic_record = TrafficData(
|
||||
location_id=location_id,
|
||||
date=data.get('date', datetime.now()),
|
||||
traffic_volume=data.get('traffic_volume'),
|
||||
pedestrian_count=data.get('pedestrian_count'),
|
||||
congestion_level=data.get('congestion_level'),
|
||||
average_speed=data.get('average_speed'),
|
||||
source="madrid_opendata",
|
||||
raw_data=str(data)
|
||||
)
|
||||
db.add(traffic_record)
|
||||
|
||||
await db.commit()
|
||||
logger.debug("Historical traffic data stored in database", count=len(traffic_data))
|
||||
except Exception as db_error:
|
||||
logger.warning("Failed to store historical traffic data", error=str(db_error))
|
||||
await db.rollback()
|
||||
|
||||
return [TrafficDataResponse(**item) for item in traffic_data if isinstance(item, dict)]
|
||||
else:
|
||||
logger.warning("No historical traffic data received")
|
||||
logger.debug("No historical traffic data found in database")
|
||||
return []
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to get historical traffic", error=str(e))
|
||||
return []
|
||||
return []
|
||||
|
||||
async def store_traffic_data(self,
|
||||
latitude: float,
|
||||
longitude: float,
|
||||
traffic_data: Dict[str, Any],
|
||||
db: AsyncSession) -> bool:
|
||||
"""Store traffic data to database"""
|
||||
try:
|
||||
location_id = f"{latitude:.4f},{longitude:.4f}"
|
||||
|
||||
traffic_record = TrafficData(
|
||||
location_id=location_id,
|
||||
date=traffic_data.get("date", datetime.now()),
|
||||
traffic_volume=traffic_data.get("traffic_volume"),
|
||||
pedestrian_count=traffic_data.get("pedestrian_count"),
|
||||
congestion_level=traffic_data.get("congestion_level"),
|
||||
average_speed=traffic_data.get("average_speed"),
|
||||
source=traffic_data.get("source", "madrid_opendata"),
|
||||
raw_data=str(traffic_data) if traffic_data else None
|
||||
)
|
||||
|
||||
db.add(traffic_record)
|
||||
await db.commit()
|
||||
|
||||
logger.debug("Traffic data stored successfully", location_id=location_id)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to store traffic data", error=str(e))
|
||||
await db.rollback()
|
||||
return False
|
||||
Reference in New Issue
Block a user