Skip exact count for full PChome review page

This commit is contained in:
OoO
2026-05-24 19:00:13 +08:00
parent 82b5616921
commit 0709392d9d
8 changed files with 54 additions and 15 deletions

View File

@@ -796,6 +796,7 @@ def fetch_competitor_review_queue_page(
search_query: str = "",
category: str = "",
status_filter: str = "",
count_total: bool = True,
) -> dict:
"""Paginated PChome review queue for operator-facing Dashboard pages."""
page = max(1, int(page or 1))
@@ -809,6 +810,7 @@ def fetch_competitor_review_queue_page(
"review_queue_page:v2:"
f"page={page}:per={per_page}:q={search_query.lower()}:cat={category}:"
f"status={status_filter}:"
f"count={int(bool(count_total))}:"
f"floor={PCHOME_MATCH_SCORE_FLOOR}"
)
return _cached_payload(
@@ -820,6 +822,7 @@ def fetch_competitor_review_queue_page(
search_query=search_query,
category=category,
status_filter=status_filter,
count_total=count_total,
),
ttl_seconds=min(COMPETITOR_INTEL_CACHE_TTL_SECONDS, 300),
)
@@ -922,6 +925,7 @@ def _fetch_competitor_review_queue_page_uncached(
search_query: str = "",
category: str = "",
status_filter: str = "",
count_total: bool = True,
) -> dict:
inspector = inspect(engine)
if not (
@@ -950,12 +954,28 @@ def _fetch_competitor_review_queue_page_uncached(
"limit": per_page,
"offset": (page - 1) * per_page,
}
page_sql = text(cte + """
, total_rows AS (
SELECT COUNT(*) AS total_count
FROM review_rows
),
paged_rows AS (
if count_total:
page_sql = text(cte + """
, total_rows AS (
SELECT COUNT(*) AS total_count
FROM review_rows
),
paged_rows AS (
SELECT *
FROM review_rows
ORDER BY
priority_rank ASC,
momo_price DESC NULLS LAST,
best_match_score DESC NULLS LAST,
attempted_at DESC NULLS LAST
LIMIT :limit OFFSET :offset
)
SELECT paged_rows.*, total_rows.total_count
FROM total_rows
LEFT JOIN paged_rows ON TRUE
""")
else:
page_sql = text(cte + """
SELECT *
FROM review_rows
ORDER BY
@@ -964,15 +984,11 @@ def _fetch_competitor_review_queue_page_uncached(
best_match_score DESC NULLS LAST,
attempted_at DESC NULLS LAST
LIMIT :limit OFFSET :offset
)
SELECT paged_rows.*, total_rows.total_count
FROM total_rows
LEFT JOIN paged_rows ON TRUE
""")
""")
with engine.connect() as conn:
rows = conn.execute(page_sql, page_params).mappings().all()
total = int(rows[0].get("total_count") or 0) if rows else 0
total = int(rows[0].get("total_count") or 0) if count_total and rows else -1
item_rows = [dict(row) for row in rows if row.get("sku")]
return {