-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter_postgres.py
More file actions
309 lines (272 loc) · 9.06 KB
/
writer_postgres.py
File metadata and controls
309 lines (272 loc) · 9.06 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
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
#
# Copyright 2025 ABSA Group Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Postgres writer module.
Handles optional initialization via AWS Secrets Manager and topic-based inserts into Postgres.
"""
import json
import os
import logging
from typing import Any, Dict, Tuple, Optional
import boto3
try:
import psycopg2 # noqa: F401
except ImportError: # pragma: no cover - environment without psycopg2
psycopg2 = None # type: ignore
from src.utils.trace_logging import log_payload_at_trace
# Define a unified psycopg2 error base for safe exception handling even if psycopg2 missing
if psycopg2 is not None: # type: ignore
try: # pragma: no cover - attribute presence depends on installed psycopg2 variant
PsycopgError = psycopg2.Error # type: ignore[attr-defined]
except AttributeError: # pragma: no cover
class PsycopgError(Exception): # type: ignore
"""Shim psycopg2 error base when psycopg2 provides no Error attribute."""
else: # fallback shim when psycopg2 absent
class PsycopgError(Exception): # type: ignore
"""Shim psycopg2 error base when psycopg2 is not installed."""
# Module level globals for typing
logger: logging.Logger = logging.getLogger(__name__)
POSTGRES: Dict[str, Any] = {"database": ""}
def init(logger_instance: logging.Logger) -> None:
"""Initialize Postgres credentials either from AWS Secrets Manager or fallback empty config.
Args:
logger_instance: Shared application logger.
"""
global logger # pylint: disable=global-statement
global POSTGRES # pylint: disable=global-statement
logger = logger_instance
secret_name = os.environ.get("POSTGRES_SECRET_NAME", "")
secret_region = os.environ.get("POSTGRES_SECRET_REGION", "")
if secret_name and secret_region:
aws_secrets = boto3.Session().client(service_name="secretsmanager", region_name=secret_region)
postgres_secret = aws_secrets.get_secret_value(SecretId=secret_name)["SecretString"]
POSTGRES = json.loads(postgres_secret)
else:
POSTGRES = {"database": ""}
logger.debug("Initialized POSTGRES writer")
def postgres_edla_write(cursor, table: str, message: Dict[str, Any]) -> None:
"""Insert a dlchange style event row.
Args:
cursor: Database cursor.
table: Target table name.
message: Event payload.
"""
logger.debug("Sending to Postgres - %s", table)
cursor.execute(
f"""
INSERT INTO {table}
(
event_id,
tenant_id,
source_app,
source_app_version,
environment,
timestamp_event,
country,
catalog_id,
operation,
"location",
"format",
format_options,
additional_info
)
VALUES
(
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s
)""",
(
message["event_id"],
message["tenant_id"],
message["source_app"],
message["source_app_version"],
message["environment"],
message["timestamp_event"],
message.get("country", ""),
message["catalog_id"],
message["operation"],
message.get("location"),
message["format"],
(json.dumps(message.get("format_options")) if "format_options" in message else None),
(json.dumps(message.get("additional_info")) if "additional_info" in message else None),
),
)
def postgres_run_write(cursor, table_runs: str, table_jobs: str, message: Dict[str, Any]) -> None:
"""Insert a run event row plus related job rows.
Args:
cursor: Database cursor.
table_runs: Runs table name.
table_jobs: Jobs table name.
message: Event payload (includes jobs array).
"""
logger.debug("Sending to Postgres - %s and %s", table_runs, table_jobs)
cursor.execute(
f"""
INSERT INTO {table_runs}
(
event_id,
job_ref,
tenant_id,
source_app,
source_app_version,
environment,
timestamp_start,
timestamp_end
)
VALUES
(
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s
)""",
(
message["event_id"],
message["job_ref"],
message["tenant_id"],
message["source_app"],
message["source_app_version"],
message["environment"],
message["timestamp_start"],
message["timestamp_end"],
),
)
for job in message["jobs"]:
cursor.execute(
f"""
INSERT INTO {table_jobs}
(
event_id,
country,
catalog_id,
status,
timestamp_start,
timestamp_end,
message,
additional_info
)
VALUES
(
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s
)""",
(
message["event_id"],
job.get("country", ""),
job["catalog_id"],
job["status"],
job["timestamp_start"],
job["timestamp_end"],
job.get("message"),
(json.dumps(job.get("additional_info")) if "additional_info" in job else None),
),
)
def postgres_test_write(cursor, table: str, message: Dict[str, Any]) -> None:
"""Insert a test topic row.
Args:
cursor: Database cursor.
table: Target table name.
message: Event payload.
"""
logger.debug("Sending to Postgres - %s", table)
cursor.execute(
f"""
INSERT INTO {table}
(
event_id,
tenant_id,
source_app,
environment,
timestamp_event,
additional_info
)
VALUES
(
%s,
%s,
%s,
%s,
%s,
%s
)""",
(
message["event_id"],
message["tenant_id"],
message["source_app"],
message["environment"],
message["timestamp"],
(json.dumps(message.get("additional_info")) if "additional_info" in message else None),
),
)
def write(topic_name: str, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
"""Dispatch insertion for a topic into the correct Postgres table(s).
Skips if Postgres not configured or psycopg2 unavailable. Returns success flag and optional error.
Args:
topic_name: Incoming topic identifier.
message: Event payload.
"""
try:
if not POSTGRES.get("database"):
logger.debug("No Postgres - skipping")
return True, None
if psycopg2 is None: # type: ignore
logger.debug("psycopg2 not available - skipping actual Postgres write")
return True, None
log_payload_at_trace(logger, "Postgres", topic_name, message)
with psycopg2.connect( # type: ignore[attr-defined]
database=POSTGRES["database"],
host=POSTGRES["host"],
user=POSTGRES["user"],
password=POSTGRES["password"],
port=POSTGRES["port"],
) as connection: # type: ignore[call-arg]
with connection.cursor() as cursor: # type: ignore
if topic_name == "public.cps.za.dlchange":
postgres_edla_write(cursor, "public_cps_za_dlchange", message)
elif topic_name == "public.cps.za.runs":
postgres_run_write(cursor, "public_cps_za_runs", "public_cps_za_runs_jobs", message)
elif topic_name == "public.cps.za.test":
postgres_test_write(cursor, "public_cps_za_test", message)
else:
msg = f"unknown topic for postgres {topic_name}"
logger.error(msg)
return False, msg
connection.commit() # type: ignore
except (RuntimeError, PsycopgError) as e: # narrowed exception set
err_msg = f"The Postgres writer with failed unknown error: {str(e)}"
logger.exception(err_msg)
return False, err_msg
return True, None