From 4566b4bcbcd60193544e1e0be1bd80994e149b40 Mon Sep 17 00:00:00 2001 From: pingpingy1 Date: Mon, 20 Nov 2023 23:48:15 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EB=B3=80=EC=88=98=EB=AA=85=20?= =?UTF-8?q?=ED=86=B5=EC=9D=BC=20(wiwName,=20sdName,=20localId,=20metroId,?= =?UTF-8?q?=20gender)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 5 +-- model/ResponseType.py | 89 ++++++++++++------------------------------ model/ScrapResult.py | 18 ++++----- routers/commonInfo.py | 12 ++++-- routers/scrapResult.py | 59 ++++++++++++++++------------ 5 files changed, 79 insertions(+), 104 deletions(-) diff --git a/main.py b/main.py index d7a97d1..579505e 100644 --- a/main.py +++ b/main.py @@ -4,8 +4,7 @@ from contextlib import asynccontextmanager from typing import Dict from model import MongoDB -from model.ResponseType import ChartResponse, SexInfo, PartyInfo, AgeInfo - +from model.ResponseType import ChartResponse, GenderInfo, PartyInfo, AgeInfo @asynccontextmanager @@ -15,7 +14,7 @@ async def initMongo(app: FastAPI): MongoDB.client.close() -new = ChartResponse[SexInfo] +new = ChartResponse[GenderInfo] app = FastAPI(lifespan=initMongo, responses={404: {"description": "Not found"}}) diff --git a/model/ResponseType.py b/model/ResponseType.py index 50153f7..0f28302 100644 --- a/model/ResponseType.py +++ b/model/ResponseType.py @@ -1,101 +1,64 @@ from pydantic import BaseModel from typing import TypeVar, Generic + class LocalInfo(BaseModel): - name : str - id : int + name: str + id: int class RegionInfo(BaseModel): - name : str - id : int + name: str + id: int local: list[LocalInfo] -class PartyInfo(BaseModel): - name : str - color : int - model_config = { - "json_schema_extra": { - "example" : { - "name": "정상이당", - "count": 10 - } - } - } +class PartyInfo(BaseModel): + name: str + color: int + model_config = {"json_schema_extra": {"example": {"name": "정상이당", "count": 10}}} class Diversity(BaseModel): - action_type : str - value : float + action_type: str + value: float model_config = { - "json_schema_extra": { - "example" : { - "action_type": "sex", - "value": 0.5 - } - } + "json_schema_extra": {"example": {"action_type": "gender", "value": 0.5}} } - class AgeInfo(BaseModel): minAge: int maxAge: int count: int model_config = { - "json_schema_extra": { - "example" : { - "minAge": 10, - "maxAge": 20, - "count": 10 - } - } + "json_schema_extra": {"example": {"minAge": 10, "maxAge": 20, "count": 10}} } + class PartyInfo(BaseModel): - party : str - count : int + party: str + count: int - model_config = { - "json_schema_extra": { - "example" : { - "party": "숭구리당당", - "count": 10 - } - } - } + model_config = {"json_schema_extra": {"example": {"party": "숭구리당당", "count": 10}}} -class SexInfo(BaseModel): - sex: str + +class GenderInfo(BaseModel): + gender: str count: int - model_config = { - "json_schema_extra": { - "example" : { - "sex": "male", - "count": 10 - } - } - } + model_config = {"json_schema_extra": {"example": {"gender": "male", "count": 10}}} + + +T = TypeVar("T", GenderInfo, PartyInfo, AgeInfo) -T = TypeVar("T", SexInfo, PartyInfo, AgeInfo) class ChartResponse(BaseModel, Generic[T]): - data : list[T] + data: list[T] model_config = { - "json_schema_extra": { - "example" : { - "data": [ - { - "sex": "male", - "count": 10 - } - ] - } - } + "json_schema_extra": {"example": {"data": [{"gender": "male", "count": 10}]}} } diff --git a/model/ScrapResult.py b/model/ScrapResult.py index 4f3f422..d53b501 100644 --- a/model/ScrapResult.py +++ b/model/ScrapResult.py @@ -3,13 +3,13 @@ from typing import TypeVar, Generic -class SexType(StrEnum): +class GenderType(StrEnum): male = "남" female = "여" class FactorType(StrEnum): - sex = "sex" + gender = "gender" age = "age" party = "party" @@ -17,8 +17,8 @@ class FactorType(StrEnum): # ============================================== # = Template Data Types = # ============================================== -class SexTemplateData(BaseModel): - sexDiversityIndex: float +class GenderTemplateData(BaseModel): + genderDiversityIndex: float class AgeTemplateData(BaseModel): @@ -32,26 +32,24 @@ class PartyTemplateData(BaseModel): # ============================================== # = Chart Data Types = # ============================================== -class SexChartDataPoint(BaseModel): - sex: SexType +class GenderChartDataPoint(BaseModel): + gender: GenderType count: int - - class AgeChartDataPoint(BaseModel): minAge: int # 닫힌 구간 maxAge: int # 닫힌 구간 count: int - class PartyChartDataPoint(BaseModel): party: str count: int -T = TypeVar("T", SexChartDataPoint, AgeChartDataPoint, PartyChartDataPoint) +T = TypeVar("T", GenderChartDataPoint, AgeChartDataPoint, PartyChartDataPoint) + class ChartData(BaseModel, Generic[T]): data: list[T] diff --git a/routers/commonInfo.py b/routers/commonInfo.py index 528f0d0..fb2ecb6 100644 --- a/routers/commonInfo.py +++ b/routers/commonInfo.py @@ -13,13 +13,17 @@ async def getRegionInfo() -> list[CommonInfo.RegionInfo]: local_districts = [] async for local in MongoDB.client.district_db.get_collection( "local_district" - ).find({"metro_id": metro["metro_id"]}): - local_districts.append(CommonInfo.LocalInfo.model_validate({"name": local["name_ko"], "id": local["local_id"]})) + ).find({"metroId": metro["metroId"]}): + local_districts.append( + CommonInfo.LocalInfo.model_validate( + {"name": local["wiwName"], "id": local["localId"]} + ) + ) regions.append( CommonInfo.RegionInfo.model_validate( { - "name": metro["name_ko"], - "id": metro["metro_id"], + "name": metro["sdName"], + "id": metro["metroId"], "local": local_districts, } ) diff --git a/routers/scrapResult.py b/routers/scrapResult.py index 8c9ac72..d6d32cc 100644 --- a/routers/scrapResult.py +++ b/routers/scrapResult.py @@ -12,10 +12,10 @@ @router.get("/template-data/{metroId}/{localId}") async def getLocalTemplateData( metroId: int, localId: int, factor: ScrapResult.FactorType -) -> BasicResponse.ErrorResponse | ScrapResult.SexTemplateData | ScrapResult.AgeTemplateData | ScrapResult.PartyTemplateData: +) -> BasicResponse.ErrorResponse | ScrapResult.GenderTemplateData | ScrapResult.AgeTemplateData | ScrapResult.PartyTemplateData: if ( await MongoDB.client.district_db["local_district"].find_one( - {"local_id": localId, "metro_id": metroId} + {"localId": localId, "metroId": metroId} ) is None ): @@ -27,16 +27,14 @@ async def getLocalTemplateData( } ) - councilors = MongoDB.client.council_db["local_councilor"].find( - {"local_id": localId} - ) + councilors = MongoDB.client.council_db["local_councilor"].find({"localId": localId}) match factor: - case ScrapResult.FactorType.sex: - sex_list = [councilor["sex"] async for councilor in councilors] - sex_diversity_index = diversity.gini_simpson(sex_list) - return ScrapResult.SexTemplateData.model_validate( - {"sexDiversityIndex": sex_diversity_index} + case ScrapResult.FactorType.gender: + gender_list = [councilor["gender"] async for councilor in councilors] + gender_diversity_index = diversity.gini_simpson(gender_list) + return ScrapResult.GenderTemplateData.model_validate( + {"genderDiversityIndex": gender_diversity_index} ) case ScrapResult.FactorType.age: @@ -47,14 +45,20 @@ async def getLocalTemplateData( ) case ScrapResult.FactorType.party: - party_list = [councilor["party"] async for councilor in councilors] + party_list = [councilor["jdName"] async for councilor in councilors] party_diversity_index = diversity.gini_simpson(party_list) return ScrapResult.PartyTemplateData.model_validate( {"partyDiversityIndex": party_diversity_index} ) -T = TypeVar("T", ScrapResult.SexChartDataPoint, ScrapResult.AgeChartDataPoint, ScrapResult.PartyChartDataPoint) +T = TypeVar( + "T", + ScrapResult.GenderChartDataPoint, + ScrapResult.AgeChartDataPoint, + ScrapResult.PartyChartDataPoint, +) + @router.get("/chart-data/{metroId}/{localId}") async def getLocalChartData( @@ -62,7 +66,7 @@ async def getLocalChartData( ) -> BasicResponse.ErrorResponse | ScrapResult.ChartData[T]: if ( await MongoDB.client.district_db["local_district"].find_one( - {"local_id": localId, "metro_id": metroId} + {"localId": localId, "metroId": metroId} ) is None ): @@ -74,16 +78,21 @@ async def getLocalChartData( } ) - councilors = MongoDB.client.council_db["local_councilor"].find( - {"local_id": localId} - ) + councilors = MongoDB.client.council_db["local_councilor"].find({"localId": localId}) match factor: - case ScrapResult.FactorType.sex: - sex_list = [councilor["sex"] async for councilor in councilors] - sex_count = diversity.count(sex_list) - return ScrapResult.ChartData[ScrapResult.SexChartDataPoint].model_validate( - {"data": [{"sex": sex, "count": sex_count[sex]} for sex in sex_count]} + case ScrapResult.FactorType.gender: + gender_list = [councilor["gender"] async for councilor in councilors] + gender_count = diversity.count(gender_list) + return ScrapResult.ChartData[ + ScrapResult.GenderChartDataPoint + ].model_validate( + { + "data": [ + {"gender": gender, "count": gender_count[gender]} + for gender in gender_count + ] + } ) case ScrapResult.FactorType.age: @@ -103,13 +112,15 @@ async def getLocalChartData( ) case ScrapResult.FactorType.party: - party_list = [councilor["party"] async for councilor in councilors] + party_list = [councilor["jdName"] async for councilor in councilors] party_count = diversity.count(party_list) - return ScrapResult.ChartData[ScrapResult.PartyChartDataPoint].model_validate( + return ScrapResult.ChartData[ + ScrapResult.PartyChartDataPoint + ].model_validate( { "data": [ {"party": party, "count": party_count[party]} for party in party_count ] } - ) \ No newline at end of file + )