@@ -100,8 +100,6 @@ def task_validate_log_files(self, collections=[], from_date=None, until_date=Non
100100 username (str, optional): The username of the user initiating the task. Defaults to None.
101101 ignore_date (bool, optional): If True, ignore the date of the log file. Defaults to False.
102102 """
103- user = _get_user (self .request , username = username , user_id = user_id )
104-
105103 logging .info (f'Validating log files for collections: { collections } .' )
106104
107105 visible_dates = _get_visible_dates (from_date , until_date , days_to_go_back )
@@ -113,34 +111,53 @@ def task_validate_log_files(self, collections=[], from_date=None, until_date=Non
113111 for log_file in models .LogFile .objects .filter (status = choices .LOG_FILE_STATUS_CREATED , collection__acron3 = col ):
114112 file_ctime = date_utils .get_date_obj_from_timestamp (log_file .stat_result [LOGFILE_STAT_RESULT_CTIME_INDEX ])
115113 if file_ctime in visible_dates or ignore_date :
116- logging . info ( f'Validating log file { log_file .path } for collection { log_file . collection . acron3 } .' )
114+ task_validate_log_file . apply_async ( args = ( log_file .hash , user_id , username ) )
117115
118- buffer_size , sample_size = _fetch_validation_parameters (col )
119-
120- val_result = utils .validate_file (path = log_file .path , buffer_size = buffer_size , sample_size = sample_size )
121- if 'datetimes' in val_result .get ('content' , {}).get ('summary' , {}):
122- del val_result ['content' ]['summary' ]['datetimes' ]
123116
124- try :
125- log_file .validation ['result' ] = json .dumps (val_result , cls = DjangoJSONEncoder ) if val_result else {}
126- log_file .validation ['parameters' ] = {'buffer_size' : buffer_size , 'sample_size' : sample_size }
127- except json .JSONDecodeError as e :
128- logging .error (f'Error serializing validation result: { e } ' )
129- log_file .validation = {}
117+ @celery_app .task (bind = True , name = _ ('Validate log file' ), timelimit = - 1 )
118+ def task_validate_log_file (self , log_file_hash , user_id = None , username = None ):
119+ """
120+ Task to validate a specific log file.
130121
131- if val_result .get ('is_valid' , {}).get ('all' , False ):
132- models .LogFileDate .create_or_update (
133- user = user ,
134- log_file = log_file ,
135- date = val_result .get ('probably_date' , '' ),
136- )
137- log_file .status = choices .LOG_FILE_STATUS_QUEUED
122+ Parameters:
123+ log_file_id (int): The ID of the log file to validate.
124+ user_id (int, optional): The ID of the user initiating the task. Defaults to None.
125+ username (str, optional): The username of the user initiating the task. Defaults to None.
126+ """
127+ user = _get_user (self .request , username = username , user_id = user_id )
128+ log_file = models .LogFile .objects .get (hash = log_file_hash )
129+ collection = log_file .collection .acron3
138130
139- else :
140- log_file .status = choices .LOG_FILE_STATUS_INVALIDATED
131+ buffer_size , sample_size = _fetch_validation_parameters (collection )
141132
142- logging .info (f'Log file { log_file .path } ({ log_file .collection .acron3 } ) has status { log_file .status } .' )
143- log_file .save ()
133+ logging .info (f'Validating log file { log_file .path } .' )
134+ val_result = utils .validate_file (path = log_file .path , buffer_size = buffer_size , sample_size = sample_size )
135+ if 'datetimes' in val_result .get ('content' , {}).get ('summary' , {}):
136+ del val_result ['content' ]['summary' ]['datetimes' ]
137+
138+ if 'probably_date' in val_result :
139+ val_result ['probably_date' ] = date_utils .get_date_str (val_result ['probably_date' ])
140+
141+ try :
142+ log_file .validation = val_result
143+ log_file .validation .update ({'buffer_size' : buffer_size , 'sample_size' : sample_size })
144+ except json .JSONDecodeError as e :
145+ logging .error (f'Error serializing validation result: { e } ' )
146+ log_file .validation = {}
147+
148+ if val_result .get ('is_valid' , {}).get ('all' , False ):
149+ models .LogFileDate .create_or_update (
150+ user = user ,
151+ log_file = log_file ,
152+ date = val_result .get ('probably_date' , '' ),
153+ )
154+ log_file .status = choices .LOG_FILE_STATUS_QUEUED
155+
156+ else :
157+ log_file .status = choices .LOG_FILE_STATUS_INVALIDATED
158+
159+ logging .info (f'Log file { log_file .path } ({ log_file .collection .acron3 } ) has status { log_file .status } .' )
160+ log_file .save ()
144161
145162
146163def _fetch_validation_parameters (collection , default_buffer_size = 0.1 , default_sample_size = 2048 ):
@@ -199,8 +216,7 @@ def _check_missing_logs_for_date(user, collection, date):
199216@celery_app .task (bind = True , name = _ ('Generate log files count report' ))
200217def task_log_files_count_status_report (self , collections = [], from_date = None , until_date = None , user_id = None , username = None ):
201218 from_date , until_date = date_utils .get_date_range_str (from_date , until_date )
202- possible_dates_n = len (date_utils .get_date_objs_from_date_range (from_date , until_date ))
203-
219+
204220 from_date_obj = date_utils .get_date_obj (from_date )
205221 until_date_obj = date_utils .get_date_obj (until_date )
206222
0 commit comments