diff --git a/model/ScrapResultLocal.py b/model/ScrapResultLocal.py index 9e4ee09..bab4e3f 100644 --- a/model/ScrapResultLocal.py +++ b/model/ScrapResultLocal.py @@ -14,7 +14,9 @@ class GenderTemplateDataPoint(BaseModel): localId: int genderDiversityIndex: float current: GenderTemplateDataPoint + currentCandidate: GenderTemplateDataPoint prev: GenderTemplateDataPoint + prevCandidate: GenderTemplateDataPoint meanMalePop: float meanFemalePop: float diff --git a/model/ScrapResultMetro.py b/model/ScrapResultMetro.py index 8d60537..90dd843 100644 --- a/model/ScrapResultMetro.py +++ b/model/ScrapResultMetro.py @@ -13,7 +13,9 @@ class GenderTemplateDataPoint(BaseModel): metroId: int genderDiversityIndex: float current: GenderTemplateDataPoint + currentCandidate: GenderTemplateDataPoint prev: GenderTemplateDataPoint + prevCandidate: GenderTemplateDataPoint meanMalePop: float meanFemalePop: float diff --git a/model/ScrapResultNational.py b/model/ScrapResultNational.py index 66c847c..0c0fd74 100644 --- a/model/ScrapResultNational.py +++ b/model/ScrapResultNational.py @@ -12,7 +12,9 @@ class GenderTemplateDataPoint(BaseModel): genderDiversityIndex: float current: GenderTemplateDataPoint + currentCandidate: GenderTemplateDataPoint prev: GenderTemplateDataPoint + prevCandidate: GenderTemplateDataPoint class AgeTemplateDataNational(BaseModel): diff --git a/routers/scrapResultLocal.py b/routers/scrapResultLocal.py index 7867a90..1252365 100644 --- a/routers/scrapResultLocal.py +++ b/routers/scrapResultLocal.py @@ -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( @@ -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", @@ -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( @@ -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"] @@ -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", @@ -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]: @@ -465,7 +497,7 @@ async def getLocalChartData( "is_elected": True, "localId": localId, "metroId": metroId, - "year": year + "year": year, } ) .limit(1) @@ -492,7 +524,7 @@ async def getLocalChartData( "method": "equal", "localId": localId, "metroId": metroId, - "year": year + "year": year, } ) .limit(1) @@ -525,7 +557,7 @@ async def getLocalChartData( "is_elected": True, "localId": localId, "metroId": metroId, - "year": year + "year": year, } ) .limit(1) diff --git a/routers/scrapResultMetro.py b/routers/scrapResultMetro.py index 0543300..92c4bc7 100644 --- a/routers/scrapResultMetro.py +++ b/routers/scrapResultMetro.py @@ -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}) @@ -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], } ) @@ -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"] @@ -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( { @@ -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]: @@ -499,7 +529,7 @@ async def getMetroChartData( "level": 1, "is_elected": True, "metroId": metroId, - "year": year + "year": year, } ) .to_list(5) diff --git a/routers/scrapResultNational.py b/routers/scrapResultNational.py index fca7d5b..045307c 100644 --- a/routers/scrapResultNational.py +++ b/routers/scrapResultNational.py @@ -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} @@ -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], } ) @@ -63,7 +76,16 @@ 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], } ) @@ -71,15 +93,25 @@ async def getNationalTemplateData( { "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["여"], + }, } )