Spaces:
Sleeping
Sleeping
File size: 7,448 Bytes
e74367e |
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 |
# admin/data_manager.py
"""Data management functionality for admin operations"""
import sqlite3
import json
from datetime import datetime
import random
import uuid
import pandas as pd
from pathlib import Path
from faker import Faker
class RobataDataManager:
def __init__(self, db_path: str = "robata.db"):
"""Initialize database manager"""
self.db_path = db_path
self.setup_database()
def setup_database(self):
"""Create database tables if they don't exist"""
conn = sqlite3.connect(self.db_path)
c = conn.cursor()
# Create tables
c.executescript('''
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
email TEXT UNIQUE,
name TEXT,
role TEXT,
department TEXT,
title TEXT,
created_at TEXT
);
CREATE TABLE IF NOT EXISTS accounts (
id TEXT PRIMARY KEY,
name TEXT,
industry TEXT,
status TEXT,
website TEXT,
annual_revenue REAL,
employee_count INTEGER,
created_at TEXT
);
CREATE TABLE IF NOT EXISTS contacts (
id TEXT PRIMARY KEY,
account_id TEXT,
first_name TEXT,
last_name TEXT,
email TEXT,
title TEXT,
department TEXT,
influence_level TEXT,
created_at TEXT,
FOREIGN KEY (account_id) REFERENCES accounts (id)
);
CREATE TABLE IF NOT EXISTS interactions (
id TEXT PRIMARY KEY,
type TEXT,
account_id TEXT,
owner_id TEXT,
transcript TEXT,
summary TEXT,
sentiment_score REAL,
metadata TEXT,
created_at TEXT,
FOREIGN KEY (account_id) REFERENCES accounts (id),
FOREIGN KEY (owner_id) REFERENCES users (id)
);
''')
conn.commit()
conn.close()
def generate_sample_data(self):
"""Generate and insert sample data"""
fake = Faker()
conn = sqlite3.connect(self.db_path)
c = conn.cursor()
# Generate users
users = []
roles = ['sales_rep'] * 6 + ['regional_lead'] * 3 + ['head_of_sales']
departments = ['Sales', 'Consulting', 'Technology', 'Operations', 'Strategy']
for _ in range(10):
department = random.choice(departments)
role = random.choice(roles)
user = (
str(uuid.uuid4()),
fake.company_email(),
fake.name(),
role,
department,
f"Senior {department} Consultant",
datetime.now().isoformat()
)
users.append(user)
c.executemany(
'INSERT OR REPLACE INTO users (id, email, name, role, department, title, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)',
users
)
# Generate accounts
accounts = []
client_companies = [
('Quantum Financial', 'Financial Services'),
('HealthTech Solutions', 'Healthcare'),
('InnovateMfg', 'Manufacturing'),
('RetailPro Global', 'Retail'),
('TechVision Corp', 'Technology'),
('EnergyFuture Ltd', 'Energy')
]
for name, industry in client_companies:
account = (
str(uuid.uuid4()),
name,
industry,
random.choice(['active', 'active', 'prospect']),
f"https://www.{name.lower().replace(' ', '')}.com",
round(random.uniform(10, 500), 2),
random.randint(100, 10000),
datetime.now().isoformat()
)
accounts.append(account)
c.executemany(
'INSERT OR REPLACE INTO accounts VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
accounts
)
# Generate contacts and interactions
contacts = []
interactions = []
for account in accounts:
# Generate contacts for each account
for _ in range(random.randint(3, 7)):
contact = (
str(uuid.uuid4()),
account[0],
fake.first_name(),
fake.last_name(),
fake.email(),
random.choice(['CTO', 'CIO', 'IT Director', 'Innovation Lead']),
random.choice(['IT', 'Operations', 'Finance', 'Strategy']),
random.choice(['high', 'medium', 'low']),
datetime.now().isoformat()
)
contacts.append(contact)
# Generate interactions
for _ in range(random.randint(3, 8)):
metadata = {
'location': 'Virtual' if random.random() < 0.7 else 'In-Person',
'duration': random.randint(30, 120),
'platform': random.choice(['Zoom', 'Teams', 'Google Meet'])
}
interaction = (
str(uuid.uuid4()),
random.choice(['meeting', 'call', 'email', 'presentation']),
account[0],
random.choice(users)[0],
fake.text(max_nb_chars=200),
fake.text(max_nb_chars=100),
round(random.uniform(0, 1), 2),
json.dumps(metadata),
datetime.now().isoformat()
)
interactions.append(interaction)
c.executemany(
'INSERT OR REPLACE INTO contacts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
contacts
)
c.executemany(
'INSERT OR REPLACE INTO interactions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
interactions
)
conn.commit()
conn.close()
return len(users), len(accounts), len(contacts), len(interactions)
def get_dashboard_data(self):
"""Get data for dashboard display"""
conn = sqlite3.connect(self.db_path)
# Get counts
counts = {}
counts['accounts'] = pd.read_sql('SELECT COUNT(*) as count FROM accounts', conn).iloc[0]['count']
counts['contacts'] = pd.read_sql('SELECT COUNT(*) as count FROM contacts', conn).iloc[0]['count']
counts['interactions'] = pd.read_sql('SELECT COUNT(*) as count FROM interactions', conn).iloc[0]['count']
# Get recent interactions
recent_interactions = pd.read_sql('''
SELECT i.*, a.name as account_name, u.name as owner_name
FROM interactions i
JOIN accounts a ON i.account_id = a.id
JOIN users u ON i.owner_id = u.id
ORDER BY i.created_at DESC
LIMIT 10
''', conn)
# Get account distribution
account_distribution = pd.read_sql('''
SELECT industry, COUNT(*) as count
FROM accounts
GROUP BY industry
''', conn)
conn.close()
return counts, recent_interactions, account_distribution |