proxy_reputation.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from datetime import datetime
  2. from sqlalchemy import (
  3. Column,
  4. Integer,
  5. BigInteger,
  6. String,
  7. Boolean,
  8. DateTime,
  9. UniqueConstraint,
  10. Index,
  11. )
  12. from app.core.database import Base
  13. class ProxyReputation(Base):
  14. """
  15. 代理出口 IP 维度信誉状态表
  16. 以 (plugin_name, egress_ip) 为主键聚合分析各 IP 的健康度与信誉分
  17. """
  18. __tablename__ = "proxy_reputation"
  19. id = Column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True)
  20. plugin_name = Column(String(64), nullable=False, comment="插件名称,如 vfs_plugin")
  21. egress_ip = Column(String(64), nullable=False, comment="公网出口IP")
  22. score = Column(Integer, nullable=False, default=60, comment="信誉分 0-100")
  23. state = Column(String(16), nullable=False, default="probation", comment="trusted, probation, blocked")
  24. total_calls = Column(Integer, nullable=False, default=0, comment="总调用次数")
  25. success_calls = Column(Integer, nullable=False, default=0, comment="成功调用次数")
  26. consecutive_successes = Column(Integer, nullable=False, default=0, comment="连续成功次数")
  27. consecutive_fails = Column(Integer, nullable=False, default=0, comment="连续失败次数")
  28. last_http_code = Column(Integer, nullable=True, comment="最近一次 HTTP 响应码")
  29. last_error_type = Column(String(128), nullable=True, comment="最近一次错误类型")
  30. last_seen = Column(Integer, nullable=True, comment="最近一次调用秒级时间戳")
  31. unblock_time = Column(DateTime, nullable=True, comment="封禁解封时间点")
  32. created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
  33. updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
  34. __table_args__ = (
  35. UniqueConstraint("plugin_name", "egress_ip", name="uk_plugin_ip"),
  36. Index("idx_plugin_state", "plugin_name", "state"),
  37. )
  38. class ProxyTelemetryLog(Base):
  39. """
  40. 代理调用流水日志表
  41. 用于原始调用记录留存及全局故障率滑动窗口统计
  42. """
  43. __tablename__ = "proxy_telemetry_log"
  44. id = Column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True)
  45. client_id = Column(String(64), nullable=False, comment="上报客户端ID")
  46. plugin_name = Column(String(64), nullable=False, comment="插件名称")
  47. egress_ip = Column(String(64), nullable=False, comment="出口IP")
  48. local_port = Column(Integer, nullable=True, comment="客户端本地代理端口")
  49. node_name = Column(String(128), nullable=True, comment="节点标签名称")
  50. action = Column(String(32), nullable=False, comment="调用场景: spawn, query, book 等")
  51. success = Column(Boolean, nullable=False, comment="是否成功")
  52. http_code = Column(Integer, nullable=False, comment="HTTP状态码")
  53. error_type = Column(String(128), nullable=True, comment="错误描述")
  54. latency_ms = Column(Integer, nullable=False, default=0, comment="耗时毫秒")
  55. timestamp = Column(Integer, nullable=False, comment="客户端上报时间戳")
  56. created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
  57. __table_args__ = (
  58. Index("idx_telemetry_created_at", "created_at"),
  59. Index("idx_telemetry_plugin_created", "plugin_name", "created_at"),
  60. )