This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
aris_ccd_nonfiscal_district.py
413 lines (351 loc) · 12.1 KB
/
aris_ccd_nonfiscal_district.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from datetime import timedelta, datetime
from multiprocessing import connection
from pickle import TRUE
import airflow
import code_executer
from airflow import DAG
from airflow.operators.python import PythonOperator, ShortCircuitOperator
#from airflow.operators.python_operator import BranchPythonOperator
#from airflow.contrib.operators.ssh_operator import SSHOperator
from airflow.contrib.hooks.ssh_hook import SSHHook
from airflow.utils.edgemodifier import Label
import requests
SERVICE_GIT_DIR = 'C:\\ARIS\\autoDigest\\ccd' # File housing ARIS repos on SAS server's C drive
QC_Run = "False"
Download_Data = "False"
sas_variables = {'Year' : "2021",
'Version':"0a" }
#Year = "2020"
# default args
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'email': ['mtrihn@air.org', 'gchickering@air.org'],
'email_on_failure': TRUE,
'email_on_retry': False,
'start_date': datetime.now() - timedelta(minutes=20),
'retries': 0,
'retry_delay': timedelta(minutes=5)
}
# Define Main DAG for CCD pipeline
dag = DAG(dag_id='aris_ccd_nonfiscal_district_etl',
default_args=default_args,
# schedule_interval='0,10,20,30,40,50 * * * *',
dagrun_timeout=timedelta(seconds=3600))
def check_airflow_to_azure():
ssh = SSHHook(ssh_conn_id="svc_202205_sasdev")
ssh_client = None
print(ssh)
try:
ssh_client = ssh.get_conn()
temp = ssh_client.load_system_host_keys()
print(temp)
finally:
if ssh_client:
ssh_client.close()
def check_azure_to_database():
command = 'cd ' + SERVICE_GIT_DIR + '\\DB-Generation' + ' && python check_connections_azure_to_db.py'
error_strings= ["Closing db connection"]
results = connect_to_server_qc(command, error_strings)
print(results)
if results == False:
results = True
else:
results = False
return (results)
def check_azure_to_NCES():
command = 'cd ' + SERVICE_GIT_DIR + '\\DB-Generation' + ' && python check_connections_azure_to_nces.py'
error_strings= ["Response [200]"]
results = connect_to_server_qc(command, error_strings)
print(results)
if results == False:
results = True
else:
results = False
return (results)
def connect_to_server(run_command):
print(run_command)
ssh = SSHHook(ssh_conn_id="svc_202205_sasdev")
ssh_client = None
print(ssh)
try:
ssh_client = ssh.get_conn()
ssh_client.load_system_host_keys()
command = run_command
stdin, stdout, stderr = ssh_client.exec_command(command)
out = stdout.read().decode().strip()
error = stderr.read().decode().strip()
print(out)
print(error)
finally:
if ssh_client:
ssh_client.close()
def connect_to_server_qc(run_command,error_strings_list):
'''
Purpose: check output of sas log files.
'''
error_strings= error_strings_list
main_flag = 0
ssh = SSHHook(ssh_conn_id="svc_202205_sasdev")
ssh_client = None
print(ssh)
try:
ssh_client = ssh.get_conn()
ssh_client.load_system_host_keys()
command = run_command
print(command)
stdin, stdout, stderr = ssh_client.exec_command(command)
stdout.channel.recv_exit_status()
lines = stdout.readlines()
for line in lines:
print(line.strip())
if any(strings in line for strings in error_strings):
main_flag = 1
error = stderr.read().decode().strip()
print(error)
finally:
if ssh_client:
ssh_client.close()
if main_flag == 1:
return(False)
else:
return(True)
def links(Download_Data):
'''
Purpose: execute ccd_data_list_downloader.py on command line to generate list of CCD links
'''
if(Download_Data == "False"):
return True
command = 'cd ' + SERVICE_GIT_DIR + '\\IO' + '&& python ccd_data_list_downloader.py'
error_strings= ["Finished checking for links"]
results = connect_to_server_qc(command, error_strings)
print(results)
if results == False:
results = True
else:
results = False
return (results)
def download_dat():
'''
Purpose: execute ccd_data_downloader.py on command line to download CCD data
'''
error_strings= ["Finished downloading data from website"]
command = 'cd ' + SERVICE_GIT_DIR + '\\IO' ' && python ccd_data_downloader.py'
results = connect_to_server_qc(command, error_strings)
print(results)
if results == False:
results = True
else:
results = False
return (results)
def download_dodea_dat():
'''
Purpose: execute ccd_dodea_downloader.py on command line to download CCD data
'''
command = 'cd ' + SERVICE_GIT_DIR + '\\IO' ' && python ccd_dodea_downloader.py'
connect_to_server(command)
def ccd_edge_downloader():
'''
Purpose: execute ccd_edge_downloader.py on command line to download CCD data
'''
command = 'cd ' + SERVICE_GIT_DIR + '\\IO' ' && python ccd_edge_downloader.py'
connect_to_server(command)
def nonfiscal(year, version):
'''
Purpose: execute ccd_nonfiscal_state_RE2.sas on command line to generate nonfiscal long data from ccd data
'''
command = 'cd ' + SERVICE_GIT_DIR + '\\SAS' + '&& sas ccd_nonfiscal_state-RE2.sas -set cnfyr ' + year + ' -set cnfv ' + version
connect_to_server(command)
def nonfiscal_district():
'''
Purpose: execute t318 SAS code
'''
command = 'cd ' + SERVICE_GIT_DIR + '\\SAS' + ' && sas ccd_nonfiscal_district'
connect_to_server(command)
def district_convert():
'''
Purpose: execute t318 SAS code
'''
command = 'cd ' + SERVICE_GIT_DIR + '\\SAS' + ' && sas ccd_district_convert'
connect_to_server(command)
def write_to_db(year):
'''
Purpose: Write Output file to the db
'''
file = 'Output-CCD-ST-' + year + '.xlsx'
print(file)
command = 'cd ' + SERVICE_GIT_DIR + '\\DB-Generation' + ' && python write_to_db.py ' + year + ' nonfiscal ' + file
connect_to_server(command)
def qc_sas_logs(qc_run):
'''
Purpose: check output of sas log files.
'''
if(qc_run == "False"):
return True
else:
command = 'cd ' + SERVICE_GIT_DIR + '\\DB-Generation' + ' && python sas_parser.py ccd_nonfiscal_state-RE2.log'
error_strings= ["Critical Errors"]
results = connect_to_server_qc(command, error_strings)
return (results)
def qc_sas_output(qc_run, year):
'''
Purpose: check output of sas output files
'''
file = 'Output-CCD-ST-' + year + '.xlsx'
print(file)
command = 'cd ' + SERVICE_GIT_DIR + '\\DB-Generation' + ' && python qc_sas_output.py ' + year + ' nonfiscal ' + file
if(qc_run == "False"):
return True
else:
connect_to_server(command)
return True
def qc_database_linking(qc_run, year):
##This is the file to run through the script
file = 'Output-CCD-ST-' + year + '.xlsx'
if(qc_run == "False"):
return True
else:
error_strings= ["Please resolve these duplicated values issue", "Discrepancy found between Sas output file and database value"]
command = 'cd ' + SERVICE_GIT_DIR + '\\DB-Generation' + ' && python qc_database.py ' + year + ' nonfiscal ' + file
results = connect_to_server_qc(command, error_strings)
return (results)
def mrt_nonfiscal_district():
'''
Purpose: execute write_mrt.py on command line to generate mrt from nonfiscal long and write to database.
'''
ssh = SSHHook(ssh_conn_id="svc_202205_sasdev")
ssh_client = None
print(ssh)
try:
ssh_client = ssh.get_conn()
ssh_client.load_system_host_keys()
command = 'cd ' + SERVICE_GIT_DIR + '\\DB-Generation' + ' && python write_mrt_nonfiscal_district.py'
stdin, stdout, stderr = ssh_client.exec_command(command)
out = stdout.read().decode().strip()
error = stderr.read().decode().strip()
print(out)
print(error)
finally:
if ssh_client:
ssh_client.close()
############# Operators ##################
##Check Connections
check_airflow_to_azure = PythonOperator(
task_id = "check_airflow_to_azure",
python_callable = check_airflow_to_azure,
trigger_rule='all_success',
dag = dag
)
check_azure_to_database = ShortCircuitOperator(
task_id = "check_azure_to_database",
python_callable = check_azure_to_database,
trigger_rule='all_success',
dag = dag
)
check_azure_to_NCES = ShortCircuitOperator(
task_id = "check_azure_to_NCES",
python_callable = check_azure_to_NCES,
trigger_rule='all_success',
dag = dag
)
#Download CCD Links
download_links = ShortCircuitOperator(
task_id='download_links',
python_callable=links,
op_kwargs= {"Download_Data": Download_Data},
dag=dag
)
# # Download CCD Data
download_data = ShortCircuitOperator(
task_id='download_data',
python_callable=download_dat,
dag=dag
)
# # Download Dodea Data
download_dodea_data = PythonOperator(
task_id='download_dodea_data',
python_callable= download_dodea_dat,
dag=dag
)
##Download Edge Data
download_edge_data = PythonOperator(
task_id='download_edge_data',
python_callable= ccd_edge_downloader,
dag=dag
)
# Generate Nonfiscal state from CCD Data with SAS
gen_nonfiscal = PythonOperator(
task_id='gen_nonfiscal',
python_callable=nonfiscal,
trigger_rule='all_success',
op_kwargs= {"year": sas_variables['Year'],
"version": sas_variables['Version']},
dag=dag
)
# Generate Nonfiscal district from CCD Data with SAS
gen_nonfiscal_district = PythonOperator(
task_id='gen_nonfiscal_district',
python_callable=nonfiscal_district,
trigger_rule='all_success',
op_kwargs= {"year": sas_variables['Year'],
"version": sas_variables['Version']},
dag=dag
)
# Generate Nonfiscal district from CCD Data with SAS
gen_district_wide = PythonOperator(
task_id='gen_district_wide',
python_callable=district_convert,
trigger_rule='all_success',
op_kwargs= {"year": sas_variables['Year'],
"version": sas_variables['Version']},
dag=dag
)
#QC Steps
#QC Sas Logs
qc_sas_logs = ShortCircuitOperator(
task_id='qc_sas_logs',
python_callable=qc_sas_logs,
op_kwargs= {"qc_run": QC_Run},
trigger_rule='all_success',
dag=dag
)
#QC Sas Output
qc_sas_output = ShortCircuitOperator(
task_id='qc_sas_output',
python_callable= qc_sas_output,
op_kwargs= {"qc_run": QC_Run,
"year": sas_variables['Year']},
trigger_rule='all_success',
dag=dag
)
#QC Data written to DB
qc_database = ShortCircuitOperator(
task_id = "qc_database",
python_callable = qc_database_linking,
op_kwargs= {"qc_run": QC_Run,
"year": sas_variables['Year']},
trigger_rule='all_success',
dag = dag
)
##Write Data to DB
write_to_db = PythonOperator(
task_id='write_to_db',
python_callable=write_to_db,
op_kwargs= {"year": sas_variables['Year']},
trigger_rule = "none_failed",
dag=dag
)
load_mrt_nonfiscal_district = PythonOperator(
task_id = "load_mrt_nonfiscal_district",
python_callable = mrt_nonfiscal_district,
dag = dag
)
check_airflow_to_azure >> Label("Checking Connections") >> check_azure_to_NCES >> check_azure_to_database >> Label("Check for New Links") >> download_links
download_links >> Label("Downloading Data") >> download_data >> download_dodea_data >> download_edge_data
download_edge_data >> Label("Running Sas Script") >> gen_nonfiscal >> Label("QC Checks:Sas Output") >> qc_sas_logs >> gen_district_wide
gen_district_wide >> Label("QC Checks:Sas Output") >> qc_sas_logs >> gen_nonfiscal_district
gen_nonfiscal_district >> Label("QC Checks:Sas Output") >> qc_sas_logs >> qc_sas_output
qc_sas_output >> Label("Write to DB") >> write_to_db >> Label("QC Check:Database")>> qc_database
qc_database >> Label("Create Tables")
##>> load_mrt_nonfiscal_state
download_links >> download_dat >> gen_nonfiscal_district >> gen_district_wide >> load_mrt_nonfiscal_district