Fix empty BGP route false positives and add batch alias UX.

Skip Dest/Network headers when Total routes is 0; capture direction from match groups; expose workbook compare/data/sheet stats and prefer batch aliases in compare/cutover pickers.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-09-22 10:58:57 +08:00
parent f321413dbf
commit 1fcd613bc6
16 changed files with 327 additions and 39 deletions

View file

@ -20,6 +20,24 @@ _ROUTE_RE = re.compile(
)
_DIR_RE = re.compile(r"(?i)\bneighbor\s+(in|out)\s+")
_NEI_RE = re.compile(r"(?i)\bneighbor\s+(?:in|out)\s+(\S+)")
_TOTAL_RE = re.compile(r"(?i)total\s+number\s+of\s+routes\s*:\s*(\d+)")
_HEADER_NETS = frozenset(
{
"network",
"dest",
"destination",
"next",
"hop",
"metric",
"locprf",
"loc_prf",
"intag",
"rtprf",
"tag",
"path",
"from",
}
)
def _detect_direction(command: str, params: dict[str, str] | None) -> str:
@ -36,15 +54,24 @@ def _detect_neighbor(command: str, params: dict[str, str] | None) -> str:
return m.group(1).strip() if m else ""
def _looks_like_prefix(net: str) -> bool:
tok = str(net or "").strip()
if not tok or tok.lower() in _HEADER_NETS:
return False
if re.fullmatch(r"\d{1,3}(?:\.\d{1,3}){3}/\d{1,2}", tok):
return True
# IPv6 prefix / bare address
if ":" in tok and re.search(r"[0-9A-Fa-f]:", tok):
return True
return False
def _split_rest(rest: str) -> tuple[str, str, str, str]:
"""Parse trailing Metric LocPrf Tag/RtPrf Path columns (some may be blank)."""
parts = str(rest or "").split()
if not parts:
return "", "", "", ""
# Last token(s) are AS path + origin; path ends with i|e|?
path = " ".join(parts)
metric = loc = tag = ""
# Heuristic: numeric-only leading fields are metric/loc/tag when present
nums: list[str] = []
path_parts: list[str] = []
for p in parts:
@ -62,6 +89,12 @@ def _split_rest(rest: str) -> tuple[str, str, str, str]:
return metric, loc, tag, path
def _empty_if_total_zero(raw_text: str) -> bool:
"""True when device reports Total number of routes: 0."""
m = _TOTAL_RE.search(str(raw_text or ""))
return bool(m and int(m.group(1)) == 0)
def _map_fsm_rows(
rows: list[dict[str, Any]],
*,
@ -74,7 +107,10 @@ def _map_fsm_rows(
seen: set[str] = set()
for r in rows:
net = row_get(r, "NETWORK", "network")
if not net or net.lower() == "network" or net in seen:
if not _looks_like_prefix(net) or net in seen:
continue
nh = row_get(r, "NEXT_HOP", "next_hop")
if str(nh or "").strip().lower() in _HEADER_NETS:
continue
seen.add(net)
path = row_get(r, "PATH", "path")
@ -85,7 +121,7 @@ def _map_fsm_rows(
"neighbor": neighbor[:128],
"direction": direction[:8],
"network": net[:128],
"next_hop": row_get(r, "NEXT_HOP", "next_hop")[:128],
"next_hop": nh[:128],
"metric": row_get(r, "METRIC", "metric")[:32],
"loc_prf": row_get(r, "LOC_PRF", "loc_prf")[:32],
"tag": row_get(r, "TAG", "RT_PRF", "tag")[:32],
@ -115,19 +151,21 @@ def _hand_parse(
if not line.strip():
continue
low = line.strip().lower()
if low.startswith("network") or "next hop" in low:
if low.startswith(("network", "dest ", "destination")):
continue
if low.startswith("status") or low.startswith("origin") or low.startswith("routes "):
if "next hop" in low or low.startswith("status") or low.startswith("origin"):
continue
if low.startswith("routes ") or low.startswith("current as"):
continue
if low.startswith("local ") or low.startswith("remote ") or low.startswith("total "):
continue
if low.startswith("route distinguisher"):
if low.startswith("route distinguisher") or low.startswith("valid ") or low.startswith("invalid "):
continue
m = _ROUTE_RE.match(line)
if not m:
continue
net = m.group("net")
if net in seen:
if not _looks_like_prefix(net) or net in seen:
continue
seen.add(net)
metric, loc, tag, path = _split_rest(m.group("rest"))
@ -165,6 +203,9 @@ def normalize_bgp_route(
vrf = _detect_vrf(command, params)
neighbor = _detect_neighbor(command, params)
direction = _detect_direction(command, params)
if _empty_if_total_zero(raw_text):
return []
tables = dict(fsm_tables or {})
if not any(tables.get(k) for k in RULE_KEYS):
platform = resolve_cli_platform(

View file

@ -965,7 +965,7 @@ def _zte_status_profiles() -> list[ParseProfile]:
parser_id="bgp_route",
title="BGP VPNv4 Neighbor In",
command_template="show bgp vpnv4 unicast neighbor in <neighbor> | one-line",
match=r"(?i)^\s*show\s+bgp\s+vpnv4\s+unicast\s+neighbor\s+in\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$",
match=r"(?i)^\s*show\s+bgp\s+vpnv4\s+unicast\s+neighbor\s+(?P<direction>in)\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$",
textfsm_command="show bgp vpnv4 unicast neighbor in",
description="Routes learned from VPNv4 neighbor; summary aux for peer state.",
placeholders=[_BGP_NEIGHBOR_VPNV4],
@ -990,7 +990,7 @@ def _zte_status_profiles() -> list[ParseProfile]:
parser_id="bgp_route",
title="BGP VPNv4 Neighbor Out",
command_template="show bgp vpnv4 unicast neighbor out <neighbor> | one-line",
match=r"(?i)^\s*show\s+bgp\s+vpnv4\s+unicast\s+neighbor\s+out\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$",
match=r"(?i)^\s*show\s+bgp\s+vpnv4\s+unicast\s+neighbor\s+(?P<direction>out)\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$",
textfsm_command="show bgp vpnv4 unicast neighbor out",
description="Routes advertised to VPNv4 neighbor (large; bind neighbor).",
placeholders=[_BGP_NEIGHBOR_VPNV4],
@ -1016,7 +1016,7 @@ def _zte_status_profiles() -> list[ParseProfile]:
parser_id="bgp_route",
title="BGP VPNv6 Neighbor In",
command_template="show bgp vpnv6 unicast neighbor in <neighbor> | one-line",
match=r"(?i)^\s*show\s+bgp\s+vpnv6\s+unicast\s+neighbor\s+in\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$",
match=r"(?i)^\s*show\s+bgp\s+vpnv6\s+unicast\s+neighbor\s+(?P<direction>in)\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$",
textfsm_command="show bgp vpnv6 unicast neighbor in",
description="Routes learned from VPNv6 neighbor; summary aux for peer state.",
placeholders=[_BGP_NEIGHBOR_VPNV6],
@ -1041,7 +1041,7 @@ def _zte_status_profiles() -> list[ParseProfile]:
parser_id="bgp_route",
title="BGP VPNv6 Neighbor Out",
command_template="show bgp vpnv6 unicast neighbor out <neighbor> | one-line",
match=r"(?i)^\s*show\s+bgp\s+vpnv6\s+unicast\s+neighbor\s+out\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$",
match=r"(?i)^\s*show\s+bgp\s+vpnv6\s+unicast\s+neighbor\s+(?P<direction>out)\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$",
textfsm_command="show bgp vpnv6 unicast neighbor out",
description="Routes advertised to VPNv6 neighbor (large; bind neighbor).",
placeholders=[_BGP_NEIGHBOR_VPNV6],
@ -1069,7 +1069,7 @@ def _zte_status_profiles() -> list[ParseProfile]:
command_template="show bgp vpnv4 unicast vrf <vrf> neighbor in <neighbor> | one-line",
match=(
r"(?i)^\s*show\s+bgp\s+vpnv4\s+unicast\s+vrf\s+(?P<vrf>\S+)\s+"
r"neighbor\s+in\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$"
r"neighbor\s+(?P<direction>in)\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$"
),
textfsm_command="show bgp vpnv4 unicast neighbor in",
description="Per-VRF CE peer routes; discover (vrf,neighbor) from BGP peer intent.",
@ -1097,7 +1097,7 @@ def _zte_status_profiles() -> list[ParseProfile]:
command_template="show bgp vpnv4 unicast vrf <vrf> neighbor out <neighbor> | one-line",
match=(
r"(?i)^\s*show\s+bgp\s+vpnv4\s+unicast\s+vrf\s+(?P<vrf>\S+)\s+"
r"neighbor\s+out\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$"
r"neighbor\s+(?P<direction>out)\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$"
),
textfsm_command="show bgp vpnv4 unicast neighbor out",
description="Per-VRF CE peer advertised routes; discover pairs from BGP peer intent.",
@ -1126,7 +1126,7 @@ def _zte_status_profiles() -> list[ParseProfile]:
command_template="show bgp vpnv6 unicast vrf <vrf> neighbor in <neighbor> | one-line",
match=(
r"(?i)^\s*show\s+bgp\s+vpnv6\s+unicast\s+vrf\s+(?P<vrf>\S+)\s+"
r"neighbor\s+in\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$"
r"neighbor\s+(?P<direction>in)\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$"
),
textfsm_command="show bgp vpnv6 unicast neighbor in",
description="Per-VRF IPv6 CE peer routes; discover pairs from BGP peer intent.",
@ -1154,7 +1154,7 @@ def _zte_status_profiles() -> list[ParseProfile]:
command_template="show bgp vpnv6 unicast vrf <vrf> neighbor out <neighbor> | one-line",
match=(
r"(?i)^\s*show\s+bgp\s+vpnv6\s+unicast\s+vrf\s+(?P<vrf>\S+)\s+"
r"neighbor\s+out\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$"
r"neighbor\s+(?P<direction>out)\s+(?P<neighbor>\S+)(?:\s*\|\s*one-line)?\s*$"
),
textfsm_command="show bgp vpnv6 unicast neighbor out",
description="Per-VRF IPv6 CE advertised routes; discover pairs from BGP peer intent.",

View file

@ -44,6 +44,7 @@ def apply_biz_state_schema(conn: Connection) -> None:
"ALTER TABLE biz_state_task ADD COLUMN IF NOT EXISTS daily_keep_count INTEGER DEFAULT 10",
"ALTER TABLE biz_state_batch ADD COLUMN IF NOT EXISTS is_baseline BOOLEAN DEFAULT FALSE",
"ALTER TABLE biz_state_batch ADD COLUMN IF NOT EXISTS baseline_marked_at TIMESTAMP",
"ALTER TABLE biz_state_batch ADD COLUMN IF NOT EXISTS alias VARCHAR(128) DEFAULT ''",
"CREATE INDEX IF NOT EXISTS ix_biz_state_batch_is_baseline ON biz_state_batch (is_baseline)",
"ALTER TABLE biz_migration_batch ADD COLUMN IF NOT EXISTS accept_status VARCHAR(32) DEFAULT 'none'",
"ALTER TABLE biz_migration_batch ADD COLUMN IF NOT EXISTS accept_run_id VARCHAR(64) DEFAULT ''",

View file

@ -466,6 +466,7 @@ def _batch_list_item(b: BizStateBatch, protect: dict[str, Any]) -> dict[str, Any
"message": b.message,
"ne_name": b.ne_name or "",
"ne_id": b.ne_id or "",
"alias": str(getattr(b, "alias", "") or ""),
"started_at": b.started_at.isoformat() + "Z" if b.started_at else None,
"ended_at": b.ended_at.isoformat() + "Z" if b.ended_at else None,
"is_baseline": bool(getattr(b, "is_baseline", False)),
@ -510,6 +511,15 @@ def set_batch_baseline(db: Session, batch_id: str, *, marked: bool) -> dict[str,
return _batch_list_item(b, batch_protect_info(db, batch_id))
def set_batch_alias(db: Session, batch_id: str, *, alias: str) -> dict[str, Any]:
b = db.get(BizStateBatch, batch_id)
if not b:
raise HTTPException(status_code=404, detail="batch_not_found")
b.alias = str(alias or "").strip()[:128]
db.commit()
return _batch_list_item(b, batch_protect_info(db, batch_id))
def delete_batch(db: Session, batch_id: str) -> dict[str, Any]:
b = db.get(BizStateBatch, batch_id)
if not b:
@ -702,6 +712,8 @@ def get_batch(db: Session, batch_id: str) -> dict[str, Any]:
}
for mid in sheets_order
]
sheet_count = len(sheets)
sheets_with_data = sum(1 for s in sheets if int(s.get("row_count") or 0) > 0)
return {
"id": b.id,
@ -710,6 +722,7 @@ def get_batch(db: Session, batch_id: str) -> dict[str, Any]:
"command_count": b.command_count,
"row_count": b.row_count,
"message": b.message,
"alias": str(getattr(b, "alias", "") or ""),
"started_at": b.started_at.isoformat() + "Z" if b.started_at else None,
"ended_at": b.ended_at.isoformat() + "Z" if b.ended_at else None,
"is_baseline": bool(getattr(b, "is_baseline", False)),
@ -718,6 +731,10 @@ def get_batch(db: Session, batch_id: str) -> dict[str, Any]:
else None,
"protected": bool(protect.get("protected")),
"protect_reasons": list(protect.get("reasons") or []),
# Workbook header: 对比项 / 有数据 / 表格
"compare_item_count": sheet_count,
"sheets_with_data": sheets_with_data,
"sheet_count": sheet_count,
"commands": cmd_payload,
"sheets": sheets,
}