From c8b6e221439b6f3d3ede5d463b8015a5b69b7c69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E4=BF=8A=E7=94=B7?= Date: Mon, 5 Jan 2026 14:38:36 +0800 Subject: [PATCH] feat(home page): optimize the statistical interface --- api/app/controllers/__init__.py | 2 + api/app/repositories/home_page_repository.py | 86 ++++++++++++++++---- api/app/schemas/home_page_schema.py | 11 ++- api/app/services/home_page_service.py | 28 ++++--- 4 files changed, 97 insertions(+), 30 deletions(-) diff --git a/api/app/controllers/__init__.py b/api/app/controllers/__init__.py index e786fb65..0b07d0c9 100644 --- a/api/app/controllers/__init__.py +++ b/api/app/controllers/__init__.py @@ -34,6 +34,7 @@ from . import ( prompt_optimizer_controller, tool_controller, memory_forget_controller, + home_page_controller, ) from . import user_memory_controllers @@ -72,5 +73,6 @@ manager_router.include_router(prompt_optimizer_controller.router) manager_router.include_router(memory_reflection_controller.router) manager_router.include_router(tool_controller.router) manager_router.include_router(memory_forget_controller.router) +manager_router.include_router(home_page_controller.router) __all__ = ["manager_router"] diff --git a/api/app/repositories/home_page_repository.py b/api/app/repositories/home_page_repository.py index e37f1f00..888071ac 100644 --- a/api/app/repositories/home_page_repository.py +++ b/api/app/repositories/home_page_repository.py @@ -13,39 +13,73 @@ from app.models.app_model import App class HomePageRepository: @staticmethod - def get_model_statistics(db: Session, tenant_id: UUID, month_start: datetime) -> tuple[int, int]: + def get_model_statistics(db: Session, tenant_id: UUID, week_start: datetime) -> tuple[int, int, int, float]: """获取模型统计数据""" total_models = db.query(ModelConfig).filter( ModelConfig.tenant_id == tenant_id, ModelConfig.is_active == True ).count() - - new_models_this_month = db.query(ModelConfig).filter( + + total_llm = db.query(ModelConfig).filter( ModelConfig.tenant_id == tenant_id, ModelConfig.is_active == True, - ModelConfig.created_at >= month_start + ModelConfig.type == "llm" + ).count() + + total_embedding = db.query(ModelConfig).filter( + ModelConfig.tenant_id == tenant_id, + ModelConfig.is_active == True, + ModelConfig.type == "embedding" ).count() - return total_models, new_models_this_month + new_models_this_week = db.query(ModelConfig).filter( + ModelConfig.tenant_id == tenant_id, + ModelConfig.is_active == True, + ModelConfig.created_at >= week_start + ).count() + + if total_models == 0: + growth_rate = 0.0 + elif new_models_this_week == 0: + growth_rate = 0.0 + else: + last_week_total = total_models - new_models_this_week + if last_week_total == 0: + growth_rate = 100.0 + else: + growth_rate = round((new_models_this_week / last_week_total) * 100, 2) + + return total_models, total_llm, total_embedding, growth_rate @staticmethod - def get_workspace_statistics(db: Session, tenant_id: UUID, month_start: datetime) -> tuple[int, int]: + def get_workspace_statistics(db: Session, tenant_id: UUID, week_start: datetime) -> tuple[int, int, float]: """获取工作空间统计数据""" active_workspaces = db.query(Workspace).filter( Workspace.tenant_id == tenant_id, Workspace.is_active == True ).count() - new_workspaces_this_month = db.query(Workspace).filter( + new_workspaces_this_week = db.query(Workspace).filter( Workspace.tenant_id == tenant_id, Workspace.is_active == True, - Workspace.created_at >= month_start + Workspace.created_at >= week_start ).count() + + if active_workspaces == 0: + growth_rate = 0.0 + elif new_workspaces_this_week == 0: + growth_rate = 0.0 + else: + last_week_workspaces = active_workspaces - new_workspaces_this_week + if last_week_workspaces == 0: + growth_rate = 100.0 + else: + growth_rate = round((new_workspaces_this_week / last_week_workspaces) * 100, 2) - return active_workspaces, new_workspaces_this_month + return active_workspaces, new_workspaces_this_week, growth_rate @staticmethod - def get_user_statistics(db: Session, tenant_id: UUID, month_start: datetime) -> tuple[int, int]: + def get_user_statistics(db: Session, tenant_id: UUID, week_start: datetime) -> tuple[int, int, float]: """获取用户统计数据""" workspace_ids = db.query(Workspace.id).filter( Workspace.tenant_id == tenant_id, @@ -61,20 +95,31 @@ class HomePageRepository: App.status == "active" ).count() - new_users_this_month = db.query(EndUser).join( + new_users_this_week = db.query(EndUser).join( App, EndUser.app_id == App.id ).filter( App.workspace_id.in_(workspace_ids), App.is_active == True, App.status == "active", - EndUser.created_at >= month_start + EndUser.created_at >= week_start ).count() + + if total_users == 0: + growth_rate = 0.0 + elif new_users_this_week == 0: + growth_rate = 0.0 + else: + last_week_users = total_users - new_users_this_week + if last_week_users == 0: + growth_rate = 100.0 + else: + growth_rate = round((new_users_this_week / last_week_users) * 100, 2) - return total_users, new_users_this_month + return total_users, new_users_this_week, growth_rate @staticmethod - def get_app_statistics(db: Session, tenant_id: UUID, week_start: datetime) -> tuple[int, int]: + def get_app_statistics(db: Session, tenant_id: UUID, week_start: datetime) -> tuple[int, int, float]: """获取应用统计数据""" workspace_ids = db.query(Workspace.id).filter( Workspace.tenant_id == tenant_id, @@ -93,8 +138,19 @@ class HomePageRepository: App.status == "active", App.created_at >= week_start ).count() + + if running_apps == 0: + growth_rate = 0.0 + elif new_apps_this_week == 0: + growth_rate = 0.0 + else: + last_week_apps = running_apps - new_apps_this_week + if last_week_apps == 0: + growth_rate = 100.0 + else: + growth_rate = round((new_apps_this_week / last_week_apps) * 100, 2) - return running_apps, new_apps_this_week + return running_apps, new_apps_this_week, growth_rate @staticmethod def get_workspaces_with_counts(db: Session, tenant_id: UUID) -> tuple[list[Workspace], Dict[UUID, int], Dict[UUID, int]]: diff --git a/api/app/schemas/home_page_schema.py b/api/app/schemas/home_page_schema.py index de223e17..8fdc8144 100644 --- a/api/app/schemas/home_page_schema.py +++ b/api/app/schemas/home_page_schema.py @@ -8,13 +8,18 @@ from app.core.api_key_utils import datetime_to_timestamp class HomeStatistics(BaseModel): """首页统计数据""" total_models: int - new_models_this_month: int + total_llm: int + total_embedding: int + model_week_growth_rate: float active_workspaces: int - new_workspaces_this_month: int + new_workspaces_this_week: int + workspace_week_growth_rate: float total_users: int - new_users_this_month: int + new_users_this_week: int + user_week_growth_rate: float running_apps: int new_apps_this_week: int + app_week_growth_rate: float class WorkspaceInfo(BaseModel): """工作空间信息""" diff --git a/api/app/services/home_page_service.py b/api/app/services/home_page_service.py index 909da25f..87419cb2 100644 --- a/api/app/services/home_page_service.py +++ b/api/app/services/home_page_service.py @@ -12,36 +12,40 @@ class HomePageService: """获取首页统计数据""" # 计算时间范围 now = datetime.now() - month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) week_start = now - timedelta(days=now.weekday()) week_start = week_start.replace(hour=0, minute=0, second=0, microsecond=0) # 获取各项统计数据 - total_models, new_models_this_month = HomePageRepository.get_model_statistics( - db, tenant_id, month_start + total_models, total_llm, total_embedding, model_week_growth_rate = HomePageRepository.get_model_statistics( + db, tenant_id, week_start ) - active_workspaces, new_workspaces_this_month = HomePageRepository.get_workspace_statistics( - db, tenant_id, month_start + active_workspaces, new_workspaces_this_week, workspace_week_growth_rate = HomePageRepository.get_workspace_statistics( + db, tenant_id, week_start ) - total_users, new_users_this_month = HomePageRepository.get_user_statistics( - db, tenant_id, month_start + total_users, new_users_this_week, user_week_growth_rate = HomePageRepository.get_user_statistics( + db, tenant_id, week_start ) - running_apps, new_apps_this_week = HomePageRepository.get_app_statistics( + running_apps, new_apps_this_week, app_week_growth_rate = HomePageRepository.get_app_statistics( db, tenant_id, week_start ) return HomeStatistics( total_models=total_models, - new_models_this_month=new_models_this_month, + total_llm=total_llm, + total_embedding=total_embedding, + model_week_growth_rate=model_week_growth_rate, active_workspaces=active_workspaces, - new_workspaces_this_month=new_workspaces_this_month, + new_workspaces_this_week=new_workspaces_this_week, + workspace_week_growth_rate=workspace_week_growth_rate, total_users=total_users, - new_users_this_month=new_users_this_month, + new_users_this_week=new_users_this_week, + user_week_growth_rate=user_week_growth_rate, running_apps=running_apps, - new_apps_this_week=new_apps_this_week + new_apps_this_week=new_apps_this_week, + app_week_growth_rate=app_week_growth_rate ) @staticmethod