Commit e78099e7 authored by Vũ Hoàng Anh's avatar Vũ Hoàng Anh

refactor: extract hardcoded DB table names into constants (Phase 1)

parent 6289082f
...@@ -8,15 +8,13 @@ Outfit Matches Viewer API ...@@ -8,15 +8,13 @@ Outfit Matches Viewer API
import logging import logging
from fastapi import APIRouter, Query from fastapi import APIRouter, Query
from common.constants import TABLE_OUTFIT_MATCHES, TABLE_PRODUCTS
# StarRocks import deferred to runtime to avoid crash when USE_LOCAL_SQLITE=true # StarRocks import deferred to runtime to avoid crash when USE_LOCAL_SQLITE=true
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/outfit-matches", tags=["Outfit Matches Viewer"]) router = APIRouter(prefix="/api/outfit-matches", tags=["Outfit Matches Viewer"])
PRODUCT_TABLE = "test_db.magento_product_dimension_with_text_embedding"
MATCH_TABLE = "dashboard_canifa.ai_outfit_product_matches"
def _use_sqlite(): def _use_sqlite():
from config import USE_LOCAL_SQLITE from config import USE_LOCAL_SQLITE
...@@ -39,25 +37,25 @@ async def outfit_stats(): ...@@ -39,25 +37,25 @@ async def outfit_stats():
if _use_sqlite(): if _use_sqlite():
conn = _get_sqlite() conn = _get_sqlite()
c = conn.cursor() c = conn.cursor()
c.execute("SELECT COUNT(DISTINCT anchor_product_code) as anchors FROM pg__dashboard_canifa__ai_outfit_product_matches") c.execute(f"SELECT COUNT(DISTINCT anchor_product_code) as anchors FROM {TABLE_OUTFIT_MATCHES}")
anchors = c.fetchone()["anchors"] anchors = c.fetchone()["anchors"]
c.execute("SELECT COUNT(*) as total FROM pg__dashboard_canifa__ai_outfit_product_matches") c.execute(f"SELECT COUNT(*) as total FROM {TABLE_OUTFIT_MATCHES}")
total_matches = c.fetchone()["total"] total_matches = c.fetchone()["total"]
c.execute("SELECT COUNT(DISTINCT magento_ref_code) as total FROM sr__test_db__magento_product_dimension_with_text_embedding") c.execute(f"SELECT COUNT(DISTINCT magento_ref_code) as total FROM {TABLE_PRODUCTS}")
total_products = c.fetchone()["total"] total_products = c.fetchone()["total"]
c.execute(""" c.execute(f"""
SELECT match_role, COUNT(*) as cnt SELECT match_role, COUNT(*) as cnt
FROM pg__dashboard_canifa__ai_outfit_product_matches FROM {TABLE_OUTFIT_MATCHES}
GROUP BY match_role ORDER BY cnt DESC GROUP BY match_role ORDER BY cnt DESC
""") """)
by_role = [dict(r) for r in c.fetchall()] by_role = [dict(r) for r in c.fetchall()]
conn.close() conn.close()
else: else:
db = get_starrocks_connection() db = get_starrocks_connection()
anchors = db.execute_query(f"SELECT COUNT(DISTINCT anchor_product_code) as anchors FROM {MATCH_TABLE}")[0]["anchors"] anchors = db.execute_query(f"SELECT COUNT(DISTINCT anchor_product_code) as anchors FROM {TABLE_OUTFIT_MATCHES}")[0]["anchors"]
total_matches = db.execute_query(f"SELECT COUNT(*) as total FROM {MATCH_TABLE}")[0]["total"] total_matches = db.execute_query(f"SELECT COUNT(*) as total FROM {TABLE_OUTFIT_MATCHES}")[0]["total"]
total_products = db.execute_query(f"SELECT COUNT(DISTINCT magento_ref_code) as total FROM {PRODUCT_TABLE}")[0]["total"] total_products = db.execute_query(f"SELECT COUNT(DISTINCT magento_ref_code) as total FROM {TABLE_PRODUCTS}")[0]["total"]
by_role = db.execute_query(f"SELECT match_role, COUNT(*) as cnt FROM {MATCH_TABLE} GROUP BY match_role ORDER BY cnt DESC") by_role = db.execute_query(f"SELECT match_role, COUNT(*) as cnt FROM {TABLE_OUTFIT_MATCHES} GROUP BY match_role ORDER BY cnt DESC")
return { return {
"ok": True, "ok": True,
...@@ -105,10 +103,10 @@ async def list_products_with_matches( ...@@ -105,10 +103,10 @@ async def list_products_with_matches(
p.product_line_vn as product_line, p.gender_by_product as gender, p.product_line_vn as product_line, p.gender_by_product as gender,
p.age_by_product as age, p.sale_price, p.age_by_product as age, p.sale_price,
COALESCE(m.match_count, 0) as match_count COALESCE(m.match_count, 0) as match_count
FROM sr__test_db__magento_product_dimension_with_text_embedding p FROM {TABLE_PRODUCTS} p
LEFT JOIN ( LEFT JOIN (
SELECT anchor_product_code, COUNT(*) as match_count SELECT anchor_product_code, COUNT(*) as match_count
FROM pg__dashboard_canifa__ai_outfit_product_matches FROM {TABLE_OUTFIT_MATCHES}
GROUP BY anchor_product_code GROUP BY anchor_product_code
) m ON p.magento_ref_code = m.anchor_product_code ) m ON p.magento_ref_code = m.anchor_product_code
{where} {where}
...@@ -122,10 +120,10 @@ async def list_products_with_matches( ...@@ -122,10 +120,10 @@ async def list_products_with_matches(
# Count total # Count total
count_sql = f""" count_sql = f"""
SELECT COUNT(*) as cnt SELECT COUNT(*) as cnt
FROM sr__test_db__magento_product_dimension_with_text_embedding p FROM {TABLE_PRODUCTS} p
LEFT JOIN ( LEFT JOIN (
SELECT anchor_product_code, COUNT(*) as match_count SELECT anchor_product_code, COUNT(*) as match_count
FROM pg__dashboard_canifa__ai_outfit_product_matches FROM {TABLE_OUTFIT_MATCHES}
GROUP BY anchor_product_code GROUP BY anchor_product_code
) m ON p.magento_ref_code = m.anchor_product_code ) m ON p.magento_ref_code = m.anchor_product_code
{where} {where}
...@@ -158,11 +156,11 @@ async def get_product_matches(code: str): ...@@ -158,11 +156,11 @@ async def get_product_matches(code: str):
c = conn.cursor() c = conn.cursor()
# Get anchor product info # Get anchor product info
c.execute(""" c.execute(f"""
SELECT magento_ref_code as code, product_name as name, SELECT magento_ref_code as code, product_name as name,
product_line_vn as product_line, gender_by_product as gender, product_line_vn as product_line, gender_by_product as gender,
age_by_product as age, sale_price, master_color as color age_by_product as age, sale_price, master_color as color
FROM sr__test_db__magento_product_dimension_with_text_embedding FROM {TABLE_PRODUCTS}
WHERE magento_ref_code = ? WHERE magento_ref_code = ?
""", (code,)) """, (code,))
product = c.fetchone() product = c.fetchone()
...@@ -171,10 +169,10 @@ async def get_product_matches(code: str): ...@@ -171,10 +169,10 @@ async def get_product_matches(code: str):
product = dict(product) product = dict(product)
# Get matches — deduplicate by match_product_code (keep highest score) # Get matches — deduplicate by match_product_code (keep highest score)
c.execute(""" c.execute(f"""
SELECT m.match_product_code, m.match_product_name, m.match_role, SELECT m.match_product_code, m.match_product_name, m.match_role,
MAX(m.score) as score, m.ai_reason, m.match_gender, m.match_age MAX(m.score) as score, m.ai_reason, m.match_gender, m.match_age
FROM pg__dashboard_canifa__ai_outfit_product_matches m FROM {TABLE_OUTFIT_MATCHES} m
WHERE m.anchor_product_code = ? WHERE m.anchor_product_code = ?
GROUP BY m.match_product_code, m.match_role GROUP BY m.match_product_code, m.match_role
ORDER BY m.match_role, m.score DESC ORDER BY m.match_role, m.score DESC
......
...@@ -5,6 +5,7 @@ import httpx ...@@ -5,6 +5,7 @@ import httpx
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
from common.cache import redis_cache from common.cache import redis_cache
from common.constants import TABLE_PRODUCTS
from common.sqlite_db import sqlite_db from common.sqlite_db import sqlite_db
router = APIRouter(prefix="/api/stock-cache", tags=["Stock Cache"]) router = APIRouter(prefix="/api/stock-cache", tags=["Stock Cache"])
...@@ -26,7 +27,7 @@ async def sync_stock(skus: list[str] = None): ...@@ -26,7 +27,7 @@ async def sync_stock(skus: list[str] = None):
if not skus: if not skus:
try: try:
# Fetch real SKUs from the SQLite database # Fetch real SKUs from the SQLite database
rows = await sqlite_db.fetch_all("SELECT DISTINCT product_color_code FROM sr__test_db__magento_product_dimension_with_text_embedding WHERE product_color_code IS NOT NULL LIMIT 2000") rows = await sqlite_db.fetch_all(f"SELECT DISTINCT product_color_code FROM {TABLE_PRODUCTS} WHERE product_color_code IS NOT NULL LIMIT 2000")
if rows: if rows:
skus = [r["product_color_code"] for r in rows] skus = [r["product_color_code"] for r in rows]
else: else:
...@@ -155,7 +156,7 @@ async def list_stock_cache(limit: int = 500): ...@@ -155,7 +156,7 @@ async def list_stock_cache(limit: int = 500):
placeholders = ",".join(["?"] * len(skus)) placeholders = ",".join(["?"] * len(skus))
query = f""" query = f"""
SELECT product_color_code, product_name, product_image_url_thumbnail, sale_price, original_price, product_line_vn SELECT product_color_code, product_name, product_image_url_thumbnail, sale_price, original_price, product_line_vn
FROM sr__test_db__magento_product_dimension_with_text_embedding FROM {TABLE_PRODUCTS}
WHERE product_color_code IN ({placeholders}) WHERE product_color_code IN ({placeholders})
""" """
try: try:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment