File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -237,13 +237,25 @@ def migrate_dump_to_delta_and_handle_status_transistions(collection_id):
237
237
@celery_app .task (name = "generate_metrics" )
238
238
def generate_metrics (task_id ):
239
239
"""
240
- To asynchronously generate metrics
240
+ Asynchronously generate metrics and save to a downloadable file
241
241
"""
242
242
try :
243
243
# Generate a file path in the media directory
244
244
metrics_dir = os .path .join (settings .MEDIA_ROOT , 'metrics' )
245
245
os .makedirs (metrics_dir , exist_ok = True )
246
246
247
+ # Clean up old metrics files
248
+ for filename in os .listdir (metrics_dir ):
249
+ if filename .startswith ('metrics_' ) and (filename .endswith ('.csv' ) or filename .endswith ('.tmp' )):
250
+ # Skip the current task's files
251
+ if not filename .startswith (f'metrics_{ task_id } ' ):
252
+ file_path = os .path .join (metrics_dir , filename )
253
+ try :
254
+ os .remove (file_path )
255
+ print (f"Deleted old metrics file: { filename } " )
256
+ except Exception as e :
257
+ print (f"Failed to delete { filename } : { str (e )} " )
258
+
247
259
# Use a temporary file during generation
248
260
temp_file_path = os .path .join (metrics_dir , f'metrics_{ task_id } .tmp' )
249
261
final_file_path = os .path .join (metrics_dir , f'metrics_{ task_id } .csv' )
You can’t perform that action at this time.
0 commit comments