2024-06-07 18:46:43 -05:00
|
|
|
import logging
|
|
|
|
|
from airflow.exceptions import AirflowFailException
|
|
|
|
|
from airflow.operators.empty import EmptyOperator
|
|
|
|
|
from airflow.operators.python import PythonOperator
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from airflow import DAG
|
|
|
|
|
from airflow.providers.snowflake.hooks.snowflake import SnowflakeHook
|
2024-09-30 12:21:29 +01:00
|
|
|
|
2024-06-07 18:46:43 -05:00
|
|
|
# from openpyxl.workbook import Workbook
|
|
|
|
|
import pandas as pd
|
|
|
|
|
import boto3
|
|
|
|
|
import io
|
|
|
|
|
|
|
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
SNOWFLAKE_CONN_ID = "doczy_uat_snowflake"
|
|
|
|
|
TAGS = ["QC", "uat", "etl", "QC-Summery"]
|
|
|
|
|
DAG_ID = "qc_report_dag"
|
2024-06-07 18:46:43 -05:00
|
|
|
|
|
|
|
|
bucket = "doczy-dev-infra-mwaa-resources"
|
|
|
|
|
object_key = "outputs/"
|
|
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
DATABASE = "DOCZY_UAT"
|
2024-06-07 18:46:43 -05:00
|
|
|
SCHEMA = "STG"
|
|
|
|
|
TABLE_NAME = "TRAINING_DATA_RAW"
|
|
|
|
|
|
|
|
|
|
# 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-06-07 18:46:43 -05:00
|
|
|
|
|
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
args = {"owner": "Airflow", "start_date": datetime(2022, 1, 1), "retries": 0}
|
|
|
|
|
dag = DAG(dag_id=DAG_ID, default_args=args, schedule=None, tags=TAGS)
|
2024-06-07 18:46:43 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def getData():
|
|
|
|
|
# Setup connection to Snowflake
|
|
|
|
|
dwh_hook = SnowflakeHook(snowflake_conn_id=SNOWFLAKE_CONN_ID)
|
|
|
|
|
conn = dwh_hook.get_conn() # Get the raw connection
|
2024-09-30 12:21:29 +01:00
|
|
|
|
2024-06-07 18:46:43 -05:00
|
|
|
# Your query and the database details
|
|
|
|
|
qc_query = f"SELECT * FROM {DATABASE}.{SCHEMA}.{TABLE_NAME}"
|
2024-09-30 12:21:29 +01:00
|
|
|
|
2024-06-07 18:46:43 -05:00
|
|
|
# Fetch data into a Pandas DataFrame
|
|
|
|
|
df = pd.read_sql(qc_query, conn)
|
|
|
|
|
|
|
|
|
|
# Initialize S3 client
|
|
|
|
|
s3 = boto3.client("s3")
|
|
|
|
|
|
|
|
|
|
# Create a buffer to hold the data
|
|
|
|
|
with io.StringIO() as csv_buffer:
|
|
|
|
|
df.to_csv(csv_buffer, index=False)
|
2024-09-30 12:21:29 +01:00
|
|
|
|
2024-06-07 18:46:43 -05:00
|
|
|
# Save the data to S3
|
|
|
|
|
response = s3.put_object(
|
2024-09-30 12:21:29 +01:00
|
|
|
Bucket=bucket,
|
|
|
|
|
Key=object_key + "snowflake_table_results.csv",
|
|
|
|
|
Body=csv_buffer.getvalue(),
|
2024-06-07 18:46:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
status = response.get("ResponseMetadata", {}).get("HTTPStatusCode")
|
|
|
|
|
|
|
|
|
|
if status == 200:
|
|
|
|
|
print(f"Successful S3 put_object response. Status - {status}")
|
|
|
|
|
else:
|
2024-09-30 12:21:29 +01:00
|
|
|
raise AirflowFailException(
|
|
|
|
|
f"Unsuccessful S3 put_object response. Status - {status}"
|
|
|
|
|
)
|
|
|
|
|
|
2024-06-07 18:46:43 -05:00
|
|
|
oldData = df
|
|
|
|
|
newData = df
|
|
|
|
|
|
|
|
|
|
with io.BytesIO() as output:
|
2024-09-30 12:21:29 +01:00
|
|
|
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
2024-06-07 18:46:43 -05:00
|
|
|
oldData.to_excel(writer, sheet_name="Old")
|
|
|
|
|
newData.to_excel(writer, sheet_name="new")
|
|
|
|
|
dat = oldData.compare(newData, align_axis=0, keep_shape=True)
|
|
|
|
|
dat.to_excel(writer, sheet_name="qc")
|
|
|
|
|
response = s3.put_object(
|
2024-09-30 12:21:29 +01:00
|
|
|
Bucket=bucket, Key=object_key + "QC_Report.xlsx", Body=output.getvalue()
|
2024-06-07 18:46:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
print("Dataframe is written to S3 successfully.")
|
|
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
|
2024-06-07 18:46:43 -05:00
|
|
|
with dag:
|
2024-09-30 12:21:29 +01:00
|
|
|
begin_job = EmptyOperator(task_id="Begin")
|
2024-06-07 18:46:43 -05:00
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
get_data_from_snowflake = PythonOperator(
|
|
|
|
|
task_id="get_data_from_snowflake", python_callable=getData
|
|
|
|
|
)
|
2024-06-07 18:46:43 -05:00
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
end_job = EmptyOperator(task_id="End")
|
2024-06-07 18:46:43 -05:00
|
|
|
|
|
|
|
|
|
2024-09-30 12:21:29 +01:00
|
|
|
begin_job >> get_data_from_snowflake >> end_job
|