hot_event_strategy.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from typing import Any
  2. from app.core.config import settings
  3. from app.strategies.batch_date import today_yyyymmdd
  4. from app.strategies.base import (
  5. BaseStrategy,
  6. DemandCandidate,
  7. GenerateContext,
  8. )
  9. from app.strategies.odps._utils import round_weight
  10. from app.strategies.sources.hot_content_sync_log import fetch_hot_content_sync_log_by_partition
  11. from app.strategies.staging_store import insert_staging_rows_skip_duplicates
  12. class HotEventStrategy(BaseStrategy):
  13. """新热事件:从 hot_content_odps_sync_log 同步至 strategy_staging。"""
  14. strategy_id = "hot_event"
  15. name = "新热事件"
  16. version = "1.0.0"
  17. def validate_config(self, config: dict[str, Any]) -> bool:
  18. if not settings.hot_content_mysql_configured:
  19. return False
  20. return isinstance(config, dict)
  21. def generate(self, context: GenerateContext) -> list[DemandCandidate]:
  22. partition_dt = today_yyyymmdd()
  23. source_strategy = str(context.params.get("source_strategy") or self.name).strip()
  24. rows = fetch_hot_content_sync_log_by_partition(
  25. partition_dt=partition_dt,
  26. strategy=source_strategy,
  27. )
  28. candidates: list[DemandCandidate] = []
  29. for row in rows:
  30. partition = str(row.get("partition_dt") or partition_dt).strip()
  31. demand_name = str(row.get("demand_name") or "").strip()
  32. if not partition or not demand_name:
  33. continue
  34. demand_type = row.get("demand_type")
  35. parsed_type = str(demand_type).strip() if demand_type else None
  36. candidates.append(
  37. DemandCandidate(
  38. content=demand_name,
  39. demand_id=str(row["demand_id"]),
  40. demand_type=parsed_type,
  41. priority_score=round_weight(row.get("weight")),
  42. extra={"batch_date": partition},
  43. )
  44. )
  45. return candidates
  46. def write_staging(
  47. self,
  48. *,
  49. context: GenerateContext,
  50. candidates: list[DemandCandidate],
  51. ) -> dict[str, Any]:
  52. return insert_staging_rows_skip_duplicates(
  53. strategy_config_id=self.strategy_id,
  54. strategy_name=self.name,
  55. candidates=candidates,
  56. )