Ezhil commited on
Commit
15c3d68
·
1 Parent(s): fe4c053

changes in buisness service

Browse files
__pycache__/logger.cpython-310.pyc CHANGED
Binary files a/__pycache__/logger.cpython-310.pyc and b/__pycache__/logger.cpython-310.pyc differ
 
__pycache__/main.cpython-310.pyc CHANGED
Binary files a/__pycache__/main.cpython-310.pyc and b/__pycache__/main.cpython-310.pyc differ
 
logs/backend.log ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ 2025-02-12 11:16:48,494 - INFO - CSV file loaded successfully.
2
+ 2025-02-12 11:16:48,817 - INFO - Started server process [19640]
3
+ 2025-02-12 11:16:48,820 - INFO - Waiting for application startup.
4
+ 2025-02-12 11:16:48,823 - INFO - Application startup complete.
5
+ 2025-02-12 11:16:48,825 - INFO - Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
routers/__pycache__/continent.cpython-310.pyc ADDED
Binary file (567 Bytes). View file
 
routers/continent.py CHANGED
@@ -1,9 +1,11 @@
1
  from fastapi import APIRouter
2
- from services.continent_services import get_continent_stats_service
3
  from models.pydantic_model import ContinentStats
4
 
5
  router = APIRouter()
6
 
7
  @router.get("/{continent}/", response_model=ContinentStats)
8
  def get_continent_stats(continent: str):
9
- return get_continent_stats_service(continent)
 
 
 
1
  from fastapi import APIRouter
2
+ from services.continent_services import get_continent_data
3
  from models.pydantic_model import ContinentStats
4
 
5
  router = APIRouter()
6
 
7
  @router.get("/{continent}/", response_model=ContinentStats)
8
  def get_continent_stats(continent: str):
9
+ return get_continent_data(continent)
10
+
11
+
services/__pycache__/continent_services.cpython-310.pyc ADDED
Binary file (1.45 kB). View file
 
services/continent_services.py CHANGED
@@ -1,42 +1,49 @@
1
  import pandas as pd
2
- from fastapi import HTTPException
3
- from models.pydantic_model import ContinentStats
4
- from logger import logger
5
 
6
- # Load dataset
 
 
 
 
 
 
7
  try:
8
- df = pd.read_csv("data/world_population.csv")
9
- logger.info("CSV file loaded successfully.")
10
  except Exception as e:
11
- logger.error(f"Failed to load CSV: {e}")
12
- raise HTTPException(status_code=500, detail="Internal server error")
13
-
14
- # Aggregate statistics by continent
15
- continent_stats = df.groupby("Continent").agg(
16
- Total_Countries=('Country', 'count'),
17
- Total_Population=('Population', 'sum'),
18
- Average_Population=('Population', 'mean'),
19
- Total_Area=('Area', 'sum'),
20
- max_population=('Population', 'max'),
21
- min_population=('Population', 'min'),
22
- ).reset_index()
23
-
24
- # Compute Population Density
25
- continent_stats["Population_Density"] = (
26
- continent_stats["Total_Population"] / continent_stats["Total_Area"]
27
- )
28
-
29
- def get_continent_stats_service(continent: str) -> ContinentStats:
30
- """
31
- Fetch statistics for a given continent.
32
- """
33
- logger.info(f"Fetching statistics for continent: {continent}")
34
-
35
- result = continent_stats[continent_stats["Continent"] == continent]
 
 
 
36
 
37
  if result.empty:
38
- logger.warning(f"Continent not found: {continent}")
39
- raise HTTPException(status_code=404, detail="Continent not found")
40
 
41
- result_dict = result.iloc[0].to_dict()
42
- return ContinentStats(**result_dict)
 
1
  import pandas as pd
2
+ # from backend.population_pandas import get_continents, get_continent_data
3
+ from Backend.logger import logger
 
4
 
5
+ import os
6
+
7
+ # file_path = os.path.join(os.path.dirname(__file__), "../../data/world_population.csv")
8
+ file_path = os.path.join(os.path.dirname(__file__), "../data/world_population.csv")
9
+ # file_path = os.path.abspath(file_path) # Convert to absolute path
10
+
11
+ file_path = os.path.abspath(file_path) # Convert to absolute path
12
  try:
13
+ df = pd.read_csv(file_path)
14
+ logger.info(f"CSV file loaded successfully from: {file_path}")
15
  except Exception as e:
16
+ logger.error(f"Error loading CSV file from {file_path}: {e}")
17
+ df = None # Prevent NameError if file loading fails
18
+
19
+ if df is not None:
20
+ # Perform the aggregations only if df is successfully loaded
21
+ continent_stats = df.groupby("Continent").agg(
22
+ Total_Countries=('Country', 'count'),
23
+ Total_Population=('Population', 'sum'),
24
+ Average_Population=('Population', 'mean'),
25
+ Total_Area=('Area', 'sum'),
26
+ max_population=('Population', 'max'),
27
+ min_population=('Population', 'min'),
28
+ Country_Max_Population=('Population', lambda x: df.loc[x.idxmax(), 'Country']),
29
+ Country_Min_Population=('Population', lambda x: df.loc[x.idxmin(), 'Country'])
30
+ ).reset_index()
31
+
32
+ # Compute Population Density
33
+ continent_stats["Population_Density"] = (
34
+ continent_stats["Total_Population"] / continent_stats["Total_Area"]
35
+ )
36
+
37
+ logger.info("Data processing completed.")
38
+
39
+
40
+ def get_continent_data(continent):
41
+ """Returns statistics for a specific continent."""
42
+ logger.info(f"Fetching data for continent: {continent}")
43
+ result = continent_stats[continent_stats["Continent"] == continent].squeeze()
44
 
45
  if result.empty:
46
+ logger.warning(f"No data found for continent: {continent}")
47
+ return {}
48
 
49
+ return result.to_dict()