From 0e49e7a4ff4c3a60fa46f62a4b68f89de1ec71e0 Mon Sep 17 00:00:00 2001 From: happycastle <41810556+happycastle114@users.noreply.github.com> Date: Thu, 30 Nov 2023 02:20:04 +0900 Subject: [PATCH 1/3] add: year select --- routers/scrapResultLocal.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/routers/scrapResultLocal.py b/routers/scrapResultLocal.py index 85eca06..b04b1e5 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 + metroId: int, localId: int, factor: FactorType, year:int = 2022 ) -> ErrorResponse | GenderTemplateDataLocal | AgeTemplateDataLocal | PartyTemplateDataLocal: if ( await client.district_db["local_district"].find_one( @@ -64,6 +64,10 @@ 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", @@ -71,7 +75,7 @@ async def getLocalTemplateData( "is_elected": True, "localId": localId, "metroId": metroId, - "year": years[-1], + "year": years[year_index], } ) @@ -82,7 +86,7 @@ async def getLocalTemplateData( "is_elected": True, "localId": localId, "metroId": metroId, - "year": years[-2], + "year": years[year_index - 1], } ) @@ -95,7 +99,7 @@ async def getLocalTemplateData( "councilorType": "local_councilor", "level": 2, "is_elected": True, - "year": years[-1], + "year": years[year_index], } }, { @@ -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["여"], }, @@ -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( { @@ -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, @@ -339,6 +348,10 @@ 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", @@ -346,7 +359,7 @@ async def getLocalTemplateData( "is_elected": True, "localId": localId, "metroId": metroId, - "year": years[-1], + "year": years[year_index], }, { "_id": 0, @@ -365,7 +378,7 @@ async def getLocalTemplateData( "is_elected": False, "localId": localId, "metroId": metroId, - "year": years[-1], + "year": years[year_index], }, { "_id": 0, @@ -384,7 +397,7 @@ async def getLocalTemplateData( "is_elected": True, "localId": localId, "metroId": metroId, - "year": years[-2], + "year": years[year_index - 1], }, { "_id": 0, From 122ce959918f7661c18a000fb2b1a7dbcee55217 Mon Sep 17 00:00:00 2001 From: happycastle <41810556+happycastle114@users.noreply.github.com> Date: Thu, 30 Nov 2023 02:36:17 +0900 Subject: [PATCH 2/3] add: year on local --- routers/scrapResultLocal.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routers/scrapResultLocal.py b/routers/scrapResultLocal.py index b04b1e5..7867a90 100644 --- a/routers/scrapResultLocal.py +++ b/routers/scrapResultLocal.py @@ -436,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]: @@ -465,9 +465,9 @@ async def getLocalChartData( "is_elected": True, "localId": localId, "metroId": metroId, + "year": year } ) - .sort({"year": -1}) .limit(1) .to_list(5) )[0] @@ -492,9 +492,9 @@ async def getLocalChartData( "method": "equal", "localId": localId, "metroId": metroId, + "year": year } ) - .sort({"year": -1}) .limit(1) .to_list(5) )[0] @@ -525,9 +525,9 @@ async def getLocalChartData( "is_elected": True, "localId": localId, "metroId": metroId, + "year": year } ) - .sort({"year": -1}) .limit(1) .to_list(5) )[0] From 212cb6d291d4830edf4078d8fb0f802e015ea18a Mon Sep 17 00:00:00 2001 From: happycastle <41810556+happycastle114@users.noreply.github.com> Date: Thu, 30 Nov 2023 02:36:24 +0900 Subject: [PATCH 3/3] add: year on metro --- routers/scrapResultMetro.py | 49 +++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/routers/scrapResultMetro.py b/routers/scrapResultMetro.py index d3351a5..0543300 100644 --- a/routers/scrapResultMetro.py +++ b/routers/scrapResultMetro.py @@ -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, @@ -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}) @@ -57,6 +57,9 @@ 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( { @@ -64,7 +67,7 @@ async def getMetroTemplateData( "level": 1, "is_elected": True, "metroId": metroId, - "year": years[-1], + "year": years[year_index], } ) @@ -74,7 +77,7 @@ async def getMetroTemplateData( "level": 1, "is_elected": True, "metroId": metroId, - "year": years[-2], + "year": years[year_index-1], } ) @@ -87,7 +90,7 @@ async def getMetroTemplateData( "councilorType": "metro_councilor", "level": 1, "is_elected": True, - "year": years[-1], + "year": years[year_index], } }, { @@ -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["여"], }, @@ -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( { @@ -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, @@ -259,7 +265,7 @@ async def getMetroTemplateData( ], }, "indexHistoryParagraph": { - "mostRecentYear": years[-1], + "mostRecentYear": years[year_index], "history": [ { "year": year, @@ -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, @@ -345,7 +355,7 @@ async def getMetroTemplateData( "level": 1, "is_elected": False, "metroId": metroId, - "year": years[-1], + "year": years[year_index], }, { "_id": 0, @@ -362,7 +372,7 @@ async def getMetroTemplateData( "level": 1, "is_elected": True, "metroId": metroId, - "year": years[-2], + "year": years[year_index - 1], }, { "_id": 0, @@ -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]: @@ -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] @@ -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 = [ @@ -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(