-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathpipeline_utils.py
More file actions
262 lines (228 loc) · 9.12 KB
/
Copy pathpipeline_utils.py
File metadata and controls
262 lines (228 loc) · 9.12 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
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
import time
from datetime import UTC, datetime, timedelta
import psycopg
from loguru import logger
from snowflake.connector import ProgrammingError
from snowflake.connector.errors import ForbiddenError
from snowflake.connector.network import ReauthenticationRequest, RetryRequest
from batch_worker import LoadingBatchWorkerClient
from constants import (
CLAIM_INSTITUTIONAL_ITEM_SS_TABLE,
CLAIM_INSTITUTIONAL_SS_TABLE,
CLAIM_PROFESSIONAL_ITEM_SS_TABLE,
CLAIM_PROFESSIONAL_SS_TABLE,
DEFAULT_MAX_DATE,
DEFAULT_PARTITION,
PHASE_1_CUTOFF,
)
from extractor import PostgresExtractor, SnowflakeExtractor, Source
from load_partition import LoadPartition
from loader import LoadType, PostgresLoader, get_connection_string, should_track_load_progress
from model.base_model import (
LoadMode,
T,
stale_phase_1_claims_query,
)
from model.idr_beneficiary_low_income_subsidy_cmbnd import IdrBeneficiaryLowIncomeSubsidyCmbnd
from model.idr_beneficiary_ma_part_d_enrollment import IdrBeneficiaryMaPartDEnrollment
from model.idr_beneficiary_ma_part_d_enrollment_rx import IdrBeneficiaryMaPartDEnrollmentRx
from model.load_progress import LoadProgress
from settings import BENEFICIARY_PART_D_PRUNE_BATCH_LIMIT, BENEFICIARY_PRUNE_BATCH_LIMIT
def get_progress(
load_mode: LoadMode,
source: Source,
table_name: str,
start_time: datetime,
partition: LoadPartition,
) -> LoadProgress | None:
if not should_track_load_progress(load_mode):
return None
return PostgresExtractor(
load_mode=load_mode, cls=LoadProgress, partition=partition
).extract_single(
LoadProgress.fetch_query(partition, start_time, source),
{LoadProgress.query_placeholder(): table_name},
)
def extract_and_load(
cls: type[T],
source: Source,
load_mode: LoadMode,
job_start: datetime,
load_type: LoadType,
worker_client: LoadingBatchWorkerClient,
partition: LoadPartition | None = None,
) -> bool:
partition = partition or DEFAULT_PARTITION
if source == Source.SNOWFLAKE:
data_extractor = SnowflakeExtractor(cls=cls, partition=partition)
else:
data_extractor = PostgresExtractor(load_mode=load_mode, cls=cls, partition=partition)
logger.info("loading {}", cls.table())
last_error = datetime.min.replace(tzinfo=UTC)
loader = PostgresLoader()
error_count = 0
max_errors = 3
while True:
try:
progress = get_progress(load_mode, source, cls.table(), job_start, partition)
if progress:
logger.info(
"progress for {} {} - last_ts: {} job_start_ts: {} batch_complete_ts: {}",
cls.table(),
progress.batch_partition,
progress.last_ts,
progress.job_start_ts,
progress.batch_complete_ts,
)
else:
logger.info("no previous progress for {} - {}", cls.table(), partition.name)
data_iter = (
data_extractor.extract_full_idr_data(source)
if cls.should_fully_sync_delete_diff()
else data_extractor.extract_idr_data(progress, job_start, source)
)
res = loader.load(
data_iter,
cls,
job_start,
partition,
progress,
load_type,
load_mode,
worker_client,
)
data_extractor.close()
return res
# Snowflake will throw a reauth error if the pipeline has been running for several hours
# but it seems to be wrapped in a ProgrammingError.
# Unclear the best way to handle this, it will require a bit more trial and error
except (
ReauthenticationRequest,
RetryRequest,
ForbiddenError,
ProgrammingError,
) as ex:
time_expired = datetime.now(UTC) - last_error > timedelta(seconds=10)
if time_expired:
error_count = 0
error_count += 1
if error_count < max_errors:
last_error = datetime.now(UTC)
logger.opt(exception=True).warning("received transient error, retrying...")
data_extractor.reconnect()
else:
logger.error("max attempts exceeded")
raise ex
time.sleep(1)
except Exception as ex:
logger.opt(exception=True).error("error loading {}", cls.table())
raise ex
def prune_phase_1_ss_claims(
cls: type[T],
load_mode: LoadMode,
job_start: datetime,
) -> bool:
shared_claim_tables = {
CLAIM_INSTITUTIONAL_SS_TABLE: CLAIM_INSTITUTIONAL_ITEM_SS_TABLE,
CLAIM_PROFESSIONAL_SS_TABLE: CLAIM_PROFESSIONAL_ITEM_SS_TABLE,
}
claim_table = cls.table()
item_table = shared_claim_tables.get(claim_table)
if item_table is None:
return True
prune_cutoff_date = job_start - timedelta(days=PHASE_1_CUTOFF)
logger.info("pruning phase 1 ss claims older than {}", prune_cutoff_date)
prune_query, params = stale_phase_1_claims_query(claim_table, item_table, prune_cutoff_date)
with psycopg.connect(get_connection_string(load_mode)) as conn:
for target_table in [item_table, claim_table]:
total_row_count = 0
while True:
with conn.transaction():
res = conn.execute(
f"""DELETE FROM {target_table} WHERE clm_uniq_id IN ({prune_query})""", # type: ignore
params,
)
total_row_count += res.rowcount
logger.info("pruned {} rows from {}", res.rowcount, item_table)
if res.rowcount == 0:
logger.info("Total rows pruned from {}: {}", item_table, total_row_count)
break
return True
def prune_bene_lis_cmbnd(
load_mode: LoadMode,
) -> bool:
bene_table = IdrBeneficiaryLowIncomeSubsidyCmbnd.table()
logger.info("pruning obsolete lis beneficiaries")
with psycopg.connect(get_connection_string(load_mode)) as conn, conn.transaction():
while True:
res = conn.execute(
f"""
DELETE FROM {bene_table}
WHERE (bene_sk, bene_cmbnd_deemd_efctv_dt, idr_trans_obslt_ts) IN (
SELECT bene_sk, bene_cmbnd_deemd_efctv_dt, idr_trans_obslt_ts
FROM {bene_table}
WHERE idr_trans_obslt_ts < %s
LIMIT %s
)
""", # type: ignore
(DEFAULT_MAX_DATE, BENEFICIARY_PRUNE_BATCH_LIMIT),
)
logger.info("pruned {} rows from {}", res.rowcount, bene_table)
if res.rowcount < BENEFICIARY_PRUNE_BATCH_LIMIT:
break
return True
def prune_bene_ma_part_d(
load_mode: LoadMode,
) -> bool:
bene_table = IdrBeneficiaryMaPartDEnrollment.table()
logger.info("pruning obsolete part d beneficiaries", DEFAULT_MAX_DATE)
with psycopg.connect(get_connection_string(load_mode)) as conn, conn.transaction():
while True:
res = conn.execute(
f"""
DELETE FROM {bene_table}
WHERE (bene_sk, bene_enrlmt_bgn_dt, bene_enrlmt_pgm_type_cd) IN (
SELECT bene_sk, bene_enrlmt_bgn_dt, bene_enrlmt_pgm_type_cd
FROM {bene_table}
WHERE idr_trans_obslt_ts < %s
LIMIT %s
)
""", # type: ignore
(DEFAULT_MAX_DATE, BENEFICIARY_PART_D_PRUNE_BATCH_LIMIT),
)
logger.info("pruned {} rows from {}", res.rowcount, bene_table)
if res.rowcount < BENEFICIARY_PART_D_PRUNE_BATCH_LIMIT:
break
return True
def prune_bene_ma_part_d_rx(
load_mode: LoadMode,
) -> bool:
bene_table = IdrBeneficiaryMaPartDEnrollmentRx.table()
logger.info("pruning obsolete part d rx beneficiaries", DEFAULT_MAX_DATE)
with psycopg.connect(get_connection_string(load_mode)) as conn, conn.transaction():
while True:
res = conn.execute(
f"""
DELETE FROM {bene_table}
WHERE (bene_sk,
bene_cntrct_num,
bene_pbp_num,
bene_enrlmt_bgn_dt,
bene_enrlmt_pdp_rx_info_bgn_dt
) IN (
SELECT bene_sk,
bene_cntrct_num,
bene_pbp_num,
bene_enrlmt_bgn_dt,
bene_enrlmt_pdp_rx_info_bgn_dt
FROM {bene_table}
WHERE idr_trans_obslt_ts < %s
LIMIT %s
)
""", # type: ignore
(DEFAULT_MAX_DATE, BENEFICIARY_PART_D_PRUNE_BATCH_LIMIT),
)
logger.info("pruned {} rows from {}", res.rowcount, bene_table)
if res.rowcount < BENEFICIARY_PART_D_PRUNE_BATCH_LIMIT:
break
return True