File size: 22,825 Bytes
e35f53d
 
 
 
 
 
77bf716
 
e35f53d
77bf716
 
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
77bf716
 
 
 
e35f53d
 
 
 
 
 
77bf716
e35f53d
 
 
682de52
e35f53d
 
682de52
e35f53d
 
682de52
e35f53d
 
 
 
 
 
682de52
e35f53d
 
682de52
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682de52
e35f53d
77bf716
e35f53d
 
 
 
 
77bf716
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77bf716
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77bf716
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682de52
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77bf716
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682de52
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682de52
 
e35f53d
 
 
 
682de52
e35f53d
682de52
e35f53d
 
77bf716
e35f53d
 
682de52
e35f53d
 
 
682de52
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682de52
e35f53d
 
 
 
 
682de52
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682de52
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682de52
e35f53d
 
682de52
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682de52
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77bf716
e35f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77bf716
66de5aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e35f53d
66de5aa
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
import os
import uuid
import json
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
from werkzeug.utils import secure_filename
import threading
import time
import logging
from scipy import stats
import matplotlib
matplotlib.use('Agg')  # Use non-interactive backend
import matplotlib.pyplot as plt
import seaborn as sns
import io
import base64
from apscheduler.schedulers.background import BackgroundScheduler
import atexit

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = Flask(__name__)
CORS(app)

# Configuration
UPLOAD_FOLDER = '/tmp/uploads'
PROCESSED_FOLDER = '/tmp/processed'
MAX_FILE_SIZE = 512 * 1024 * 1024  # 512MB
ALLOWED_EXTENSIONS = {'csv', 'xlsx', 'xls', 'json', 'parquet', 'tsv'}
FILE_EXPIRY_HOURS = 1

# Ensure directories exist
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(PROCESSED_FOLDER, exist_ok=True)

# File storage to track sessions and files
file_storage = {}

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

def get_file_age(filepath):
    """Get file age in hours"""
    if os.path.exists(filepath):
        file_time = os.path.getmtime(filepath)
        return (time.time() - file_time) / 3600
    return float('inf')

def cleanup_old_files():
    """Remove files older than FILE_EXPIRY_HOURS"""
    try:
        for folder in [UPLOAD_FOLDER, PROCESSED_FOLDER]:
            for root, dirs, files in os.walk(folder):
                for file in files:
                    filepath = os.path.join(root, file)
                    if get_file_age(filepath) > FILE_EXPIRY_HOURS:
                        os.remove(filepath)
                        logger.info(f"Cleaned up old file: {filepath}")
        
        # Clean up file_storage entries
        current_time = datetime.now()
        sessions_to_remove = []
        for session_id, files in file_storage.items():
            files_to_remove = []
            for file_id, file_info in files.items():
                file_time = datetime.fromisoformat(file_info['timestamp'])
                if (current_time - file_time).total_seconds() > FILE_EXPIRY_HOURS * 3600:
                    files_to_remove.append(file_id)
            
            for file_id in files_to_remove:
                del files[file_id]
            
            if not files:
                sessions_to_remove.append(session_id)
        
        for session_id in sessions_to_remove:
            del file_storage[session_id]
            
    except Exception as e:
        logger.error(f"Error during cleanup: {str(e)}")

# Setup scheduler for automatic cleanup
scheduler = BackgroundScheduler()
scheduler.add_job(func=cleanup_old_files, trigger="interval", minutes=15)
scheduler.start()
atexit.register(lambda: scheduler.shutdown())

def load_data_file(filepath, filename):
    """Load data from various file formats"""
    try:
        file_ext = filename.rsplit('.', 1)[1].lower()
        
        if file_ext == 'csv':
            return pd.read_csv(filepath)
        elif file_ext in ['xlsx', 'xls']:
            return pd.read_excel(filepath)
        elif file_ext == 'json':
            return pd.read_json(filepath)
        elif file_ext == 'parquet':
            return pd.read_parquet(filepath)
        elif file_ext == 'tsv':
            return pd.read_csv(filepath, sep='\t')
        else:
            raise ValueError(f"Unsupported file format: {file_ext}")
    except Exception as e:
        raise Exception(f"Error loading file: {str(e)}")

def perform_basic_statistics(df, columns=None):
    """Perform basic statistical analysis"""
    if columns:
        df = df[columns]
    
    numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
    categorical_cols = df.select_dtypes(exclude=[np.number]).columns.tolist()
    
    result = {
        'numeric_summary': {},
         'categorical_summary': {},
        'general_info': {
            'total_rows': len(df),
            'total_columns': len(df.columns),
            'numeric_columns': len(numeric_cols),
            'categorical_columns': len(categorical_cols),
            'missing_values': df.isnull().sum().to_dict()
        }
    }
    
    # Numeric statistics
    if numeric_cols:
        numeric_stats = df[numeric_cols].describe()
        result['numeric_summary'] = numeric_stats.to_dict()
    
    # Categorical statistics
    if categorical_cols:
        for col in categorical_cols:
            result['categorical_summary'][col] = {
                'unique_values': df[col].nunique(),
                'top_values': df[col].value_counts().head(10).to_dict(),
                'missing_count': df[col].isnull().sum()
            }
    
    return result

def perform_groupby_analysis(df, group_column, target_column, operation='mean', filters=None):
    """Perform group by analysis"""
    # Apply filters if provided
    if filters:
        for f in filters:
            col, op, val = f['column'], f['operator'], f['value']
            if op == '>':
                df = df[df[col] > val]
            elif op == '<':
                df = df[df[col] < val]
            elif op == '==':
                df = df[df[col] == val]
            elif op == '!=':
                df = df[df[col] != val]
            elif op == '>=':
                df = df[df[col] >= val]
            elif op == '<=':
                df = df[df[col] <= val]
    
    # Perform groupby operation
    grouped = df.groupby(group_column)[target_column]
    
    if operation == 'mean':
        result = grouped.mean()
    elif operation == 'sum':
        result = grouped.sum()
    elif operation == 'count':
        result = grouped.count()
    elif operation == 'max':
        result = grouped.max()
    elif operation == 'min':
        result = grouped.min()
    elif operation == 'std':
        result = grouped.std()
    else:
        raise ValueError(f"Unsupported operation: {operation}")
    
    return {
        'result': result.to_dict(),
        'operation': operation,
        'group_column': group_column,
        'target_column': target_column,
        'total_groups': len(result)
    }

def perform_correlation_analysis(df, columns=None, method='pearson'):
    """Perform correlation analysis"""
    if columns:
        df = df[columns]
    
    # Only numeric columns
    numeric_df = df.select_dtypes(include=[np.number])
    
    if numeric_df.empty:
        raise ValueError("No numeric columns found for correlation analysis")
    
    correlation_matrix = numeric_df.corr(method=method)
    
    return {
        'correlation_matrix': correlation_matrix.to_dict(),
        'method': method,
        'columns': numeric_df.columns.tolist()
    }

def detect_outliers(df, columns=None, method='iqr'):
    """Detect outliers in numeric columns"""
    if columns:
        df = df[columns]
    
    numeric_df = df.select_dtypes(include=[np.number])
    outliers = {}
    
    for col in numeric_df.columns:
        if method == 'iqr':
            Q1 = numeric_df[col].quantile(0.25)
            Q3 = numeric_df[col].quantile(0.75)
            IQR = Q3 - Q1
            lower_bound = Q1 - 1.5 * IQR
            upper_bound = Q3 + 1.5 * IQR
            
            outlier_indices = numeric_df[(numeric_df[col] < lower_bound) | 
                                       (numeric_df[col] > upper_bound)].index.tolist()
            
        elif method == 'zscore':
            z_scores = np.abs(stats.zscore(numeric_df[col].dropna()))
            outlier_indices = numeric_df[z_scores > 3].index.tolist()
        
        outliers[col] = {
            'count': len(outlier_indices),
            'indices': outlier_indices[:100],  # Limit to first 100
            'percentage': (len(outlier_indices) / len(numeric_df)) * 100
        }
    
    return outliers

def generate_visualization(df, chart_type, x_column, y_column=None, group_column=None):
    """Generate visualization and return base64 encoded image"""
    plt.figure(figsize=(10, 6))
    
    try:
        if chart_type == 'histogram':
            plt.hist(df[x_column], bins=30, alpha=0.7)
            plt.xlabel(x_column)
            plt.ylabel('Frequency')
            plt.title(f'Histogram of {x_column}')
            
        elif chart_type == 'scatter':
            if not y_column:
                raise ValueError("Y column required for scatter plot")
            plt.scatter(df[x_column], df[y_column], alpha=0.6)
            plt.xlabel(x_column)
            plt.ylabel(y_column)
            plt.title(f'{x_column} vs {y_column}')
            
        elif chart_type == 'bar':
            if group_column:
                grouped = df.groupby(group_column)[x_column].mean() if pd.api.types.is_numeric_dtype(df[x_column]) else df[group_column].value_counts()
            else:
                grouped = df[x_column].value_counts().head(20)
            
            grouped.plot(kind='bar')
            plt.xlabel(group_column or x_column)
            plt.ylabel('Count' if not pd.api.types.is_numeric_dtype(df[x_column]) else f'Mean {x_column}')
            plt.title(f'Bar Chart')
            plt.xticks(rotation=45)
            
        elif chart_type == 'line':
            if y_column:
                plt.plot(df[x_column], df[y_column])
                plt.xlabel(x_column)
                plt.ylabel(y_column)
            else:
                df[x_column].plot()
                plt.ylabel(x_column)
            plt.title('Line Chart')
            
        elif chart_type == 'box':
            if group_column:
                df.boxplot(column=x_column, by=group_column)
            else:
                df.boxplot(column=x_column)
            plt.title('Box Plot')
        
        plt.tight_layout()
        
        # Convert plot to base64 string
        img_buffer = io.BytesIO()
        plt.savefig(img_buffer, format='png', dpi=150, bbox_inches='tight')
        img_buffer.seek(0)
        img_base64 = base64.b64encode(img_buffer.getvalue()).decode()
        plt.close()
        
        return img_base64
        
    except Exception as e:
        plt.close()
        raise Exception(f"Error generating visualization: {str(e)}")

def parse_natural_language_query(query, df_columns):
    """Simple natural language query parser"""
    query_lower = query.lower()
    
    # Define operation keywords
    operations = {
        'average': 'mean', 'mean': 'mean', 'avg': 'mean',
        'sum': 'sum', 'total': 'sum',
        'count': 'count', 'number': 'count',
        'max': 'max', 'maximum': 'max', 'highest': 'max',
        'min': 'min', 'minimum': 'min', 'lowest': 'min'
    }
    
    # Find operation
    operation = 'mean'  # default
    for keyword, op in operations.items():
        if keyword in query_lower:
            operation = op
            break
    
    # Find columns mentioned in query
    mentioned_columns = [col for col in df_columns if col.lower() in query_lower]
    
    # Simple parsing patterns
    if 'by' in query_lower and len(mentioned_columns) >= 2:
        # Group by analysis
        target_col = mentioned_columns[0]
        group_col = mentioned_columns[-1]
        
        return {
            'analysisType': 'groupby',
            'parameters': {
                'groupByColumn': group_col,
                'targetColumn': target_col,
                'operation': operation
            }
        }
    elif 'correlation' in query_lower:
        return {
            'analysisType': 'correlation',
            'parameters': {
                'columns': mentioned_columns if mentioned_columns else None
            }
        }
    elif any(word in query_lower for word in ['chart', 'plot', 'graph', 'visualize']):
        chart_type = 'bar'  # default
        if 'scatter' in query_lower:
            chart_type = 'scatter'
        elif 'line' in query_lower:
            chart_type = 'line'
        elif 'histogram' in query_lower:
            chart_type = 'histogram'
        
        return {
            'analysisType': 'visualization',
            'parameters': {
                'chartType': chart_type,
                'xColumn': mentioned_columns[0] if mentioned_columns else None,
                'yColumn': mentioned_columns[1] if len(mentioned_columns) > 1 else None
            }
        }
    else:
        # Default to basic statistics
        return {
            'analysisType': 'statistics',
            'parameters': {
                'columns': mentioned_columns if mentioned_columns else None
            }
        }

@app.route('/api/health', methods=['GET'])
def health_check():
    return jsonify({'status': 'healthy', 'timestamp': datetime.now().isoformat()})

@app.route('/api/upload', methods=['POST'])
def upload_file():
    try:
        if 'file' not in request.files:
            return jsonify({'error': 'No file provided'}), 400
        
        file = request.files['file']
        session_id = request.form.get('sessionId')
        
        if not session_id:
            return jsonify({'error': 'Session ID required'}), 400
        
        if file.filename == '':
            return jsonify({'error': 'No file selected'}), 400
        
        if not allowed_file(file.filename):
            return jsonify({'error': 'File type not supported'}), 400
        
        # Check file size
        file.seek(0, 2)  # Seek to end
        file_size = file.tell()
        file.seek(0)  # Reset to beginning
        
        if file_size > MAX_FILE_SIZE:
            return jsonify({'error': f'File too large. Maximum size is {MAX_FILE_SIZE // (1024*1024)}MB'}), 400
        
        # Generate unique file ID and secure filename
        file_id = str(uuid.uuid4())
        filename = secure_filename(file.filename)
        
        # Create session directory
        session_dir = os.path.join(UPLOAD_FOLDER, session_id)
        os.makedirs(session_dir, exist_ok=True)
        
        # Save file
        filepath = os.path.join(session_dir, f"{file_id}_{filename}")
        file.save(filepath)
        
        # Store file info
        if session_id not in file_storage:
            file_storage[session_id] = {}
        
        file_storage[session_id][file_id] = {
            'filename': filename,
            'filepath': filepath,
            'size': file_size,
            'timestamp': datetime.now().isoformat()
        }
        
        return jsonify({
            'fileId': file_id,
            'filename': filename,
            'size': file_size,
            'message': 'File uploaded successfully'
        })
        
    except Exception as e:
        logger.error(f"Upload error: {str(e)}")
        return jsonify({'error': str(e)}), 500

@app.route('/api/preview/<file_id>', methods=['GET'])
def preview_file(file_id):
    try:
        session_id = request.args.get('sessionId')
        if not session_id or session_id not in file_storage:
            return jsonify({'error': 'Invalid session'}), 400
        
        if file_id not in file_storage[session_id]:
            return jsonify({'error': 'File not found'}), 404
        
        file_info = file_storage[session_id][file_id]
        
        # Load data and get preview
        df = load_data_file(file_info['filepath'], file_info['filename'])
        
        preview_data = {
            'columns': df.columns.tolist(),
            'dtypes': df.dtypes.astype(str).to_dict(),
            'shape': df.shape,
            'head': df.head(5).to_dict('records'),
            'missing_values': df.isnull().sum().to_dict()
        }
        
        return jsonify(preview_data)
        
    except Exception as e:
        logger.error(f"Preview error: {str(e)}")
        return jsonify({'error': str(e)}), 500

@app.route('/api/analyze', methods=['POST'])
def analyze_data():
    try:
        data = request.get_json()
        session_id = data.get('sessionId')
        file_id = data.get('fileId')
        analysis_type = data.get('analysisType')
        parameters = data.get('parameters', {})
        natural_query = data.get('naturalQuery')
        
        if not all([session_id, file_id]):
            return jsonify({'error': 'Session ID and File ID required'}), 400
        
        if session_id not in file_storage or file_id not in file_storage[session_id]:
            return jsonify({'error': 'File not found'}), 404
        
        file_info = file_storage[session_id][file_id]
        df = load_data_file(file_info['filepath'], file_info['filename'])
        
        # Handle natural language query
        if natural_query and not analysis_type:
            parsed_query = parse_natural_language_query(natural_query, df.columns.tolist())
            analysis_type = parsed_query['analysisType']
            parameters = parsed_query['parameters']
        
        result = {}
        
        if analysis_type == 'statistics':
            result = perform_basic_statistics(df, parameters.get('columns'))
            
        elif analysis_type == 'groupby':
            result = perform_groupby_analysis(
                df,
                parameters.get('groupByColumn'),
                parameters.get('targetColumn'),
                parameters.get('operation', 'mean'),
                parameters.get('filters')
            )
            
        elif analysis_type == 'correlation':
            result = perform_correlation_analysis(
                df,
                parameters.get('columns'),
                parameters.get('method', 'pearson')
            )
            
        elif analysis_type == 'outliers':
            result = detect_outliers(
                df,
                parameters.get('columns'),
                parameters.get('method', 'iqr')
            )
            
        elif analysis_type == 'visualization':
            chart_base64 = generate_visualization(
                df,
                parameters.get('chartType', 'bar'),
                parameters.get('xColumn'),
                parameters.get('yColumn'),
                parameters.get('groupColumn')
            )
            result = {
                'chart': chart_base64,
                'chartType': parameters.get('chartType', 'bar')
            }
            
        else:
            return jsonify({'error': 'Invalid analysis type'}), 400
        
        # Save result to processed folder
        result_id = str(uuid.uuid4())
        result_dir = os.path.join(PROCESSED_FOLDER, session_id)
        os.makedirs(result_dir, exist_ok=True)
        
        result_filepath = os.path.join(result_dir, f"{result_id}_result.json")
        with open(result_filepath, 'w') as f:
            json.dump(result, f, indent=2, default=str)
        
        return jsonify({
            'resultId': result_id,
            'result': result,
            'analysisType': analysis_type,
            'timestamp': datetime.now().isoformat()
        })
        
    except Exception as e:
        logger.error(f"Analysis error: {str(e)}")
        return jsonify({'error': str(e)}), 500

@app.route('/api/files/<session_id>', methods=['GET'])
def list_files(session_id):
    try:
        if session_id not in file_storage:
            return jsonify({'files': []})
        
        files = []
        for file_id, file_info in file_storage[session_id].items():
            # Check if file still exists
            if os.path.exists(file_info['filepath']):
                files.append({
                    'fileId': file_id,
                    'filename': file_info['filename'],
                    'size': file_info['size'],
                    'timestamp': file_info['timestamp']
                })
        
        return jsonify({'files': files})
        
    except Exception as e:
        logger.error(f"List files error: {str(e)}")
        return jsonify({'error': str(e)}), 500

@app.route('/api/file/<file_id>', methods=['DELETE'])
def delete_file(file_id):
    try:
        session_id = request.args.get('sessionId')
        if not session_id or session_id not in file_storage:
            return jsonify({'error': 'Invalid session'}), 400
        
        if file_id not in file_storage[session_id]:
            return jsonify({'error': 'File not found'}), 404
        
        file_info = file_storage[session_id][file_id]
        
        # Remove file from filesystem
        if os.path.exists(file_info['filepath']):
            os.remove(file_info['filepath'])
        
        # Remove from storage
        del file_storage[session_id][file_id]
        
        return jsonify({'message': 'File deleted successfully'})
        
    except Exception as e:
        logger.error(f"Delete error: {str(e)}")
        return jsonify({'error': str(e)}), 500

@app.route('/api/download/<result_id>', methods=['GET'])
def download_result(result_id):
    try:
        session_id = request.args.get('sessionId')
        format_type = request.args.get('format', 'json')
        
        if not session_id:
            return jsonify({'error': 'Session ID required'}), 400
        
        result_filepath = os.path.join(PROCESSED_FOLDER, session_id, f"{result_id}_result.json")
        
        if not os.path.exists(result_filepath):
            return jsonify({'error': 'Result not found'}), 404
        
        if format_type == 'json':
            return send_file(result_filepath, as_attachment=True, 
                           download_name=f"analysis_result_{result_id}.json")
        else:
            return jsonify({'error': 'Format not supported'}), 400
            
    except Exception as e:
        logger.error(f"Download error: {str(e)}")
        return jsonify({'error': str(e)}), 500

@app.route('/', methods=['GET'])
def home():
    return jsonify({
        'message': 'Data Analytics API is running!',
        'version': '1.0.0',
        'endpoints': {
            'health': '/api/health',
            'upload': '/api/upload',
            'preview': '/api/preview/<file_id>',
            'analyze': '/api/analyze',
            'files': '/api/files/<session_id>',
            'delete': '/api/file/<file_id>',
            'download': '/api/download/<result_id>'
        },
        'timestamp': datetime.now().isoformat()
    })

@app.errorhandler(404)
def not_found(error):
    return jsonify({
        'error': 'Endpoint not found',
        'message': 'Please check the API documentation',
        'available_endpoints': [
            '/',
            '/api/health',
            '/api/upload',
            '/api/preview/<file_id>',
            '/api/analyze',
            '/api/files/<session_id>',
            '/api/file/<file_id>',
            '/api/download/<result_id>'
        ]
    }), 404

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860, debug=True)