You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
orquestrador/app/db/mock_seed.py

158 lines
4.2 KiB
Python

import random
from datetime import datetime
from app.core.settings import settings
from app.db.mock_database import SessionMockLocal
from app.db.mock_models import Customer, Order, Vehicle
VEHICLE_MODELS = [
"Toyota Corolla",
"Honda Civic",
"Chevrolet Onix",
"Hyundai HB20",
"Volkswagen T-Cross",
"Jeep Compass",
"Fiat Argo",
"Nissan Kicks",
"Renault Duster",
"Ford Ranger",
]
CATEGORIES = ["hatch", "sedan", "suv", "pickup"]
NAMES = [
"Ana Souza",
"Bruno Lima",
"Carla Mendes",
"Diego Santos",
"Eduarda Alves",
"Felipe Rocha",
"Gabriela Costa",
"Henrique Martins",
"Isabela Ferreira",
"Joao Ribeiro",
]
TARGET_VEHICLE_COUNT = 180
TARGET_CUSTOMER_COUNT = 320
TARGET_ORDER_COUNT = 80
VEHICLE_PRICE_BANDS = (
38990,
42990,
46990,
50990,
54990,
58990,
62990,
66990,
69990,
73990,
77990,
82990,
87990,
93990,
99990,
106990,
114990,
123990,
132990,
141990,
)
def _cpf_from_index(index: int) -> str:
"""Gera um CPF numerico deterministico de 11 digitos a partir do indice."""
return str(10_000_000_000 + index).zfill(11)
def _vehicle_price_from_index(index: int, rng: random.Random) -> float:
band_price = VEHICLE_PRICE_BANDS[index % len(VEHICLE_PRICE_BANDS)]
cycle_increment = (index // len(VEHICLE_PRICE_BANDS)) * 750
noise = rng.randint(-1800, 2200)
return float(max(35000, band_price + cycle_increment + noise))
def _seed_vehicle_records(existing_count: int, target_count: int, rng: random.Random) -> list[Vehicle]:
vehicles = []
for idx in range(existing_count, target_count):
model = VEHICLE_MODELS[idx % len(VEHICLE_MODELS)]
category = CATEGORIES[idx % len(CATEGORIES)]
vehicles.append(
Vehicle(
modelo=f"{model} {2020 + (idx % 6)}",
categoria=category,
preco=_vehicle_price_from_index(idx, rng),
)
)
return vehicles
def _seed_customer_records(existing_count: int, target_count: int) -> list[Customer]:
customers = []
for idx in range(existing_count, target_count):
entropy = (idx * 9973) % 10_000
customers.append(
Customer(
cpf=_cpf_from_index(idx),
nome=f"{NAMES[idx % len(NAMES)]} {idx + 1}",
score=300 + (entropy % 550),
limite_credito=float(30_000 + (entropy * 12)),
possui_restricao=(idx % 11 == 0),
)
)
return customers
def _seed_order_records(existing_count: int, target_count: int) -> list[Order]:
orders = []
created = datetime(2026, 1, 1, 8, 0, 0)
for idx in range(existing_count, target_count):
orders.append(
Order(
numero_pedido=f"PED-MOCK-2026-{idx + 1:05d}",
cpf=_cpf_from_index(idx),
status="Ativo",
created_at=created,
)
)
return orders
def seed_mock_data() -> None:
"""Popula dados mock iniciais de veiculos, clientes e pedidos quando habilitado."""
if not settings.mock_seed_enabled:
return
rng = random.Random(42)
db = SessionMockLocal()
try:
vehicle_count = db.query(Vehicle).count()
if vehicle_count < TARGET_VEHICLE_COUNT:
vehicles = _seed_vehicle_records(
existing_count=vehicle_count,
target_count=TARGET_VEHICLE_COUNT,
rng=rng,
)
db.add_all(vehicles)
db.commit()
customer_count = db.query(Customer).count()
if customer_count < TARGET_CUSTOMER_COUNT:
customers = _seed_customer_records(
existing_count=customer_count,
target_count=TARGET_CUSTOMER_COUNT,
)
db.add_all(customers)
db.commit()
order_count = db.query(Order).count()
if order_count < TARGET_ORDER_COUNT:
orders = _seed_order_records(
existing_count=order_count,
target_count=TARGET_ORDER_COUNT,
)
db.add_all(orders)
db.commit()
finally:
db.close()