Update app.py
Browse files
app.py
CHANGED
|
@@ -32,12 +32,14 @@ class TaskManager:
|
|
| 32 |
'total': 470,
|
| 33 |
'results_file': f'results_{task_id}.txt',
|
| 34 |
'start_time': datetime.now(),
|
| 35 |
-
'errors': []
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
def update_progress(self, task_id, progress):
|
| 39 |
if task_id in self.tasks:
|
| 40 |
self.tasks[task_id]['progress'] = progress
|
|
|
|
| 41 |
|
| 42 |
def add_error(self, task_id, error):
|
| 43 |
if task_id in self.tasks:
|
|
@@ -46,6 +48,7 @@ class TaskManager:
|
|
| 46 |
def complete_task(self, task_id):
|
| 47 |
if task_id in self.tasks:
|
| 48 |
self.tasks[task_id]['status'] = 'completed'
|
|
|
|
| 49 |
|
| 50 |
def get_task(self, task_id):
|
| 51 |
return self.tasks.get(task_id)
|
|
@@ -85,7 +88,7 @@ def generate_synthetic_data(file_path, task_id):
|
|
| 85 |
f.write(f"--- Requête {i+1}/470 ---\n")
|
| 86 |
f.write(response.text)
|
| 87 |
f.write("\n" + "="*60 + "\n\n")
|
| 88 |
-
f.flush() # Forcer l'écriture
|
| 89 |
|
| 90 |
# Mettre à jour le progrès
|
| 91 |
task_manager.update_progress(task_id, i + 1)
|
|
@@ -100,6 +103,7 @@ def generate_synthetic_data(file_path, task_id):
|
|
| 100 |
task_manager.add_error(task_id, error_msg)
|
| 101 |
f.write(f"ERREUR - Requête {i+1}: {str(e)}\n")
|
| 102 |
f.write("="*60 + "\n\n")
|
|
|
|
| 103 |
print(error_msg)
|
| 104 |
|
| 105 |
task_manager.complete_task(task_id)
|
|
@@ -112,7 +116,9 @@ def generate_synthetic_data(file_path, task_id):
|
|
| 112 |
|
| 113 |
@app.route('/')
|
| 114 |
def index():
|
| 115 |
-
|
|
|
|
|
|
|
| 116 |
|
| 117 |
@app.route('/upload', methods=['POST'])
|
| 118 |
def upload_file():
|
|
@@ -160,7 +166,8 @@ def get_status(task_id):
|
|
| 160 |
'total': task['total'],
|
| 161 |
'percentage': round((task['progress'] / task['total']) * 100, 2),
|
| 162 |
'errors_count': len(task['errors']),
|
| 163 |
-
'start_time': task['start_time'].strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
| 164 |
})
|
| 165 |
|
| 166 |
@app.route('/download/<task_id>')
|
|
@@ -174,10 +181,40 @@ def download_results(task_id):
|
|
| 174 |
if not os.path.exists(results_file):
|
| 175 |
return jsonify({'error': 'Fichier de résultats non trouvé'}), 404
|
| 176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
return send_file(
|
| 178 |
results_file,
|
| 179 |
as_attachment=True,
|
| 180 |
-
download_name=
|
| 181 |
)
|
| 182 |
|
| 183 |
@app.route('/tasks')
|
|
@@ -192,10 +229,34 @@ def list_tasks():
|
|
| 192 |
'total': task_info['total'],
|
| 193 |
'percentage': round((task_info['progress'] / task_info['total']) * 100, 2),
|
| 194 |
'start_time': task_info['start_time'].strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
| 195 |
'errors_count': len(task_info['errors'])
|
| 196 |
})
|
| 197 |
|
|
|
|
|
|
|
|
|
|
| 198 |
return jsonify(task_list)
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
| 201 |
app.run(debug=True, threaded=True)
|
|
|
|
| 32 |
'total': 470,
|
| 33 |
'results_file': f'results_{task_id}.txt',
|
| 34 |
'start_time': datetime.now(),
|
| 35 |
+
'errors': [],
|
| 36 |
+
'last_update': datetime.now()
|
| 37 |
}
|
| 38 |
|
| 39 |
def update_progress(self, task_id, progress):
|
| 40 |
if task_id in self.tasks:
|
| 41 |
self.tasks[task_id]['progress'] = progress
|
| 42 |
+
self.tasks[task_id]['last_update'] = datetime.now()
|
| 43 |
|
| 44 |
def add_error(self, task_id, error):
|
| 45 |
if task_id in self.tasks:
|
|
|
|
| 48 |
def complete_task(self, task_id):
|
| 49 |
if task_id in self.tasks:
|
| 50 |
self.tasks[task_id]['status'] = 'completed'
|
| 51 |
+
self.tasks[task_id]['last_update'] = datetime.now()
|
| 52 |
|
| 53 |
def get_task(self, task_id):
|
| 54 |
return self.tasks.get(task_id)
|
|
|
|
| 88 |
f.write(f"--- Requête {i+1}/470 ---\n")
|
| 89 |
f.write(response.text)
|
| 90 |
f.write("\n" + "="*60 + "\n\n")
|
| 91 |
+
f.flush() # Forcer l'écriture immédiate
|
| 92 |
|
| 93 |
# Mettre à jour le progrès
|
| 94 |
task_manager.update_progress(task_id, i + 1)
|
|
|
|
| 103 |
task_manager.add_error(task_id, error_msg)
|
| 104 |
f.write(f"ERREUR - Requête {i+1}: {str(e)}\n")
|
| 105 |
f.write("="*60 + "\n\n")
|
| 106 |
+
f.flush() # Forcer l'écriture même en cas d'erreur
|
| 107 |
print(error_msg)
|
| 108 |
|
| 109 |
task_manager.complete_task(task_id)
|
|
|
|
| 116 |
|
| 117 |
@app.route('/')
|
| 118 |
def index():
|
| 119 |
+
# Servir directement le HTML depuis le même dossier
|
| 120 |
+
with open('index.html', 'r', encoding='utf-8') as f:
|
| 121 |
+
return f.read()
|
| 122 |
|
| 123 |
@app.route('/upload', methods=['POST'])
|
| 124 |
def upload_file():
|
|
|
|
| 166 |
'total': task['total'],
|
| 167 |
'percentage': round((task['progress'] / task['total']) * 100, 2),
|
| 168 |
'errors_count': len(task['errors']),
|
| 169 |
+
'start_time': task['start_time'].strftime('%Y-%m-%d %H:%M:%S'),
|
| 170 |
+
'last_update': task['last_update'].strftime('%Y-%m-%d %H:%M:%S')
|
| 171 |
})
|
| 172 |
|
| 173 |
@app.route('/download/<task_id>')
|
|
|
|
| 181 |
if not os.path.exists(results_file):
|
| 182 |
return jsonify({'error': 'Fichier de résultats non trouvé'}), 404
|
| 183 |
|
| 184 |
+
# Vérifier si c'est un téléchargement partiel
|
| 185 |
+
is_partial = request.args.get('partial', 'false').lower() == 'true'
|
| 186 |
+
|
| 187 |
+
if is_partial and task['status'] == 'running':
|
| 188 |
+
# Créer un fichier temporaire avec les données actuelles
|
| 189 |
+
temp_file = os.path.join(RESULTS_FOLDER, f'temp_results_{task_id}.txt')
|
| 190 |
+
|
| 191 |
+
# Copier le contenu actuel vers le fichier temporaire
|
| 192 |
+
try:
|
| 193 |
+
with open(results_file, 'r', encoding='utf-8') as original:
|
| 194 |
+
content = original.read()
|
| 195 |
+
|
| 196 |
+
with open(temp_file, 'w', encoding='utf-8') as temp:
|
| 197 |
+
temp.write(content)
|
| 198 |
+
temp.write(f"\n\n--- TÉLÉCHARGEMENT PARTIEL ---\n")
|
| 199 |
+
temp.write(f"Téléchargé le: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
|
| 200 |
+
temp.write(f"Progrès: {task['progress']}/{task['total']} ({round((task['progress'] / task['total']) * 100, 2)}%)\n")
|
| 201 |
+
temp.write("Génération en cours... Ce fichier contient les données générées jusqu'à présent.\n")
|
| 202 |
+
|
| 203 |
+
return send_file(
|
| 204 |
+
temp_file,
|
| 205 |
+
as_attachment=True,
|
| 206 |
+
download_name=f'donnees_synthetiques_partiel_{task_id}.txt'
|
| 207 |
+
)
|
| 208 |
+
except Exception as e:
|
| 209 |
+
return jsonify({'error': f'Erreur lors de la création du fichier partiel: {str(e)}'}), 500
|
| 210 |
+
|
| 211 |
+
# Téléchargement normal (complet)
|
| 212 |
+
download_name = f'donnees_synthetiques_{"complet" if task["status"] == "completed" else "actuel"}_{task_id}.txt'
|
| 213 |
+
|
| 214 |
return send_file(
|
| 215 |
results_file,
|
| 216 |
as_attachment=True,
|
| 217 |
+
download_name=download_name
|
| 218 |
)
|
| 219 |
|
| 220 |
@app.route('/tasks')
|
|
|
|
| 229 |
'total': task_info['total'],
|
| 230 |
'percentage': round((task_info['progress'] / task_info['total']) * 100, 2),
|
| 231 |
'start_time': task_info['start_time'].strftime('%Y-%m-%d %H:%M:%S'),
|
| 232 |
+
'last_update': task_info['last_update'].strftime('%Y-%m-%d %H:%M:%S'),
|
| 233 |
'errors_count': len(task_info['errors'])
|
| 234 |
})
|
| 235 |
|
| 236 |
+
# Trier par heure de début (plus récent en premier)
|
| 237 |
+
task_list.sort(key=lambda x: x['start_time'], reverse=True)
|
| 238 |
+
|
| 239 |
return jsonify(task_list)
|
| 240 |
|
| 241 |
+
@app.route('/cleanup')
|
| 242 |
+
def cleanup_temp_files():
|
| 243 |
+
"""Nettoyer les fichiers temporaires (optionnel)"""
|
| 244 |
+
try:
|
| 245 |
+
temp_files_deleted = 0
|
| 246 |
+
for filename in os.listdir(RESULTS_FOLDER):
|
| 247 |
+
if filename.startswith('temp_results_'):
|
| 248 |
+
file_path = os.path.join(RESULTS_FOLDER, filename)
|
| 249 |
+
os.remove(file_path)
|
| 250 |
+
temp_files_deleted += 1
|
| 251 |
+
|
| 252 |
+
return jsonify({
|
| 253 |
+
'message': f'{temp_files_deleted} fichiers temporaires supprimés'
|
| 254 |
+
})
|
| 255 |
+
except Exception as e:
|
| 256 |
+
return jsonify({'error': f'Erreur lors du nettoyage: {str(e)}'}), 500
|
| 257 |
+
|
| 258 |
if __name__ == '__main__':
|
| 259 |
+
print("🚀 Démarrage du serveur...")
|
| 260 |
+
print("📂 Dossiers créés:", UPLOAD_FOLDER, RESULTS_FOLDER)
|
| 261 |
+
print("🌐 Application disponible sur: http://localhost:5000")
|
| 262 |
app.run(debug=True, threaded=True)
|