Skip to content

Commit 2bcc695

Browse files
Merge pull request #310 from cityofaustin/charlie/311-reporting-updates
Open311 Scraping DAG
2 parents 29bdd4e + c325dbd commit 2bcc695

3 files changed

Lines changed: 111 additions & 22 deletions

File tree

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# test locally with: docker compose run --rm airflow-cli dags test dts_csr_report_publishing
1+
# test locally with: docker compose run --rm airflow-cli dags test dts_311_report_publishing
22

33
from os import getenv
44

@@ -49,7 +49,7 @@
4949
"opitem": "Executive Dashboard",
5050
"opfield": "datasets.Revenue",
5151
},
52-
"CSR_DATASET": {
52+
"REQUESTS_DATASET": {
5353
"opitem": "Executive Dashboard",
5454
"opfield": "datasets.CSR",
5555
},
@@ -92,7 +92,7 @@
9292
}
9393

9494
CUR_YEAR_SECRETS = {
95-
"CSR_ENDPOINT": {
95+
"REQUESTS_ENDPOINT": {
9696
"opitem": "Executive Dashboard",
9797
"opfield": "csr.Current FY Endpoint",
9898
},
@@ -107,7 +107,7 @@
107107
}
108108

109109
PREV_YEAR_SECRETS = {
110-
"CSR_ENDPOINT": {
110+
"REQUESTS_ENDPOINT": {
111111
"opitem": "Executive Dashboard",
112112
"opfield": "csr.Previous FY Endpoint",
113113
},
@@ -122,7 +122,7 @@
122122
}
123123

124124
TWO_YEARS_AGO_SECRETS = {
125-
"CSR_ENDPOINT": {
125+
"REQUESTS_ENDPOINT": {
126126
"opitem": "Executive Dashboard",
127127
"opfield": "csr.Two Years Ago FY Endpoint",
128128
},
@@ -142,14 +142,14 @@
142142
TWO_YEARS_AGO_SECRETS.update(OTHER_SECRETS)
143143

144144
with DAG(
145-
dag_id="dts_csr_report_publishing",
145+
dag_id="dts_311_report_publishing",
146146
description="Downloads reports of 311 service requests for TPW and publishes it in a Socrata dataset.",
147147
default_args=default_args,
148148
schedule_interval=(
149149
"36 2,13 * * *" if DEPLOYMENT_ENVIRONMENT == "production" else None
150150
),
151151
dagrun_timeout=timedelta(minutes=60),
152-
tags=["repo:dts-311-reporting", "socrata", "csr"],
152+
tags=["repo:dts-311-reporting", "socrata", "311"],
153153
catchup=False,
154154
) as dag:
155155
docker_image = "atddocker/dts-311-reporting:production"
@@ -159,12 +159,12 @@
159159
two_years_env = get_env_vars_task(TWO_YEARS_AGO_SECRETS)
160160

161161
t1 = DockerOperator(
162-
task_id="cur_year_csr_report_to_socrata",
162+
task_id="cur_year_requests_report_to_socrata",
163163
image=docker_image,
164164
docker_conn_id="docker_default",
165165
api_version="auto",
166166
auto_remove="force",
167-
command=f"python etl/csr_to_socrata.py",
167+
command="python -m etl.csv_reporting.requests_to_socrata",
168168
environment=cur_year_env,
169169
tty=True,
170170
force_pull=True,
@@ -176,7 +176,7 @@
176176
docker_conn_id="docker_default",
177177
api_version="auto",
178178
auto_remove="force",
179-
command=f"python etl/flex_notes_to_socrata.py",
179+
command="python -m etl.csv_reporting.flex_notes_to_socrata",
180180
environment=cur_year_env,
181181
tty=True,
182182
)
@@ -187,18 +187,18 @@
187187
docker_conn_id="docker_default",
188188
api_version="auto",
189189
auto_remove="force",
190-
command=f"python etl/activities_to_socrata.py",
190+
command="python -m etl.csv_reporting.activities_to_socrata",
191191
environment=cur_year_env,
192192
tty=True,
193193
)
194194

195195
t4 = DockerOperator(
196-
task_id="prev_year_csr_report_to_socrata",
196+
task_id="prev_year_requests_report_to_socrata",
197197
image=docker_image,
198198
docker_conn_id="docker_default",
199199
api_version="auto",
200200
auto_remove="force",
201-
command=f"python etl/csr_to_socrata.py",
201+
command="python -m etl.csv_reporting.requests_to_socrata",
202202
environment=prev_year_env,
203203
tty=True,
204204
)
@@ -209,7 +209,7 @@
209209
docker_conn_id="docker_default",
210210
api_version="auto",
211211
auto_remove="force",
212-
command=f"python etl/flex_notes_to_socrata.py",
212+
command="python -m etl.csv_reporting.flex_notes_to_socrata",
213213
environment=prev_year_env,
214214
tty=True,
215215
)
@@ -220,18 +220,18 @@
220220
docker_conn_id="docker_default",
221221
api_version="auto",
222222
auto_remove="force",
223-
command=f"python etl/activities_to_socrata.py",
223+
command="python -m etl.csv_reporting.activities_to_socrata",
224224
environment=prev_year_env,
225225
tty=True,
226226
)
227227

228228
t7 = DockerOperator(
229-
task_id="two_years_ago_csr_report_to_socrata",
229+
task_id="two_years_ago_requests_report_to_socrata",
230230
image=docker_image,
231231
docker_conn_id="docker_default",
232232
api_version="auto",
233233
auto_remove="force",
234-
command=f"python etl/csr_to_socrata.py",
234+
command="python -m etl.csv_reporting.requests_to_socrata",
235235
environment=two_years_env,
236236
tty=True,
237237
)
@@ -242,7 +242,7 @@
242242
docker_conn_id="docker_default",
243243
api_version="auto",
244244
auto_remove="force",
245-
command=f"python etl/flex_notes_to_socrata.py",
245+
command="python -m etl.csv_reporting.flex_notes_to_socrata",
246246
environment=two_years_env,
247247
tty=True,
248248
)
@@ -253,7 +253,7 @@
253253
docker_conn_id="docker_default",
254254
api_version="auto",
255255
auto_remove="force",
256-
command=f"python etl/activities_to_socrata.py",
256+
command="python -m etl.csv_reporting.activities_to_socrata",
257257
environment=two_years_env,
258258
tty=True,
259259
)

dags/dts_open_311_scrape.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# test locally with: docker compose run --rm airflow-cli dags test dts_open_311_scrape
2+
3+
from os import getenv
4+
5+
from datetime import timedelta
6+
7+
from airflow.decorators import task
8+
from airflow.models import DAG
9+
from airflow.operators.docker_operator import DockerOperator
10+
from pendulum import datetime, duration, now
11+
12+
from utils.onepassword import get_env_vars_task
13+
from utils.slack_operator import task_fail_slack_alert
14+
from utils.time import get_previous_run_date
15+
16+
DEPLOYMENT_ENVIRONMENT = getenv("ENVIRONMENT", "development")
17+
18+
default_args = {
19+
"owner": "airflow",
20+
"depends_on_past": False,
21+
"start_date": datetime(2015, 12, 1, tz="America/Chicago"),
22+
"email_on_failure": False,
23+
"email_on_retry": False,
24+
"retries": 0,
25+
"on_failure_callback": task_fail_slack_alert,
26+
}
27+
28+
REQUIRED_SECRETS = {
29+
"SO_WEB": {
30+
"opitem": "Socrata Key ID, Secret, and Token",
31+
"opfield": "socrata.endpoint",
32+
},
33+
"SO_TOKEN": {
34+
"opitem": "Socrata Key ID, Secret, and Token",
35+
"opfield": "socrata.appToken",
36+
},
37+
"SO_SECRET": {
38+
"opitem": "Socrata Key ID, Secret, and Token",
39+
"opfield": "socrata.apiKeySecret",
40+
},
41+
"SO_KEY": {
42+
"opitem": "Socrata Key ID, Secret, and Token",
43+
"opfield": "socrata.apiKeyId",
44+
},
45+
"REALTIME_DATASET": {
46+
"opitem": "Executive Dashboard",
47+
"opfield": "datasets.Realtime",
48+
},
49+
"OPEN_311_API_KEY": {
50+
"opitem": "Austin Open 311 API key",
51+
"opfield": "production.API key",
52+
},
53+
"OPEN_311_API_BASE_URL": {
54+
"opitem": "Austin Open 311 API key",
55+
"opfield": "production.Base URL",
56+
},
57+
}
58+
59+
60+
with DAG(
61+
dag_id="dts_open_311_scrape",
62+
description="Downloads CSRs from Open311 API and publishes them in a Socrata dataset",
63+
default_args=default_args,
64+
schedule_interval=(
65+
"*/5 * * * *" if DEPLOYMENT_ENVIRONMENT == "production" else None
66+
),
67+
dagrun_timeout=timedelta(minutes=5),
68+
tags=["repo:dts-311-reporting", "socrata", "311", "open311"],
69+
catchup=False,
70+
) as dag:
71+
docker_image = "atddocker/dts-311-reporting:production"
72+
73+
env_vars = get_env_vars_task(REQUIRED_SECRETS)
74+
one_day_ago = now("America/Chicago").subtract(days=1)
75+
date_arg = get_previous_run_date(fallback_date=one_day_ago.to_iso8601_string())
76+
77+
t1 = DockerOperator(
78+
task_id="open311_to_socrata",
79+
image=docker_image,
80+
docker_conn_id="docker_default",
81+
api_version="auto",
82+
auto_remove="force",
83+
command=f"python -m etl.open311.open311_to_socrata -d {date_arg["last_run_datetime_iso"]}",
84+
environment=env_vars,
85+
tty=True,
86+
force_pull=True,
87+
)
88+
89+
t1

dags/utils/time.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
task_id="get_previous_run_date",
77
multiple_outputs=True,
88
)
9-
def get_previous_run_date(**context):
9+
def get_previous_run_date(fallback_date="1970-01-01", **context):
1010
"""Task to return the last successful run date in UTC datetime format.
1111
1212
Args:
@@ -16,9 +16,9 @@ def get_previous_run_date(**context):
1616
Returns:
1717
Dict: dict containing the last run datetime and other future formats
1818
"""
19-
last_run_datetime = context.get("prev_start_date_success") or parse("1970-01-01")
19+
last_run_datetime = context.get("prev_start_date_success") or parse(fallback_date)
2020

2121
return {
2222
"last_run_datetime": last_run_datetime,
23-
# add other formats here
23+
"last_run_datetime_iso": last_run_datetime.isoformat(),
2424
}

0 commit comments

Comments
 (0)