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 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/env python3
"""
FRED ML - Integration and Testing Script
Comprehensive integration of all updates and system testing
"""

import os
import sys
import subprocess
import logging
from pathlib import Path
from datetime import datetime
import json

# Setup logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

class FREDMLIntegration:
    """Comprehensive integration and testing for FRED ML system"""
    
    def __init__(self):
        self.root_dir = Path(__file__).parent.parent
        self.test_results = {}
        self.integration_status = {}
        
    def run_integration_checklist(self):
        """Run comprehensive integration checklist"""
        logger.info("๐Ÿš€ Starting FRED ML Integration and Testing")
        logger.info("=" * 60)
        
        # 1. Directory Structure Validation
        self.validate_directory_structure()
        
        # 2. Dependencies Check
        self.check_dependencies()
        
        # 3. Configuration Validation
        self.validate_configurations()
        
        # 4. Code Quality Checks
        self.run_code_quality_checks()
        
        # 5. Unit Tests
        self.run_unit_tests()
        
        # 6. Integration Tests
        self.run_integration_tests()
        
        # 7. Advanced Analytics Tests
        self.test_advanced_analytics()
        
        # 8. Streamlit UI Test
        self.test_streamlit_ui()
        
        # 9. Documentation Check
        self.validate_documentation()
        
        # 10. Final Integration Report
        self.generate_integration_report()
        
    def validate_directory_structure(self):
        """Validate and organize directory structure"""
        logger.info("๐Ÿ“ Validating directory structure...")
        
        required_dirs = [
            'src/analysis',
            'src/core',
            'src/visualization',
            'src/lambda',
            'scripts',
            'tests/unit',
            'tests/integration',
            'tests/e2e',
            'docs',
            'config',
            'data/exports',
            'data/processed',
            'frontend',
            'infrastructure',
            'deploy'
        ]
        
        for dir_path in required_dirs:
            full_path = self.root_dir / dir_path
            if not full_path.exists():
                full_path.mkdir(parents=True, exist_ok=True)
                logger.info(f"โœ… Created directory: {dir_path}")
            else:
                logger.info(f"โœ… Directory exists: {dir_path}")
        
        # Check for required files
        required_files = [
            'src/analysis/economic_forecasting.py',
            'src/analysis/economic_segmentation.py',
            'src/analysis/statistical_modeling.py',
            'src/analysis/comprehensive_analytics.py',
            'src/core/enhanced_fred_client.py',
            'frontend/app.py',
            'scripts/run_advanced_analytics.py',
            'scripts/comprehensive_demo.py',
            'config/pipeline.yaml',
            'requirements.txt',
            'README.md'
        ]
        
        missing_files = []
        for file_path in required_files:
            full_path = self.root_dir / file_path
            if not full_path.exists():
                missing_files.append(file_path)
            else:
                logger.info(f"โœ… File exists: {file_path}")
        
        if missing_files:
            logger.error(f"โŒ Missing files: {missing_files}")
            self.integration_status['directory_structure'] = False
        else:
            logger.info("โœ… Directory structure validation passed")
            self.integration_status['directory_structure'] = True
    
    def check_dependencies(self):
        """Check and validate dependencies"""
        logger.info("๐Ÿ“ฆ Checking dependencies...")
        
        try:
            # Check if requirements.txt exists and is valid
            requirements_file = self.root_dir / 'requirements.txt'
            if requirements_file.exists():
                with open(requirements_file, 'r') as f:
                    requirements = f.read()
                
                # Check for key dependencies
                key_deps = [
                    'fredapi',
                    'pandas',
                    'numpy',
                    'scikit-learn',
                    'scipy',
                    'statsmodels',
                    'streamlit',
                    'plotly',
                    'boto3'
                ]
                
                missing_deps = []
                for dep in key_deps:
                    if dep not in requirements:
                        missing_deps.append(dep)
                
                if missing_deps:
                    logger.warning(f"โš ๏ธ Missing dependencies: {missing_deps}")
                else:
                    logger.info("โœ… All key dependencies found in requirements.txt")
                
                self.integration_status['dependencies'] = True
            else:
                logger.error("โŒ requirements.txt not found")
                self.integration_status['dependencies'] = False
                
        except Exception as e:
            logger.error(f"โŒ Error checking dependencies: {e}")
            self.integration_status['dependencies'] = False
    
    def validate_configurations(self):
        """Validate configuration files"""
        logger.info("โš™๏ธ Validating configurations...")
        
        config_files = [
            'config/pipeline.yaml',
            'config/settings.py',
            '.github/workflows/scheduled.yml'
        ]
        
        config_status = True
        for config_file in config_files:
            full_path = self.root_dir / config_file
            if full_path.exists():
                logger.info(f"โœ… Configuration file exists: {config_file}")
            else:
                logger.error(f"โŒ Missing configuration file: {config_file}")
                config_status = False
        
        # Check cron job configuration
        pipeline_config = self.root_dir / 'config/pipeline.yaml'
        if pipeline_config.exists():
            with open(pipeline_config, 'r') as f:
                content = f.read()
                if 'schedule: "0 0 1 */3 *"' in content:
                    logger.info("โœ… Quarterly cron job configuration found")
                else:
                    logger.warning("โš ๏ธ Cron job configuration may not be quarterly")
        
        self.integration_status['configurations'] = config_status
    
    def run_code_quality_checks(self):
        """Run code quality checks"""
        logger.info("๐Ÿ” Running code quality checks...")
        
        try:
            # Check for Python syntax errors
            python_files = list(self.root_dir.rglob("*.py"))
            
            syntax_errors = []
            for py_file in python_files:
                try:
                    with open(py_file, 'r') as f:
                        compile(f.read(), str(py_file), 'exec')
                except SyntaxError as e:
                    syntax_errors.append(f"{py_file}: {e}")
            
            if syntax_errors:
                logger.error(f"โŒ Syntax errors found: {syntax_errors}")
                self.integration_status['code_quality'] = False
            else:
                logger.info("โœ… No syntax errors found")
                self.integration_status['code_quality'] = True
                
        except Exception as e:
            logger.error(f"โŒ Error in code quality checks: {e}")
            self.integration_status['code_quality'] = False
    
    def run_unit_tests(self):
        """Run unit tests"""
        logger.info("๐Ÿงช Running unit tests...")
        
        try:
            # Check if tests directory exists
            tests_dir = self.root_dir / 'tests'
            if not tests_dir.exists():
                logger.warning("โš ๏ธ Tests directory not found")
                self.integration_status['unit_tests'] = False
                return
            
            # Run pytest if available
            try:
                result = subprocess.run(
                    [sys.executable, '-m', 'pytest', 'tests/unit/', '-v'],
                    capture_output=True,
                    text=True,
                    cwd=self.root_dir
                )
                
                if result.returncode == 0:
                    logger.info("โœ… Unit tests passed")
                    self.integration_status['unit_tests'] = True
                else:
                    logger.error(f"โŒ Unit tests failed: {result.stderr}")
                    self.integration_status['unit_tests'] = False
                    
            except FileNotFoundError:
                logger.warning("โš ๏ธ pytest not available, skipping unit tests")
                self.integration_status['unit_tests'] = False
                
        except Exception as e:
            logger.error(f"โŒ Error running unit tests: {e}")
            self.integration_status['unit_tests'] = False
    
    def run_integration_tests(self):
        """Run integration tests"""
        logger.info("๐Ÿ”— Running integration tests...")
        
        try:
            # Test FRED API connection
            from config.settings import FRED_API_KEY
            if FRED_API_KEY:
                logger.info("โœ… FRED API key configured")
                self.integration_status['fred_api'] = True
            else:
                logger.warning("โš ๏ธ FRED API key not configured")
                self.integration_status['fred_api'] = False
            
            # Test AWS configuration
            try:
                import boto3
                logger.info("โœ… AWS SDK available")
                self.integration_status['aws_sdk'] = True
            except ImportError:
                logger.warning("โš ๏ธ AWS SDK not available")
                self.integration_status['aws_sdk'] = False
            
            # Test analytics modules
            try:
                sys.path.append(str(self.root_dir / 'src'))
                from src.analysis.comprehensive_analytics import ComprehensiveAnalytics
                from src.core.enhanced_fred_client import EnhancedFREDClient
                logger.info("โœ… Analytics modules available")
                self.integration_status['analytics_modules'] = True
            except ImportError as e:
                logger.error(f"โŒ Analytics modules not available: {e}")
                self.integration_status['analytics_modules'] = False
                
        except Exception as e:
            logger.error(f"โŒ Error in integration tests: {e}")
            self.integration_status['integration_tests'] = False
    
    def test_advanced_analytics(self):
        """Test advanced analytics functionality"""
        logger.info("๐Ÿ”ฎ Testing advanced analytics...")
        
        try:
            # Test analytics modules import
            sys.path.append(str(self.root_dir / 'src'))
            
            # Test Enhanced FRED Client
            try:
                from src.core.enhanced_fred_client import EnhancedFREDClient
                logger.info("โœ… Enhanced FRED Client available")
                self.integration_status['enhanced_fred_client'] = True
            except ImportError as e:
                logger.error(f"โŒ Enhanced FRED Client not available: {e}")
                self.integration_status['enhanced_fred_client'] = False
            
            # Test Economic Forecasting
            try:
                from src.analysis.economic_forecasting import EconomicForecaster
                logger.info("โœ… Economic Forecasting available")
                self.integration_status['economic_forecasting'] = True
            except ImportError as e:
                logger.error(f"โŒ Economic Forecasting not available: {e}")
                self.integration_status['economic_forecasting'] = False
            
            # Test Economic Segmentation
            try:
                from src.analysis.economic_segmentation import EconomicSegmentation
                logger.info("โœ… Economic Segmentation available")
                self.integration_status['economic_segmentation'] = True
            except ImportError as e:
                logger.error(f"โŒ Economic Segmentation not available: {e}")
                self.integration_status['economic_segmentation'] = False
            
            # Test Statistical Modeling
            try:
                from src.analysis.statistical_modeling import StatisticalModeling
                logger.info("โœ… Statistical Modeling available")
                self.integration_status['statistical_modeling'] = True
            except ImportError as e:
                logger.error(f"โŒ Statistical Modeling not available: {e}")
                self.integration_status['statistical_modeling'] = False
            
            # Test Comprehensive Analytics
            try:
                from src.analysis.comprehensive_analytics import ComprehensiveAnalytics
                logger.info("โœ… Comprehensive Analytics available")
                self.integration_status['comprehensive_analytics'] = True
            except ImportError as e:
                logger.error(f"โŒ Comprehensive Analytics not available: {e}")
                self.integration_status['comprehensive_analytics'] = False
                
        except Exception as e:
            logger.error(f"โŒ Error testing advanced analytics: {e}")
    
    def test_streamlit_ui(self):
        """Test Streamlit UI"""
        logger.info("๐ŸŽจ Testing Streamlit UI...")
        
        try:
            # Check if Streamlit app exists
            streamlit_app = self.root_dir / 'frontend/app.py'
            if streamlit_app.exists():
                logger.info("โœ… Streamlit app exists")
                
                # Check for required imports
                with open(streamlit_app, 'r') as f:
                    content = f.read()
                    
                required_imports = [
                    'streamlit',
                    'plotly',
                    'pandas',
                    'boto3'
                ]
                
                missing_imports = []
                for imp in required_imports:
                    if imp not in content:
                        missing_imports.append(imp)
                
                if missing_imports:
                    logger.warning(f"โš ๏ธ Missing imports in Streamlit app: {missing_imports}")
                else:
                    logger.info("โœ… All required imports found in Streamlit app")
                
                self.integration_status['streamlit_ui'] = True
            else:
                logger.error("โŒ Streamlit app not found")
                self.integration_status['streamlit_ui'] = False
                
        except Exception as e:
            logger.error(f"โŒ Error testing Streamlit UI: {e}")
            self.integration_status['streamlit_ui'] = False
    
    def validate_documentation(self):
        """Validate documentation"""
        logger.info("๐Ÿ“š Validating documentation...")
        
        doc_files = [
            'README.md',
            'docs/ADVANCED_ANALYTICS_SUMMARY.md',
            'docs/CONVERSATION_SUMMARY.md'
        ]
        
        doc_status = True
        for doc_file in doc_files:
            full_path = self.root_dir / doc_file
            if full_path.exists():
                logger.info(f"โœ… Documentation exists: {doc_file}")
            else:
                logger.warning(f"โš ๏ธ Missing documentation: {doc_file}")
                doc_status = False
        
        self.integration_status['documentation'] = doc_status
    
    def generate_integration_report(self):
        """Generate comprehensive integration report"""
        logger.info("๐Ÿ“Š Generating integration report...")
        
        # Calculate overall status
        total_checks = len(self.integration_status)
        passed_checks = sum(1 for status in self.integration_status.values() if status)
        overall_status = "โœ… PASSED" if passed_checks == total_checks else "โŒ FAILED"
        
        # Generate report
        report = {
            "timestamp": datetime.now().isoformat(),
            "overall_status": overall_status,
            "summary": {
                "total_checks": total_checks,
                "passed_checks": passed_checks,
                "failed_checks": total_checks - passed_checks,
                "success_rate": f"{(passed_checks/total_checks)*100:.1f}%"
            },
            "detailed_results": self.integration_status
        }
        
        # Save report
        report_file = self.root_dir / 'integration_report.json'
        with open(report_file, 'w') as f:
            json.dump(report, f, indent=2)
        
        # Print summary
        logger.info("=" * 60)
        logger.info("๐Ÿ“Š INTEGRATION REPORT")
        logger.info("=" * 60)
        logger.info(f"Overall Status: {overall_status}")
        logger.info(f"Total Checks: {total_checks}")
        logger.info(f"Passed: {passed_checks}")
        logger.info(f"Failed: {total_checks - passed_checks}")
        logger.info(f"Success Rate: {(passed_checks/total_checks)*100:.1f}%")
        logger.info("=" * 60)
        
        # Print detailed results
        logger.info("Detailed Results:")
        for check, status in self.integration_status.items():
            status_icon = "โœ…" if status else "โŒ"
            logger.info(f"  {status_icon} {check}")
        
        logger.info("=" * 60)
        logger.info(f"Report saved to: {report_file}")
        
        return report
    
    def prepare_for_github(self):
        """Prepare for GitHub submission"""
        logger.info("๐Ÿš€ Preparing for GitHub submission...")
        
        # Check git status
        try:
            result = subprocess.run(
                ['git', 'status', '--porcelain'],
                capture_output=True,
                text=True,
                cwd=self.root_dir
            )
            
            if result.stdout.strip():
                logger.info("๐Ÿ“ Changes detected:")
                logger.info(result.stdout)
                
                # Suggest git commands
                logger.info("\n๐Ÿ“‹ Suggested git commands:")
                logger.info("git add .")
                logger.info("git commit -m 'feat: Integrate advanced analytics and enterprise UI'")
                logger.info("git push origin main")
            else:
                logger.info("โœ… No changes detected")
                
        except Exception as e:
            logger.error(f"โŒ Error checking git status: {e}")

def main():
    """Main integration function"""
    integrator = FREDMLIntegration()
    
    try:
        # Run integration checklist
        integrator.run_integration_checklist()
        
        # Prepare for GitHub
        integrator.prepare_for_github()
        
        logger.info("๐ŸŽ‰ Integration and testing completed!")
        
    except Exception as e:
        logger.error(f"โŒ Integration failed: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()