2024-02-29 12:45:05 -06:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from airflow import DAG
|
|
|
|
|
from airflow.providers.snowflake.operators.snowflake import SnowflakeOperator
|
|
|
|
|
from airflow.providers.snowflake.hooks.snowflake import SnowflakeHook
|
|
|
|
|
from airflow.operators.python import PythonOperator
|
|
|
|
|
from airflow.utils.trigger_rule import TriggerRule
|
|
|
|
|
from airflow.operators.empty import EmptyOperator
|
|
|
|
|
from airflow.models import Variable
|
|
|
|
|
from airflow.exceptions import AirflowFailException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SNOWFLAKE_CONN_ID = "doczy_dev_snowflake"
|
|
|
|
|
DAG_ID = "load_training_results"
|
2024-09-30 12:21:29 +01:00
|
|
|
DATABASE = "DOCZY_DEV"
|
2024-02-29 12:45:05 -06:00
|
|
|
# bucket = "airflow-data-ingestion"
|
|
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
TAGS = ["dev", "training_interface", "dataload"]
|
2024-02-29 12:45:05 -06:00
|
|
|
|
|
|
|
|
# Trigger rules
|
2024-09-30 12:21:29 +01:00
|
|
|
ALL_SUCCESS = "all_success"
|
|
|
|
|
ALL_FAILED = "all_failed"
|
|
|
|
|
ALL_DONE = "all_done"
|
|
|
|
|
ONE_SUCCESS = "one_success"
|
|
|
|
|
ONE_FAILED = "one_failed"
|
2024-02-29 12:45:05 -06:00
|
|
|
|
2024-03-07 17:17:49 -06:00
|
|
|
# Passing empty params for now, this will be overridden by the payload from the trigger
|
|
|
|
|
# These params can also be set from the Airflow UI while manually triggering the DAG
|
2024-09-30 12:21:29 +01:00
|
|
|
default_params = {"training_results_file_name": "", "attempt_logs_file_name": ""}
|
2024-02-29 12:45:05 -06:00
|
|
|
# This will be replaced with the payload from the event after API connection is setup
|
2024-03-07 17:17:49 -06:00
|
|
|
# training_results_file_name = "training_results_sample.csv"
|
|
|
|
|
# attempt_logs_file_name = "attempt_logs_sample.csv"
|
2024-02-29 12:45:05 -06:00
|
|
|
|
|
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
def call_stored_proc(proc_name, file_type, params):
|
|
|
|
|
dwh_hook = SnowflakeHook(snowflake_conn_id=SNOWFLAKE_CONN_ID)
|
2024-02-29 12:45:05 -06:00
|
|
|
with dwh_hook.get_conn() as conn:
|
|
|
|
|
# dwh_hook.set_autocommit(conn,autocommit=False)
|
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
|
2024-03-07 17:17:49 -06:00
|
|
|
# Added new parameter file_type to determine the file name to be passed to the stored procedure
|
|
|
|
|
# The bucket name is set by default to "doczy-dev-infra-raw-data-ingestion" and the files should be ALWAYS save under training_interface/ path for now
|
2024-09-30 12:21:29 +01:00
|
|
|
if file_type == "training_results":
|
|
|
|
|
file_name = params["training_results_file_name"]
|
|
|
|
|
elif file_type == "attempt_logs":
|
|
|
|
|
file_name = params["attempt_logs_file_name"]
|
2024-03-07 17:17:49 -06:00
|
|
|
|
2024-02-29 12:45:05 -06:00
|
|
|
cur.execute(f"CALL {DATABASE}.STG.{proc_name}('{file_name}');")
|
|
|
|
|
result = cur.fetchone()
|
2024-09-30 12:21:29 +01:00
|
|
|
if result[0] == "Setup, Load, and Audit Complete":
|
|
|
|
|
logger.info("PROCEDURE EXECUTED SUCCESSFULLY")
|
2024-02-29 12:45:05 -06:00
|
|
|
else:
|
2024-09-30 12:21:29 +01:00
|
|
|
raise AirflowFailException(
|
|
|
|
|
"Check the DAG logs for more information. ERROR FROM SNOWFLAKE: ",
|
|
|
|
|
result,
|
|
|
|
|
)
|
2024-02-29 12:45:05 -06:00
|
|
|
logger.info(f"QUERY EXECUTION RESULT: {str(result)}")
|
|
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
|
2024-02-29 12:45:05 -06:00
|
|
|
dag = DAG(
|
|
|
|
|
DAG_ID,
|
|
|
|
|
start_date=datetime(2024, 1, 1),
|
2024-09-30 12:21:29 +01:00
|
|
|
default_args={"snowflake_conn_id": SNOWFLAKE_CONN_ID, "retries": 0},
|
2024-02-29 12:45:05 -06:00
|
|
|
tags=TAGS,
|
|
|
|
|
catchup=False,
|
2024-03-07 17:17:49 -06:00
|
|
|
schedule=None,
|
2024-09-30 12:21:29 +01:00
|
|
|
params=default_params,
|
2024-02-29 12:45:05 -06:00
|
|
|
)
|
2024-09-30 12:21:29 +01:00
|
|
|
|
|
|
|
|
begin_job = EmptyOperator(task_id="Begin")
|
2024-02-29 12:45:05 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
load_training_results = PythonOperator(
|
|
|
|
|
task_id="load_training_results",
|
|
|
|
|
python_callable=call_stored_proc,
|
|
|
|
|
dag=dag,
|
2024-09-30 12:21:29 +01:00
|
|
|
op_kwargs={"proc_name": "LOAD_TRAINING_RESULTS", "file_type": "training_results"},
|
2024-02-29 12:45:05 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
load_attempt_logs_sp = PythonOperator(
|
|
|
|
|
task_id="load_attempt_logs",
|
|
|
|
|
python_callable=call_stored_proc,
|
|
|
|
|
dag=dag,
|
2024-09-30 12:21:29 +01:00
|
|
|
op_kwargs={"proc_name": "LOAD_ATTEMPT_LOGS", "file_type": "attempt_logs"},
|
2024-02-29 12:45:05 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
end_job = EmptyOperator(task_id="End")
|
2024-02-29 12:45:05 -06:00
|
|
|
|
|
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
begin_job >> load_training_results >> load_attempt_logs_sp >> end_job
|