Skip to content

Commit

Permalink
Merge pull request #14 from NewWays-TechForImpactKAIST/13-연도-선택-추가
Browse files Browse the repository at this point in the history
13 연도 선택 추가
  • Loading branch information
happycastle114 committed Nov 30, 2023
2 parents 31e8f8b + 212cb6d commit a769866
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
41 changes: 27 additions & 14 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
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 @@ -64,14 +64,18 @@ async def getLocalTemplateData(
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": "local_councilor",
"level": 2,
"is_elected": True,
"localId": localId,
"metroId": metroId,
"year": years[-1],
"year": years[year_index],
}
)

Expand All @@ -82,7 +86,7 @@ async def getLocalTemplateData(
"is_elected": True,
"localId": localId,
"metroId": metroId,
"year": years[-2],
"year": years[year_index - 1],
}
)

Expand All @@ -95,7 +99,7 @@ async def getLocalTemplateData(
"councilorType": "local_councilor",
"level": 2,
"is_elected": True,
"year": years[-1],
"year": years[year_index],
}
},
{
Expand All @@ -119,12 +123,12 @@ async def getLocalTemplateData(
"localId": localId,
"genderDiversityIndex": local_stat["genderDiversityIndex"],
"current": {
"year": years[-1],
"year": years[year_index],
"malePop": current["남"],
"femalePop": current["여"],
},
"prev": {
"year": years[-2],
"year": years[year_index - 1],
"malePop": previous["남"],
"femalePop": previous["여"],
},
Expand Down Expand Up @@ -166,6 +170,11 @@ async def getLocalTemplateData(
}
)
years.sort()

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 @@ -217,7 +226,7 @@ async def getLocalTemplateData(
)
.to_list(500)
)[0]
most_recent_year = age_stat_elected["year"]
most_recent_year = year
age_stat_candidate = await client.stats_db["age_stat"].find_one(
{
"level": 2,
Expand Down Expand Up @@ -339,14 +348,18 @@ async def getLocalTemplateData(
years.sort()
assert len(years) >= 2

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",
"level": 2,
"is_elected": True,
"localId": localId,
"metroId": metroId,
"year": years[-1],
"year": years[year_index],
},
{
"_id": 0,
Expand All @@ -365,7 +378,7 @@ async def getLocalTemplateData(
"is_elected": False,
"localId": localId,
"metroId": metroId,
"year": years[-1],
"year": years[year_index],
},
{
"_id": 0,
Expand All @@ -384,7 +397,7 @@ async def getLocalTemplateData(
"is_elected": True,
"localId": localId,
"metroId": metroId,
"year": years[-2],
"year": years[year_index - 1],
},
{
"_id": 0,
Expand Down Expand Up @@ -423,7 +436,7 @@ async def getLocalTemplateData(

@router.get("/chart-data/{metroId}/{localId}")
async def getLocalChartData(
metroId: int, localId: int, factor: FactorType
metroId: int, localId: int, factor: FactorType, year:int = 2022
) -> ErrorResponse | ChartData[GenderChartDataPoint] | ChartData[
AgeChartDataPoint
] | ChartData[PartyChartDataPoint]:
Expand Down Expand Up @@ -452,9 +465,9 @@ async def getLocalChartData(
"is_elected": True,
"localId": localId,
"metroId": metroId,
"year": year
}
)
.sort({"year": -1})
.limit(1)
.to_list(5)
)[0]
Expand All @@ -479,9 +492,9 @@ async def getLocalChartData(
"method": "equal",
"localId": localId,
"metroId": metroId,
"year": year
}
)
.sort({"year": -1})
.limit(1)
.to_list(5)
)[0]
Expand Down Expand Up @@ -512,9 +525,9 @@ async def getLocalChartData(
"is_elected": True,
"localId": localId,
"metroId": metroId,
"year": year
}
)
.sort({"year": -1})
.limit(1)
.to_list(5)
)[0]
Expand Down
49 changes: 28 additions & 21 deletions routers/scrapResultMetro.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TypeVar
from fastapi import APIRouter
from model.BasicResponse import ErrorResponse, REGION_CODE_ERR
from model.BasicResponse import ErrorResponse, REGION_CODE_ERR, NO_DATA_ERROR_RESPONSE
from model.MongoDB import client
from model.ScrapResultCommon import (
GenderChartDataPoint,
Expand All @@ -24,7 +24,7 @@

@router.get("/template-data/{metroId}")
async def getMetroTemplateData(
metroId: int, factor: FactorType
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 @@ -57,14 +57,17 @@ async def getMetroTemplateData(
)
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": "metro_councilor",
"level": 1,
"is_elected": True,
"metroId": metroId,
"year": years[-1],
"year": years[year_index],
}
)

Expand All @@ -74,7 +77,7 @@ async def getMetroTemplateData(
"level": 1,
"is_elected": True,
"metroId": metroId,
"year": years[-2],
"year": years[year_index-1],
}
)

Expand All @@ -87,7 +90,7 @@ async def getMetroTemplateData(
"councilorType": "metro_councilor",
"level": 1,
"is_elected": True,
"year": years[-1],
"year": years[year_index],
}
},
{
Expand All @@ -110,12 +113,12 @@ async def getMetroTemplateData(
"metroId": metroId,
"genderDiversityIndex": metro_stat["genderDiversityIndex"],
"current": {
"year": years[-1],
"year": years[year_index],
"malePop": current["남"],
"femalePop": current["여"],
},
"prev": {
"year": years[-2],
"year": years[year_index-1],
"malePop": previous["남"],
"femalePop": previous["여"],
},
Expand Down Expand Up @@ -155,6 +158,10 @@ async def getMetroTemplateData(
}
)
years.sort()
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 @@ -195,15 +202,14 @@ async def getMetroTemplateData(
"councilorType": "metro_councilor",
"is_elected": True,
"metroId": metroId,
"year": years[year_index],
}
},
{"$sort": {"year": -1}},
{"$limit": 1},
]
)
.to_list(500)
)[0]
most_recent_year = age_stat_elected["year"]
most_recent_year = year
age_stat_candidate = await client.stats_db["age_stat"].find_one(
{
"level": 1,
Expand Down Expand Up @@ -259,7 +265,7 @@ async def getMetroTemplateData(
],
},
"indexHistoryParagraph": {
"mostRecentYear": years[-1],
"mostRecentYear": years[year_index],
"history": [
{
"year": year,
Expand Down Expand Up @@ -322,13 +328,17 @@ async def getMetroTemplateData(
years.sort()
assert len(years) >= 2

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

current_elected = client.stats_db["party_hist"].find(
{
"councilorType": "metro_councilor",
"level": 1,
"is_elected": True,
"metroId": metroId,
"year": years[-1],
"year": years[year_index],
},
{
"_id": 0,
Expand All @@ -345,7 +355,7 @@ async def getMetroTemplateData(
"level": 1,
"is_elected": False,
"metroId": metroId,
"year": years[-1],
"year": years[year_index],
},
{
"_id": 0,
Expand All @@ -362,7 +372,7 @@ async def getMetroTemplateData(
"level": 1,
"is_elected": True,
"metroId": metroId,
"year": years[-2],
"year": years[year_index - 1],
},
{
"_id": 0,
Expand Down Expand Up @@ -407,7 +417,7 @@ async def getMetroTemplateData(

@router.get("/chart-data/{metroId}")
async def getMetroChartData(
metroId: int, factor: FactorType
metroId: int, factor: FactorType, year:int = 2022
) -> ErrorResponse | ChartData[GenderChartDataPoint] | ChartData[
AgeChartDataPoint
] | ChartData[PartyChartDataPoint]:
Expand All @@ -433,10 +443,9 @@ async def getMetroChartData(
"level": 1,
"is_elected": True,
"metroId": metroId,
"year": year,
}
)
.sort({"year": -1})
.limit(1)
.to_list(5)
)[0]

Expand All @@ -459,10 +468,9 @@ async def getMetroChartData(
"is_elected": True,
"method": "equal",
"metroId": metroId,
"year": year,
}
)
.sort({"year": -1})
.limit(1)
.to_list(5)
)[0]
age_list = [
Expand Down Expand Up @@ -491,10 +499,9 @@ async def getMetroChartData(
"level": 1,
"is_elected": True,
"metroId": metroId,
"year": year
}
)
.sort({"year": -1})
.limit(1)
.to_list(5)
)[0]
return ChartData[PartyChartDataPoint].model_validate(
Expand Down

0 comments on commit a769866

Please sign in to comment.