-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhistory_tables_dag.py
303 lines (273 loc) · 9.15 KB
/
history_tables_dag.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
"""
The history_table_export DAG exports trades, assets, ledgers, operations, transactions, effects, and contract events
from the history archives and loads the data into the corresponding BigQuery tables. It also performs a Delete operation
based on the batch interval value to perform clean up before the Insert to avoid any potential duplication issues.
It is scheduled to export information to BigQuery at regular intervals.
"""
from ast import literal_eval
from datetime import datetime
from json import loads
from airflow import DAG
from airflow.models.variable import Variable
from kubernetes.client import models as k8s
from stellar_etl_airflow import macros
from stellar_etl_airflow.build_batch_stats import build_batch_stats
from stellar_etl_airflow.build_bq_insert_job_task import build_bq_insert_job
from stellar_etl_airflow.build_cross_dependency_task import build_cross_deps
from stellar_etl_airflow.build_del_ins_from_gcs_to_bq_task import (
build_del_ins_from_gcs_to_bq_task,
)
from stellar_etl_airflow.build_del_ins_operator import (
create_del_ins_task,
initialize_task_vars,
)
from stellar_etl_airflow.build_delete_data_task import build_delete_data_task
from stellar_etl_airflow.build_export_task import build_export_task
from stellar_etl_airflow.build_gcs_to_bq_task import build_gcs_to_bq_task
from stellar_etl_airflow.build_time_task import build_time_task
from stellar_etl_airflow.default import (
alert_sla_miss,
get_default_dag_args,
init_sentry,
)
init_sentry()
# Initialize the DAG
dag = DAG(
"history_table_export",
default_args=get_default_dag_args(),
start_date=datetime(2024, 7, 10, 14, 30),
catchup=True,
description="This DAG exports information for the trades, assets, ledgers, operations, transactions, effects and contract events history tables.",
schedule_interval="*/10 * * * *",
params={
"alias": "cc",
},
render_template_as_native_obj=True,
user_defined_filters={
"fromjson": lambda s: loads(s),
"container_resources": lambda s: k8s.V1ResourceRequirements(requests=s),
"literal_eval": lambda e: literal_eval(e),
},
user_defined_macros={
"subtract_data_interval": macros.subtract_data_interval,
"batch_run_date_as_datetime_string": macros.batch_run_date_as_datetime_string,
},
# sla_miss_callback=alert_sla_miss,
)
# Initialize batch metadata variables
batch_id = macros.get_batch_id()
batch_date = "{{ batch_run_date_as_datetime_string(dag, data_interval_start) }}"
# Fetch necessary variables
table_names = Variable.get("table_ids", deserialize_json=True)
public_project = "{{ var.value.public_project }}"
public_dataset = "{{ var.value.public_dataset }}"
use_testnet = literal_eval(Variable.get("use_testnet"))
use_futurenet = literal_eval(Variable.get("use_futurenet"))
use_captive_core = literal_eval(Variable.get("use_captive_core"))
txmeta_datastore_path = "{{ var.value.txmeta_datastore_path }}"
"""
The time task reads in the execution time of the current run, as well as the next
execution time. It converts these two times into ledger ranges.
"""
time_task = build_time_task(dag, use_testnet=use_testnet, use_futurenet=use_futurenet)
"""
The build batch stats task will take a snapshot of the DAG run_id, execution date,
start and end ledgers so that reconciliation and data validation are easier. The
record is written to an internal dataset for data eng use only.
"""
write_op_stats = build_batch_stats(dag, table_names["operations"])
write_trade_stats = build_batch_stats(dag, table_names["trades"])
write_effects_stats = build_batch_stats(dag, table_names["effects"])
write_tx_stats = build_batch_stats(dag, table_names["transactions"])
write_ledger_stats = build_batch_stats(dag, table_names["ledgers"])
write_asset_stats = build_batch_stats(dag, table_names["assets"])
write_contract_events_stats = build_batch_stats(dag, "contract_events")
"""
The export tasks call export commands on the Stellar ETL using the ledger range from the time task.
The results of the command are stored in a file. There is one task for each of the data types that
can be exported from the history archives.
The DAG sleeps for 30 seconds after the export_task writes to the file to give the poststart.sh
script time to copy the file over to the correct directory. If there is no sleep, the load task
starts prematurely and will not load data.
"""
op_export_task = build_export_task(
dag,
"archive",
"export_operations",
"{{ var.json.output_file_names.operations }}",
use_testnet=use_testnet,
use_futurenet=use_futurenet,
use_gcs=True,
use_captive_core=use_captive_core,
txmeta_datastore_path=txmeta_datastore_path,
resource_cfg="stellaretl",
)
trade_export_task = build_export_task(
dag,
"archive",
"export_trades",
"{{ var.json.output_file_names.trades }}",
use_testnet=use_testnet,
use_futurenet=use_futurenet,
use_gcs=True,
use_captive_core=use_captive_core,
txmeta_datastore_path=txmeta_datastore_path,
resource_cfg="stellaretl",
)
effects_export_task = build_export_task(
dag,
"archive",
"export_effects",
"effects.txt",
use_testnet=use_testnet,
use_futurenet=use_futurenet,
use_gcs=True,
use_captive_core=use_captive_core,
txmeta_datastore_path=txmeta_datastore_path,
resource_cfg="stellaretl",
)
tx_export_task = build_export_task(
dag,
"archive",
"export_transactions",
"{{ var.json.output_file_names.transactions }}",
use_testnet=use_testnet,
use_futurenet=use_futurenet,
use_gcs=True,
use_captive_core=use_captive_core,
txmeta_datastore_path=txmeta_datastore_path,
resource_cfg="stellaretl",
)
ledger_export_task = build_export_task(
dag,
"archive",
"export_ledgers",
"{{ var.json.output_file_names.ledgers }}",
use_testnet=use_testnet,
use_futurenet=use_futurenet,
use_gcs=True,
use_captive_core=use_captive_core,
txmeta_datastore_path=txmeta_datastore_path,
resource_cfg="stellaretl",
)
asset_export_task = build_export_task(
dag,
"archive",
"export_assets",
"{{ var.json.output_file_names.assets }}",
use_testnet=use_testnet,
use_futurenet=use_futurenet,
use_gcs=True,
use_captive_core=use_captive_core,
txmeta_datastore_path=txmeta_datastore_path,
resource_cfg="stellaretl",
)
contract_events_export_task = build_export_task(
dag,
"archive",
"export_contract_events",
"{{ var.json.output_file_names.contract_events }}",
use_testnet=use_testnet,
use_futurenet=use_futurenet,
use_gcs=True,
use_captive_core=use_captive_core,
txmeta_datastore_path=txmeta_datastore_path,
resource_cfg="stellaretl",
)
"""
The delete part of the task checks to see if the given partition/batch id exists in
Bigquery. If it does, the records are deleted prior to reinserting the batch.
The Insert part of the task receive the location of the file in Google Cloud storage through Airflow's XCOM system.
Then, the task merges the unique entries in the file into the corresponding table in BigQuery.
"""
del_ins_tasks = {}
export_tasks = {
"operations": op_export_task,
"trades": trade_export_task,
"effects": effects_export_task,
"transactions": tx_export_task,
"ledgers": ledger_export_task,
"assets": asset_export_task,
"contract_events": contract_events_export_task,
}
for table_id, export_task in export_tasks.items():
table_name = table_names[table_id]
task_vars = initialize_task_vars(
table_id,
table_name,
export_task.task_id,
batch_id,
batch_date,
public_project,
public_dataset,
)
del_ins_tasks[table_id] = create_del_ins_task(
dag, task_vars, build_del_ins_from_gcs_to_bq_task
)
"""
Delete and Insert operations for the Enriched History Operations table
"""
delete_enrich_op_pub_task = build_delete_data_task(
dag,
public_project,
public_dataset,
"enriched_history_operations",
"pub",
)
insert_enrich_op_pub_task = build_bq_insert_job(
dag,
public_project,
public_dataset,
"enriched_history_operations",
partition=True,
cluster=True,
dataset_type="pub",
)
dedup_assets_pub_task = build_bq_insert_job(
dag,
public_project,
public_dataset,
table_names["assets"],
partition=True,
cluster=True,
create=True,
dataset_type="pub",
)
# Set Task dependencies
(
time_task
>> write_op_stats
>> op_export_task
>> del_ins_tasks["operations"]
>> delete_enrich_op_pub_task
>> insert_enrich_op_pub_task
)
(time_task >> write_trade_stats >> trade_export_task >> del_ins_tasks["trades"])
(time_task >> write_effects_stats >> effects_export_task >> del_ins_tasks["effects"])
(
time_task
>> write_tx_stats
>> tx_export_task
>> del_ins_tasks["transactions"]
>> delete_enrich_op_pub_task
)
(
time_task
>> write_ledger_stats
>> ledger_export_task
>> del_ins_tasks["ledgers"]
>> delete_enrich_op_pub_task
)
(
time_task
>> write_asset_stats
>> asset_export_task
>> del_ins_tasks["assets"]
>> dedup_assets_pub_task
)
(
time_task
>> write_contract_events_stats
>> contract_events_export_task
>> del_ins_tasks["contract_events"]
)