Singtel_Use_Case1 / fast_explorer.py
cosmoruler
stuck already
db6dcad
#!/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()