File size: 6,260 Bytes
db6dcad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Fast Enhanced Data Explorer (skips slow AI setup)
"""
import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')

# CSV file path
csv_file_path = "C:/Users/Cosmo/Desktop/NTU Peak Singtel/outsystems_sample_logs_6months.csv"

class FastDataExplorer:
    """Fast data explorer with optional AI capabilities"""
    
    def __init__(self, csv_path=csv_file_path):
        self.csv_path = csv_path
        self.df = None
        self.agent = None
        print("πŸš€ Fast Data Explorer initialized!")
        print("πŸ’‘ AI features can be setup later via option 9")
    
    def setup_ai_on_demand(self):
        """Setup AI only when requested"""
        if self.agent is not None:
            print("βœ… AI already configured!")
            return True
            
        print("πŸ€– Setting up AI on demand...")
        try:
            # Try Ollama first
            import ollama
            models = ollama.list()
            if models and 'models' in models and len(models['models']) > 0:
                print("βœ… Ollama detected - configuring...")
                
                # Simple Ollama wrapper
                class SimpleOllama:
                    def run(self, prompt):
                        try:
                            response = ollama.generate(model='llama2', prompt=prompt)
                            return response['response']
                        except Exception as e:
                            return f"Error: {e}"
                
                self.agent = SimpleOllama()
                print("βœ… AI configured with Ollama!")
                return True
                
        except Exception as e:
            print(f"⚠️ AI setup failed: {e}")
            
        # Fallback: No AI
        print("❌ AI not available - using manual analysis only")
        return False
    
    def load_data(self):
        """Load the CSV data"""
        print(f"\nπŸ“ Loading data from: {self.csv_path}")
        
        try:
            if not os.path.exists(self.csv_path):
                print(f"❌ Error: File not found at {self.csv_path}")
                return None
            
            self.df = pd.read_csv(self.csv_path)
            
            print("=== DATA LOADED SUCCESSFULLY ===")
            print(f"πŸ“ File: {os.path.basename(self.csv_path)}")
            print(f"πŸ“Š Dataset shape: {self.df.shape}")
            print(f"πŸ“‹ Columns: {list(self.df.columns)}")
            print("\n=== FIRST 5 ROWS ===")
            print(self.df.head())
            
            return self.df
            
        except Exception as e:
            print(f"Error loading data: {str(e)}")
            return None
    
    def quick_analysis(self):
        """Quick manual analysis"""
        if self.df is None:
            print("❌ No data loaded.")
            return
            
        print("\n=== QUICK ANALYSIS ===")
        print(f"πŸ“Š Shape: {self.df.shape}")
        print(f"πŸ“‹ Columns: {list(self.df.columns)}")
        
        # Log level analysis
        if 'LogLevel' in self.df.columns:
            print("\nπŸ“ˆ Log Level Distribution:")
            print(self.df['LogLevel'].value_counts())
            
        # Error analysis
        if 'ErrorId' in self.df.columns:
            error_count = self.df['ErrorId'].notna().sum()
            print(f"\n🚨 Errors found: {error_count} out of {len(self.df)} records")
            
        # Time analysis
        if 'Timestamp' in self.df.columns:
            print(f"\nπŸ“… Time range: {self.df['Timestamp'].min()} to {self.df['Timestamp'].max()}")
    
    def ai_analysis(self, query):
        """AI analysis with on-demand setup"""
        if self.df is None:
            print("❌ No data loaded.")
            return
            
        if self.agent is None:
            print("πŸ€– Setting up AI...")
            if not self.setup_ai_on_demand():
                return
        
        print(f"\nπŸ” Analyzing: {query}")
        
        # Prepare simple data summary
        data_summary = f"""
Data Analysis Request:
Dataset has {self.df.shape[0]} rows and {self.df.shape[1]} columns.
Columns: {list(self.df.columns)}

Sample data:
{self.df.head(2).to_string()}

Question: {query}

Please provide insights about this OutSystems log data.
"""
        
        try:
            response = self.agent.run(data_summary)
            print("\n" + "="*50)
            print("πŸ€– AI ANALYSIS RESULT")
            print("="*50)
            print(response)
            print("="*50)
        except Exception as e:
            print(f"❌ AI analysis failed: {e}")
    
    def interactive_menu(self):
        """Interactive menu"""
        while True:
            print("\n" + "="*40)
            print("πŸš€ FAST DATA EXPLORER")
            print("="*40)
            print("1. Load data")
            print("2. Quick analysis")
            print("3. Show data summary")
            print("4. AI analysis (auto-setup)")
            print("5. Setup AI manually")
            print("6. Exit")
            print("="*40)
            
            choice = input("Choice (1-6): ").strip()
            
            if choice == '1':
                self.load_data()
            elif choice == '2':
                self.quick_analysis()
            elif choice == '3':
                if self.df is not None:
                    print(f"\nπŸ“Š Summary: {self.df.shape[0]} rows, {self.df.shape[1]} columns")
                    print(f"πŸ“‹ Columns: {list(self.df.columns)}")
                else:
                    print("❌ No data loaded.")
            elif choice == '4':
                query = input("πŸ’¬ Your question: ").strip()
                if query:
                    self.ai_analysis(query)
                else:
                    print("❌ No question entered.")
            elif choice == '5':
                self.setup_ai_on_demand()
            elif choice == '6':
                print("πŸ‘‹ Goodbye!")
                break
            else:
                print("❌ Invalid choice.")

if __name__ == "__main__":
    explorer = FastDataExplorer()
    explorer.interactive_menu()