|
|
import pandas as pd |
|
|
from fastapi import HTTPException |
|
|
from models.pydantic_model import ContinentStats |
|
|
from logger import logger |
|
|
|
|
|
|
|
|
try: |
|
|
df = pd.read_csv("data/world_population.csv") |
|
|
logger.info("CSV file loaded successfully.") |
|
|
except Exception as e: |
|
|
logger.error(f"Failed to load CSV: {e}") |
|
|
raise HTTPException(status_code=500, detail="Internal server error") |
|
|
|
|
|
|
|
|
continent_stats = df.groupby("Continent").agg( |
|
|
Total_Countries=('Country', 'count'), |
|
|
Total_Population=('Population', 'sum'), |
|
|
Average_Population=('Population', 'mean'), |
|
|
Total_Area=('Area', 'sum'), |
|
|
max_population=('Population', 'max'), |
|
|
min_population=('Population', 'min'), |
|
|
).reset_index() |
|
|
|
|
|
|
|
|
continent_stats["Population_Density"] = ( |
|
|
continent_stats["Total_Population"] / continent_stats["Total_Area"] |
|
|
) |
|
|
|
|
|
def get_continent_stats_service(continent: str) -> ContinentStats: |
|
|
""" |
|
|
Fetch statistics for a given continent. |
|
|
""" |
|
|
logger.info(f"Fetching statistics for continent: {continent}") |
|
|
|
|
|
result = continent_stats[continent_stats["Continent"] == continent] |
|
|
|
|
|
if result.empty: |
|
|
logger.warning(f"Continent not found: {continent}") |
|
|
raise HTTPException(status_code=404, detail="Continent not found") |
|
|
|
|
|
result_dict = result.iloc[0].to_dict() |
|
|
return ContinentStats(**result_dict) |
|
|
|