File size: 4,064 Bytes
383cb38 | 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 | from datetime import datetime, timedelta
import os
import sys
import uuid
# Add backend to path
sys.path.append(os.path.join(os.getcwd(), "backend"))
from accounting.models import Bill, BillStatus, Entity, EntityType, Transaction, TransactionStatus
from ecommerce.models import EcommerceCustomer, EcommerceOrder, EcommerceOrderItem, Subscription
from marketing.models import ChannelType, MarketingChannel
from saas.models import SaaSTier
from sales.models import Deal, Lead
from service_delivery.models import Contract, Milestone, Project
from core.database import SessionLocal, engine
from core.models import AgentJob, Workspace
def seed_forensics():
workspace_id = "default-workspace"
with SessionLocal() as db:
# 1. Ensure workspace exists
ws = db.query(Workspace).filter(Workspace.id == workspace_id).first()
if not ws:
# Use raw SQL for Workspace to avoid matches on learning_phase_completed if column missing
from sqlalchemy import text
db.execute(text("INSERT INTO workspaces (id, name, status) VALUES (:id, :name, :status)"),
{"id": workspace_id, "name": "Forensics Demo", "status": "active"})
db.commit()
# 2. Vendor Price Drift
vendor = Entity(
id=str(uuid.uuid4()),
workspace_id=workspace_id,
name="Global Logistics Inc",
type=EntityType.VENDOR
)
db.add(vendor)
db.flush()
# Historical bills (avg $1000)
for i in range(5):
bill = Bill(
workspace_id=workspace_id,
vendor_id=vendor.id,
amount=1000.0,
issue_date=datetime.now() - timedelta(days=30 * (i + 2)),
due_date=datetime.now() - timedelta(days=30 * (i + 1)),
status=BillStatus.PAID
)
db.add(bill)
# Recent drifted bill ($1200 -> 20% drift)
drifted_bill = Bill(
workspace_id=workspace_id,
vendor_id=vendor.id,
amount=1200.0,
issue_date=datetime.now() - timedelta(days=5),
due_date=datetime.now() + timedelta(days=25),
status=BillStatus.OPEN,
description="Monthly Shipping - Surcharge applied"
)
db.add(drifted_bill)
# 3. Underpricing
customer = EcommerceCustomer(
id=str(uuid.uuid4()),
workspace_id=workspace_id,
email="owner@theshop.com",
first_name="Store",
last_name="Owner"
)
db.add(customer)
db.flush()
order = EcommerceOrder(
id=str(uuid.uuid4()),
workspace_id=workspace_id,
customer_id=customer.id,
total_price=45.0,
status="paid"
)
db.add(order)
db.flush()
item = EcommerceOrderItem(
id=str(uuid.uuid4()),
order_id=order.id,
sku="WIDGET-001",
title="Eco-Friendly Widget",
price=45.0,
quantity=1
)
db.add(item)
# 4. Subscription Waste (Zombie)
sub = Subscription(
id=str(uuid.uuid4()),
workspace_id=workspace_id,
customer_id=customer.id,
plan_name="Project Management Pro",
mrr=99.0,
status="canceled",
canceled_at=datetime.now() - timedelta(days=10)
)
db.add(sub)
db.flush()
# Recent transaction for this canceled sub
tx = Transaction(
id=str(uuid.uuid4()),
workspace_id=workspace_id,
source="bank_feed",
status=TransactionStatus.POSTED,
transaction_date=datetime.now() - timedelta(days=2),
description="Project Management Pro Periodic",
amount=99.0
)
db.add(tx)
db.commit()
print("✅ Forensics test data seeded successfully.")
if __name__ == "__main__":
seed_forensics()
|