P1 查询历史持久化 (47 tests): - QueryHistory/QuickQuery SQLAlchemy model 建表 - query_history_service 从内存迁移到 SQLite async - 每用户500条上限自动清理 - 7个API endpoint全部对接持久化 - 10个预置快捷查询模板 P2 EXPLAIN真实解析 (40 tests): - MySQL EXPLAIN FORMAT=JSON 执行+解析 - PostgreSQL EXPLAIN (FORMAT JSON) 执行+解析 - SQLite EXPLAIN QUERY PLAN 解析 - 统一树结构输出 + 优化警告 - POST /api/v1/sql-assistant/explain endpoint Dashboard修复: - 修复重复export default导致页面崩溃 - 对接/instances /tickets /dashboard/stats真实API - 替换硬编码统计数字为API驱动 - 修复calcHealthScore安全访问 AuditLogsPage: - 修复Fragment key warning 1081 tests passed (10 system_settings failures are pre-existing)
112 lines
3.4 KiB
Python
112 lines
3.4 KiB
Python
"""查询历史和快捷查询 API — 人性化查询管理。"""
|
|
from typing import Optional
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.core.security import get_current_user
|
|
from app.models.user import User
|
|
from app.services.query_history_service import QueryHistoryService
|
|
|
|
router = APIRouter(prefix="/query-history", tags=["查询历史"])
|
|
|
|
|
|
@router.get("/my")
|
|
async def my_query_history(
|
|
db_type: Optional[str] = None,
|
|
instance_id: Optional[int] = None,
|
|
keyword: Optional[str] = None,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""获取我的查询历史。
|
|
|
|
自动记录每次查询,支持按数据库类型、实例、关键词筛选。
|
|
"""
|
|
return await QueryHistoryService.get_history(
|
|
db=db,
|
|
user_id=current_user.id,
|
|
db_type=db_type,
|
|
instance_id=instance_id,
|
|
keyword=keyword,
|
|
page=page,
|
|
page_size=page_size,
|
|
)
|
|
|
|
|
|
@router.post("/{history_id}/favorite")
|
|
async def toggle_favorite(
|
|
history_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""切换查询历史收藏状态。"""
|
|
is_fav = await QueryHistoryService.toggle_favorite(db, current_user.id, history_id)
|
|
return {"is_favorite": is_fav, "message": "已收藏" if is_fav else "已取消收藏"}
|
|
|
|
|
|
@router.get("/favorites")
|
|
async def my_favorites(
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""获取我收藏的查询。"""
|
|
return await QueryHistoryService.get_favorites(
|
|
db=db, user_id=current_user.id, page=page, page_size=page_size
|
|
)
|
|
|
|
|
|
@router.get("/statistics")
|
|
async def my_query_statistics(
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""获取我的查询统计 — 了解自己的数据使用习惯。"""
|
|
return await QueryHistoryService.get_query_statistics(db=db, user_id=current_user.id)
|
|
|
|
|
|
# ── 快捷查询 ──
|
|
|
|
@router.get("/quick-queries")
|
|
async def list_quick_queries(
|
|
db_type: Optional[str] = None,
|
|
keyword: Optional[str] = None,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""获取快捷查询列表(个人+团队共享)。"""
|
|
items = await QueryHistoryService.get_quick_queries(
|
|
db=db, user_id=current_user.id, db_type=db_type, keyword=keyword
|
|
)
|
|
return {"items": items, "total": len(items)}
|
|
|
|
|
|
@router.post("/quick-queries")
|
|
async def create_quick_query(
|
|
name: str,
|
|
query_content: str,
|
|
db_type: str,
|
|
description: str = "",
|
|
database_pattern: str = "*",
|
|
is_shared: bool = False,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""创建快捷查询。"""
|
|
qq = await QueryHistoryService.create_quick_query(
|
|
db=db,
|
|
name=name,
|
|
description=description,
|
|
query_content=query_content,
|
|
db_type=db_type,
|
|
database_pattern=database_pattern,
|
|
is_shared=is_shared,
|
|
created_by=current_user.id,
|
|
created_by_name=current_user.display_name or current_user.username,
|
|
)
|
|
return {"id": qq.id, "name": qq.name, "message": "快捷查询已创建"}
|