tharu22 commited on
Commit
22ca0f5
·
0 Parent(s):

first commit

Browse files
__pycache__/main.cpython-313.pyc ADDED
Binary file (699 Bytes). View file
 
app/__init__.py ADDED
File without changes
app/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (147 Bytes). View file
 
app/__pycache__/main.cpython-313.pyc ADDED
Binary file (703 Bytes). View file
 
app/data/world_population.csv ADDED
File without changes
app/logs/app.log ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ 2025-02-08 10:29:28,697 - INFO - Home page accessed.
2
+ 2025-02-08 10:35:45,991 - INFO - Home page accessed.
3
+ 2025-02-08 10:37:00,452 - INFO - Home page accessed.
4
+ 2025-02-08 10:37:59,156 - INFO - Home page accessed.
5
+ 2025-02-08 10:38:14,407 - INFO - Fetched stats for Asia
6
+ 2025-02-08 10:40:38,126 - INFO - Home page accessed.
app/models/__pycache__/continent.cpython-313.pyc ADDED
Binary file (1.38 kB). View file
 
app/models/continent.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel, Field
2
+
3
+ class ContinentStats(BaseModel):
4
+ Max: str = Field(description="Max population of a country in the continent")
5
+ Min: str = Field(description="Min population of a country in the continent")
6
+ Avgerage: str = Field(description="Average population in the continent")
7
+ Area: str = Field(description="Total area of the continent")
8
+ Sum: str = Field(description="Total population of the continent")
9
+ density: str = Field(description="Population density of the continent")
10
+ model_config = {
11
+ "json_schema_extra": {
12
+ "examples": [
13
+ {
14
+ "continent": "Asia",
15
+ "total_population": 4641054775,
16
+ "total_area": 44579000,
17
+ "continent_population_density": 104.1,
18
+ "max_population": {"country": "China", "population": 1411778724},
19
+ "min_population": {"country": "Maldives", "population": 521874},
20
+ "avg_population": 92821192
21
+ }
22
+ ]
23
+ }
24
+ }
app/routers/__pycache__/stats.cpython-313.pyc ADDED
Binary file (2.88 kB). View file
 
app/routers/stats.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from fastapi import APIRouter, HTTPException
3
+ from app.models.continent import ContinentStats
4
+ from app.utils.logger import logger
5
+
6
+ # Router instance
7
+ router = APIRouter()
8
+
9
+ # Load the CSV data
10
+ file_path = "C:/Users/keerthi/Downloads/world_population.csv"
11
+ df = pd.read_csv(file_path)
12
+
13
+ @router.get("/{continent}", response_model=ContinentStats)
14
+ def get_continent_stats(continent: str):
15
+ try:
16
+ # The logic for calculating stats (same as your current code)
17
+
18
+
19
+ max_population = df.groupby('Continent')['Population'].max()
20
+ min_population = df.groupby('Continent')['Population'].min()
21
+ max_country = df.groupby('Continent')['Country'].max()
22
+ min_country = df.groupby('Continent')['Country'].min()
23
+ average_population = df.groupby('Continent')['Population'].mean()
24
+ total_area = df.groupby('Continent')['Area'].sum()
25
+ total_population = df.groupby('Continent')['Population'].sum() # Prepare the response
26
+ response = {}
27
+
28
+ # Max population and country
29
+ max_pop = max_population[continent]
30
+ max_pop_country = max_country[continent]
31
+ response["Max"] = f"{continent}'s maximum population is {max_pop} in {max_pop_country}"
32
+
33
+ # Min population and country
34
+ min_pop = min_population[continent]
35
+ min_pop_country = min_country[continent]
36
+ response["Min"] = f"{continent}'s minimum population is {min_pop} in {min_pop_country}"
37
+
38
+ # Average population
39
+ avg_pop = average_population[continent]
40
+ response["Avgerage"] = f"{continent}'s average population is {avg_pop}"
41
+
42
+ # Area
43
+ area = total_area[continent]
44
+ response["Area"] = f"{continent}'s total area is {area}"
45
+
46
+ # Total population
47
+ total_pop = total_population[continent]
48
+ response["Sum"] = f"{continent}'s total population is {total_pop}"
49
+
50
+ # Population density
51
+ continent_density = total_population / total_area
52
+ response["density"] = f"{continent}'s population density is {continent_density[continent]}"
53
+
54
+ # Log the request and result
55
+ logger.info(f"Fetched stats for {continent}")
56
+
57
+ return response
58
+
59
+ if continent not in df['Continent'].values:
60
+ raise HTTPException(status_code=404, detail=f"Continent '{continent}' not found")
61
+ except Exception as e:
62
+ logger.error(f"Error occurred while fetching stats for {continent}: {e}")
63
+ raise HTTPException(status_code=500, detail="Internal Server Error")
app/utils/__pycache__/logger.cpython-313.pyc ADDED
Binary file (606 Bytes). View file
 
app/utils/logger.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ # Logging configuration
4
+ logging.basicConfig(
5
+ level=logging.INFO,
6
+ format="%(asctime)s - %(levelname)s - %(message)s",
7
+ handlers=[
8
+ logging.FileHandler("C:/java lab/python/pandas/pydantic/app/logs/app.log"), # Log to a file
9
+ logging.StreamHandler() # Log to console
10
+ ]
11
+ )
12
+
13
+ logger = logging.getLogger(__name__)
main.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from app.routers import stats
3
+ from app.utils.logger import logger
4
+
5
+ app = FastAPI()
6
+
7
+ # Include the stats router
8
+ app.include_router(stats.router)
9
+
10
+ @app.get("/")
11
+ def home():
12
+ logger.info("Home page accessed.")
13
+ return {"message": "Welcome to the Home Page"}
14
+