config.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from urllib.parse import quote_plus
  2. from pydantic_settings import BaseSettings, SettingsConfigDict
  3. class Settings(BaseSettings):
  4. app_name: str = "denamd-server"
  5. app_env: str = "dev"
  6. app_host: str = "0.0.0.0"
  7. app_port: int = 8000
  8. cors_allow_origins: str = "*"
  9. scheduler_heartbeat_seconds: int = 3600
  10. mysql_host: str = ""
  11. mysql_port: int = 3306
  12. mysql_user: str = "root"
  13. mysql_password: str = ""
  14. mysql_database: str = ""
  15. # 新热事件来源库(hot_content_* 表),与需求池库分离
  16. hot_content_mysql_host: str = ""
  17. hot_content_mysql_port: int = 3306
  18. hot_content_mysql_user: str = ""
  19. hot_content_mysql_password: str = ""
  20. hot_content_mysql_database: str = ""
  21. hot_content_mysql_charset: str = "utf8mb4"
  22. odps_access_id: str = "LTAI9EBa0bd5PrDa"
  23. odps_access_key: str = "vAalxds7YxhfOA2yVv8GziCg3Y87v5"
  24. odps_project: str = "loghubods"
  25. odps_endpoint: str = "http://service.odps.aliyun.com/api"
  26. demand_pool_source_table: str = "dwd_multi_demand_pool_di"
  27. demand_pool_secondary_source_table: str = "dwd_demand_pool_di"
  28. # 次源 dwd_demand_pool_di 同步开关;false 时仅同步主源 dwd_multi_demand_pool_di
  29. demand_pool_secondary_sync_enabled: bool = False
  30. demand_pool_secondary_strategy: str = "近期需求"
  31. demand_pool_secondary_default_ext_info: str = "{}"
  32. demand_pool_target_table: str = "multi_demand_pool_di"
  33. demand_pool_initial_partitions: str = "20260507,20260508,20260509"
  34. demand_pool_hourly_sync_enabled: bool = True
  35. demand_pool_hourly_sync_minute: int = 0
  36. demand_pool_daily_strategy_alert_enabled: bool = True
  37. demand_pool_daily_strategy_alert_hour: int = 10
  38. demand_pool_daily_strategy_alert_minute: int = 0
  39. feishu_webhook_url: str = ""
  40. feishu_webhook_timeout_seconds: int = 30
  41. # 默认不校验 HTTPS 证书,避免公司代理自签链导致发不出消息;生产若需严格校验可设为 true
  42. feishu_webhook_verify_ssl: bool = False
  43. hot_demand_pool_strategy: str = "新热事件"
  44. hot_content_wxindex_threshold: float = 1_000_000.0
  45. model_config = SettingsConfigDict(
  46. env_file=".env",
  47. env_file_encoding="utf-8",
  48. extra="ignore",
  49. )
  50. @property
  51. def mysql_dsn(self) -> str:
  52. return (
  53. "mysql+pymysql://"
  54. f"{quote_plus(self.mysql_user)}:{quote_plus(self.mysql_password)}"
  55. f"@{self.mysql_host}:{self.mysql_port}/{quote_plus(self.mysql_database)}"
  56. )
  57. @property
  58. def hot_content_mysql_configured(self) -> bool:
  59. return bool(self.hot_content_mysql_host.strip() and self.hot_content_mysql_database.strip())
  60. @property
  61. def hot_content_mysql_dsn(self) -> str:
  62. charset = quote_plus(self.hot_content_mysql_charset or "utf8mb4")
  63. return (
  64. "mysql+pymysql://"
  65. f"{quote_plus(self.hot_content_mysql_user)}:"
  66. f"{quote_plus(self.hot_content_mysql_password)}"
  67. f"@{self.hot_content_mysql_host}:{self.hot_content_mysql_port}/"
  68. f"{quote_plus(self.hot_content_mysql_database)}?charset={charset}"
  69. )
  70. @property
  71. def demand_pool_initial_partition_list(self) -> list[str]:
  72. return [
  73. partition.strip()
  74. for partition in self.demand_pool_initial_partitions.split(",")
  75. if partition.strip()
  76. ]
  77. @property
  78. def cors_allow_origin_list(self) -> list[str]:
  79. if self.cors_allow_origins.strip() == "*":
  80. return ["*"]
  81. return [
  82. origin.strip()
  83. for origin in self.cors_allow_origins.split(",")
  84. if origin.strip()
  85. ]
  86. settings = Settings()