-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from pingpingy1/main
[feat] 연령 히스토그램 endpoint 추가
- Loading branch information
Showing
7 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from pydantic import BaseModel | ||
from enum import StrEnum | ||
|
||
|
||
class AgeHistDataTypes(StrEnum): | ||
elected = "elected" | ||
candidate = "candidate" | ||
|
||
|
||
class AgeHistMethodTypes(StrEnum): | ||
equal = "equal" | ||
kmeans = "kmeans" | ||
|
||
|
||
class AgeHistDataPoint(BaseModel): | ||
minAge: int | ||
maxAge: int | ||
count: int | ||
ageGroup: int | ||
|
||
|
||
class MetroAgeHistData(BaseModel): | ||
metroId: int | ||
data: list[AgeHistDataPoint] | ||
|
||
|
||
class LocalAgeHistData(BaseModel): | ||
metroId: int | ||
localId: int | ||
data: list[AgeHistDataPoint] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
SUCCESS = 200 | ||
REGION_CODE_ERR = 400 | ||
COLLECTION_NOT_EXIST_ERR = 600 | ||
|
||
|
||
class MessageResponse(BaseModel): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from fastapi import APIRouter | ||
from model import BasicResponse, MongoDB | ||
from model.AgeHist import AgeHistDataTypes, AgeHistMethodTypes, MetroAgeHistData | ||
|
||
|
||
router = APIRouter(prefix="/localCouncil", tags=["localCouncil"]) | ||
|
||
|
||
@router.get("/age-hist/{metroId}") | ||
async def getMetroAgeHistData( | ||
metroId: int, ageHistType: AgeHistDataTypes, year: int, method: AgeHistMethodTypes | ||
) -> BasicResponse.ErrorResponse | MetroAgeHistData: | ||
if ( | ||
await MongoDB.client.district_db["metro_district"].find_one( | ||
{"metroId": metroId} | ||
) | ||
is None | ||
): | ||
return BasicResponse.ErrorResponse.model_validate( | ||
{ | ||
"error": "RegionCodeError", | ||
"code": BasicResponse.REGION_CODE_ERR, | ||
"message": f"No metro district with metroId {metroId}.", | ||
} | ||
) | ||
|
||
match ageHistType: | ||
case AgeHistDataTypes.elected: | ||
collection_name = f"지선-당선_{year}_1level_{method}" | ||
case AgeHistDataTypes.candidate: | ||
collection_name = f"지선-후보_{year}_1level_{method}" | ||
|
||
if collection_name not in await MongoDB.client.age_hist_db.list_collection_names(): | ||
return BasicResponse.ErrorResponse.model_validate( | ||
{ | ||
"error": "CollectionNotExistError", | ||
"code": BasicResponse.COLLECTION_NOT_EXIST_ERR, | ||
"message": f"No collection with name f{collection_name}. Perhaps the year is wrong?", | ||
} | ||
) | ||
|
||
histogram = await MongoDB.client.age_hist_db[collection_name].find_one( | ||
{"metroId": metroId} | ||
) | ||
|
||
return MetroAgeHistData.model_validate( | ||
{"metroId": metroId, "data": histogram["data"]} | ||
) | ||
|
||
|
||
@router.get("/age-hist/{metroId}/{localId}") | ||
async def getLocalAgeHistData( | ||
metroId: int, | ||
localId: int, | ||
ageHistType: AgeHistDataTypes, | ||
year: int, | ||
method: AgeHistMethodTypes, | ||
) -> BasicResponse.ErrorResponse | MetroAgeHistData: | ||
if ( | ||
await MongoDB.client.district_db["local_district"].find_one( | ||
{"metroId": metroId, "localId": localId} | ||
) | ||
is None | ||
): | ||
return BasicResponse.ErrorResponse.model_validate( | ||
{ | ||
"error": "RegionCodeError", | ||
"code": BasicResponse.REGION_CODE_ERR, | ||
"message": f"No local district with metroId {metroId} and localId {localId}.", | ||
} | ||
) | ||
|
||
match ageHistType: | ||
case AgeHistDataTypes.elected: | ||
collection_name = f"지선-당선_{year}_2level_{method}" | ||
case AgeHistDataTypes.candidate: | ||
collection_name = f"지선-후보_{year}_2level_{method}" | ||
|
||
if collection_name not in await MongoDB.client.age_hist_db.list_collection_names(): | ||
return BasicResponse.ErrorResponse.model_validate( | ||
{ | ||
"error": "CollectionNotExistError", | ||
"code": BasicResponse.COLLECTION_NOT_EXIST_ERR, | ||
"message": f"No collection with name f{collection_name}. Perhaps the year is wrong?", | ||
} | ||
) | ||
|
||
histogram = await MongoDB.client.age_hist_db[collection_name].find_one( | ||
{"metroId": metroId, "localId": localId} | ||
) | ||
|
||
return MetroAgeHistData.model_validate( | ||
{"metroId": metroId, "localId": localId, "data": histogram["data"]} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters