@@ -167,9 +167,6 @@ def generate_report_entities(
167167 feature .validations .append (validation_report_entity )
168168 entities .append (feature )
169169
170- # Process notices and compute counters
171- counters = process_validation_report_notices (json_report ["notices" ])
172-
173170 for notice in json_report ["notices" ]:
174171 notice_entity = Notice (
175172 dataset_id = dataset .id ,
@@ -181,14 +178,26 @@ def generate_report_entities(
181178 dataset .notices .append (notice_entity )
182179 entities .append (notice_entity )
183180
181+ # Process notices and compute counters
182+ populate_counters (dataset .notices , validation_report_entity )
183+ return entities
184+
185+
186+ def populate_counters (notices , validation_report_entity ):
187+ """
188+ Populates the validation report entity with counters based on the notices.
189+ :param notices: Notices
190+ :param validation_report_entity: validation report entity
191+ """
192+ counters = process_validation_report_notices (notices )
193+
184194 # Update the validation report entity with computed counters
185195 validation_report_entity .total_info = counters ["total_info" ]
186196 validation_report_entity .total_warning = counters ["total_warning" ]
187197 validation_report_entity .total_error = counters ["total_error" ]
188198 validation_report_entity .unique_info_count = counters ["unique_info_count" ]
189199 validation_report_entity .unique_warning_count = counters ["unique_warning_count" ]
190200 validation_report_entity .unique_error_count = counters ["unique_error_count" ]
191- return entities
192201
193202
194203def populate_service_date (dataset , json_report , timezone = None ):
@@ -327,9 +336,25 @@ def compute_validation_report_counters(request, db_session: Session):
327336 """
328337 batch_size = 100 # Number of reports to process in each batch
329338 offset = 0
339+ notice_exists = (
340+ db_session .query (Notice )
341+ .filter (Notice .validation_report_id == Validationreport .id )
342+ .exists ()
343+ )
344+
330345 while True :
331346 validation_reports = (
332- db_session .query (Validationreport ).limit (batch_size ).offset (offset ).all ()
347+ db_session .query (Validationreport )
348+ .filter (
349+ (Validationreport .unique_info_count == 0 )
350+ & (Validationreport .unique_warning_count == 0 )
351+ & (Validationreport .unique_error_count == 0 )
352+ & notice_exists
353+ )
354+ .order_by (Validationreport .validated_at .desc ())
355+ .limit (batch_size )
356+ .offset (offset )
357+ .all ()
333358 )
334359 print (
335360 f"Processing { len (validation_reports )} validation reports from offset { offset } ."
@@ -339,16 +364,7 @@ def compute_validation_report_counters(request, db_session: Session):
339364 break
340365
341366 for report in validation_reports :
342- counters = process_validation_report_notices (report .notices )
343-
344- # Update the report with computed counters
345- report .total_info = counters ["total_info" ]
346- report .total_warning = counters ["total_warning" ]
347- report .total_error = counters ["total_error" ]
348- report .unique_info_count = counters ["unique_info_count" ]
349- report .unique_warning_count = counters ["unique_warning_count" ]
350- report .unique_error_count = counters ["unique_error_count" ]
351-
367+ populate_counters (report .notices , report )
352368 logging .info (
353369 f"Updated ValidationReport { report .id } with counters: "
354370 f"INFO={ report .total_info } , WARNING={ report .total_warning } , ERROR={ report .total_error } , "
@@ -359,8 +375,9 @@ def compute_validation_report_counters(request, db_session: Session):
359375 # Commit the changes for the current batch
360376 db_session .commit ()
361377
362- # Move to the next batch
363- offset += batch_size
378+ # Last page
379+ if len (validation_reports ) < batch_size :
380+ break
364381
365382 return {"message" : "Validation report counters computed successfully." }, 200
366383
0 commit comments