Skip to content

Commit

Permalink
Merge pull request #80 from EnMarche/maximum_history
Browse files Browse the repository at this point in the history
Maximum history
  • Loading branch information
mlabarrere committed Jan 18, 2022
2 parents f7d9a97 + d353f78 commit 163c62f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
11 changes: 8 additions & 3 deletions app/crud/jemengage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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],
Expand Down
12 changes: 6 additions & 6 deletions app/crud/mail_campaign.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
)
Expand All @@ -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(
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 2 additions & 0 deletions app/models/models_enmarche.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 15 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +53,9 @@

# app.add_middleware(PyInstrumentProfilerMiddleware)

# Constant VAR
MAX_HISTORY = 30


def get_db():
db = SessionLocal()
Expand Down Expand Up @@ -163,39 +166,40 @@ 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"]
]


@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,
}

Expand Down

0 comments on commit 163c62f

Please sign in to comment.