Skip to content

Commit 5fa9dd8

Browse files
committed
Merge branch 'master' into text_colour_fix
Merge from master
2 parents 6032e11 + 59f8ab7 commit 5fa9dd8

20 files changed

Lines changed: 199 additions & 106 deletions

File tree

app/airflow/dags/libs/SR_processing/core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ def process_data_dictionary(**kwargs) -> None:
7979
except Exception as e:
8080
logging.error(f"Error processing data dictionary: {str(e)}")
8181
update_job_status(
82-
stage=JobStageType.BUILD_CONCEPTS_FROM_DICT,
82+
stage=JobStageType.UPLOAD_SCAN_REPORT,
8383
status=StageStatusType.FAILED,
8484
scan_report=scan_report_id,
85+
details=f"Upload failed: {str(e)}",
8586
)
8687
raise e
8788

@@ -156,6 +157,7 @@ def process_and_create_scan_report_entries(**kwargs) -> None:
156157
stage=JobStageType.UPLOAD_SCAN_REPORT,
157158
status=StageStatusType.FAILED,
158159
scan_report=scan_report_id,
160+
details=f"Upload failed: {str(e)}",
159161
)
160162
raise e
161163

@@ -170,6 +172,7 @@ def process_and_create_scan_report_entries(**kwargs) -> None:
170172
stage=JobStageType.UPLOAD_SCAN_REPORT,
171173
status=StageStatusType.FAILED,
172174
scan_report=scan_report_id,
175+
details=f"Upload failed: {str(e)}",
173176
)
174177
raise e
175178

@@ -191,6 +194,7 @@ def process_and_create_scan_report_entries(**kwargs) -> None:
191194
stage=JobStageType.UPLOAD_SCAN_REPORT,
192195
status=StageStatusType.FAILED,
193196
scan_report=scan_report_id,
197+
details=f"Upload failed: {str(e)}",
194198
)
195199
raise e
196200

@@ -202,6 +206,7 @@ def process_and_create_scan_report_entries(**kwargs) -> None:
202206
stage=JobStageType.UPLOAD_SCAN_REPORT,
203207
status=StageStatusType.FAILED,
204208
scan_report=scan_report_id,
209+
details="Scan report processing failed: No tables found in the scan report",
205210
)
206211
# Remove the temp file after processing
207212
# TODO: double check with KubernetesExecutor if this is the best way to do this

app/airflow/dags/libs/SR_processing/db_services.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def update_temp_data_dictionary_table(
133133
stage=JobStageType.UPLOAD_SCAN_REPORT,
134134
status=StageStatusType.FAILED,
135135
scan_report=scan_report_id,
136+
details=f"Upload failed: {str(e)}",
136137
)
137138
raise e
138139

app/airflow/dags/libs/auto_mapping/core_get_existing_concepts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ def find_existing_concepts(**kwargs) -> None:
8383
scan_report_table=table_id,
8484
stage=JobStageType.GENERATE_RULES,
8585
status=StageStatusType.FAILED,
86-
details=f"Error when finding existing concepts",
86+
details=f"Error when finding existing concepts: {str(e)}",
8787
)
8888
raise

app/airflow/dags/libs/auto_mapping/core_rules_creation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ def create_mapping_rules(**kwargs) -> None:
101101
scan_report_table=table_id,
102102
stage=JobStageType.GENERATE_RULES,
103103
status=StageStatusType.FAILED,
104-
details=f"Error when creating mapping rules for existing concepts",
104+
details=f"Error when creating mapping rules for existing concepts: {str(e)}",
105105
)
106106
raise AirflowException(f"Database error in create_mapping_rules: {str(e)}")

app/airflow/dags/libs/auto_mapping/find_R_concepts_to_reuse.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def find_matching_value(**kwargs):
164164
scan_report_table=table_id,
165165
stage=JobStageType.REUSE_CONCEPTS,
166166
status=StageStatusType.FAILED,
167-
details=f"Error when finding eligible concepts for reuse at the value level",
167+
details=f"Error when finding eligible concepts for reuse at the value level: {str(e)}",
168168
)
169169
raise
170170
else:
@@ -223,7 +223,7 @@ def find_matching_field(**kwargs):
223223
scan_report_table=table_id,
224224
stage=JobStageType.REUSE_CONCEPTS,
225225
status=StageStatusType.FAILED,
226-
details=f"Error when finding eligible concepts for reuse at the field level",
226+
details=f"Error when finding eligible concepts for reuse at the field level: {str(e)}",
227227
)
228228
raise
229229
else:
@@ -253,7 +253,14 @@ def create_reusing_concepts(**kwargs):
253253
logging.info(f"Successfully found object ids for reusing concepts")
254254
except Exception as e:
255255
logging.error(f"Failed to find object ids for reusing concepts: {str(e)}")
256-
raise
256+
update_job_status(
257+
scan_report=scan_report_id,
258+
scan_report_table=table_id,
259+
stage=JobStageType.REUSE_CONCEPTS,
260+
status=StageStatusType.FAILED,
261+
details=f"Error when finding object ids for reusing concepts: {str(e)}",
262+
)
263+
raise e
257264
# Create R concepts
258265
try:
259266
pg_hook.run(create_concept_query, parameters={"table_id": table_id})
@@ -272,9 +279,9 @@ def create_reusing_concepts(**kwargs):
272279
scan_report_table=table_id,
273280
stage=JobStageType.REUSE_CONCEPTS,
274281
status=StageStatusType.FAILED,
275-
details=f"Error when creating R (Reused) concepts",
282+
details=f"Error when creating R (Reused) concepts: {str(e)}",
276283
)
277-
raise
284+
raise e
278285
else:
279286
update_job_status(
280287
scan_report=scan_report_id,

app/airflow/dags/libs/auto_mapping/find_standard_V_concepts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def find_standard_concepts(**kwargs) -> None:
8989
scan_report_table=table_id,
9090
stage=JobStageType.BUILD_CONCEPTS_FROM_DICT,
9191
status=StageStatusType.FAILED,
92-
details=f"Error in creating temp_standard_concepts_{table_id} table",
92+
details=f"Error in creating temp_standard_concepts_{table_id} table: {str(e)}",
9393
)
9494
raise
9595

@@ -156,7 +156,7 @@ def find_standard_concepts(**kwargs) -> None:
156156
scan_report_table=table_id,
157157
stage=JobStageType.BUILD_CONCEPTS_FROM_DICT,
158158
status=StageStatusType.FAILED,
159-
details=f"Error in finding standard concepts for field ID {sr_field_id}",
159+
details=f"Error in finding standard concepts for field ID {sr_field_id}: {str(e)}",
160160
)
161161
raise
162162

@@ -229,5 +229,5 @@ def create_standard_concepts(**kwargs) -> None:
229229
scan_report_table=table_id,
230230
stage=JobStageType.BUILD_CONCEPTS_FROM_DICT,
231231
status=StageStatusType.FAILED,
232-
details=f"Error when creating standard concepts",
232+
details=f"Error when creating standard concepts: {str(e)}",
233233
)

app/airflow/dags/libs/rules_export/core.py

Lines changed: 79 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def pre_process_rules(**kwargs) -> None:
4444
scan_report=scan_report_id,
4545
stage=JobStageType.DOWNLOAD_RULES,
4646
status=StageStatusType.FAILED,
47-
details=f"Error creating or updating the temp table for rules export for scan report {scan_report_id}",
47+
details=f"Error creating or updating the temp table: {str(e)}",
4848
)
4949
raise e
5050

@@ -60,85 +60,95 @@ def build_and_upload_rules_file(**kwargs) -> None:
6060
scan_report_name = validated_params["scan_report_name"]
6161
file_type = validated_params["file_type"]
6262

63-
# Setup file config. (Credit: @AndyRae)
64-
file_handlers: Dict[str, FileHandlerConfig] = {
65-
"csv": FileHandlerConfig(
66-
lambda: build_rules_csv(scan_report_id),
67-
"mapping_csv",
68-
"csv",
69-
),
70-
"json": FileHandlerConfig(
71-
lambda: build_rules_json(scan_report_name, scan_report_id),
72-
"mapping_json",
73-
"json",
74-
),
75-
}
76-
config = file_handlers[file_type]
77-
78-
# Generate it
79-
file = config.handler()
80-
file_type_value = config.file_type_value
81-
file_extension = config.file_extension
63+
try:
64+
# Setup file config. (Credit: @AndyRae)
65+
file_handlers: Dict[str, FileHandlerConfig] = {
66+
"csv": FileHandlerConfig(
67+
lambda: build_rules_csv(scan_report_id),
68+
"mapping_csv",
69+
"csv",
70+
),
71+
"json": FileHandlerConfig(
72+
lambda: build_rules_json(scan_report_name, scan_report_id),
73+
"mapping_json",
74+
"json",
75+
),
76+
}
77+
config = file_handlers[file_type]
8278

83-
# build file name
84-
filename = f"Rules - {scan_report_name} - {scan_report_id} - {datetime.now()}.{file_extension}"
79+
# Generate it
80+
file = config.handler()
81+
file_type_value = config.file_type_value
82+
file_extension = config.file_extension
8583

86-
# Upload to blob storage
87-
upload_blob_to_storage(
88-
container_name="rules-exports",
89-
blob_name=filename,
90-
data=file,
91-
content_type=file_type,
92-
)
84+
# build file name
85+
filename = f"Rules - {scan_report_name} - {scan_report_id} - {datetime.now()}.{file_extension}"
9386

94-
# Get file_type_id from files_filetype table
95-
file_type_id_query = """
96-
SELECT id FROM files_filetype WHERE value = %(file_type_value)s
97-
"""
98-
file_type_id_result = pg_hook.get_first(
99-
file_type_id_query, parameters={"file_type_value": file_type_value}
100-
)
101-
if not file_type_id_result:
102-
raise ValueError(f"No file type found for value: {file_type_value}")
103-
file_type_id = file_type_id_result[0]
104-
105-
try:
106-
# Create file entry in the database
107-
pg_hook.run(
108-
create_file_entry_query,
109-
parameters={
110-
"name": filename,
111-
"file_url": filename,
112-
"user_id": user_id,
113-
"file_type_id": file_type_id,
114-
"scan_report_id": scan_report_id,
115-
"created_at": datetime.now(),
116-
"updated_at": datetime.now(),
117-
},
118-
)
119-
update_job_status(
120-
scan_report=scan_report_id,
121-
stage=JobStageType.DOWNLOAD_RULES,
122-
status=StageStatusType.COMPLETE,
123-
details="Rules file successfully created and uploaded to blob storage",
87+
# Upload to blob storage
88+
upload_blob_to_storage(
89+
container_name="rules-exports",
90+
blob_name=filename,
91+
data=file,
92+
content_type=file_type,
12493
)
12594

126-
if AIRFLOW_DEBUG_MODE == "true":
127-
return
128-
# Clean up
129-
pg_hook.run(
130-
"DROP TABLE IF EXISTS temp_rules_export_%(scan_report_id)s_%(file_type)s"
131-
% {
132-
"scan_report_id": scan_report_id,
133-
"file_type": file_type,
134-
}
95+
# Get file_type_id from files_filetype table
96+
file_type_id_query = """
97+
SELECT id FROM files_filetype WHERE value = %(file_type_value)s
98+
"""
99+
file_type_id_result = pg_hook.get_first(
100+
file_type_id_query, parameters={"file_type_value": file_type_value}
135101
)
102+
if not file_type_id_result:
103+
raise ValueError(f"No file type found for value: {file_type_value}")
104+
file_type_id = file_type_id_result[0]
105+
106+
try:
107+
# Create file entry in the database
108+
pg_hook.run(
109+
create_file_entry_query,
110+
parameters={
111+
"name": filename,
112+
"file_url": filename,
113+
"user_id": user_id,
114+
"file_type_id": file_type_id,
115+
"scan_report_id": scan_report_id,
116+
"created_at": datetime.now(),
117+
"updated_at": datetime.now(),
118+
},
119+
)
120+
update_job_status(
121+
scan_report=scan_report_id,
122+
stage=JobStageType.DOWNLOAD_RULES,
123+
status=StageStatusType.COMPLETE,
124+
details="Rules file successfully created and uploaded to blob storage",
125+
)
126+
127+
if AIRFLOW_DEBUG_MODE == "true":
128+
return
129+
# Clean up
130+
pg_hook.run(
131+
"DROP TABLE IF EXISTS temp_rules_export_%(scan_report_id)s_%(file_type)s"
132+
% {
133+
"scan_report_id": scan_report_id,
134+
"file_type": file_type,
135+
}
136+
)
137+
except Exception as e:
138+
logging.error(f"Error creating file entry: {str(e)}")
139+
update_job_status(
140+
scan_report=scan_report_id,
141+
stage=JobStageType.DOWNLOAD_RULES,
142+
status=StageStatusType.FAILED,
143+
details=f"Export rules file failed: {str(e)}",
144+
)
145+
raise e
136146
except Exception as e:
137147
logging.error(f"Error creating file entry: {str(e)}")
138148
update_job_status(
139149
scan_report=scan_report_id,
140150
stage=JobStageType.DOWNLOAD_RULES,
141151
status=StageStatusType.FAILED,
142-
details=f"Error creating file entry in the database for scan report {scan_report_id}",
152+
details=f"Export rules file failed: {str(e)}",
143153
)
144154
raise e

app/airflow/dags/libs/rules_export/file_services.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def build_rules_json(scan_report_name: str, scan_report_id: int) -> BytesIO:
7878
scan_report=scan_report_id,
7979
stage=JobStageType.DOWNLOAD_RULES,
8080
status=StageStatusType.FAILED,
81-
details=f"Error building rules JSON for scan report {scan_report_id}",
81+
details=f"Error building rules JSON for scan report {scan_report_id}: {str(e)}",
8282
)
8383
raise e
8484

@@ -207,6 +207,6 @@ def build_rules_csv(scan_report_id: int) -> BytesIO:
207207
scan_report=scan_report_id,
208208
stage=JobStageType.DOWNLOAD_RULES,
209209
status=StageStatusType.FAILED,
210-
details=f"Error building rules CSV for scan report {scan_report_id}",
210+
details=f"Error building rules CSV for scan report {scan_report_id}: {str(e)}",
211211
)
212212
raise e

app/airflow/dags/libs/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def update_job_status(
9292
SET upload_status_id = (
9393
SELECT id FROM mapping_uploadstatus
9494
WHERE value = %(status_value)s
95-
)
95+
),
96+
upload_status_details = %(details)s
9697
WHERE id = %(scan_report)s
9798
"""
9899

app/api/api/serializers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class Meta:
109109
"data_partner",
110110
"mapping_status",
111111
"upload_status",
112+
"upload_status_details",
112113
"created_at",
113114
"hidden",
114115
"author",

0 commit comments

Comments
 (0)