hot_content_sync_log.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from typing import Any
  2. from sqlalchemy import text
  3. from app.core.config import settings
  4. from app.db.hot_content_mysql import HotContentSessionLocal
  5. def _hot_strategy_name(strategy: str | None = None) -> str:
  6. value = (strategy or settings.hot_demand_pool_strategy or "新热事件").strip()
  7. return value or "新热事件"
  8. def fetch_hot_content_sync_log_by_partition(
  9. *,
  10. partition_dt: str,
  11. strategy: str | None = None,
  12. ) -> list[dict[str, Any]]:
  13. strategy_value = _hot_strategy_name(strategy)
  14. query = text(
  15. """
  16. SELECT
  17. partition_dt,
  18. strategy,
  19. demand_id,
  20. demand_name,
  21. demand_type,
  22. weight
  23. FROM hot_content_odps_sync_log
  24. WHERE partition_dt = :partition_dt
  25. AND strategy = :strategy
  26. ORDER BY id ASC
  27. """
  28. )
  29. with HotContentSessionLocal() as session:
  30. rows = session.execute(
  31. query,
  32. {
  33. "partition_dt": partition_dt,
  34. "strategy": strategy_value,
  35. },
  36. ).mappings().all()
  37. results: list[dict[str, Any]] = []
  38. for row in rows:
  39. demand_id = str(row.get("demand_id") or "").strip()
  40. demand_name = str(row.get("demand_name") or "").strip()
  41. if not demand_id or not demand_name:
  42. continue
  43. results.append(
  44. {
  45. "partition_dt": str(row.get("partition_dt") or partition_dt).strip(),
  46. "strategy": str(row.get("strategy") or strategy_value).strip(),
  47. "demand_id": demand_id,
  48. "demand_name": demand_name,
  49. "demand_type": str(row.get("demand_type") or "").strip() or None,
  50. "weight": row.get("weight"),
  51. }
  52. )
  53. return results