-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
863 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ | |
"core", | ||
"qfdmd", | ||
"qfdmo", | ||
"data", | ||
"corsheaders", | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
dags/sources/tasks/airflow_logic/db_write_suggestion_task.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import logging | ||
|
||
from airflow import DAG | ||
from airflow.operators.python import PythonOperator | ||
from sources.tasks.business_logic.db_write_suggestion import db_write_suggestion | ||
from utils import logging_utils as log | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def db_write_suggestion_task(dag: DAG) -> PythonOperator: | ||
return PythonOperator( | ||
task_id="db_write_suggestion", | ||
python_callable=db_write_suggestion_wrapper, | ||
dag=dag, | ||
) | ||
|
||
|
||
def db_write_suggestion_wrapper(**kwargs) -> None: | ||
dag_name = kwargs["dag"].dag_display_name or kwargs["dag"].dag_id | ||
run_id = kwargs["run_id"] | ||
dfs_acteur = kwargs["ti"].xcom_pull(task_ids="db_data_prepare") | ||
df_acteur_to_delete = dfs_acteur["df_acteur_to_delete"] | ||
df_acteur_to_create = dfs_acteur["df_acteur_to_create"] | ||
df_acteur_to_update = dfs_acteur["df_acteur_to_update"] | ||
|
||
log.preview("dag_name", dag_name) | ||
log.preview("run_id", run_id) | ||
log.preview("df_acteur_to_delete", df_acteur_to_delete) | ||
log.preview("df_acteur_to_create", df_acteur_to_create) | ||
log.preview("df_acteur_to_update", df_acteur_to_update) | ||
|
||
if ( | ||
df_acteur_to_create.empty | ||
and df_acteur_to_delete.empty | ||
and df_acteur_to_update.empty | ||
): | ||
logger.warning("!!! Aucune suggestion à traiter pour cette source !!!") | ||
# set the task to airflow skip status | ||
kwargs["ti"].xcom_push(key="skip", value=True) | ||
return | ||
|
||
return db_write_suggestion( | ||
dag_name=dag_name, | ||
run_id=run_id, | ||
df_acteur_to_create=df_acteur_to_create, | ||
df_acteur_to_delete=df_acteur_to_delete, | ||
df_acteur_to_update=df_acteur_to_update, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import json | ||
import logging | ||
from datetime import datetime | ||
|
||
import pandas as pd | ||
from shared.tasks.database_logic.db_manager import PostgresConnectionManager | ||
from sources.config import shared_constants as constants | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def db_write_suggestion( | ||
dag_name: str, | ||
run_id: str, | ||
df_acteur_to_create: pd.DataFrame, | ||
df_acteur_to_delete: pd.DataFrame, | ||
df_acteur_to_update: pd.DataFrame, | ||
): | ||
|
||
metadata = {} | ||
|
||
run_name = run_id.replace("__", " - ") | ||
|
||
insert_suggestion( | ||
df=df_acteur_to_create, | ||
metadata=metadata, | ||
dag_name=f"{dag_name} - AJOUT", | ||
run_name=run_name, | ||
action_type=constants.SUGGESTION_SOURCE_AJOUT, | ||
) | ||
insert_suggestion( | ||
df=df_acteur_to_delete, | ||
metadata=metadata, | ||
dag_name=f"{dag_name} - SUPRESSION", | ||
run_name=run_name, | ||
action_type=constants.SUGGESTION_SOURCE_SUPRESSION, | ||
) | ||
insert_suggestion( | ||
df=df_acteur_to_update, | ||
metadata=metadata, | ||
dag_name=f"{dag_name} - MISES A JOUR", | ||
run_name=run_name, | ||
action_type=constants.SUGGESTION_SOURCE_MISESAJOUR, | ||
) | ||
|
||
|
||
def insert_suggestion( | ||
df: pd.DataFrame, metadata: dict, dag_name: str, run_name: str, action_type: str | ||
): | ||
if df.empty: | ||
return | ||
engine = PostgresConnectionManager().engine | ||
current_date = datetime.now() | ||
|
||
with engine.connect() as conn: | ||
# Insert a new suggestion | ||
result = conn.execute( | ||
""" | ||
INSERT INTO data_suggestioncohorte | ||
( | ||
identifiant_action, | ||
identifiant_execution, | ||
type_action, | ||
statut, | ||
metadata, | ||
cree_le, | ||
modifie_le | ||
) | ||
VALUES (%s, %s, %s, %s, %s, %s, %s) | ||
RETURNING ID; | ||
""", | ||
( | ||
dag_name, | ||
run_name, | ||
action_type, # FIXME: spécialiser les sources | ||
constants.SUGGESTION_AVALIDER, | ||
json.dumps(metadata), | ||
current_date, | ||
current_date, | ||
), | ||
) | ||
suggestion_cohorte_id = result.fetchone()[0] | ||
|
||
# Insert dag_run_change | ||
df["type_action"] = action_type | ||
df["suggestion_cohorte_id"] = suggestion_cohorte_id | ||
df["statut"] = constants.SUGGESTION_AVALIDER | ||
df[["suggestion", "suggestion_cohorte_id", "type_action", "statut"]].to_sql( | ||
"data_suggestionunitaire", | ||
engine, | ||
if_exists="append", | ||
index=False, | ||
method="multi", | ||
chunksize=1000, | ||
) |
Oops, something went wrong.