diff --git a/app/crud/jemengage.py b/app/crud/jemengage.py index e0491f8..295417e 100644 --- a/app/crud/jemengage.py +++ b/app/crud/jemengage.py @@ -115,12 +115,17 @@ def survey_datas_export(query): } -def get_survey_datas(db: Session, scope: dict, survey_uuid): +def get_survey_datas(db: Session, scope: dict, max_history, survey_uuid): query = ( db.query(JemarcheDataSurvey) .join(JemarcheDataSurvey.data_survey, isouter=True) ) + if max_history: + query = ( + query.filter(JemarcheDataSurvey.updated_at >= date.today() - timedelta(days=max_history)) + ) + if survey_uuid: query = ( query @@ -177,7 +182,7 @@ def get_geo_matched_zone(db: Session, zones: dict): return None -def get_survey(db: Session, scope: dict, survey_uuid): +def get_survey(db: Session, scope: dict, max_history, survey_uuid): returned_zones = scope2dict(scope, True) if (query := get_geo_matched_zone(db, returned_zones)): geo_matched_zone = query.first() @@ -187,7 +192,7 @@ def get_survey(db: Session, scope: dict, survey_uuid): "longitude": geo_matched_zone.longitude or 2.418889, } - return dict(get_survey_datas(db, scope, survey_uuid), **res) + return dict(get_survey_datas(db, scope, max_history, survey_uuid), **res) return { "zone_name": "Zone non implémentée", #next(iter(returned_zones.values()))[0], diff --git a/app/crud/mail_campaign.py b/app/crud/mail_campaign.py index b593f00..4aefef0 100644 --- a/app/crud/mail_campaign.py +++ b/app/crud/mail_campaign.py @@ -1,5 +1,5 @@ """Endpoints de notre api.""" -from datetime import datetime +from datetime import date, timedelta from typing import List from app.crud.enmarche import get_child @@ -50,7 +50,7 @@ def filter_role(db: Session, query: Query, zones: List[GeoZone], role: str): return query -async def get_campaign_reports(db: Session, zone: GeoZone, since: datetime, role: str): +async def get_campaign_reports(db: Session, zone: GeoZone, max_history: int, role: str): """Method to CRUD /campaign/reports""" query = ( db.query( @@ -80,7 +80,7 @@ async def get_campaign_reports(db: Session, zone: GeoZone, since: datetime, role ) .join(MailChimpCampaign.message) .filter(AdherentMessages.status == "sent") - .filter(AdherentMessages.sent_at >= since) + .filter(AdherentMessages.sent_at >= date.today() - timedelta(days=max_history)) .join(MailChimpCampaign.report) .join(AdherentMessages.author) ) @@ -91,12 +91,12 @@ async def get_campaign_reports(db: Session, zone: GeoZone, since: datetime, role return { "zone": zone.name, - "depuis": since, + "since": date.today() - timedelta(days=max_history), "campagnes": query.order_by(AdherentMessages.sent_at.desc()).all(), } -async def get_mail_ratios(db: Session, zone: GeoZone, since: datetime, role: str): +async def get_mail_ratios(db: Session, zone: GeoZone, max_history: int, role: str): """Method to CRUD /campaign/reportsRatios""" query = ( db.query( @@ -129,7 +129,7 @@ async def get_mail_ratios(db: Session, zone: GeoZone, since: datetime, role: str .select_from(MailChimpCampaignReport) .join(MailChimpCampaignReport.mailchimp_campaign) .join(MailChimpCampaign.message) - .filter(AdherentMessages.sent_at >= since) + .filter(AdherentMessages.sent_at >= date.today() - timedelta(days=max_history)) .join(AdherentMessages.author) ) if role != "national": diff --git a/app/models/models_enmarche.py b/app/models/models_enmarche.py index 369646c..90095e3 100644 --- a/app/models/models_enmarche.py +++ b/app/models/models_enmarche.py @@ -224,6 +224,8 @@ class JemarcheDataSurvey(Base): gender = Column(String, nullable=True) latitude = Column(Float, nullable=True) longitude = Column(Float, nullable=True) + created_at = Column(DateTime, nullable=False) + updated_at = Column(DateTime, nullable=False) class JecouteDataSurvey(Base): diff --git a/main.py b/main.py index 4ed2dda..d71cb78 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ """A FastAPI application on Cloud Run""" import base64 import json -from datetime import datetime +from datetime import date, timedelta from os import environ import sentry_sdk @@ -53,6 +53,9 @@ # app.add_middleware(PyInstrumentProfilerMiddleware) +# Constant VAR +MAX_HISTORY = 30 + def get_db(): db = SessionLocal() @@ -163,22 +166,23 @@ async def jemengage_users( @app.get("/jemengage/survey", response_class=ORJSONResponse, response_model=schemas.JemarcheDataSurveyOut) async def jemengage_survey( + max_history: Optional[conint(ge=1)] = MAX_HISTORY, + survey_uuid: Optional[constr(min_length=36, max_length=36)] = None, selected_scope: dict = Depends(get_scopes), - db: Session = Depends(get_db), - survey_uuid: Optional[constr(min_length=36, max_length=36)] = None + db: Session = Depends(get_db) ): - return jemengage.get_survey(db, selected_scope, survey_uuid) + return jemengage.get_survey(db, selected_scope, max_history, survey_uuid) @app.get("/mailCampaign/reports", response_class=ORJSONResponse) async def mail_reports( + max_history: Optional[conint(ge=1)] = MAX_HISTORY, selected_scope: dict = Depends(get_scopes), - db: Session = Depends(get_db), - since: datetime = datetime(2021, 1, 1), + db: Session = Depends(get_db) ): return [ await mail_campaign.get_campaign_reports( - db, zone, since, selected_scope["code"] + db, zone, max_history, selected_scope["code"] ) for zone in selected_scope["zones"] ] @@ -186,16 +190,16 @@ async def mail_reports( @app.get("/mailCampaign/reportsRatios", response_class=ORJSONResponse) async def mail_ratios( + max_history: Optional[conint(ge=1)] = MAX_HISTORY, selected_scope: dict = Depends(get_scopes), - db: Session = Depends(get_db), - since: datetime = datetime(2021, 1, 1), + db: Session = Depends(get_db) ): result = await mail_campaign.get_mail_ratios( - db, selected_scope['zones'], since, selected_scope['code'] + db, selected_scope['zones'], max_history, selected_scope['code'] ) return { "zones": [zone.name for zone in selected_scope["zones"]], - "depuis": since, + "since": date.today() - timedelta(days=max_history), **result, }