Add initial work from Codex
This commit is contained in:
1
backend/app/db/__init__.py
Normal file
1
backend/app/db/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Database helpers for warehouse connections."""
|
||||
34
backend/app/db/engine.py
Normal file
34
backend/app/db/engine.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import create_engine, event
|
||||
from sqlalchemy.engine import Engine
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
def _create_read_only_engine(connection_url: str) -> Engine:
|
||||
engine = create_engine(
|
||||
connection_url, pool_pre_ping=True, pool_recycle=3600, future=True
|
||||
)
|
||||
|
||||
@event.listens_for(engine, "connect")
|
||||
def _on_connect(dbapi_connection, _connection_record) -> None:
|
||||
cursor = dbapi_connection.cursor()
|
||||
try:
|
||||
cursor.execute("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;")
|
||||
finally:
|
||||
cursor.close()
|
||||
|
||||
return engine
|
||||
|
||||
|
||||
def create_warehouse_engines() -> dict[str, Engine]:
|
||||
return {
|
||||
"wwi": _create_read_only_engine(settings.wwi_connection_url),
|
||||
"aw": _create_read_only_engine(settings.aw_connection_url),
|
||||
}
|
||||
|
||||
|
||||
def dispose_engines(engines: dict[str, Engine]) -> None:
|
||||
for engine in engines.values():
|
||||
engine.dispose()
|
||||
27
backend/app/db/postgres.py
Normal file
27
backend/app/db/postgres.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.postgres_models import Base
|
||||
|
||||
|
||||
def create_postgres_engine() -> Engine:
|
||||
return create_engine(
|
||||
settings.postgres_connection_url,
|
||||
pool_pre_ping=True,
|
||||
pool_recycle=3600,
|
||||
future=True,
|
||||
)
|
||||
|
||||
|
||||
def initialize_postgres_schema(engine: Engine) -> None:
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def create_postgres_session_factory(engine: Engine) -> sessionmaker[Session]:
|
||||
return sessionmaker(
|
||||
bind=engine, autoflush=False, autocommit=False, expire_on_commit=False
|
||||
)
|
||||
86
backend/app/db/postgres_models.py
Normal file
86
backend/app/db/postgres_models.py
Normal file
@@ -0,0 +1,86 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import JSON, DateTime, Float, Integer, String, Text
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
|
||||
def _utcnow() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class AuditLog(Base):
|
||||
__tablename__ = "audit_logs"
|
||||
|
||||
id: Mapped[str] = mapped_column(
|
||||
String(36), primary_key=True, default=lambda: str(uuid4())
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=_utcnow, index=True
|
||||
)
|
||||
method: Mapped[str] = mapped_column(String(12), index=True)
|
||||
path: Mapped[str] = mapped_column(String(300), index=True)
|
||||
query_string: Mapped[str] = mapped_column(String(1000), default="")
|
||||
status_code: Mapped[int] = mapped_column(Integer, index=True)
|
||||
duration_ms: Mapped[float] = mapped_column(Float)
|
||||
trace_id: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
|
||||
span_id: Mapped[str | None] = mapped_column(String(16), nullable=True, index=True)
|
||||
client_ip: Mapped[str | None] = mapped_column(String(120), nullable=True)
|
||||
user_agent: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
details: Mapped[dict] = mapped_column(JSON, default=dict)
|
||||
|
||||
|
||||
class ForecastRun(Base):
|
||||
__tablename__ = "forecast_runs"
|
||||
|
||||
id: Mapped[str] = mapped_column(
|
||||
String(36), primary_key=True, default=lambda: str(uuid4())
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=_utcnow, index=True
|
||||
)
|
||||
horizon_days: Mapped[int] = mapped_column(Integer)
|
||||
point_count: Mapped[int] = mapped_column(Integer)
|
||||
trigger_source: Mapped[str] = mapped_column(String(64), index=True)
|
||||
trace_id: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
|
||||
span_id: Mapped[str | None] = mapped_column(String(16), nullable=True, index=True)
|
||||
payload: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
|
||||
|
||||
class RankingRun(Base):
|
||||
__tablename__ = "ranking_runs"
|
||||
|
||||
id: Mapped[str] = mapped_column(
|
||||
String(36), primary_key=True, default=lambda: str(uuid4())
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=_utcnow, index=True
|
||||
)
|
||||
top_n: Mapped[int] = mapped_column(Integer)
|
||||
item_count: Mapped[int] = mapped_column(Integer)
|
||||
trigger_source: Mapped[str] = mapped_column(String(64), index=True)
|
||||
trace_id: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
|
||||
span_id: Mapped[str | None] = mapped_column(String(16), nullable=True, index=True)
|
||||
payload: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
|
||||
|
||||
class RecommendationRun(Base):
|
||||
__tablename__ = "recommendation_runs"
|
||||
|
||||
id: Mapped[str] = mapped_column(
|
||||
String(36), primary_key=True, default=lambda: str(uuid4())
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=_utcnow, index=True
|
||||
)
|
||||
item_count: Mapped[int] = mapped_column(Integer)
|
||||
trigger_source: Mapped[str] = mapped_column(String(64), index=True)
|
||||
trace_id: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
|
||||
span_id: Mapped[str | None] = mapped_column(String(16), nullable=True, index=True)
|
||||
payload: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
167
backend/app/db/queries.py
Normal file
167
backend/app/db/queries.py
Normal file
@@ -0,0 +1,167 @@
|
||||
from __future__ import annotations
|
||||
|
||||
AW_DAILY_SALES_QUERIES = [
|
||||
"""
|
||||
SELECT
|
||||
CAST(d.FullDateAlternateKey AS date) AS sale_date,
|
||||
SUM(f.SalesAmount) AS revenue,
|
||||
SUM(f.TotalProductCost) AS cost,
|
||||
SUM(f.OrderQuantity) AS quantity,
|
||||
COUNT_BIG(*) AS orders
|
||||
FROM dbo.FactInternetSales AS f
|
||||
INNER JOIN dbo.DimDate AS d ON d.DateKey = f.OrderDateKey
|
||||
GROUP BY CAST(d.FullDateAlternateKey AS date)
|
||||
ORDER BY sale_date;
|
||||
""",
|
||||
"""
|
||||
SELECT
|
||||
CAST(OrderDate AS date) AS sale_date,
|
||||
SUM(SalesAmount) AS revenue,
|
||||
SUM(TotalProductCost) AS cost,
|
||||
SUM(OrderQuantity) AS quantity,
|
||||
COUNT_BIG(*) AS orders
|
||||
FROM dbo.FactInternetSales
|
||||
GROUP BY CAST(OrderDate AS date)
|
||||
ORDER BY sale_date;
|
||||
""",
|
||||
]
|
||||
|
||||
WWI_DAILY_SALES_QUERIES = [
|
||||
"""
|
||||
SELECT
|
||||
CAST(i.InvoiceDate AS date) AS sale_date,
|
||||
SUM(il.ExtendedPrice) AS revenue,
|
||||
SUM(il.TaxAmount) AS cost,
|
||||
SUM(il.Quantity) AS quantity,
|
||||
COUNT_BIG(DISTINCT i.InvoiceID) AS orders
|
||||
FROM Sales.Invoices AS i
|
||||
INNER JOIN Sales.InvoiceLines AS il ON il.InvoiceID = i.InvoiceID
|
||||
GROUP BY CAST(i.InvoiceDate AS date)
|
||||
ORDER BY sale_date;
|
||||
""",
|
||||
"""
|
||||
SELECT
|
||||
CAST(i.InvoiceDate AS date) AS sale_date,
|
||||
SUM(il.UnitPrice * il.Quantity) AS revenue,
|
||||
CAST(0 AS float) AS cost,
|
||||
SUM(il.Quantity) AS quantity,
|
||||
COUNT_BIG(DISTINCT i.InvoiceID) AS orders
|
||||
FROM Sales.Invoices AS i
|
||||
INNER JOIN Sales.InvoiceLines AS il ON il.InvoiceID = i.InvoiceID
|
||||
GROUP BY CAST(i.InvoiceDate AS date)
|
||||
ORDER BY sale_date;
|
||||
""",
|
||||
]
|
||||
|
||||
AW_PRODUCT_PERFORMANCE_QUERIES = [
|
||||
"""
|
||||
SELECT
|
||||
p.ProductAlternateKey AS product_id,
|
||||
p.EnglishProductName AS product_name,
|
||||
COALESCE(sc.EnglishProductSubcategoryName, 'Unknown') AS category_name,
|
||||
SUM(f.SalesAmount) AS revenue,
|
||||
SUM(f.TotalProductCost) AS cost,
|
||||
SUM(f.OrderQuantity) AS quantity,
|
||||
COUNT_BIG(*) AS orders
|
||||
FROM dbo.FactInternetSales AS f
|
||||
INNER JOIN dbo.DimProduct AS p ON p.ProductKey = f.ProductKey
|
||||
LEFT JOIN dbo.DimProductSubcategory AS sc ON sc.ProductSubcategoryKey = p.ProductSubcategoryKey
|
||||
GROUP BY p.ProductAlternateKey, p.EnglishProductName, sc.EnglishProductSubcategoryName
|
||||
ORDER BY revenue DESC;
|
||||
""",
|
||||
"""
|
||||
SELECT
|
||||
CAST(ProductKey AS nvarchar(100)) AS product_id,
|
||||
CAST(ProductKey AS nvarchar(100)) AS product_name,
|
||||
'Unknown' AS category_name,
|
||||
SUM(SalesAmount) AS revenue,
|
||||
SUM(TotalProductCost) AS cost,
|
||||
SUM(OrderQuantity) AS quantity,
|
||||
COUNT_BIG(*) AS orders
|
||||
FROM dbo.FactInternetSales
|
||||
GROUP BY ProductKey
|
||||
ORDER BY revenue DESC;
|
||||
""",
|
||||
]
|
||||
|
||||
WWI_PRODUCT_PERFORMANCE_QUERIES = [
|
||||
"""
|
||||
SELECT
|
||||
CAST(s.StockItemID AS nvarchar(100)) AS product_id,
|
||||
s.StockItemName AS product_name,
|
||||
COALESCE(cg.StockGroupName, 'Unknown') AS category_name,
|
||||
SUM(il.ExtendedPrice) AS revenue,
|
||||
SUM(il.TaxAmount) AS cost,
|
||||
SUM(il.Quantity) AS quantity,
|
||||
COUNT_BIG(*) AS orders
|
||||
FROM Sales.InvoiceLines AS il
|
||||
INNER JOIN Warehouse.StockItems AS s ON s.StockItemID = il.StockItemID
|
||||
LEFT JOIN Warehouse.StockItemStockGroups AS sig ON sig.StockItemID = s.StockItemID
|
||||
LEFT JOIN Warehouse.StockGroups AS cg ON cg.StockGroupID = sig.StockGroupID
|
||||
GROUP BY s.StockItemID, s.StockItemName, cg.StockGroupName
|
||||
ORDER BY revenue DESC;
|
||||
""",
|
||||
"""
|
||||
SELECT
|
||||
CAST(il.StockItemID AS nvarchar(100)) AS product_id,
|
||||
CAST(il.StockItemID AS nvarchar(100)) AS product_name,
|
||||
'Unknown' AS category_name,
|
||||
SUM(il.UnitPrice * il.Quantity) AS revenue,
|
||||
CAST(0 AS float) AS cost,
|
||||
SUM(il.Quantity) AS quantity,
|
||||
COUNT_BIG(*) AS orders
|
||||
FROM Sales.InvoiceLines AS il
|
||||
GROUP BY il.StockItemID
|
||||
ORDER BY revenue DESC;
|
||||
""",
|
||||
]
|
||||
|
||||
AW_CUSTOMER_QUERIES = [
|
||||
"""
|
||||
SELECT
|
||||
CAST(c.CustomerAlternateKey AS nvarchar(100)) AS customer_id,
|
||||
c.FirstName + ' ' + c.LastName AS customer_name,
|
||||
SUM(f.SalesAmount) AS revenue,
|
||||
COUNT_BIG(*) AS orders
|
||||
FROM dbo.FactInternetSales AS f
|
||||
INNER JOIN dbo.DimCustomer AS c ON c.CustomerKey = f.CustomerKey
|
||||
GROUP BY c.CustomerAlternateKey, c.FirstName, c.LastName
|
||||
ORDER BY revenue DESC;
|
||||
""",
|
||||
"""
|
||||
SELECT
|
||||
CAST(CustomerKey AS nvarchar(100)) AS customer_id,
|
||||
CAST(CustomerKey AS nvarchar(100)) AS customer_name,
|
||||
SUM(SalesAmount) AS revenue,
|
||||
COUNT_BIG(*) AS orders
|
||||
FROM dbo.FactInternetSales
|
||||
GROUP BY CustomerKey
|
||||
ORDER BY revenue DESC;
|
||||
""",
|
||||
]
|
||||
|
||||
WWI_CUSTOMER_QUERIES = [
|
||||
"""
|
||||
SELECT
|
||||
CAST(c.CustomerID AS nvarchar(100)) AS customer_id,
|
||||
c.CustomerName AS customer_name,
|
||||
SUM(il.ExtendedPrice) AS revenue,
|
||||
COUNT_BIG(DISTINCT i.InvoiceID) AS orders
|
||||
FROM Sales.Invoices AS i
|
||||
INNER JOIN Sales.InvoiceLines AS il ON il.InvoiceID = i.InvoiceID
|
||||
INNER JOIN Sales.Customers AS c ON c.CustomerID = i.CustomerID
|
||||
GROUP BY c.CustomerID, c.CustomerName
|
||||
ORDER BY revenue DESC;
|
||||
""",
|
||||
"""
|
||||
SELECT
|
||||
CAST(i.CustomerID AS nvarchar(100)) AS customer_id,
|
||||
CAST(i.CustomerID AS nvarchar(100)) AS customer_name,
|
||||
SUM(il.UnitPrice * il.Quantity) AS revenue,
|
||||
COUNT_BIG(DISTINCT i.InvoiceID) AS orders
|
||||
FROM Sales.Invoices AS i
|
||||
INNER JOIN Sales.InvoiceLines AS il ON il.InvoiceID = i.InvoiceID
|
||||
GROUP BY i.CustomerID
|
||||
ORDER BY revenue DESC;
|
||||
""",
|
||||
]
|
||||
Reference in New Issue
Block a user