File size: 3,935 Bytes
723f9ab | 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 | import pandas as pd
from app.metadata import metadata
from app.table_manager import table_manager
from app.row_manager import row_manager
def init_sample_data(user_store=None, actor='system'):
"""Initialize sample databases and tables with data for a user"""
# Create sample databases
dbs = ['ecommerce', 'analytics', 'users_db']
for db_name in dbs:
metadata.create_database(db_name, user_store)
# Create ecommerce tables
table_manager.create('ecommerce', 'products', [
{'name': 'id', 'type': 'int'},
{'name': 'name', 'type': 'text'},
{'name': 'price', 'type': 'float'},
{'name': 'stock', 'type': 'int'},
{'name': 'category', 'type': 'text'}
], user_store)
products_data = pd.DataFrame([
{'id': 1, 'name': 'Laptop', 'price': 999.99, 'stock': 50, 'category': 'Electronics'},
{'id': 2, 'name': 'Mouse', 'price': 29.99, 'stock': 200, 'category': 'Electronics'},
{'id': 3, 'name': 'Keyboard', 'price': 79.99, 'stock': 150, 'category': 'Electronics'},
{'id': 4, 'name': 'Monitor', 'price': 299.99, 'stock': 75, 'category': 'Electronics'},
{'id': 5, 'name': 'Desk Chair', 'price': 199.99, 'stock': 30, 'category': 'Furniture'},
])
row_manager.write_df('ecommerce', 'products', products_data, actor, user_store)
table_manager.create('ecommerce', 'orders', [
{'name': 'order_id', 'type': 'int'},
{'name': 'customer_id', 'type': 'int'},
{'name': 'product_id', 'type': 'int'},
{'name': 'quantity', 'type': 'int'},
{'name': 'total', 'type': 'float'},
{'name': 'status', 'type': 'text'}
], user_store)
orders_data = pd.DataFrame([
{'order_id': 1001, 'customer_id': 501, 'product_id': 1, 'quantity': 1, 'total': 999.99, 'status': 'shipped'},
{'order_id': 1002, 'customer_id': 502, 'product_id': 2, 'quantity': 2, 'total': 59.98, 'status': 'delivered'},
{'order_id': 1003, 'customer_id': 503, 'product_id': 4, 'quantity': 1, 'total': 299.99, 'status': 'pending'},
])
row_manager.write_df('ecommerce', 'orders', orders_data, actor, user_store)
# Create users_db tables
table_manager.create('users_db', 'customers', [
{'name': 'id', 'type': 'int'},
{'name': 'username', 'type': 'text'},
{'name': 'email', 'type': 'text'},
{'name': 'country', 'type': 'text'},
{'name': 'joined_date', 'type': 'text'}
], user_store)
customers_data = pd.DataFrame([
{'id': 501, 'username': 'john_doe', 'email': 'john@example.com', 'country': 'USA', 'joined_date': '2024-01-15'},
{'id': 502, 'username': 'jane_smith', 'email': 'jane@example.com', 'country': 'UK', 'joined_date': '2024-02-20'},
{'id': 503, 'username': 'bob_wilson', 'email': 'bob@example.com', 'country': 'Canada', 'joined_date': '2024-03-10'},
{'id': 504, 'username': 'alice_brown', 'email': 'alice@example.com', 'country': 'Australia', 'joined_date': '2024-03-25'},
])
row_manager.write_df('users_db', 'customers', customers_data, actor, user_store)
# Create analytics tables
table_manager.create('analytics', 'page_views', [
{'name': 'id', 'type': 'int'},
{'name': 'page', 'type': 'text'},
{'name': 'views', 'type': 'int'},
{'name': 'date', 'type': 'text'}
], user_store)
analytics_data = pd.DataFrame([
{'id': 1, 'page': '/home', 'views': 1500, 'date': '2024-03-01'},
{'id': 2, 'page': '/products', 'views': 890, 'date': '2024-03-01'},
{'id': 3, 'page': '/about', 'views': 450, 'date': '2024-03-01'},
{'id': 4, 'page': '/contact', 'views': 320, 'date': '2024-03-01'},
])
row_manager.write_df('analytics', 'page_views', analytics_data, actor, user_store)
return {"ok": True, "message": "Sample data initialized with 3 databases and 5 tables"}
|