feat(home page): optimize the statistical interface

This commit is contained in:
谢俊男
2026-01-05 14:38:36 +08:00
parent a07e151c95
commit c8b6e22143
4 changed files with 97 additions and 30 deletions

View File

@@ -34,6 +34,7 @@ from . import (
prompt_optimizer_controller, prompt_optimizer_controller,
tool_controller, tool_controller,
memory_forget_controller, memory_forget_controller,
home_page_controller,
) )
from . import user_memory_controllers 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(memory_reflection_controller.router)
manager_router.include_router(tool_controller.router) manager_router.include_router(tool_controller.router)
manager_router.include_router(memory_forget_controller.router) manager_router.include_router(memory_forget_controller.router)
manager_router.include_router(home_page_controller.router)
__all__ = ["manager_router"] __all__ = ["manager_router"]

View File

@@ -13,39 +13,73 @@ from app.models.app_model import App
class HomePageRepository: class HomePageRepository:
@staticmethod @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( total_models = db.query(ModelConfig).filter(
ModelConfig.tenant_id == tenant_id, ModelConfig.tenant_id == tenant_id,
ModelConfig.is_active == True ModelConfig.is_active == True
).count() ).count()
new_models_this_month = db.query(ModelConfig).filter( total_llm = db.query(ModelConfig).filter(
ModelConfig.tenant_id == tenant_id, ModelConfig.tenant_id == tenant_id,
ModelConfig.is_active == True, 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() ).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 @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( active_workspaces = db.query(Workspace).filter(
Workspace.tenant_id == tenant_id, Workspace.tenant_id == tenant_id,
Workspace.is_active == True Workspace.is_active == True
).count() ).count()
new_workspaces_this_month = db.query(Workspace).filter( new_workspaces_this_week = db.query(Workspace).filter(
Workspace.tenant_id == tenant_id, Workspace.tenant_id == tenant_id,
Workspace.is_active == True, Workspace.is_active == True,
Workspace.created_at >= month_start Workspace.created_at >= week_start
).count() ).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 @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_ids = db.query(Workspace.id).filter(
Workspace.tenant_id == tenant_id, Workspace.tenant_id == tenant_id,
@@ -61,20 +95,31 @@ class HomePageRepository:
App.status == "active" App.status == "active"
).count() ).count()
new_users_this_month = db.query(EndUser).join( new_users_this_week = db.query(EndUser).join(
App, App,
EndUser.app_id == App.id EndUser.app_id == App.id
).filter( ).filter(
App.workspace_id.in_(workspace_ids), App.workspace_id.in_(workspace_ids),
App.is_active == True, App.is_active == True,
App.status == "active", App.status == "active",
EndUser.created_at >= month_start EndUser.created_at >= week_start
).count() ).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 @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_ids = db.query(Workspace.id).filter(
Workspace.tenant_id == tenant_id, Workspace.tenant_id == tenant_id,
@@ -93,8 +138,19 @@ class HomePageRepository:
App.status == "active", App.status == "active",
App.created_at >= week_start App.created_at >= week_start
).count() ).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 @staticmethod
def get_workspaces_with_counts(db: Session, tenant_id: UUID) -> tuple[list[Workspace], Dict[UUID, int], Dict[UUID, int]]: def get_workspaces_with_counts(db: Session, tenant_id: UUID) -> tuple[list[Workspace], Dict[UUID, int], Dict[UUID, int]]:

View File

@@ -8,13 +8,18 @@ from app.core.api_key_utils import datetime_to_timestamp
class HomeStatistics(BaseModel): class HomeStatistics(BaseModel):
"""首页统计数据""" """首页统计数据"""
total_models: int total_models: int
new_models_this_month: int total_llm: int
total_embedding: int
model_week_growth_rate: float
active_workspaces: int active_workspaces: int
new_workspaces_this_month: int new_workspaces_this_week: int
workspace_week_growth_rate: float
total_users: int total_users: int
new_users_this_month: int new_users_this_week: int
user_week_growth_rate: float
running_apps: int running_apps: int
new_apps_this_week: int new_apps_this_week: int
app_week_growth_rate: float
class WorkspaceInfo(BaseModel): class WorkspaceInfo(BaseModel):
"""工作空间信息""" """工作空间信息"""

View File

@@ -12,36 +12,40 @@ class HomePageService:
"""获取首页统计数据""" """获取首页统计数据"""
# 计算时间范围 # 计算时间范围
now = datetime.now() 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 = now - timedelta(days=now.weekday())
week_start = week_start.replace(hour=0, minute=0, second=0, microsecond=0) week_start = week_start.replace(hour=0, minute=0, second=0, microsecond=0)
# 获取各项统计数据 # 获取各项统计数据
total_models, new_models_this_month = HomePageRepository.get_model_statistics( total_models, total_llm, total_embedding, model_week_growth_rate = HomePageRepository.get_model_statistics(
db, tenant_id, month_start db, tenant_id, week_start
) )
active_workspaces, new_workspaces_this_month = HomePageRepository.get_workspace_statistics( active_workspaces, new_workspaces_this_week, workspace_week_growth_rate = HomePageRepository.get_workspace_statistics(
db, tenant_id, month_start db, tenant_id, week_start
) )
total_users, new_users_this_month = HomePageRepository.get_user_statistics( total_users, new_users_this_week, user_week_growth_rate = HomePageRepository.get_user_statistics(
db, tenant_id, month_start 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 db, tenant_id, week_start
) )
return HomeStatistics( return HomeStatistics(
total_models=total_models, 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, 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, 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, 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 @staticmethod