customeragent-api / server /scripts /seed_dashboard_data.py
anasraza526's picture
Clean deploy to Hugging Face
ac90985
import sys
import os
from datetime import datetime, timedelta
import random
# Add current directory to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from app.core.database import SessionLocal, engine, Base
from app.models.user import User
from app.models.website import Website
from app.models.chat_session import ChatSession
from app.models.faq import FAQ
from app.core.security import get_password_hash
# Ensure tables exist
Base.metadata.create_all(bind=engine)
def seed_data():
db = SessionLocal()
try:
print("🌱 Seeding dashboard data...")
# 1. Get or Create Test User
email = "test@example.com"
user = db.query(User).filter(User.email == email).first()
if not user:
print(f"Creating user {email}...")
user = User(email=email, hashed_password=get_password_hash("password123"))
db.add(user)
db.commit()
db.refresh(user)
else:
print(f"User {email} found.")
# 2. Get or Create Website
website = db.query(Website).filter(Website.owner_id == user.id).first()
if not website:
print("Creating sample website...")
website = Website(
url="https://example.com",
name="Example Store",
owner_id=user.id,
is_verified=True
)
db.add(website)
db.commit()
db.refresh(website)
else:
print(f"Website {website.url} found.")
# 3. Create Chat Sessions (Last 30 days)
print("Generating chat sessions...")
# Check if we already have sessions
count = db.query(ChatSession).filter(ChatSession.website_id == website.id).count()
if count < 10:
now = datetime.utcnow()
# Create 50 random sessions
for i in range(50):
days_ago = random.randint(0, 30)
started_at = now - timedelta(days=days_ago, hours=random.randint(0, 23))
session = ChatSession(
session_id=f"sess_{random.randint(10000, 99999)}",
website_id=website.id,
visitor_email=f"visitor{i}@test.com",
message_count=random.randint(2, 10),
started_at=started_at,
last_activity_at=started_at + timedelta(minutes=random.randint(5, 30)),
ended_at=started_at + timedelta(minutes=random.randint(5, 30)),
is_active=False,
messages=[
{"text": "Hi, I have a question", "isUser": True, "timestamp": started_at.isoformat()},
{"text": "Sure, how can I help?", "isUser": False, "timestamp": (started_at + timedelta(seconds=5)).isoformat()}
]
)
db.add(session)
db.commit()
print("βœ… Added 50 chat sessions.")
else:
print("Skipping chat sessions (already exist).")
# 4. Create FAQs
print("Generating FAQs...")
if db.query(FAQ).filter(FAQ.website_id == website.id).count() == 0:
faqs = [
FAQ(website_id=website.id, question="What are your shipping rates?", answer="We offer free shipping on orders over $50."),
FAQ(website_id=website.id, question="Do you accept returns?", answer="Yes, within 30 days of purchase."),
FAQ(website_id=website.id, question="Where are you located?", answer="We are based in New York City.")
]
db.add_all(faqs)
db.commit()
print("βœ… Added 3 sample FAQs.")
else:
print("Skipping FAQs (already exist).")
print("πŸŽ‰ Seeding complete! Dashboard should now show real data.")
except Exception as e:
print(f"❌ Error seeding data: {e}")
db.rollback()
finally:
db.close()
if __name__ == "__main__":
seed_data()