""" Batch runner for all analysis scripts Runs all analyses in sequence and generates a summary report To use: 1. Add your analysis scripts to the ANALYSIS_SCRIPTS list below 2. Run: python run_all_analyses.py """ import subprocess import sys from pathlib import Path from datetime import datetime import time # ============================================================================ # CONFIGURATION # ============================================================================ # List of analysis scripts to run # TODO: Add your analysis scripts here ANALYSIS_SCRIPTS = [ # Example structure - customize for your analyses: # 'check_annual_revenue.py', # 'revenue_analysis.py', # 'geographic_analysis.py', # 'customer_segmentation.py', # 'product_analysis.py', # Add your analysis scripts here... ] # Timeout per script (in seconds) SCRIPT_TIMEOUT = 600 # 10 minutes # ============================================================================ # HELPER FUNCTIONS # ============================================================================ def run_script(script_path): """Run a single analysis script""" script_name = Path(script_path).name print(f"\n{'='*60}") print(f"Running: {script_name}") print(f"{'='*60}") start_time = time.time() try: result = subprocess.run( [sys.executable, script_path], capture_output=True, text=True, timeout=SCRIPT_TIMEOUT ) elapsed = time.time() - start_time if result.returncode == 0: print(f"✅ {script_name} completed successfully ({elapsed:.1f}s)") if result.stdout: # Print last 10 lines of output lines = result.stdout.strip().split('\n') if len(lines) > 10: print(" ... (output truncated)") for line in lines[-10:]: print(f" {line}") else: for line in lines: print(f" {line}") return True, elapsed, None else: print(f"❌ {script_name} failed ({elapsed:.1f}s)") if result.stderr: print(f" Error: {result.stderr[:500]}") return False, elapsed, result.stderr except subprocess.TimeoutExpired: elapsed = time.time() - start_time print(f"⏱️ {script_name} timed out after {elapsed:.1f}s") return False, elapsed, "Timeout" except Exception as e: elapsed = time.time() - start_time print(f"❌ {script_name} error: {str(e)}") return False, elapsed, str(e) # ============================================================================ # MAIN FUNCTION # ============================================================================ def main(): """Run all analysis scripts""" from config import COMPANY_NAME print(f"\n{'='*60}") print(f"{COMPANY_NAME} Sales Analysis - Batch Runner") print(f"Started: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") print(f"{'='*60}\n") # Check which scripts exist existing_scripts = [] missing_scripts = [] for script in ANALYSIS_SCRIPTS: script_path = Path(script) if script_path.exists(): existing_scripts.append(script) else: missing_scripts.append(script) if missing_scripts: print(f"⚠️ Warning: {len(missing_scripts)} scripts not found:") for script in missing_scripts: print(f" - {script}") print() if not existing_scripts: print("❌ No analysis scripts found!") print(" Please add analysis scripts to ANALYSIS_SCRIPTS list in run_all_analyses.py") return print(f"Found {len(existing_scripts)} analysis scripts to run\n") # Run scripts results = [] total_start = time.time() for script in existing_scripts: success, elapsed, error = run_script(script) results.append({ 'script': script, 'success': success, 'elapsed': elapsed, 'error': error }) total_elapsed = time.time() - total_start # Print summary print(f"\n{'='*60}") print("Batch Run Summary") print(f"{'='*60}\n") successful = [r for r in results if r['success']] failed = [r for r in results if not r['success']] print(f"Total scripts: {len(results)}") print(f"✅ Successful: {len(successful)}") print(f"❌ Failed: {len(failed)}") print(f"⏱️ Total time: {total_elapsed/60:.1f} minutes\n") if failed: print("Failed scripts:") for r in failed: print(f" ❌ {r['script']} ({r['elapsed']:.1f}s)") if r['error']: print(f" Error: {r['error'][:100]}") print() # Save summary to file summary_file = Path('analysis_run_summary.txt') with open(summary_file, 'w') as f: f.write(f"{COMPANY_NAME} Sales Analysis - Batch Run Summary\n") f.write(f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n") f.write(f"{'='*60}\n\n") f.write(f"Total scripts: {len(results)}\n") f.write(f"Successful: {len(successful)}\n") f.write(f"Failed: {len(failed)}\n") f.write(f"Total time: {total_elapsed/60:.1f} minutes\n\n") if successful: f.write("Successful scripts:\n") for r in successful: f.write(f" ✅ {r['script']} ({r['elapsed']:.1f}s)\n") f.write("\n") if failed: f.write("Failed scripts:\n") for r in failed: f.write(f" ❌ {r['script']} ({r['elapsed']:.1f}s)\n") if r['error']: f.write(f" Error: {r['error']}\n") print(f"Summary saved to: {summary_file}") if __name__ == "__main__": main()