Files
planet/backend/app/services/datasource_connectivity.py
T
linkong 3b1b9ef68c
ci / backend (push) Waiting to run
ci / frontend (push) Waiting to run
release: bump version to 0.83.0
2026-12-02 18:53:58 +08:00

670 lines
25 KiB
Python

"""Connectivity validation helpers for built-in datasource overrides."""
from __future__ import annotations
from datetime import UTC, datetime
import csv
import hashlib
import io
import json
import os
from typing import Any
from urllib.parse import urlparse
import httpx
from sqlalchemy import func, select
from app.core.data_sources import get_data_sources_config
from app.core.datasource_defaults import DEFAULT_DATASOURCES
from app.core.enums import JobStatus
from app.models.collected_data import CollectedData
from app.models.datasource import DataSource
from app.models.datasource_config import DataSourceConfig
from app.models.earth_news import EarthNewsItem
from app.models.system_setting import SystemSetting
from app.services.barentswatch import (
BarentsWatchConfig,
_read_zshrc_env,
fetch_barentswatch_access_token,
resolve_barentswatch_config,
)
CONNECTIVITY_VALIDATION_KEY = "connectivity_validation"
CONNECTIVITY_STORE_CATEGORY = "datasource_connectivity_validations"
SUPPORTED_CREDENTIAL_PROVIDERS = {"barentswatch", "spacetrack", "aisstream"}
_ARCGIS_SOURCES = frozenset(
{"arcgis_cables", "arcgis_landing_points", "arcgis_cable_landing_relation"}
)
_EPOCH_CSV_URL = "https://epoch.ai/data/gpu_clusters.csv"
def _is_masked_secret(value: Any) -> bool:
if not isinstance(value, str):
return False
stripped = value.strip()
return bool(stripped) and set(stripped) <= {"•", "*"}
def _clean_override_value(override: dict[str, str] | None, key: str, *, secret: bool = False) -> str:
if not isinstance(override, dict):
return ""
raw = str(override.get(key) or "").strip()
if not raw or (secret and _is_masked_secret(raw)):
return ""
return raw
def _sha256_json(payload: Any) -> str:
encoded = json.dumps(payload, ensure_ascii=False, sort_keys=True, default=str).encode("utf-8")
return hashlib.sha256(encoded).hexdigest()
def _resolve_spacetrack_credentials() -> tuple[str, str, str]:
zshrc_env = _read_zshrc_env()
username = os.getenv("SPACETRACK_USERNAME") or zshrc_env.get("SPACETRACK_USERNAME") or ""
password = os.getenv("SPACETRACK_PASSWORD") or zshrc_env.get("SPACETRACK_PASSWORD") or ""
source = "environment" if os.getenv("SPACETRACK_USERNAME") or os.getenv("SPACETRACK_PASSWORD") else ""
if not source and (username or password):
source = "~/.zshrc"
return username, password, source or "missing"
async def resolve_spacetrack_credentials(
db=None,
credential_override: dict[str, str] | None = None,
) -> tuple[str, str, str]:
"""Resolve Space-Track credentials (`draft override -> DataSourceConfig -> env -> ~/.zshrc`)."""
db_user = ""
db_pass = ""
if db is not None and hasattr(db, "execute"):
try:
result = await db.execute(
select(DataSourceConfig)
.where(DataSourceConfig.name == "spacetrack_tle")
.where(DataSourceConfig.is_active.is_(True))
)
record = result.scalar_one_or_none()
if record is not None:
auth_cfg = record.auth_config or {}
runtime_cfg = record.config or {}
db_user = str(auth_cfg.get("username") or runtime_cfg.get("username") or "").strip()
db_pass = str(auth_cfg.get("password") or runtime_cfg.get("password") or "").strip()
except Exception:
db_user = ""
db_pass = ""
env_user, env_pass, env_source = _resolve_spacetrack_credentials()
username = db_user or env_user
password = db_pass or env_pass
source = "datasource_config" if (db_user or db_pass) else env_source
ov_user = _clean_override_value(credential_override, "username")
ov_pass = _clean_override_value(credential_override, "password", secret=True)
if ov_user or ov_pass:
return ov_user or username, ov_pass or password, "draft"
return username, password, source or "missing"
async def _resolve_barentswatch_with_override(
db,
endpoint: str,
credential_override: dict[str, str] | None = None,
) -> BarentsWatchConfig:
"""Resolve BarentsWatch credentials merging partial draft overrides with saved config."""
saved = await resolve_barentswatch_config(db)
ov_id = _clean_override_value(credential_override, "client_id")
ov_secret = _clean_override_value(credential_override, "client_secret", secret=True)
has_override = bool(ov_id or ov_secret)
return BarentsWatchConfig(
endpoint=str(endpoint or "").strip() or saved.endpoint,
client_id=ov_id or saved.client_id,
client_secret=ov_secret or saved.client_secret,
credential_source="draft" if has_override else saved.credential_source,
endpoint_source="draft" if str(endpoint or "").strip() else saved.endpoint_source,
)
async def _resolve_aisstream_api_key(
db=None,
credential_override: dict[str, str] | None = None,
) -> tuple[str, str]:
ov_key = _clean_override_value(credential_override, "api_key", secret=True)
if ov_key:
return ov_key, "draft"
env_key = os.getenv("AISSTREAM_API_KEY")
zshrc_key = _read_zshrc_env().get("AISSTREAM_API_KEY")
if db is not None:
result = await db.execute(
select(DataSourceConfig)
.where(DataSourceConfig.name == "aisstream_vessels")
.where(DataSourceConfig.is_active.is_(True))
)
record = result.scalar_one_or_none()
if record:
auth_config = record.auth_config or {}
runtime_config = record.config or {}
api_key = auth_config.get("api_key") or runtime_config.get("api_key")
if api_key:
return str(api_key), "datasource_config"
if env_key:
return env_key, "environment"
if zshrc_key:
return zshrc_key, "~/.zshrc"
return "", "missing"
def strip_connectivity_validation(config: dict | None) -> dict:
cleaned = dict(config or {})
cleaned.pop(CONNECTIVITY_VALIDATION_KEY, None)
return cleaned
def merge_connectivity_validation(existing_config: dict | None, next_config: dict | None) -> dict:
merged = strip_connectivity_validation(next_config)
validation = (existing_config or {}).get(CONNECTIVITY_VALIDATION_KEY)
if validation:
merged[CONNECTIVITY_VALIDATION_KEY] = validation
return merged
def get_connectivity_validation(config: DataSourceConfig | None) -> dict | None:
validation = (config.config or {}).get(CONNECTIVITY_VALIDATION_KEY) if config else None
return validation if isinstance(validation, dict) else None
async def build_builtin_connectivity_checksum(
source: str,
endpoint: str,
auth_type: str,
headers: dict | None,
config: dict | None,
db=None,
credential_override: dict[str, str] | None = None,
) -> tuple[str, dict[str, Any]]:
defaults = DEFAULT_DATASOURCES.get(source, {})
credential_provider = defaults.get("credential_provider")
credential_fingerprint = ""
credential_source = "none"
has_credentials = not defaults.get("requires_credentials", False)
if credential_provider == "barentswatch":
barentswatch_config = await _resolve_barentswatch_with_override(
db, endpoint, credential_override
)
client_id = barentswatch_config.client_id
client_secret = barentswatch_config.client_secret
credential_source = barentswatch_config.credential_source
has_credentials = bool(client_id and client_secret)
credential_fingerprint = _sha256_json(
{
"client_id": client_id,
"client_secret": client_secret,
}
)
elif credential_provider == "spacetrack":
username, password, credential_source = await resolve_spacetrack_credentials(
db, credential_override
)
has_credentials = bool(username and password)
credential_fingerprint = _sha256_json(
{
"username": username,
"password": password,
}
)
elif credential_provider == "aisstream":
api_key, credential_source = await _resolve_aisstream_api_key(db, credential_override)
has_credentials = bool(api_key)
credential_fingerprint = _sha256_json({"api_key": api_key})
elif defaults.get("requires_credentials"):
credential_source = str(credential_provider or "unsupported")
checksum_payload = {
"source": source,
"endpoint": endpoint,
"auth_type": "none",
"headers": headers or {},
"config": strip_connectivity_validation(config),
"credential_provider": credential_provider or "none",
"credential_fingerprint": credential_fingerprint,
}
return _sha256_json(checksum_payload), {
"requires_credentials": bool(defaults.get("requires_credentials", False)),
"credential_provider": credential_provider,
"credential_source": credential_source,
"has_credentials": has_credentials,
}
def _prepare_builtin_http_probe(
source: str,
endpoint: str,
headers: dict[str, str],
) -> tuple[str, Any, dict[str, str]]:
"""Build lightweight protocol-accurate probe URL, params, and headers for a built-in source."""
probe_url = str(endpoint or "").strip()
probe_params: Any = None
probe_headers = dict(headers)
if source == "bgpstream_bgp":
base = probe_url.rstrip("/")
probe_url = base if base.endswith("/data") else f"{base}/data"
end = int(datetime.now(UTC).timestamp()) - 3600
start = end - 3600
probe_params = [
("projects[]", "routeviews"),
("collectors[]", "route-views2"),
("types[]", "updates"),
("intervals[]", f"{start},{end}"),
]
probe_headers.setdefault("User-Agent", "Planet-Intelligence-System/1.0 (Python/collector)")
elif source == "celestrak_tle":
if "?" not in probe_url:
probe_params = {"CATNR": "25544", "FORMAT": "json"}
elif source in _ARCGIS_SOURCES:
if probe_url.rstrip("/").endswith("/query") and "f=" not in probe_url:
probe_params = {"where": "1=1", "returnCountOnly": "true", "f": "json"}
elif source == "epoch_ai_gpu":
parsed = urlparse(probe_url)
if parsed.hostname == "epoch.ai" and not parsed.path.endswith(".csv"):
probe_url = _EPOCH_CSV_URL
elif source == "ris_live_bgp":
probe_headers.setdefault(
"X-RIS-Subscribe",
json.dumps({"host": "rrc00", "type": "UPDATE", "require": "announcements"}),
)
probe_headers.setdefault("User-Agent", "Planet-Intelligence-System/1.0 (Python/collector)")
return probe_url, probe_params, probe_headers
async def _read_bounded_probe_bytes(response: httpx.Response, limit: int = 65536) -> bytes:
chunks: list[bytes] = []
size = 0
async for chunk in response.aiter_bytes():
chunks.append(chunk)
size += len(chunk)
if size >= limit:
break
return b"".join(chunks)[:limit]
async def _validate_builtin_probe_response(source: str, response: httpx.Response) -> str | None:
"""Validate response content for sources that return HTTP 200 even on invalid queries."""
if source == "ris_live_bgp":
first_line = ""
async for line in response.aiter_lines():
stripped = line.strip()
if stripped:
first_line = stripped
break
if not first_line:
return "RIS Live 流未返回任何有效消息。"
try:
payload = json.loads(first_line)
except json.JSONDecodeError:
return "RIS Live 返回非 JSON 响应。"
return None if isinstance(payload, dict) else "RIS Live 响应结构异常。"
if source not in _ARCGIS_SOURCES | {"bgpstream_bgp", "celestrak_tle", "epoch_ai_gpu"}:
return None
limit = 4096 if source == "epoch_ai_gpu" else 65536
raw = await _read_bounded_probe_bytes(response, limit=limit)
text = raw.decode(response.encoding or "utf-8", errors="replace").lstrip("\ufeff").strip()
if source == "epoch_ai_gpu":
if text.lower().startswith(("<!doctype", "<html")):
return "Epoch AI 返回了 HTML 页面而非 CSV 数据集。"
first_line = next((line for line in text.splitlines() if line.strip()), "")
headers_row = next(csv.reader(io.StringIO(first_line)), [])
if not {"Name", "Country"}.issubset(set(headers_row)):
return "Epoch AI CSV 表头校验失败(缺少 Name / Country)。"
return None
if source == "celestrak_tle":
if text.lower().startswith("invalid query"):
return f"CelesTrak 参数校验失败:{text[:120]}"
try:
parsed = json.loads(text)
except json.JSONDecodeError:
return "CelesTrak 未返回有效 JSON。"
return None if isinstance(parsed, list) else "CelesTrak 返回格式不是 JSON 数组。"
try:
parsed_obj = json.loads(text)
except json.JSONDecodeError:
return "接口返回了非 JSON 内容(可能是 HTML 错误页)。"
if not isinstance(parsed_obj, dict):
return "接口返回的 JSON 结构无效。"
if parsed_obj.get("error"):
return f"上游接口报错:{str(parsed_obj['error'])[:120]}"
if source == "bgpstream_bgp" and "data" not in parsed_obj:
return "BGPStream Broker 响应缺少 data 字段。"
return None
async def test_builtin_connectivity(
source: str,
endpoint: str,
auth_type: str,
headers: dict | None,
config: dict | None,
db=None,
credential_override: dict[str, str] | None = None,
) -> dict[str, Any]:
defaults = DEFAULT_DATASOURCES.get(source)
if not defaults:
return {
"success": False,
"message": "未知内置采集器,无法执行连接校验。",
}
checksum, credential_context = await build_builtin_connectivity_checksum(
source,
endpoint,
auth_type,
headers,
config,
db,
credential_override,
)
if credential_context["requires_credentials"] and not credential_context["has_credentials"]:
return {
"success": False,
"checksum": checksum,
"stage": "credentials",
"message": "该采集器需要凭证,请先到采集器凭证设置中配置。",
"settings_tab": "collector_credentials",
**credential_context,
}
if (
credential_context["requires_credentials"]
and credential_context["credential_provider"] not in SUPPORTED_CREDENTIAL_PROVIDERS
):
return {
"success": False,
"checksum": checksum,
"stage": "credentials",
"message": "该采集器的凭证链路尚未接入,暂时无法完成连接校验。",
"settings_tab": "collector_credentials",
**credential_context,
}
if source == "media_news_archive":
if db is not None and hasattr(db, "execute"):
try:
await db.execute(select(func.count(EarthNewsItem.id)))
except Exception as exc:
return {
"success": False,
"checksum": checksum,
"stage": "internal",
"message": f"本地新闻归档表检查失败:{exc.__class__.__name__}",
"error": str(exc),
**credential_context,
}
return {
"success": True,
"checksum": checksum,
"stage": "internal",
"message": "内部新闻归档采集器无需外部连接,本地数据源正常。",
**credential_context,
}
request_headers = {str(key): str(value) for key, value in (headers or {}).items()}
request_config = strip_connectivity_validation(config)
timeout = float(request_config.get("timeout") or 30)
request_endpoint = endpoint
if credential_context["credential_provider"] == "aisstream":
if not str(request_endpoint).startswith(("ws://", "wss://")):
return {
"success": False,
"checksum": checksum,
"stage": "endpoint",
"message": "AISStream endpoint 必须是 ws:// 或 wss:// WebSocket 地址。",
**credential_context,
}
return {
"success": True,
"checksum": checksum,
"stage": "credentials",
"message": "AISStream 凭证已配置,WebSocket endpoint 格式有效。",
**credential_context,
}
probe_url, probe_params, request_headers = _prepare_builtin_http_probe(
source, request_endpoint, request_headers
)
try:
async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as client:
if credential_context["credential_provider"] == "barentswatch":
barentswatch_config = await _resolve_barentswatch_with_override(
db, request_endpoint, credential_override
)
token = await fetch_barentswatch_access_token(client, barentswatch_config)
if not token:
return {
"success": False,
"checksum": checksum,
"stage": "token",
"message": "凭证可读取,但 token 响应中没有 access_token。",
"settings_tab": "collector_credentials",
**credential_context,
}
request_headers["Authorization"] = f"Bearer {token}"
elif credential_context["credential_provider"] == "spacetrack":
username, password, _source = await resolve_spacetrack_credentials(
db, credential_override
)
login_url = "https://www.space-track.org/ajaxauth/login"
login_response = await client.post(
login_url,
data={
"identity": username,
"password": password,
},
)
login_response.raise_for_status()
started = datetime.now(UTC)
async with client.stream(
"GET",
probe_url,
params=probe_params,
headers=request_headers,
) as response:
response.raise_for_status()
status_code = response.status_code
body_error = await _validate_builtin_probe_response(source, response)
elapsed_ms = (datetime.now(UTC) - started).total_seconds() * 1000
if body_error:
return {
"success": False,
"checksum": checksum,
"stage": "endpoint",
"message": body_error,
"error": body_error,
"status_code": status_code,
**credential_context,
}
return {
"success": True,
"checksum": checksum,
"stage": "endpoint",
"message": "连接验证成功。",
"status_code": status_code,
"response_time_ms": elapsed_ms,
**credential_context,
}
except httpx.HTTPStatusError as exc:
return {
"success": False,
"checksum": checksum,
"stage": "endpoint",
"message": f"连接验证失败:HTTP {exc.response.status_code}",
"error": f"HTTP Error: {exc.response.status_code}",
**credential_context,
}
except httpx.HTTPError as exc:
return {
"success": False,
"checksum": checksum,
"stage": "network",
"message": f"连接验证失败:{exc.__class__.__name__}",
"error": str(exc),
**credential_context,
}
def make_success_validation(checksum: str, result: dict[str, Any]) -> dict[str, Any]:
return {
"checksum": checksum,
"status": "success",
"validated_at": datetime.now(UTC).isoformat(),
"status_code": result.get("status_code"),
"credential_source": result.get("credential_source"),
}
def is_builtin_validation_current(config: DataSourceConfig | None, checksum: str) -> bool:
validation = get_connectivity_validation(config)
return bool(
validation
and validation.get("status") == "success"
and validation.get("checksum") == checksum
)
async def get_connectivity_store(db) -> dict[str, Any]:
result = await db.execute(
select(SystemSetting).where(SystemSetting.category == CONNECTIVITY_STORE_CATEGORY)
)
record = result.scalar_one_or_none()
return dict(record.payload or {}) if record and isinstance(record.payload, dict) else {}
async def save_connectivity_success(
db,
source: str,
checksum: str,
result: dict[str, Any],
*,
connected_by: str,
) -> dict[str, Any]:
store = await get_connectivity_store(db)
validation = {
**make_success_validation(checksum, result),
"connected_by": connected_by,
}
store[source] = validation
existing = await db.execute(
select(SystemSetting).where(SystemSetting.category == CONNECTIVITY_STORE_CATEGORY)
)
record = existing.scalar_one_or_none()
if record is None:
db.add(SystemSetting(category=CONNECTIVITY_STORE_CATEGORY, payload=store))
else:
record.payload = store
return validation
async def load_builtin_override_config(db, source: str) -> DataSourceConfig | None:
result = await db.execute(
select(DataSourceConfig)
.where(DataSourceConfig.name == source)
.where(DataSourceConfig.is_active.is_(True))
)
return result.scalar_one_or_none()
async def get_builtin_effective_candidate(db, source: str) -> dict[str, Any]:
override = await load_builtin_override_config(db, source)
default_endpoint = get_data_sources_config().get_yaml_url(source)
return {
"name": source,
"endpoint": (override.endpoint if override and override.endpoint else default_endpoint) or "",
"auth_type": override.auth_type if override else "none",
"headers": override.headers if override else {},
"config": strip_connectivity_validation(override.config if override else {}),
}
async def has_collected_data(db, source: str) -> bool:
result = await db.execute(select(func.count(CollectedData.id)).where(CollectedData.source == source))
if (result.scalar() or 0) > 0:
return True
datasource_result = await db.execute(select(DataSource).where(DataSource.source == source))
datasource = datasource_result.scalar_one_or_none()
return bool(datasource and datasource.last_status == JobStatus.SUCCESS.value)
async def get_builtin_connection_status(
db,
source: str,
endpoint: str,
auth_type: str,
headers: dict | None,
config: dict | None,
) -> dict[str, Any]:
checksum, credential_context = await build_builtin_connectivity_checksum(
source,
endpoint,
auth_type,
headers,
config,
db,
)
store = await get_connectivity_store(db)
validation = store.get(source)
if isinstance(validation, dict) and validation.get("status") == "success":
if validation.get("checksum") == checksum:
return {
"connected": True,
"checksum": checksum,
"connected_by": validation.get("connected_by") or "connection_button",
"message": "当前配置已完成连接验证。",
**credential_context,
}
effective = await get_builtin_effective_candidate(db, source)
effective_checksum, _ = await build_builtin_connectivity_checksum(
source,
effective["endpoint"],
effective["auth_type"],
effective["headers"],
effective["config"],
db,
)
if checksum == effective_checksum and await has_collected_data(db, source):
return {
"connected": True,
"checksum": checksum,
"connected_by": "collection",
"message": "当前配置已有成功采集数据,视为已连接。",
**credential_context,
}
if isinstance(validation, dict) and validation.get("status") == "success":
return {
"connected": False,
"checksum": checksum,
"connected_by": None,
"message": "接口地址或凭证指纹已变化,请重新点击连接验证。",
**credential_context,
}
return {
"connected": False,
"checksum": checksum,
"connected_by": None,
"message": "当前配置尚未连接,请点击连接验证。",
**credential_context,
}