fix: list nacos registrations across visible namespaces
This commit is contained in:
parent
52182a13ad
commit
17117b57b3
@ -233,7 +233,7 @@ def list_services(namespace_id: str) -> list[str]:
|
|||||||
{
|
{
|
||||||
"pageNo": str(page_no),
|
"pageNo": str(page_no),
|
||||||
"pageSize": str(NACOS_PAGE_SIZE),
|
"pageSize": str(NACOS_PAGE_SIZE),
|
||||||
"namespaceId": namespace_id,
|
"namespaceId": namespace_id or None,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -269,7 +269,7 @@ def list_instances(namespace_id: str, service_name: str, group_name: str) -> lis
|
|||||||
{
|
{
|
||||||
"serviceName": service_name,
|
"serviceName": service_name,
|
||||||
"groupName": group_name,
|
"groupName": group_name,
|
||||||
"namespaceId": namespace_id,
|
"namespaceId": namespace_id or None,
|
||||||
"healthyOnly": "false",
|
"healthyOnly": "false",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -300,8 +300,35 @@ def build_nacos_payload() -> dict[str, Any]:
|
|||||||
namespaces = list_namespaces()
|
namespaces = list_namespaces()
|
||||||
# 解析目标 namespace。
|
# 解析目标 namespace。
|
||||||
namespace_id, namespace_name = resolve_namespace(namespaces)
|
namespace_id, namespace_name = resolve_namespace(namespaces)
|
||||||
# 拉取目标 namespace 下所有服务。
|
|
||||||
raw_services = list_services(namespace_id)
|
# 先把命名空间归一化。
|
||||||
|
normalized_namespaces = [
|
||||||
|
{
|
||||||
|
"namespaceId": str(item.get("namespace") or item.get("namespaceId") or "").strip(),
|
||||||
|
"name": str(item.get("namespaceShowName") or item.get("namespace") or "").strip(),
|
||||||
|
"configCount": int(item.get("configCount") or 0),
|
||||||
|
"type": int(item.get("type") or 0),
|
||||||
|
"visible": True,
|
||||||
|
}
|
||||||
|
for item in namespaces
|
||||||
|
]
|
||||||
|
|
||||||
|
# 建立当前可见 namespace id 集合。
|
||||||
|
visible_ids = {item["namespaceId"] for item in normalized_namespaces}
|
||||||
|
# 建立当前可见 namespace 名称集合。
|
||||||
|
visible_names = {item["name"] for item in normalized_namespaces}
|
||||||
|
|
||||||
|
# 如果配置里的 namespace 不在可见列表里,就补一个兜底项。
|
||||||
|
if (namespace_id and namespace_id not in visible_ids) or (namespace_name and namespace_name not in visible_names):
|
||||||
|
normalized_namespaces.append(
|
||||||
|
{
|
||||||
|
"namespaceId": namespace_id,
|
||||||
|
"name": namespace_name,
|
||||||
|
"configCount": 0,
|
||||||
|
"type": 0,
|
||||||
|
"visible": False,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
# 归一化后的服务列表。
|
# 归一化后的服务列表。
|
||||||
services: list[dict[str, Any]] = []
|
services: list[dict[str, Any]] = []
|
||||||
@ -314,12 +341,17 @@ def build_nacos_payload() -> dict[str, Any]:
|
|||||||
# 统计全健康服务数量。
|
# 统计全健康服务数量。
|
||||||
healthy_service_total = 0
|
healthy_service_total = 0
|
||||||
|
|
||||||
|
# 逐个 namespace 读取服务和实例。
|
||||||
|
for namespace in normalized_namespaces:
|
||||||
|
# 当前 namespace 的服务列表。
|
||||||
|
raw_services = list_services(namespace["namespaceId"])
|
||||||
|
|
||||||
# 逐个服务拉实例详情。
|
# 逐个服务拉实例详情。
|
||||||
for raw_name in raw_services:
|
for raw_name in raw_services:
|
||||||
# 解析 group 和真正的 serviceName。
|
# 解析 group 和真正的 serviceName。
|
||||||
group_name, service_name = split_grouped_service_name(raw_name)
|
group_name, service_name = split_grouped_service_name(raw_name)
|
||||||
# 拉取当前服务实例。
|
# 拉取当前服务实例。
|
||||||
instance_items = list_instances(namespace_id, service_name, group_name)
|
instance_items = list_instances(namespace["namespaceId"], service_name, group_name)
|
||||||
# 归一化实例结构。
|
# 归一化实例结构。
|
||||||
normalized_instances: list[dict[str, Any]] = []
|
normalized_instances: list[dict[str, Any]] = []
|
||||||
|
|
||||||
@ -376,6 +408,8 @@ def build_nacos_payload() -> dict[str, Any]:
|
|||||||
# 追加当前服务结构。
|
# 追加当前服务结构。
|
||||||
services.append(
|
services.append(
|
||||||
{
|
{
|
||||||
|
"namespaceId": namespace["namespaceId"],
|
||||||
|
"namespaceName": namespace["name"],
|
||||||
"rawServiceName": raw_name,
|
"rawServiceName": raw_name,
|
||||||
"serviceName": service_name,
|
"serviceName": service_name,
|
||||||
"groupName": group_name,
|
"groupName": group_name,
|
||||||
@ -389,17 +423,6 @@ def build_nacos_payload() -> dict[str, Any]:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
# 命名空间列表也做一层归一化。
|
|
||||||
normalized_namespaces = [
|
|
||||||
{
|
|
||||||
"namespaceId": str(item.get("namespace") or item.get("namespaceId") or "").strip(),
|
|
||||||
"name": str(item.get("namespaceShowName") or item.get("namespace") or "").strip(),
|
|
||||||
"configCount": int(item.get("configCount") or 0),
|
|
||||||
"type": int(item.get("type") or 0),
|
|
||||||
}
|
|
||||||
for item in namespaces
|
|
||||||
]
|
|
||||||
|
|
||||||
# 返回完整 Nacos 监控结构。
|
# 返回完整 Nacos 监控结构。
|
||||||
return {
|
return {
|
||||||
"ok": True,
|
"ok": True,
|
||||||
@ -458,4 +481,3 @@ def get_nacos_payload() -> dict[str, Any]:
|
|||||||
|
|
||||||
# 返回缓存或最新构建结果。
|
# 返回缓存或最新构建结果。
|
||||||
return NACOS_CACHE.get_or_build(builder)
|
return NACOS_CACHE.get_or_build(builder)
|
||||||
|
|
||||||
|
|||||||
@ -226,7 +226,7 @@
|
|||||||
<header class="nacos-service-head">
|
<header class="nacos-service-head">
|
||||||
<div>
|
<div>
|
||||||
<h4>{{ service.serviceName }}</h4>
|
<h4>{{ service.serviceName }}</h4>
|
||||||
<p>{{ service.groupName }}</p>
|
<p>{{ service.namespaceName || 'public' }} · {{ service.groupName }}</p>
|
||||||
</div>
|
</div>
|
||||||
<span :class="['badge', service.level === 'ok' ? 'ok' : service.level === 'warn' ? 'warn' : 'bad']">
|
<span :class="['badge', service.level === 'ok' ? 'ok' : service.level === 'warn' ? 'warn' : 'bad']">
|
||||||
{{ service.instanceTotal }} / {{ service.healthyTotal }}
|
{{ service.instanceTotal }} / {{ service.healthyTotal }}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user