142 lines
5.3 KiB
Python
142 lines
5.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Catalog Media Self-Healing Script (High-Performance Batch Engine)
|
|
Ensures that:
|
|
1. Every variant without images inherits the primary image from a sibling variant or parent product.
|
|
2. Every product without images in product_images gets a promoted primary image from its variants.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
backend_dir = Path(__file__).resolve().parent.parent
|
|
sys.path.insert(0, str(backend_dir))
|
|
os.chdir(str(backend_dir))
|
|
|
|
from dotenv import load_dotenv
|
|
load_dotenv(".env")
|
|
|
|
import app.models.db_base # noqa: F401
|
|
import ulid
|
|
from sqlalchemy import text
|
|
from app.core.database.db_session import SessionLocal
|
|
|
|
def heal_catalog_media():
|
|
db = SessionLocal()
|
|
start_time = time.time()
|
|
try:
|
|
print("[*] Fetching catalog mapping for media self-healing...")
|
|
|
|
# 1. Fetch all products and their names
|
|
prod_rows = db.execute(text("SELECT product_id, name FROM products")).fetchall()
|
|
prod_names = {r[0]: r[1] for r in prod_rows}
|
|
print(f"[*] Total products in database: {len(prod_names)}")
|
|
|
|
# 2. Fetch all variants
|
|
v_rows = db.execute(text("SELECT variant_id, product_id, sku FROM product_variants")).fetchall()
|
|
print(f"[*] Total variants in database: {len(v_rows)}")
|
|
|
|
variants_by_product = {}
|
|
product_by_variant = {}
|
|
for vid, pid, sku in v_rows:
|
|
variants_by_product.setdefault(pid, []).append(vid)
|
|
product_by_variant[vid] = pid
|
|
|
|
# 3. Fetch all variant images
|
|
vi_rows = db.execute(text(
|
|
"SELECT variant_id, image_url, is_primary, sort_order FROM variant_images WHERE image_url IS NOT NULL AND image_url != ''"
|
|
)).fetchall()
|
|
print(f"[*] Total existing variant images: {len(vi_rows)}")
|
|
|
|
v_has_images = set()
|
|
p_candidate_images = {} # product_id -> list of (is_primary, sort_order, image_url)
|
|
|
|
for vid, url, is_primary, sort_order in vi_rows:
|
|
v_has_images.add(vid)
|
|
pid = product_by_variant.get(vid)
|
|
if pid:
|
|
p_candidate_images.setdefault(pid, []).append((1 if is_primary else 0, sort_order or 0, url))
|
|
|
|
# 4. Fetch all product images
|
|
pi_rows = db.execute(text(
|
|
"SELECT product_id, image_url, sort_order FROM product_images WHERE image_url IS NOT NULL AND image_url != ''"
|
|
)).fetchall()
|
|
print(f"[*] Total existing product images: {len(pi_rows)}")
|
|
|
|
p_has_images = set()
|
|
for pid, url, sort_order in pi_rows:
|
|
p_has_images.add(pid)
|
|
if pid not in p_candidate_images:
|
|
p_candidate_images.setdefault(pid, []).append((1, sort_order or 0, url))
|
|
|
|
# 5. Determine candidates to heal
|
|
bulk_variant_images = []
|
|
bulk_product_images = []
|
|
|
|
def get_best_url(pid):
|
|
cands = p_candidate_images.get(pid)
|
|
if not cands:
|
|
return None
|
|
# Sort by is_primary DESC, sort_order ASC
|
|
sorted_cands = sorted(cands, key=lambda x: (-x[0], x[1]))
|
|
return sorted_cands[0][2]
|
|
|
|
# A) Check empty variants
|
|
for vid, pid, sku in v_rows:
|
|
if vid not in v_has_images:
|
|
best_url = get_best_url(pid)
|
|
if best_url:
|
|
bulk_variant_images.append({
|
|
"image_id": str(ulid.ULID()),
|
|
"variant_id": vid,
|
|
"image_url": best_url,
|
|
"sort_order": 0,
|
|
"is_primary": 1
|
|
})
|
|
v_has_images.add(vid)
|
|
|
|
# B) Check empty products
|
|
for pid, name in prod_names.items():
|
|
if pid not in p_has_images:
|
|
best_url = get_best_url(pid)
|
|
if best_url:
|
|
bulk_product_images.append({
|
|
"image_id": str(ulid.ULID()),
|
|
"product_id": pid,
|
|
"image_url": best_url,
|
|
"alt_text": name[:2000] if name else "Product Image",
|
|
"sort_order": 0,
|
|
"is_banner": 0
|
|
})
|
|
p_has_images.add(pid)
|
|
|
|
print(f"[*] Healing plan: {len(bulk_variant_images)} empty variants, {len(bulk_product_images)} empty products.")
|
|
|
|
# 6. Bulk Insert
|
|
if bulk_variant_images:
|
|
db.execute(text(
|
|
"INSERT INTO variant_images (image_id, variant_id, image_url, sort_order, is_primary) "
|
|
"VALUES (:image_id, :variant_id, :image_url, :sort_order, :is_primary)"
|
|
), bulk_variant_images)
|
|
|
|
if bulk_product_images:
|
|
db.execute(text(
|
|
"INSERT INTO product_images (image_id, product_id, image_url, alt_text, sort_order, is_banner) "
|
|
"VALUES (:image_id, :product_id, :image_url, :alt_text, :sort_order, :is_banner)"
|
|
), bulk_product_images)
|
|
|
|
db.commit()
|
|
elapsed = time.time() - start_time
|
|
print(f"[+] SUCCESS: Healed {len(bulk_variant_images)} variants and {len(bulk_product_images)} products in {elapsed:.2f}s!")
|
|
|
|
except Exception as e:
|
|
db.rollback()
|
|
print(f"[-] Error during media healing: {e}")
|
|
raise
|
|
finally:
|
|
db.close()
|
|
|
|
if __name__ == "__main__":
|
|
heal_catalog_media()
|