Skip to content

Commit

Permalink
Merge pull request #9 from pingpingy1/main
Browse files Browse the repository at this point in the history
[feat] indexHistoryParagraph 실제 데이터 반환
  • Loading branch information
pingpingy1 authored Nov 27, 2023
2 parents 3bc5e9a + 34e2576 commit 1c9ffb2
Show file tree
Hide file tree
Showing 8 changed files with 506 additions and 105 deletions.
6 changes: 4 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from fastapi import FastAPI, Request
from dotenv import load_dotenv
from routers import scrapResult, commonInfo, ageHist
from routers import commonInfo, ageHist, scrapResultLocal, scrapResultMetro
from contextlib import asynccontextmanager
from typing import Dict
from model import MongoDB
from model.ResponseType import ChartResponse, GenderInfo, PartyInfo, AgeInfo
from fastapi.middleware.cors import CORSMiddleware


@asynccontextmanager
async def initMongo(app: FastAPI):
MongoDB.client.connect()
Expand All @@ -32,6 +33,7 @@ async def initMongo(app: FastAPI):
allow_headers=["*"],
)

app.include_router(scrapResult.router)
app.include_router(scrapResultLocal.router)
app.include_router(scrapResultMetro.router)
app.include_router(commonInfo.router)
app.include_router(ageHist.router)
62 changes: 62 additions & 0 deletions model/ScrapResultCommon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from pydantic import BaseModel
from enum import StrEnum
from typing import TypeVar, Generic


class GenderType(StrEnum):
male = "남"
female = "여"


class FactorType(StrEnum):
gender = "gender"
age = "age"
party = "party"


# ==============================================
# = Chart Data Types =
# ==============================================
class GenderChartDataPoint(BaseModel):
gender: GenderType
count: int


class AgeChartDataPoint(BaseModel):
minAge: int # 닫힌 구간
maxAge: int # 열린 구간
count: int


class PartyChartDataPoint(BaseModel):
party: str
count: int


T = TypeVar("T", GenderChartDataPoint, AgeChartDataPoint, PartyChartDataPoint)


class ChartData(BaseModel, Generic[T]):
data: list[T]


# ==============================================
# = Scrap Result Data Types =
# ==============================================
class CouncilType(StrEnum):
local_council = "local_council"
national_council = "national_council"
metropolitan_council = "metropolitan_council"
local_leader = "local_leader"
metro_leader = "metro_leader"


class CouncilInfo(BaseModel):
name: str
party: str


class ScrapResult(BaseModel):
council_id: str
council_type: CouncilType
councilers: list[CouncilInfo]
67 changes: 3 additions & 64 deletions model/ScrapResult.py → model/ScrapResultLocal.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
from pydantic import BaseModel
from enum import StrEnum
from typing import TypeVar, Generic


class GenderType(StrEnum):
male = "남"
female = "여"


class FactorType(StrEnum):
gender = "gender"
age = "age"
party = "party"


# ==============================================
# = Template Data Types =
# ==============================================
class GenderTemplateData(BaseModel):
class GenderTemplateDataLocal(BaseModel):
genderDiversityIndex: float


class AgeTemplateData(BaseModel):
class AgeTemplateDataLocal(BaseModel):
class AgeRankingParagraphData(BaseModel):
class AgeRankingAllIndices(BaseModel):
localId: int
Expand Down Expand Up @@ -65,53 +52,5 @@ class AgeHistogramAreaData(BaseModel):
ageHistogramParagraph: AgeHistogramParagraphData


class PartyTemplateData(BaseModel):
class PartyTemplateDataLocal(BaseModel):
partyDiversityIndex: float


# ==============================================
# = Chart Data Types =
# ==============================================
class GenderChartDataPoint(BaseModel):
gender: GenderType
count: int


class AgeChartDataPoint(BaseModel):
minAge: int # 닫힌 구간
maxAge: int # 열린 구간
count: int


class PartyChartDataPoint(BaseModel):
party: str
count: int


T = TypeVar("T", GenderChartDataPoint, AgeChartDataPoint, PartyChartDataPoint)


class ChartData(BaseModel, Generic[T]):
data: list[T]


# ==============================================
# = Scrap Result Data Types =
# ==============================================
class CouncilType(StrEnum):
local_council = "local_council"
national_council = "national_council"
metropolitan_council = "metropolitan_council"
local_leader = "local_leader"
metro_leader = "metro_leader"


class CouncilInfo(BaseModel):
name: str
party: str


class ScrapResult(BaseModel):
council_id: str
council_type: CouncilType
councilers: list[CouncilInfo]
55 changes: 55 additions & 0 deletions model/ScrapResultMetro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from pydantic import BaseModel


# ==============================================
# = Template Data Types =
# ==============================================
class GenderTemplateDataMetro(BaseModel):
genderDiversityIndex: float


class AgeTemplateDataMetro(BaseModel):
class AgeRankingParagraphData(BaseModel):
class AgeRankingAllIndices(BaseModel):
metroId: 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):
metroId: int
firstQuintile: int
lastQuintile: int

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

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


class PartyTemplateDataMetro(BaseModel):
partyDiversityIndex: float
12 changes: 5 additions & 7 deletions routers/ageHist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from model.AgeHist import AgeHistDataTypes, AgeHistMethodTypes, MetroAgeHistData


router = APIRouter(prefix="/localCouncil", tags=["localCouncil"])
router = APIRouter()


@router.get("/age-hist/{metroId}")
Expand All @@ -27,9 +27,8 @@ async def getMetroAgeHistData(
histogram = await MongoDB.client.stats_db["age_hist"].find_one(
{
"level": 1,
"councilorType": (
"elected" if ageHistType == AgeHistDataTypes.elected else "candidate"
),
"councilorType": "metro_councilor",
"is_elected": ageHistType == AgeHistDataTypes.elected,
"year": year,
"method": method,
"metroId": metroId,
Expand Down Expand Up @@ -66,9 +65,8 @@ async def getLocalAgeHistData(
histogram = await MongoDB.client.stats_db["age_hist"].find_one(
{
"level": 2,
"councilorType": (
"elected" if ageHistType == AgeHistDataTypes.elected else "candidate"
),
"councilorType": "local_councilor",
"is_elected": ageHistType == AgeHistDataTypes.elected,
"year": year,
"method": method,
"metroId": metroId,
Expand Down
Loading

0 comments on commit 1c9ffb2

Please sign in to comment.