Skip to content

Commit

Permalink
Merge pull request #7 from pingpingy1/main
Browse files Browse the repository at this point in the history
[feat] 연령 템플릿 데이터 수정
  • Loading branch information
happycastle114 authored Nov 23, 2023
2 parents 3c426d0 + a6a4123 commit 32a9633
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 79 deletions.
4 changes: 2 additions & 2 deletions model/MongoDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ def __init__(self):
self.client = None
self.council_db = None
self.district_db = None
self.age_hist_db = None
self.stats_db = None

def connect(self):
self.client = AsyncIOMotorClient(os.getenv("MONGO_CONNECTION_URI"))
self.council_db = AsyncIOMotorDatabase(self.client, "council")
self.district_db = AsyncIOMotorDatabase(self.client, "district")
self.age_hist_db = AsyncIOMotorDatabase(self.client, "age_hist")
self.stats_db = AsyncIOMotorDatabase(self.client, "stats")

def close(self):
self.client.close()
Expand Down
42 changes: 41 additions & 1 deletion model/ScrapResult.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,47 @@ class GenderTemplateData(BaseModel):


class AgeTemplateData(BaseModel):
ageDiversityIndex: float
class AgeRankingParagraphData(BaseModel):
class AgeRankingAllIndices(BaseModel):
localId: int
rank: int
ageDiversityIndex: float

ageDiversityIndex: float
allIndices: list[AgeRankingAllIndices]

class AgeIndexHistoryParagraphData(BaseModel):
class AgeIndexHistoryIndexData(BaseModel):
year: int
unit: int
candidateCount: int
candidateDiversityIndex: float
candidateDiversityRank: int
electedDiversityIndex: float
electedDiversityRank: int

mostRecentYear: int
history: list[AgeIndexHistoryIndexData]

class AgeHistogramParagraphData(BaseModel):
class AgeHistogramAreaData(BaseModel):
localId: int
firstQuintile: int
lastQuintile: int

year: int
candidateCount: int
electedCount: int
firstQuintile: int
lastQuintile: int
divArea: AgeHistogramAreaData
uniArea: AgeHistogramAreaData

metroId: int
localId: int
rankingParagraph: AgeRankingParagraphData
indexHistoryParagraph: AgeIndexHistoryParagraphData
ageHistogramParagraph: AgeHistogramParagraphData


class PartyTemplateData(BaseModel):
Expand Down
55 changes: 21 additions & 34 deletions routers/ageHist.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,16 @@ async def getMetroAgeHistData(
}
)

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}
histogram = await MongoDB.client.stats_db["age_hist"].find_one(
{
"level": 1,
"councilorType": (
"elected" if ageHistType == AgeHistDataTypes.elected else "candidate"
),
"year": year,
"method": method,
"metroId": metroId,
}
)

return MetroAgeHistData.model_validate(
Expand Down Expand Up @@ -70,23 +63,17 @@ async def getLocalAgeHistData(
}
)

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}
histogram = await MongoDB.client.stats_db["age_hist"].find_one(
{
"level": 2,
"councilorType": (
"elected" if ageHistType == AgeHistDataTypes.elected else "candidate"
),
"year": year,
"method": method,
"metroId": metroId,
"localId": localId,
}
)

return MetroAgeHistData.model_validate(
Expand Down
Loading

0 comments on commit 32a9633

Please sign in to comment.