增加get_count函数及路由

This commit is contained in:
jingrow 2025-11-05 04:23:13 +08:00
parent 022ec0d016
commit 7089e04abd
4 changed files with 55 additions and 15 deletions

View File

@ -173,8 +173,9 @@ export const getWorkspace = async (name: string): Promise<{ success: boolean; da
// 获取记录总数的通用函数
export const getCount = async (pagetype: string): Promise<{ success: boolean; count?: number; message?: string }> => {
try {
const response = await axios.get(
`/api/action/jingrow.client.get_count`,
const response = await axios.post(
`/jingrow/get-count`,
null,
{
params: {
pagetype: pagetype
@ -190,7 +191,7 @@ export const getCount = async (pagetype: string): Promise<{ success: boolean; co
return {
success: false,
count: 0,
message: error.response?.data?.message || error.message || '获取记录总数失败'
message: error.response?.data?.detail || error.response?.data?.message || error.message || '获取记录总数失败'
}
}
}

View File

@ -9,7 +9,7 @@ import os
from contextvars import ContextVar
from jingrow.model.page import Page
from jingrow.config import Config
from jingrow.utils.jingrow_api import upload_file_to_jingrow
from jingrow.utils.jingrow_api import upload_file_to_jingrow, get_record_count
_local = {}
@ -120,6 +120,11 @@ def get_all(pagetype: str, filters: List[List[Any]] = None, fields: List[str] =
return get_list(pagetype, filters=filters, fields=fields, limit=None)
def get_count(pagetype: str, filters: List[List[Any]] = None, debug: bool = False, cache: bool = False) -> int:
"""获取指定PageType的记录总数"""
return get_record_count(pagetype, filters=filters, debug=debug, cache=cache)
def get_single(pagetype: str):
"""获取 single 类型 pagetype 配置,返回 {success, config|error} 结构。"""
data = get_pg(pagetype, pagetype)

View File

@ -181,19 +181,12 @@ async def batch_delete_records(request: Request, data: Dict[str, Any]):
else:
raise HTTPException(status_code=400, detail="所有记录删除失败")
@router.post("/api/action/jingrow.client.get_count")
@router.post("/jingrow/get-count")
async def get_record_count(request: Request, pagetype: str):
"""获取记录总数(估算)"""
"""获取记录总数"""
try:
data = jingrow.get_list(pagetype=pagetype, limit=100)
if not isinstance(data, list):
raise HTTPException(status_code=400, detail='获取记录总数失败')
if len(data) < 100:
estimated_count = len(data)
else:
estimated_count = len(data) + 1
return JSONResponse(content={"message": estimated_count})
count = jingrow.get_count(pagetype)
return JSONResponse(content={"message": count})
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

View File

@ -512,6 +512,47 @@ def get_package_dependencies(package_name: str):
except Exception as e:
return {'success': False, 'error': f"获取Package依赖信息异常: {str(e)}"}
def get_record_count(pagetype: str, filters: list = None, debug: bool = False, cache: bool = False):
"""
获取指定PageType的记录总数
- pagetype: PageType名称
- filters: 过滤条件形如 [[field, op, value], ...]
- debug: 是否开启调试模式
- cache: 是否使用缓存
Returns: int 记录总数失败返回 0
"""
try:
api_url = f"{Config.jingrow_server_url}/api/action/jingrow.client.get_count"
headers = get_jingrow_api_headers()
if not headers:
log_error("JINGROW_API_KEY 或 JINGROW_API_SECRET 未配置")
return 0
payload = {
"pagetype": pagetype,
"filters": filters,
"debug": debug,
"cache": cache
}
response = requests.post(api_url, json=payload, headers=headers, timeout=10)
if response.status_code == 200:
data = response.json()
# 处理服务端 action 的包裹格式
if isinstance(data, dict) and 'message' in data:
count = data['message']
else:
count = data
# 确保返回的是整数
if isinstance(count, (int, float)):
return int(count)
return 0
else:
log_error(f"获取记录总数失败: HTTP {response.status_code}")
return 0
except Exception as e:
log_error(f"获取记录总数异常: {str(e)}")
return 0
def get_record_id(pagetype: str, filters: list = None, field: str = None, value: str = None, site: str = None):
"""
按条件获取单条记录的 name