-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdownstream_dag.py
More file actions
159 lines (129 loc) · 4.65 KB
/
Copy pathdownstream_dag.py
File metadata and controls
159 lines (129 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Generate a report with the weather forecast for the cities and historical weather data.
This DAG fetches the weather forecast for the cities and historical weather data from the upstream DAGs
via XCom and generates a report with the data.
"""
from airflow.decorators import dag, task
from airflow.models.dataset import Dataset
from airflow.timetables.datasets import DatasetOrTimeSchedule
from airflow.timetables.trigger import CronTriggerTimetable
from pendulum import datetime
import pandas as pd
import json
import logging
t_log = logging.getLogger("airflow.task")
@dag(
dag_display_name="Solution Downstream DAG 📒",
start_date=datetime(2024, 6, 1),
schedule=DatasetOrTimeSchedule(
timetable=CronTriggerTimetable("0 0 * * *", timezone="UTC"),
datasets=(
Dataset("current_weather_data")
& Dataset("max_temp_data")
& (Dataset("wind_speed_data") | Dataset("wind_direction_data"))
),
# Runs every day at midnight UTC
# AND whenever both "current_weather_data" and "max_temp_data" are updated
# AS WELL AS ONE OF the datasets "wind_speed_data" OR "wind_direction_data".
),
catchup=False,
max_consecutive_failed_dag_runs=6,
doc_md=__doc__,
description="Generate a report with the weather forecast for the cities and historical weather data.",
default_args={"owner": "Astro", "retries": 3},
tags=["solution"],
)
def downstream_dag_solution():
@task
def fetch_cities_weather_table(**context) -> pd.DataFrame:
df = context["ti"].xcom_pull(
dag_id="upstream_dag_1_solution",
task_ids="create_weather_table",
include_prior_dates=True,
)
return df
@task
def fetch_max_temp_data(**context) -> dict:
data = context["ti"].xcom_pull(
dag_id="upstream_dag_2_solution",
task_ids="get_max_temp",
include_prior_dates=True,
)
return json.loads(data) if data is not None else None
@task
def fetch_wind_speed_data(**context) -> dict:
data = context["ti"].xcom_pull(
dag_id="upstream_dag_2_solution",
task_ids="get_wind_speed",
include_prior_dates=True,
)
return json.loads(data) if data is not None else None
@task
def fetch_wind_direction_data(**context) -> dict:
data = context["ti"].xcom_pull(
dag_id="upstream_dag_2_solution",
task_ids="get_wind_direction",
include_prior_dates=True,
)
return json.loads(data) if data is not None else None
@task
def fetch_wildcard_data(**context) -> dict:
data = context["ti"].xcom_pull(
dag_id="upstream_dag_2_solution",
task_ids="get_wildcard_data",
include_prior_dates=True,
)
return json.loads(data) if data is not None else None
@task
def fetch_city_dag_2(**context) -> dict:
data = context["ti"].xcom_pull(
dag_id="upstream_dag_2",
task_ids="get_lat_long_for_one_city",
include_prior_dates=True,
)
return data
@task
def generate_report(
cities_weather: pd.DataFrame,
max_temp: dict,
wind_speed: dict,
wind_direction: dict,
wildcard: dict,
city_coordinates: dict,
):
from tabulate import tabulate
if cities_weather is not None:
t_log.info("Current Weather data:")
t_log.info(
tabulate(
cities_weather, headers="keys", tablefmt="grid", showindex=True
),
)
if max_temp:
city = city_coordinates["city"]
date = max_temp["daily"]["time"][0]
t_log.info("--------------------------")
t_log.info(f"Historical Weather data for {city} on {date}:")
t_log.info(
f"Max temperature data: {max_temp['daily']['temperature_2m_max'][0]}"
)
if wind_speed:
t_log.info(
f"Wind speed data: {wind_speed['daily']['wind_speed_10m_max'][0]}"
)
if wind_direction:
t_log.info(
f"Wind direction data: {wind_direction['daily']['wind_direction_10m_dominant'][0]}"
)
t_log.info("--------------------------")
if wildcard:
t_log.info(f"Wildcard data: {wildcard}")
generate_report(
fetch_cities_weather_table(),
fetch_max_temp_data(),
fetch_wind_speed_data(),
fetch_wind_direction_data(),
fetch_wildcard_data(),
fetch_city_dag_2(),
)
downstream_dag_solution()