2026-04-20 15:02:17 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import json
|
2026-04-24 19:00:07 +08:00
|
|
|
import logging
|
|
|
|
|
import re
|
|
|
|
|
import time
|
|
|
|
|
from typing import Any
|
2026-04-20 15:02:17 +08:00
|
|
|
|
|
|
|
|
from .feishu import FeishuClient
|
|
|
|
|
from .postgres import query_postgres
|
|
|
|
|
from .service import run_feishu_summary, run_sync_brand
|
|
|
|
|
from .settings import build_app_config
|
|
|
|
|
|
|
|
|
|
|
2026-04-24 19:00:07 +08:00
|
|
|
logger = logging.getLogger("inventory_sync.cli")
|
|
|
|
|
SENSITIVE_PATTERN = re.compile(r"(?i)(password|passwd|secret|token|authorization)\s*[:=]\s*([^\s,;]+)")
|
|
|
|
|
|
|
|
|
|
LOG_FIELD_LABELS = {
|
|
|
|
|
"action": "动作",
|
|
|
|
|
"elapsed_ms": "耗时毫秒",
|
|
|
|
|
"env_path": "环境文件",
|
|
|
|
|
"error": "错误",
|
|
|
|
|
"fallback": "兜底方式",
|
|
|
|
|
"row_count": "行数",
|
|
|
|
|
"source_row_count": "源数据行数",
|
|
|
|
|
"table_count": "表数量",
|
|
|
|
|
"target_count": "目标数量",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sanitize_value(value: Any) -> Any:
|
|
|
|
|
if isinstance(value, str):
|
|
|
|
|
return SENSITIVE_PATTERN.sub(r"\1=***", value)
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def log_event(level: int, event: str, **fields: Any) -> None:
|
|
|
|
|
safe_event = sanitize_value(event)
|
|
|
|
|
details = " ".join(
|
|
|
|
|
f"{LOG_FIELD_LABELS.get(key, key)}={sanitize_value(value)!r}" for key, value in fields.items()
|
|
|
|
|
)
|
|
|
|
|
logger.log(level, "事件=%s%s", safe_event, f" {details}" if details else "")
|
|
|
|
|
|
|
|
|
|
|
2026-04-20 15:02:17 +08:00
|
|
|
def parse_args() -> argparse.Namespace:
|
|
|
|
|
parser = argparse.ArgumentParser(description="Feishu Base and PostgreSQL helper")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--action",
|
|
|
|
|
choices=["feishu", "postgres", "sync-brand", "all"],
|
|
|
|
|
default="feishu",
|
|
|
|
|
help="要执行的动作,默认 feishu",
|
|
|
|
|
)
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> int:
|
2026-04-24 19:00:07 +08:00
|
|
|
started_at = time.monotonic()
|
2026-04-20 15:02:17 +08:00
|
|
|
try:
|
|
|
|
|
args = parse_args()
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(logging.INFO, "飞书同步动作开始", action=args.action)
|
2026-04-20 15:02:17 +08:00
|
|
|
config = build_app_config()
|
|
|
|
|
if config.env_path:
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(logging.INFO, "已加载环境变量文件", env_path=config.env_path)
|
2026-04-20 15:02:17 +08:00
|
|
|
else:
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(logging.WARNING, "未找到环境变量文件", fallback="当前Shell环境变量")
|
2026-04-20 15:02:17 +08:00
|
|
|
|
|
|
|
|
client = FeishuClient(config)
|
|
|
|
|
result: dict[str, object] = {}
|
|
|
|
|
|
|
|
|
|
if args.action in {"feishu", "all"}:
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(logging.INFO, "开始读取飞书Base信息")
|
2026-04-20 15:02:17 +08:00
|
|
|
result["feishu"] = run_feishu_summary(client)
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(
|
|
|
|
|
logging.INFO,
|
|
|
|
|
"飞书Base信息读取完成",
|
|
|
|
|
table_count=result["feishu"].get("table_count") if isinstance(result["feishu"], dict) else None,
|
|
|
|
|
)
|
2026-04-20 15:02:17 +08:00
|
|
|
if args.action in {"postgres", "all"}:
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(logging.INFO, "开始查询PostgreSQL")
|
2026-04-20 15:02:17 +08:00
|
|
|
result["postgres"] = query_postgres(config)
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(
|
|
|
|
|
logging.INFO,
|
|
|
|
|
"PostgreSQL查询完成",
|
|
|
|
|
row_count=result["postgres"].get("row_count") if isinstance(result["postgres"], dict) else None,
|
|
|
|
|
)
|
2026-04-20 15:02:17 +08:00
|
|
|
if args.action in {"sync-brand", "all"}:
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(logging.INFO, "开始同步品牌数据")
|
2026-04-20 15:02:17 +08:00
|
|
|
postgres_result = result.get("postgres")
|
|
|
|
|
result["sync_brand"] = run_sync_brand(config, client, postgres_result if isinstance(postgres_result, dict) else None)
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(
|
|
|
|
|
logging.INFO,
|
|
|
|
|
"品牌数据同步完成",
|
|
|
|
|
target_count=result["sync_brand"].get("target_count") if isinstance(result["sync_brand"], dict) else None,
|
|
|
|
|
source_row_count=result["sync_brand"].get("source_row_count") if isinstance(result["sync_brand"], dict) else None,
|
|
|
|
|
)
|
2026-04-20 15:02:17 +08:00
|
|
|
|
|
|
|
|
print(json.dumps(result, ensure_ascii=False, indent=2))
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(
|
|
|
|
|
logging.INFO,
|
|
|
|
|
"飞书同步动作完成",
|
|
|
|
|
action=args.action,
|
|
|
|
|
elapsed_ms=int((time.monotonic() - started_at) * 1000),
|
|
|
|
|
)
|
2026-04-20 15:02:17 +08:00
|
|
|
return 0
|
|
|
|
|
except Exception as exc: # noqa: BLE001
|
2026-04-24 19:00:07 +08:00
|
|
|
log_event(
|
|
|
|
|
logging.ERROR,
|
|
|
|
|
"飞书同步动作失败",
|
|
|
|
|
error=exc,
|
|
|
|
|
elapsed_ms=int((time.monotonic() - started_at) * 1000),
|
|
|
|
|
)
|
2026-04-20 15:02:17 +08:00
|
|
|
return 1
|