From a68713533fe086eb7ac37caa8e04c89bc998b620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E9=B8=BF=E5=AE=87?= Date: Fri, 7 Feb 2025 16:28:50 +0800 Subject: [PATCH 1/5] restructure code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史鸿宇 --- apps/entities/response_data.py | 2 +- apps/manager/appcenter.py | 81 +++++++++++++++++----------------- apps/routers/appcenter.py | 2 +- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/apps/entities/response_data.py b/apps/entities/response_data.py index b3d26bf1..cc685823 100644 --- a/apps/entities/response_data.py +++ b/apps/entities/response_data.py @@ -289,7 +289,7 @@ class GetAppListMsg(BaseModel): """GET /api/app Result数据结构""" page_number: int = Field(..., alias="currentPage", description="当前页码") - app_count: int = Field(..., alias="total", description="总页数") + app_count: int = Field(..., alias="totalApps", description="总应用数") applications: list[AppCenterCardItem] = Field(..., description="应用列表") diff --git a/apps/manager/appcenter.py b/apps/manager/appcenter.py index aa228967..483bcc5e 100644 --- a/apps/manager/appcenter.py +++ b/apps/manager/appcenter.py @@ -325,6 +325,46 @@ class AppCenterManager: LOGGER.error(f"[AppCenterManager] Delete app failed: {e}") return False + @staticmethod + async def update_recent_app(user_sub: str, app_id: str) -> bool: + """更新用户的最近使用应用列表 + + :param user_sub: 用户唯一标识 + :param app_id: 应用唯一标识 + :return: 更新是否成功 + """ + try: + # 获取 user 集合 + user_collection = MongoDB.get_collection("user") + + # 获取当前时间戳 + current_time = round(datetime.now(tz=timezone.utc).timestamp(), 3) + + # 更新用户的 app_usage 字段 + result = await user_collection.update_one( + {"_id": user_sub}, # 查询条件 + { + "$set": { + f"app_usage.{app_id}.last_used": current_time, # 更新最后使用时间 + }, + "$inc": { + f"app_usage.{app_id}.count": 1, # 增加使用次数 + }, + }, + upsert=True, # 如果 app_usage 字段或 app_id 不存在,则创建 + ) + + # 检查更新是否成功 + if result.modified_count > 0 or result.upserted_id is not None: + LOGGER.info(f"[AppCenterManager] Updated recent app for user {user_sub}: {app_id}") + return True + LOGGER.warning(f"[AppCenterManager] No changes made for user {user_sub}") + return False + + except Exception as e: + LOGGER.error(f"[AppCenterManager] Failed to update recent app: {e}") + return False + @staticmethod def _build_filters( base_filters: dict[str, Any], @@ -377,44 +417,3 @@ class AppCenterManager: except Exception as e: LOGGER.info(f"[AppCenterManager] Get favorite app ids by user_sub failed: {e}") return [] - - @staticmethod - async def update_recent_app(user_sub: str, app_id: str) -> bool: - """更新用户的最近使用应用列表 - - :param user_sub: 用户唯一标识 - :param app_id: 应用唯一标识 - :return: 更新是否成功 - """ - try: - # 获取 user 集合 - user_collection = MongoDB.get_collection("user") - - # 获取当前时间戳 - current_time = round(datetime.now(tz=timezone.utc).timestamp(), 3) - - # 更新用户的 app_usage 字段 - result = await user_collection.update_one( - {"_id": user_sub}, # 查询条件 - { - "$set": { - f"app_usage.{app_id}.last_used": current_time, # 更新最后使用时间 - }, - "$inc": { - f"app_usage.{app_id}.count": 1, # 增加使用次数 - }, - }, - upsert=True, # 如果 app_usage 字段或 app_id 不存在,则创建 - ) - - # 检查更新是否成功 - if result.modified_count > 0 or result.upserted_id is not None: - LOGGER.info(f"[AppCenterManager] Updated recent app for user {user_sub}: {app_id}") - return True - else: - LOGGER.warning(f"[AppCenterManager] No changes made for user {user_sub}") - return False - - except Exception as e: - LOGGER.error(f"[AppCenterManager] Failed to update recent app: {e}") - return False \ No newline at end of file diff --git a/apps/routers/appcenter.py b/apps/routers/appcenter.py index d32c7559..5285c521 100644 --- a/apps/routers/appcenter.py +++ b/apps/routers/appcenter.py @@ -75,7 +75,7 @@ async def get_applications( # noqa: ANN201, PLR0913 message="查询成功", result=GetAppListMsg( currentPage=page, - total=total_apps, + totalApps=total_apps, applications=app_cards, ), ).model_dump(exclude_none=True, by_alias=True)) -- Gitee From 53c73782f512cfa668f95425a3158cb169320bf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E9=B8=BF=E5=AE=87?= Date: Fri, 7 Feb 2025 16:41:28 +0800 Subject: [PATCH 2/5] fix: clean code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史鸿宇 --- apps/entities/flow.py | 4 +- apps/manager/appcenter.py | 87 +++++++++++++++++++++++++++++++-------- apps/routers/appcenter.py | 1 - 3 files changed, 72 insertions(+), 20 deletions(-) diff --git a/apps/entities/flow.py b/apps/entities/flow.py index 554cefb9..ef54ccd4 100644 --- a/apps/entities/flow.py +++ b/apps/entities/flow.py @@ -151,8 +151,10 @@ class ServiceApiSpec(BaseModel): path: str = Field(description="OpenAPI文件路径") hash: str = Field(description="OpenAPI文件的hash值") + class FlowConfig(BaseModel): """Flow的配置信息 用于前期调试使用""" + app_id: str flow_id: str - flow_config: Flow \ No newline at end of file + flow_config: Flow diff --git a/apps/manager/appcenter.py b/apps/manager/appcenter.py index 483bcc5e..e530cbce 100644 --- a/apps/manager/appcenter.py +++ b/apps/manager/appcenter.py @@ -39,7 +39,15 @@ class AppCenterManager: page: int, page_size: int, ) -> tuple[list[AppCenterCardItem], int]: - """获取所有应用列表""" + """获取所有应用列表 + + :param user_sub: 用户唯一标识 + :param search_type: 搜索类型 + :param keyword: 搜索关键字 + :param page: 页码 + :param page_size: 每页条数 + :return: 应用列表, 总应用数 + """ try: # 构建基础搜索条件 filters: dict[str, Any] = {} @@ -96,7 +104,15 @@ class AppCenterManager: page: int, page_size: int, ) -> tuple[list[AppCenterCardItem], int]: - """获取用户应用列表""" + """获取用户应用列表 + + :param user_sub: 用户唯一标识 + :param search_type: 搜索类型 + :param keyword: 搜索关键词 + :param page: 页码 + :param page_size: 每页数量 + :return: 应用列表, 总应用数 + """ try: # 搜索条件 base_filter = {"author": user_sub} @@ -130,7 +146,15 @@ class AppCenterManager: page: int, page_size: int, ) -> tuple[list[AppCenterCardItem], int]: - """获取用户收藏的应用列表""" + """获取用户收藏的应用列表 + + :param user_sub: 用户唯一标识 + :param search_type: 搜索类型 + :param keyword: 搜索关键词 + :param page: 页码 + :param page_size: 每页数量 + :return: 应用列表,总应用数 + """ try: fav_app = await AppCenterManager._get_favorite_app_ids_by_user(user_sub) # 搜索条件 @@ -163,11 +187,16 @@ class AppCenterManager: @staticmethod async def fetch_app_data_by_id(app_id: str) -> Optional[AppPool]: - """根据应用ID获取应用元数据""" + """根据应用ID获取应用元数据 + + :param app_id: 应用ID + :return: 应用元数据 + """ try: app_collection = MongoDB.get_collection("app") db_data = await app_collection.find_one({"_id": app_id}) if not db_data: + LOGGER.warning(f"[AppCenterManager] No data found for app_id: {app_id}") return None return AppPool.model_validate(db_data) except Exception as e: @@ -176,7 +205,12 @@ class AppCenterManager: @staticmethod async def create_app(user_sub: str, data: AppData) -> Optional[str]: - """创建应用""" + """创建应用 + + :param user_sub: 用户唯一标识 + :param data: 应用数据 + :return: 应用ID + """ app_id = str(uuid.uuid4()) app = AppPool( _id=app_id, @@ -202,7 +236,12 @@ class AppCenterManager: @staticmethod async def update_app(app_id: str, data: AppData) -> bool: - """更新应用""" + """更新应用 + + :param app_id: 应用唯一标识 + :param data: 应用数据 + :return: 是否成功 + """ try: app_collection = MongoDB.get_collection("app") app_data = AppPool.model_validate(await app_collection.find_one({"_id": app_id})) @@ -232,7 +271,11 @@ class AppCenterManager: @staticmethod async def publish_app(app_id: str) -> bool: - """发布应用""" + """发布应用 + + :param app_id: 应用唯一标识 + :return: 是否成功 + """ try: app_collection = MongoDB.get_collection("app") await app_collection.update_one( @@ -246,7 +289,13 @@ class AppCenterManager: @staticmethod async def modify_favorite_app(app_id: str, user_sub: str, *, favorited: bool) -> ModFavAppFlag: - """修改收藏状态""" + """修改收藏状态 + + :param app_id: 应用唯一标识 + :param user_sub: 用户唯一标识 + :param favorited: 是否收藏 + :return: 修改结果 + """ try: app_collection = MongoDB.get_collection("app") db_data = await app_collection.find_one({"_id": app_id}) @@ -281,7 +330,12 @@ class AppCenterManager: @staticmethod async def get_recently_used_apps(count: int, user_sub: str) -> Optional[RecentAppList]: - """获取用户最近使用的应用列表""" + """获取用户最近使用的应用列表 + + :param count: 应用数量 + :param user_sub: 用户唯一标识 + :return: 最近使用的应用列表 + """ try: user_collection = MongoDB.get_collection("user") app_collection = MongoDB.get_collection("app") @@ -308,7 +362,12 @@ class AppCenterManager: @staticmethod async def delete_app(app_id: str, user_sub: str) -> bool: - """删除应用""" + """删除应用 + + :param app_id: 应用唯一标识 + :param user_sub: 用户唯一标识 + :return: 删除是否成功 + """ try: async with MongoDB.get_session() as session, await session.start_transaction(): app_collection = MongoDB.get_collection("app") @@ -334,13 +393,8 @@ class AppCenterManager: :return: 更新是否成功 """ try: - # 获取 user 集合 user_collection = MongoDB.get_collection("user") - - # 获取当前时间戳 current_time = round(datetime.now(tz=timezone.utc).timestamp(), 3) - - # 更新用户的 app_usage 字段 result = await user_collection.update_one( {"_id": user_sub}, # 查询条件 { @@ -353,14 +407,11 @@ class AppCenterManager: }, upsert=True, # 如果 app_usage 字段或 app_id 不存在,则创建 ) - - # 检查更新是否成功 if result.modified_count > 0 or result.upserted_id is not None: LOGGER.info(f"[AppCenterManager] Updated recent app for user {user_sub}: {app_id}") return True LOGGER.warning(f"[AppCenterManager] No changes made for user {user_sub}") return False - except Exception as e: LOGGER.error(f"[AppCenterManager] Failed to update recent app: {e}") return False diff --git a/apps/routers/appcenter.py b/apps/routers/appcenter.py index 5285c521..630daf03 100644 --- a/apps/routers/appcenter.py +++ b/apps/routers/appcenter.py @@ -5,7 +5,6 @@ Copyright (c) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved. from typing import Annotated, Optional, Union from fastapi import APIRouter, Body, Depends, Path, Query, status -from fastapi.requests import HTTPConnection from fastapi.responses import JSONResponse from apps.dependency.csrf import verify_csrf_token -- Gitee From b2764dcf8995c165ec75ee078f073c7a08d5b3e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E9=B8=BF=E5=AE=87?= Date: Thu, 13 Feb 2025 15:45:53 +0800 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E4=BB=A5=E4=BF=AE=E5=A4=8D=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=94=B6=E8=97=8F=E7=9A=84=E5=BA=94=E7=94=A8?= =?UTF-8?q?ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史鸿宇 --- apps/manager/appcenter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/manager/appcenter.py b/apps/manager/appcenter.py index e530cbce..10a07486 100644 --- a/apps/manager/appcenter.py +++ b/apps/manager/appcenter.py @@ -463,7 +463,7 @@ class AppCenterManager: """获取用户收藏的应用ID""" try: app_collection = MongoDB.get_collection("app") - cursor = app_collection.find({"favorites": user_sub}) + cursor = app_collection.find({"favorites": {"$in": [user_sub]}}) return [AppPool.model_validate(doc).id async for doc in cursor] except Exception as e: LOGGER.info(f"[AppCenterManager] Get favorite app ids by user_sub failed: {e}") -- Gitee From f7d5fe0924240af7f4fada97caba0f88e2d513b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E9=B8=BF=E5=AE=87?= Date: Fri, 14 Feb 2025 16:38:42 +0800 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0AppLink=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E4=B8=AD=E7=9A=84url=E5=AD=97=E6=AE=B5=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E4=B8=BAstr=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=AD=A3=E5=88=99=E8=A1=A8=E8=BE=BE=E5=BC=8F=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史鸿宇 --- apps/entities/flow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/entities/flow.py b/apps/entities/flow.py index ef54ccd4..5b2075b6 100644 --- a/apps/entities/flow.py +++ b/apps/entities/flow.py @@ -122,7 +122,7 @@ class AppLink(BaseModel): """App的相关链接""" title: str = Field(description="链接标题") - url: HttpUrl = Field(..., description="链接地址") + url: str = Field(..., description="链接地址", pattern=r"^(https|http)://.*$") class Permission(BaseModel): -- Gitee From a7bf4479b74e16d264ad7b9ee62f4114220d3b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E9=B8=BF=E5=AE=87?= Date: Fri, 14 Feb 2025 18:52:38 +0800 Subject: [PATCH 5/5] fix: clean code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史鸿宇 --- apps/manager/appcenter.py | 8 +++----- apps/manager/flow.py | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/apps/manager/appcenter.py b/apps/manager/appcenter.py index 10a07486..92683afd 100644 --- a/apps/manager/appcenter.py +++ b/apps/manager/appcenter.py @@ -2,7 +2,6 @@ Copyright (c) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved. """ -from datetime import datetime, timezone import uuid from datetime import datetime, timezone from enum import Enum @@ -64,15 +63,15 @@ class AppCenterManager: filters = { "$or": [ {"author": user_sub}, - {"published": True} - ] + {"published": True}, + ], } # 如果有关键词且是按作者搜索,额外添加关键词过滤 if keyword and search_type == SearchType.AUTHOR: filters["$and"] = [ filters["$or"], - {"author": {"$regex": keyword, "$options": "i"}} + {"author": {"$regex": keyword, "$options": "i"}}, ] # 执行应用搜索 @@ -162,7 +161,6 @@ class AppCenterManager: "_id": {"$in": fav_app}, "published": True, } - print(base_filter) filters: dict[str, Any] = AppCenterManager._build_filters( base_filter, search_type, diff --git a/apps/manager/flow.py b/apps/manager/flow.py index 29be1d23..d2b302a8 100644 --- a/apps/manager/flow.py +++ b/apps/manager/flow.py @@ -217,7 +217,7 @@ class FlowManager: nodes=[], edges=[], createdAt=flow_record["created_at"], - debug=flow_config['debug'], + debug=flow_config["debug"], ) for node_config in flow_config["steps"]: node_item = NodeItem( @@ -230,7 +230,7 @@ class FlowManager: type=node_config["type"], parameters=node_config["params"], position=PositionItem( - x=node_config['pos']['x'], y=node_config['pos']['y']), + x=node_config["pos"]["x"], y=node_config["pos"]["y"]), ) flow_item.nodes.append(node_item) -- Gitee