Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 安装插件对于不存的tag版本取最大的packageid (closed #2432) #2453

Open
wants to merge 1 commit into
base: v2.4.7-dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion apps/backend/subscription/steps/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand All @@ -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"))
Expand Down
Loading