Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Add EU polls endpoint #203

Merged
merged 12 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 64 additions & 6 deletions src/api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# local
import src.db.models as models
from src.api.schemas import ConstituencyPoliticians
from src.api.schemas import ConstituencyPoliticians, Parliament
from src.api.utils.sidejob import convert_income_level
from src.api.utils.politician import (
add_image_urls_to_politicians,
Expand Down Expand Up @@ -259,6 +259,52 @@ def get_latest_bundestag_polls(db: Session):
)


def get_latest_europarl_polls(db: Session):
current_legislature_period = 117
return (
db.query(models.Poll)
.filter(
models.Poll.field_legislature_id == current_legislature_period,
)
.order_by(models.Poll.field_poll_date.desc())
.limit(10)
.all()
)


def get_all_europarl_polls(db: Session, size: int, topic_ids: List[int] = None):
if topic_ids:
return (
db.query(models.Poll)
.filter(
(models.Poll.field_legislature_id == 117)
| (models.Poll.field_legislature_id == 62)
)
.filter(
(models.Topic.id.in_(topic_ids))
| (models.Topic.parent_id.in_(topic_ids))
)
.filter(
(models.PollHasTopic.topic_id == models.Topic.id)
& (models.Poll.id == models.PollHasTopic.poll_id)
)
.order_by(models.Poll.field_poll_date.desc())
.slice(size - 10, size)
.all()
)
else:
return (
db.query(models.Poll)
.filter(
(models.Poll.field_legislature_id == 117)
| (models.Poll.field_legislature_id == 62)
)
.order_by(models.Poll.field_poll_date.desc())
.slice(size - 10, size)
.all()
)


def get_all_bundestag_polls(db: Session, size: int, topic_ids: List[int] = None):
if topic_ids:
return (
Expand Down Expand Up @@ -333,9 +379,14 @@ def get_vote_result_by_poll_id(db: Session, poll_id: int):


# Tested with mockup
def get_polls_total(db: Session):
def get_polls_total(db: Session, parliament: Parliament):
if parliament == Parliament.BUNDESTAG:
polls = get_latest_bundestag_polls(db)
elif parliament == Parliament.EUPARL:
polls = get_latest_europarl_polls(db)
else:
raise ValueError("Invalid parliament value")
data_list = []
polls = get_latest_bundestag_polls(db)
for poll in polls:
poll_dict = {
"field_legislature_id": poll.field_legislature_id,
Expand All @@ -351,9 +402,16 @@ def get_polls_total(db: Session):
return data_list


def get_all_polls_total(db: Session, size: int, topic_ids: List[int] = None):
def get_all_polls_total(
db: Session, parliament: Parliament, size: int, topic_ids: List[int] = None
):
if parliament == Parliament.EUPARL:
polls = get_all_europarl_polls(db, size, topic_ids)
elif parliament == Parliament.BUNDESTAG:
polls = get_all_bundestag_polls(db, size, topic_ids)
else:
raise ValueError("Invalid parliament value")
data_list = []
polls = get_all_bundestag_polls(db, size, topic_ids)
for poll in polls:
poll_dict = {
"field_legislature_id": poll.field_legislature_id,
Expand Down Expand Up @@ -526,7 +584,7 @@ def get_politician_by_constituency(
if candidacy_data:
constituency_id = candidacy_data.electoral_data.constituency_id

if constituency_id not in [9607, 4721, 5309, 423]:
if constituency_id not in [12635, 9607, 4721, 5309, 423]:
politicians = (
db.query(models.Politician)
.join(models.CandidacyMandate)
Expand Down
21 changes: 18 additions & 3 deletions src/api/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# std
import logging
from typing import Optional
import time
import threading
Expand All @@ -14,6 +15,7 @@
from fastapi_redis_cache import cache
from redis import asyncio as aioredis
from sqlalchemy.orm.session import Session
from sqlalchemy.sql.expression import select


# local
Expand Down Expand Up @@ -91,7 +93,7 @@ async def add_security_headers(request: Request, call_next):
app.mount("/static", StaticFiles(directory="static"), name="static")


@app.get("/health_check")
@app.get("/redis_health_check")
async def health_check(redis_pool: aioredis.Redis = Depends(get_redis)):
pong = await redis_pool.ping()
print(f"Ping result: {pong}")
Expand All @@ -100,10 +102,23 @@ async def health_check(redis_pool: aioredis.Redis = Depends(get_redis)):
return {"status": "OK", "detail": "Redis server is responding"}


@app.get("/db_health_check")
def db_health_check(db: Session = Depends(v2.get_db)):
try:
result = db.execute(select(1))
value = result.scalar_one()
if value == 1:
return {"status": "healthy"}
except Exception as e:
# Log the exception details
logging.log(f"Database health check failed: {e}")
raise HTTPException(status_code=503, detail="Database is unhealthy")


@app.get("/")
@cache(expire=60)
def read_root(name: Optional[str] = "World"):
return {"Hello": name}
def read_root():
return {"Hello World"}


@app.get("/.well-known/ai-plugin.json")
Expand Down
6 changes: 6 additions & 0 deletions src/api/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# std
from enum import Enum
from pydantic import BaseModel, Field
from datetime import date, datetime
from typing import Optional, List, Dict
Expand Down Expand Up @@ -779,3 +780,8 @@ class PluginPolitician(FTFBaseModel):
weblinks: Optional[List]
votes_and_polls: Optional[List[VoteAndPoll]]
topic_ids_of_latest_committee: Optional[List[int]]


class Parliament(Enum):
BUNDESTAG = "bundestag"
EUPARL = "euparl"
55 changes: 55 additions & 0 deletions src/api/utils/polls.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,58 @@ def get_politcian_ids_by_bundestag_polldata_and_follow_ids(
).label
# get with politician_id candidacymandate_id where legislature_period is Bundestag_id
return polls


def get_politcian_ids_by_euparl_polldata_and_follow_ids(
polls: List[BundestagPollData], db: Session, follow_ids: List[int] = None
):
for poll in polls:
politician_list = []
if follow_ids:
query_data = (
db.query(models.CandidacyMandate)
.join(models.Vote)
.filter(
(models.CandidacyMandate.parliament_period_id == 117)
| (models.CandidacyMandate.parliament_period_id == 62)
)
.filter(
and_(
models.CandidacyMandate.politician_id.in_(follow_ids),
models.Vote.poll_id == poll["poll"]["id"],
models.Vote.vote != "no_show",
)
)
.limit(5)
.all()
)
if query_data:
[politician_list.append(item.politician_id) for item in query_data]
if len(politician_list) < 5:
random_query_data = (
db.query(models.CandidacyMandate)
.join(models.Vote)
.filter(
(models.CandidacyMandate.parliament_period_id == 117)
| (models.CandidacyMandate.parliament_period_id == 62)
)
.filter(
and_(
models.Vote.poll_id == poll["poll"]["id"],
models.Vote.vote != "no_show",
)
)
.order_by(func.random())
.limit(5 - len(politician_list))
.all()
)
[
politician_list.insert(0, item.politician_id)
for item in random_query_data
]
poll["politicians"] = politician_list
poll["last_politician"] = crud.get_entity_by_id(
db, models.Politician, poll["politicians"][-1]
).label
# get with politician_id candidacymandate_id where legislature_period is Bundestag_id
return polls
Loading
Loading