Skip to content

Commit

Permalink
[feat] 후보자 성별 데이터 제공
Browse files Browse the repository at this point in the history
  • Loading branch information
pingpingy1 committed Nov 30, 2023
1 parent a769866 commit 5031e8f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 17 deletions.
2 changes: 2 additions & 0 deletions model/ScrapResultLocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class GenderTemplateDataPoint(BaseModel):
localId: int
genderDiversityIndex: float
current: GenderTemplateDataPoint
currentCandidate: GenderTemplateDataPoint
prev: GenderTemplateDataPoint
prevCandidate: GenderTemplateDataPoint
meanMalePop: float
meanFemalePop: float

Expand Down
2 changes: 2 additions & 0 deletions model/ScrapResultMetro.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class GenderTemplateDataPoint(BaseModel):
metroId: int
genderDiversityIndex: float
current: GenderTemplateDataPoint
currentCandidate: GenderTemplateDataPoint
prev: GenderTemplateDataPoint
prevCandidate: GenderTemplateDataPoint
meanMalePop: float
meanFemalePop: float

Expand Down
2 changes: 2 additions & 0 deletions model/ScrapResultNational.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class GenderTemplateDataPoint(BaseModel):

genderDiversityIndex: float
current: GenderTemplateDataPoint
currentCandidate: GenderTemplateDataPoint
prev: GenderTemplateDataPoint
prevCandidate: GenderTemplateDataPoint


class AgeTemplateDataNational(BaseModel):
Expand Down
44 changes: 38 additions & 6 deletions routers/scrapResultLocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@router.get("/template-data/{metroId}/{localId}")
async def getLocalTemplateData(
metroId: int, localId: int, factor: FactorType, year:int = 2022
metroId: int, localId: int, factor: FactorType, year: int = 2022
) -> ErrorResponse | GenderTemplateDataLocal | AgeTemplateDataLocal | PartyTemplateDataLocal:
if (
await client.district_db["local_district"].find_one(
Expand Down Expand Up @@ -79,6 +79,17 @@ async def getLocalTemplateData(
}
)

current_candidate = await client.stats_db["gender_hist"].find_one(
{
"councilorType": "local_councilor",
"level": 2,
"is_elected": False,
"localId": localId,
"metroId": metroId,
"year": years[year_index],
}
)

previous = await client.stats_db["gender_hist"].find_one(
{
"councilorType": "local_councilor",
Expand All @@ -90,6 +101,17 @@ async def getLocalTemplateData(
}
)

previous_candidate = await client.stats_db["gender_hist"].find_one(
{
"councilorType": "local_councilor",
"level": 2,
"is_elected": False,
"localId": localId,
"metroId": metroId,
"year": years[year_index],
}
)

current_all = (
await client.stats_db["gender_hist"]
.aggregate(
Expand Down Expand Up @@ -127,11 +149,21 @@ async def getLocalTemplateData(
"malePop": current["남"],
"femalePop": current["여"],
},
"currentCandidate": {
"year": years[year_index],
"malePop": current_candidate["남"],
"femalePop": current_candidate["여"],
},
"prev": {
"year": years[year_index - 1],
"malePop": previous["남"],
"femalePop": previous["여"],
},
"prevCandidate": {
"year": years[year_index],
"malePop": previous_candidate["남"],
"femalePop": previous_candidate["여"],
},
"meanMalePop": current_all["male_tot"]
/ current_all["district_cnt"],
"meanFemalePop": current_all["female_tot"]
Expand Down Expand Up @@ -351,7 +383,7 @@ async def getLocalTemplateData(
year_index = years.index(year)
if year_index == 0:
return NO_DATA_ERROR_RESPONSE

current_elected = client.stats_db["party_hist"].find(
{
"councilorType": "local_councilor",
Expand Down Expand Up @@ -436,7 +468,7 @@ async def getLocalTemplateData(

@router.get("/chart-data/{metroId}/{localId}")
async def getLocalChartData(
metroId: int, localId: int, factor: FactorType, year:int = 2022
metroId: int, localId: int, factor: FactorType, year: int = 2022
) -> ErrorResponse | ChartData[GenderChartDataPoint] | ChartData[
AgeChartDataPoint
] | ChartData[PartyChartDataPoint]:
Expand Down Expand Up @@ -465,7 +497,7 @@ async def getLocalChartData(
"is_elected": True,
"localId": localId,
"metroId": metroId,
"year": year
"year": year,
}
)
.limit(1)
Expand All @@ -492,7 +524,7 @@ async def getLocalChartData(
"method": "equal",
"localId": localId,
"metroId": metroId,
"year": year
"year": year,
}
)
.limit(1)
Expand Down Expand Up @@ -525,7 +557,7 @@ async def getLocalChartData(
"is_elected": True,
"localId": localId,
"metroId": metroId,
"year": year
"year": year,
}
)
.limit(1)
Expand Down
42 changes: 36 additions & 6 deletions routers/scrapResultMetro.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@router.get("/template-data/{metroId}")
async def getMetroTemplateData(
metroId: int, factor: FactorType, year: int=2022
metroId: int, factor: FactorType, year: int = 2022
) -> ErrorResponse | GenderTemplateDataMetro | AgeTemplateDataMetro | PartyTemplateDataMetro:
if (
await client.district_db["metro_district"].find_one({"metroId": metroId})
Expand Down Expand Up @@ -71,13 +71,33 @@ async def getMetroTemplateData(
}
)

current_candidate = await client.stats_db["gender_hist"].find_one(
{
"councilorType": "metro_councilor",
"level": 1,
"is_elected": False,
"metroId": metroId,
"year": years[year_index],
}
)

previous = await client.stats_db["gender_hist"].find_one(
{
"councilorType": "metro_councilor",
"level": 1,
"is_elected": True,
"metroId": metroId,
"year": years[year_index-1],
"year": years[year_index - 1],
}
)

previous_candidate = await client.stats_db["gender_hist"].find_one(
{
"councilorType": "metro_councilor",
"level": 1,
"is_elected": False,
"metroId": metroId,
"year": years[year_index],
}
)

Expand Down Expand Up @@ -117,11 +137,21 @@ async def getMetroTemplateData(
"malePop": current["남"],
"femalePop": current["여"],
},
"currentCandidate": {
"year": years[year_index],
"malePop": current_candidate["남"],
"femalePop": current_candidate["여"],
},
"prev": {
"year": years[year_index-1],
"year": years[year_index - 1],
"malePop": previous["남"],
"femalePop": previous["여"],
},
"prevCandidate": {
"year": years[year_index],
"malePop": previous_candidate["남"],
"femalePop": previous_candidate["여"],
},
"meanMalePop": current_all["male_tot"]
/ current_all["district_cnt"],
"meanFemalePop": current_all["female_tot"]
Expand Down Expand Up @@ -161,7 +191,7 @@ async def getMetroTemplateData(
year_index = years.index(year)
if year_index == 0:
return NO_DATA_ERROR_RESPONSE

history_candidate = [
await client.stats_db["age_hist"].find_one(
{
Expand Down Expand Up @@ -417,7 +447,7 @@ async def getMetroTemplateData(

@router.get("/chart-data/{metroId}")
async def getMetroChartData(
metroId: int, factor: FactorType, year:int = 2022
metroId: int, factor: FactorType, year: int = 2022
) -> ErrorResponse | ChartData[GenderChartDataPoint] | ChartData[
AgeChartDataPoint
] | ChartData[PartyChartDataPoint]:
Expand Down Expand Up @@ -499,7 +529,7 @@ async def getMetroChartData(
"level": 1,
"is_elected": True,
"metroId": metroId,
"year": year
"year": year,
}
)
.to_list(5)
Expand Down
42 changes: 37 additions & 5 deletions routers/scrapResultNational.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@router.get("/template-data")
async def getNationalTemplateData(
factor: FactorType,
factor: FactorType, year: int = 2020
) -> ErrorResponse | GenderTemplateDataNational | AgeTemplateDataNational | PartyTemplateDataNational:
national_stat = await client.stats_db["diversity_index"].find_one(
{"national": True}
Expand All @@ -49,12 +49,25 @@ async def getNationalTemplateData(
years.sort()
assert len(years) >= 2

year_index = years.index(year)
if year_index == 0:
return NO_DATA_ERROR_RESPONSE

current = await client.stats_db["gender_hist"].find_one(
{
"councilorType": "national_councilor",
"level": 0,
"is_elected": True,
"year": years[-1],
"year": years[year_index],
}
)

current_candidate = await client.stats_db["gender_hist"].find_one(
{
"councilorType": "national_councilor",
"level": 0,
"is_elected": False,
"year": years[year_index - 1],
}
)

Expand All @@ -63,23 +76,42 @@ async def getNationalTemplateData(
"councilorType": "national_councilor",
"level": 0,
"is_elected": True,
"year": years[-1],
"year": years[year_index],
}
)

previous_candidate = await client.stats_db["gender_hist"].find_one(
{
"councilorType": "national_councilor",
"level": 0,
"is_elected": False,
"year": years[year_index - 1],
}
)

return GenderTemplateDataNational.model_validate(
{
"genderDiversityIndex": national_stat["genderDiversityIndex"],
"current": {
"year": years[-1],
"year": years[year_index],
"malePop": current["남"],
"femalePop": current["여"],
},
"currentCandidate": {
"year": years[year_index - 1],
"malePop": current_candidate["남"],
"femalePop": current_candidate["여"],
},
"prev": {
"year": years[-2],
"year": years[year_index],
"malePop": previous["남"],
"femalePop": previous["여"],
},
"prevCandidate": {
"year": years[year_index - 1],
"malePop": previous_candidate["남"],
"femalePop": previous_candidate["여"],
},
}
)

Expand Down

0 comments on commit 5031e8f

Please sign in to comment.