diff --git a/apps/backend/subscription/steps/adapter.py b/apps/backend/subscription/steps/adapter.py index 56e397578..c792cb2b6 100644 --- a/apps/backend/subscription/steps/adapter.py +++ b/apps/backend/subscription/steps/adapter.py @@ -281,6 +281,29 @@ def max_ids_by_key(self, contained_os_cpu_items: List[Dict[str, Any]]) -> List[i os_cpu__max_id_map[os_key] = item["id"] return list(os_cpu__max_id_map.values()) + def get_latest_package_ids(self, plugin_name: str, plugin_version: str): + # 先获取所有的 package + all_packages = models.Packages.objects.filter(project=plugin_name).values("id", "os", "cpu_arch", "version") + version_packages = {pkg["id"]: pkg for pkg in all_packages if pkg["version"] == plugin_version} + package_ids = set(version_packages.keys()) + + # 获取所有的 OS 和 CPU 架构组合 + for os in constants.PLUGIN_OS_TUPLE: + for cpu_arch in constants.CPU_TUPLE: + # 使用 any 函数来检查是否存在特定的 os 和 cpu_arch 组合,避免了多次查询 + if not any( + pkg["os"] == os and pkg["cpu_arch"] == cpu_arch + for pkg in all_packages + if pkg["version"] == plugin_version + ): + # 查找该 OS 和 CPU 架构的最大 ID + max_pkg_ids: List[int] = self.max_ids_by_key( + [pkg for pkg in all_packages if pkg["os"] == os and pkg["cpu_arch"] == cpu_arch] + ) + package_ids.update(max_pkg_ids) + + return list(package_ids) + def format2policy_packages_new( self, plugin_id: int, plugin_name: str, plugin_version: str, config_templates: List[Dict[str, Any]] ) -> List[Dict[str, Any]]: @@ -291,7 +314,8 @@ def format2policy_packages_new( if plugin_version != latest_flag or is_tag: # 如果 latest 是 tag,走取指定版本的逻辑 - packages = models.Packages.objects.filter(project=plugin_name, version=plugin_version) + pkg_ids = self.get_latest_package_ids(plugin_name, plugin_version) + packages = models.Packages.objects.filter(id__in=pkg_ids) else: max_pkg_ids: List[int] = self.max_ids_by_key( list(models.Packages.objects.filter(project=plugin_name).values("id", "os", "cpu_arch"))