mirror of
https://github.com/dptech-corp/Uni-Lab-OS.git
synced 2025-12-17 21:11:12 +00:00
correct remove_resource stats
This commit is contained in:
@@ -2,7 +2,7 @@ import traceback
|
||||
import uuid
|
||||
from pydantic import BaseModel, field_serializer, field_validator
|
||||
from pydantic import Field
|
||||
from typing import List, Tuple, Any, Dict, Literal, Optional, cast, TYPE_CHECKING
|
||||
from typing import List, Tuple, Any, Dict, Literal, Optional, cast, TYPE_CHECKING, Union
|
||||
|
||||
from unilabos.utils.log import logger
|
||||
|
||||
@@ -933,7 +933,7 @@ class DeviceNodeResourceTracker(object):
|
||||
"""
|
||||
递归遍历资源树,更新所有节点的uuid
|
||||
|
||||
Args:0
|
||||
Args:
|
||||
resource: 资源对象(可以是dict或实例)
|
||||
uuid_map: uuid映射字典,{old_uuid: new_uuid}
|
||||
|
||||
@@ -958,6 +958,27 @@ class DeviceNodeResourceTracker(object):
|
||||
|
||||
return self._traverse_and_process(resource, process)
|
||||
|
||||
def loop_gather_uuid(self, resource) -> List[str]:
|
||||
"""
|
||||
递归遍历资源树,收集所有节点的uuid
|
||||
|
||||
Args:
|
||||
resource: 资源对象(可以是dict或实例)
|
||||
|
||||
Returns:
|
||||
收集到的uuid列表
|
||||
"""
|
||||
uuid_list = []
|
||||
|
||||
def process(res):
|
||||
current_uuid = self._get_resource_attr(res, "uuid", "unilabos_uuid")
|
||||
if current_uuid:
|
||||
uuid_list.append(current_uuid)
|
||||
return 0
|
||||
|
||||
self._traverse_and_process(resource, process)
|
||||
return uuid_list
|
||||
|
||||
def _collect_uuid_mapping(self, resource):
|
||||
"""
|
||||
递归收集资源的 uuid 映射到 uuid_to_resources
|
||||
@@ -978,7 +999,7 @@ class DeviceNodeResourceTracker(object):
|
||||
|
||||
self._traverse_and_process(resource, process)
|
||||
|
||||
def _remove_uuid_mapping(self, resource):
|
||||
def _remove_uuid_mapping(self, resource) -> int:
|
||||
"""
|
||||
递归清除资源的 uuid 映射
|
||||
|
||||
@@ -991,9 +1012,10 @@ class DeviceNodeResourceTracker(object):
|
||||
if current_uuid and current_uuid in self.uuid_to_resources:
|
||||
self.uuid_to_resources.pop(current_uuid)
|
||||
logger.debug(f"移除资源UUID映射: {current_uuid} -> {res}")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
self._traverse_and_process(resource, process)
|
||||
return self._traverse_and_process(resource, process)
|
||||
|
||||
def parent_resource(self, resource):
|
||||
if id(resource) in self.resource2parent_resource:
|
||||
@@ -1048,13 +1070,12 @@ class DeviceNodeResourceTracker(object):
|
||||
removed = True
|
||||
break
|
||||
|
||||
if not removed:
|
||||
# 递归清除uuid映射
|
||||
count = self._remove_uuid_mapping(resource)
|
||||
if not count:
|
||||
logger.warning(f"尝试移除不存在的资源: {resource}")
|
||||
return False
|
||||
|
||||
# 递归清除uuid映射
|
||||
self._remove_uuid_mapping(resource)
|
||||
|
||||
# 清除 resource2parent_resource 中与该资源相关的映射
|
||||
# 需要清除:1) 该资源作为 key 的映射 2) 该资源作为 value 的映射
|
||||
keys_to_remove = []
|
||||
@@ -1077,7 +1098,9 @@ class DeviceNodeResourceTracker(object):
|
||||
self.uuid_to_resources.clear()
|
||||
self.resource2parent_resource.clear()
|
||||
|
||||
def figure_resource(self, query_resource, try_mode=False):
|
||||
def figure_resource(
|
||||
self, query_resource: Union[List[Union[dict, "PLRResource"]], dict, "PLRResource"], try_mode=False
|
||||
) -> Union[List[Union[dict, "PLRResource", List[Union[dict, "PLRResource"]]]], dict, "PLRResource"]:
|
||||
if isinstance(query_resource, list):
|
||||
return [self.figure_resource(r, try_mode) for r in query_resource]
|
||||
elif (
|
||||
|
||||
Reference in New Issue
Block a user