| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- from datetime import datetime
- from sqlalchemy import (
- Column,
- Integer,
- BigInteger,
- String,
- Boolean,
- DateTime,
- UniqueConstraint,
- Index,
- )
- from app.core.database import Base
- class ProxyReputation(Base):
- """
- 代理出口 IP 维度信誉状态表
- 以 (plugin_name, egress_ip) 为主键聚合分析各 IP 的健康度与信誉分
- """
- __tablename__ = "proxy_reputation"
- __ddl__ = """
- CREATE TABLE IF NOT EXISTS `proxy_reputation` (
- `id` BIGINT NOT NULL AUTO_INCREMENT,
- `plugin_name` VARCHAR(64) NOT NULL COMMENT '插件名称,如 vfs_plugin',
- `egress_ip` VARCHAR(64) NOT NULL COMMENT '公网出口IP',
- `score` INT NOT NULL DEFAULT 60 COMMENT '信誉分 0-100',
- `state` VARCHAR(16) NOT NULL DEFAULT 'probation' COMMENT 'trusted, probation, blocked',
- `total_calls` INT NOT NULL DEFAULT 0 COMMENT '总调用次数',
- `success_calls` INT NOT NULL DEFAULT 0 COMMENT '成功调用次数',
- `consecutive_successes` INT NOT NULL DEFAULT 0 COMMENT '连续成功次数',
- `consecutive_fails` INT NOT NULL DEFAULT 0 COMMENT '连续失败次数',
- `last_http_code` INT NULL COMMENT '最近一次 HTTP 响应码',
- `last_error_type` VARCHAR(128) NULL COMMENT '最近一次错误类型',
- `last_seen` INT NULL COMMENT '最近一次调用秒级时间戳',
- `unblock_time` DATETIME NULL COMMENT '封禁解封时间点',
- `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
- `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- PRIMARY KEY (`id`),
- UNIQUE KEY `uk_plugin_ip` (`plugin_name`,`egress_ip`),
- KEY `idx_plugin_state` (`plugin_name`,`state`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='代理出口IP信誉状态表';
- """
- id = Column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True)
- plugin_name = Column(String(64), nullable=False, comment="插件名称,如 vfs_plugin")
- egress_ip = Column(String(64), nullable=False, comment="公网出口IP")
- score = Column(Integer, nullable=False, default=60, comment="信誉分 0-100")
- state = Column(String(16), nullable=False, default="probation", comment="trusted, probation, blocked")
- total_calls = Column(Integer, nullable=False, default=0, comment="总调用次数")
- success_calls = Column(Integer, nullable=False, default=0, comment="成功调用次数")
- consecutive_successes = Column(Integer, nullable=False, default=0, comment="连续成功次数")
- consecutive_fails = Column(Integer, nullable=False, default=0, comment="连续失败次数")
- last_http_code = Column(Integer, nullable=True, comment="最近一次 HTTP 响应码")
- last_error_type = Column(String(128), nullable=True, comment="最近一次错误类型")
- last_seen = Column(Integer, nullable=True, comment="最近一次调用秒级时间戳")
- unblock_time = Column(DateTime, nullable=True, comment="封禁解封时间点")
- created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
- updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
- __table_args__ = (
- UniqueConstraint("plugin_name", "egress_ip", name="uk_plugin_ip"),
- Index("idx_plugin_state", "plugin_name", "state"),
- )
- class ProxyTelemetryLog(Base):
- """
- 代理调用流水日志表
- 用于原始调用记录留存及全局故障率滑动窗口统计
- """
- __tablename__ = "proxy_telemetry_log"
- __ddl__ = """
- CREATE TABLE IF NOT EXISTS `proxy_telemetry_log` (
- `id` BIGINT NOT NULL AUTO_INCREMENT,
- `client_id` VARCHAR(64) NOT NULL COMMENT '上报客户端ID',
- `plugin_name` VARCHAR(64) NOT NULL COMMENT '插件名称',
- `egress_ip` VARCHAR(64) NOT NULL COMMENT '出口IP',
- `local_port` INT NULL COMMENT '客户端本地代理端口',
- `node_name` VARCHAR(128) NULL COMMENT '节点标签名称',
- `action` VARCHAR(32) NOT NULL COMMENT '调用场景: spawn, query, book 等',
- `success` TINYINT(1) NOT NULL COMMENT '是否成功',
- `http_code` INT NOT NULL COMMENT 'HTTP状态码',
- `error_type` VARCHAR(128) NULL COMMENT '错误描述',
- `latency_ms` INT NOT NULL DEFAULT 0 COMMENT '耗时毫秒',
- `timestamp` INT NOT NULL COMMENT '客户端上报时间戳',
- `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (`id`),
- KEY `idx_telemetry_created_at` (`created_at`),
- KEY `idx_telemetry_plugin_created` (`plugin_name`,`created_at`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='代理调用流水日志表';
- """
- id = Column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True)
- client_id = Column(String(64), nullable=False, comment="上报客户端ID")
- plugin_name = Column(String(64), nullable=False, comment="插件名称")
- egress_ip = Column(String(64), nullable=False, comment="出口IP")
- local_port = Column(Integer, nullable=True, comment="客户端本地代理端口")
- node_name = Column(String(128), nullable=True, comment="节点标签名称")
- action = Column(String(32), nullable=False, comment="调用场景: spawn, query, book 等")
- success = Column(Boolean, nullable=False, comment="是否成功")
- http_code = Column(Integer, nullable=False, comment="HTTP状态码")
- error_type = Column(String(128), nullable=True, comment="错误描述")
- latency_ms = Column(Integer, nullable=False, default=0, comment="耗时毫秒")
- timestamp = Column(Integer, nullable=False, comment="客户端上报时间戳")
- created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
- __table_args__ = (
- Index("idx_telemetry_created_at", "created_at"),
- Index("idx_telemetry_plugin_created", "plugin_name", "created_at"),
- )
|