Skip to content

Commit

Permalink
[feat] 변수명 통일 (wiwName, sdName, localId, metroId, gender)
Browse files Browse the repository at this point in the history
  • Loading branch information
pingpingy1 committed Nov 20, 2023
1 parent 002390d commit 4566b4b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 104 deletions.
5 changes: 2 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"}})

Expand Down
89 changes: 26 additions & 63 deletions model/ResponseType.py
Original file line number Diff line number Diff line change
@@ -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}]}}
}
18 changes: 8 additions & 10 deletions model/ScrapResult.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
from typing import TypeVar, Generic


class SexType(StrEnum):
class GenderType(StrEnum):
male = "남"
female = "여"


class FactorType(StrEnum):
sex = "sex"
gender = "gender"
age = "age"
party = "party"


# ==============================================
# = Template Data Types =
# ==============================================
class SexTemplateData(BaseModel):
sexDiversityIndex: float
class GenderTemplateData(BaseModel):
genderDiversityIndex: float


class AgeTemplateData(BaseModel):
Expand All @@ -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]
Expand Down
12 changes: 8 additions & 4 deletions routers/commonInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)
Expand Down
59 changes: 35 additions & 24 deletions routers/scrapResult.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand All @@ -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:
Expand All @@ -47,22 +45,28 @@ 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(
metroId: int, localId: int, factor: ScrapResult.FactorType
) -> 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
):
Expand All @@ -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:
Expand All @@ -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
]
}
)
)

0 comments on commit 4566b4b

Please sign in to comment.