From 13f64fff9c4838b235da6477957985d66e7cd0c8 Mon Sep 17 00:00:00 2001 From: jpyoung3 <809608046@qq.com> Date: Mon, 14 Oct 2024 16:21:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=89=E8=A3=85=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=AF=B9=E4=BA=8E=E4=B8=8D=E5=AD=98=E7=9A=84tag=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=96=E6=9C=80=E5=A4=A7=E7=9A=84packageid=20(close?= =?UTF-8?q?d=20#2432)=20#=20Reviewed,=20transaction=20id:=2020560?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/backend/subscription/steps/adapter.py | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) 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"))