proxy_reputation.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. __ddl__ = """
  20. CREATE TABLE IF NOT EXISTS `proxy_reputation` (
  21. `id` BIGINT NOT NULL AUTO_INCREMENT,
  22. `plugin_name` VARCHAR(64) NOT NULL COMMENT '插件名称,如 vfs_plugin',
  23. `egress_ip` VARCHAR(64) NOT NULL COMMENT '公网出口IP',
  24. `score` INT NOT NULL DEFAULT 60 COMMENT '信誉分 0-100',
  25. `state` VARCHAR(16) NOT NULL DEFAULT 'probation' COMMENT 'trusted, probation, blocked',
  26. `total_calls` INT NOT NULL DEFAULT 0 COMMENT '总调用次数',
  27. `success_calls` INT NOT NULL DEFAULT 0 COMMENT '成功调用次数',
  28. `consecutive_successes` INT NOT NULL DEFAULT 0 COMMENT '连续成功次数',
  29. `consecutive_fails` INT NOT NULL DEFAULT 0 COMMENT '连续失败次数',
  30. `last_http_code` INT NULL COMMENT '最近一次 HTTP 响应码',
  31. `last_error_type` VARCHAR(128) NULL COMMENT '最近一次错误类型',
  32. `last_seen` INT NULL COMMENT '最近一次调用秒级时间戳',
  33. `unblock_time` DATETIME NULL COMMENT '封禁解封时间点',
  34. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  35. `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  36. PRIMARY KEY (`id`),
  37. UNIQUE KEY `uk_plugin_ip` (`plugin_name`,`egress_ip`),
  38. KEY `idx_plugin_state` (`plugin_name`,`state`)
  39. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='代理出口IP信誉状态表';
  40. """
  41. id = Column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True)
  42. plugin_name = Column(String(64), nullable=False, comment="插件名称,如 vfs_plugin")
  43. egress_ip = Column(String(64), nullable=False, comment="公网出口IP")
  44. score = Column(Integer, nullable=False, default=60, comment="信誉分 0-100")
  45. state = Column(String(16), nullable=False, default="probation", comment="trusted, probation, blocked")
  46. total_calls = Column(Integer, nullable=False, default=0, comment="总调用次数")
  47. success_calls = Column(Integer, nullable=False, default=0, comment="成功调用次数")
  48. consecutive_successes = Column(Integer, nullable=False, default=0, comment="连续成功次数")
  49. consecutive_fails = Column(Integer, nullable=False, default=0, comment="连续失败次数")
  50. last_http_code = Column(Integer, nullable=True, comment="最近一次 HTTP 响应码")
  51. last_error_type = Column(String(128), nullable=True, comment="最近一次错误类型")
  52. last_seen = Column(Integer, nullable=True, comment="最近一次调用秒级时间戳")
  53. unblock_time = Column(DateTime, nullable=True, comment="封禁解封时间点")
  54. created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
  55. updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
  56. __table_args__ = (
  57. UniqueConstraint("plugin_name", "egress_ip", name="uk_plugin_ip"),
  58. Index("idx_plugin_state", "plugin_name", "state"),
  59. )
  60. class ProxyTelemetryLog(Base):
  61. """
  62. 代理调用流水日志表
  63. 用于原始调用记录留存及全局故障率滑动窗口统计
  64. """
  65. __tablename__ = "proxy_telemetry_log"
  66. __ddl__ = """
  67. CREATE TABLE IF NOT EXISTS `proxy_telemetry_log` (
  68. `id` BIGINT NOT NULL AUTO_INCREMENT,
  69. `client_id` VARCHAR(64) NOT NULL COMMENT '上报客户端ID',
  70. `plugin_name` VARCHAR(64) NOT NULL COMMENT '插件名称',
  71. `egress_ip` VARCHAR(64) NOT NULL COMMENT '出口IP',
  72. `local_port` INT NULL COMMENT '客户端本地代理端口',
  73. `node_name` VARCHAR(128) NULL COMMENT '节点标签名称',
  74. `action` VARCHAR(32) NOT NULL COMMENT '调用场景: spawn, query, book 等',
  75. `success` TINYINT(1) NOT NULL COMMENT '是否成功',
  76. `http_code` INT NOT NULL COMMENT 'HTTP状态码',
  77. `error_type` VARCHAR(128) NULL COMMENT '错误描述',
  78. `latency_ms` INT NOT NULL DEFAULT 0 COMMENT '耗时毫秒',
  79. `timestamp` INT NOT NULL COMMENT '客户端上报时间戳',
  80. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  81. PRIMARY KEY (`id`),
  82. KEY `idx_telemetry_created_at` (`created_at`),
  83. KEY `idx_telemetry_plugin_created` (`plugin_name`,`created_at`)
  84. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='代理调用流水日志表';
  85. """
  86. id = Column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True)
  87. client_id = Column(String(64), nullable=False, comment="上报客户端ID")
  88. plugin_name = Column(String(64), nullable=False, comment="插件名称")
  89. egress_ip = Column(String(64), nullable=False, comment="出口IP")
  90. local_port = Column(Integer, nullable=True, comment="客户端本地代理端口")
  91. node_name = Column(String(128), nullable=True, comment="节点标签名称")
  92. action = Column(String(32), nullable=False, comment="调用场景: spawn, query, book 等")
  93. success = Column(Boolean, nullable=False, comment="是否成功")
  94. http_code = Column(Integer, nullable=False, comment="HTTP状态码")
  95. error_type = Column(String(128), nullable=True, comment="错误描述")
  96. latency_ms = Column(Integer, nullable=False, default=0, comment="耗时毫秒")
  97. timestamp = Column(Integer, nullable=False, comment="客户端上报时间戳")
  98. created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
  99. __table_args__ = (
  100. Index("idx_telemetry_created_at", "created_at"),
  101. Index("idx_telemetry_plugin_created", "plugin_name", "created_at"),
  102. )