diff --git a/src/actions/stats.ts b/src/actions/stats.ts index 63926e2..008b155 100644 --- a/src/actions/stats.ts +++ b/src/actions/stats.ts @@ -220,31 +220,17 @@ export type Edge = { } // 获取节点信息 -export async function getEdgeNodes(props?: { +export async function getEdgeNodes(page: number, size: number, props?: { macaddr?: string public?: string city?: string isp?: string }, -page: number = 1) { +) { try { - const size = 250 const offset = Math.max(0, (page - 1)) * size - // 构建查询条件 - let whereCondition = eq(edge.active, 1) + const limit = Math.min(100, Math.max(10, size)) - if (props?.macaddr) { - whereCondition = sql`${whereCondition} AND ${edge.macaddr} = ${props.macaddr}` - } - if (props?.public) { - whereCondition = sql`${whereCondition} AND ${edge.public} LIKE ${`%${props.public}%`}` - } - if (props?.city) { - whereCondition = sql`${whereCondition} AND ${cityhash.city} LIKE ${`%${props.city}%`}` - } - if (props?.isp) { - whereCondition = sql`${whereCondition} AND ${edge.isp} LIKE ${`%${props.isp}%`}` - } const [total, data] = await Promise.all([ drizzle.$count(edge, eq(edge.active, 1)), drizzle @@ -264,14 +250,15 @@ page: number = 1) { .where(eq(edge.active, 1)) .orderBy(edge.id) .offset(offset) - .limit(size), + .limit(limit), ]) return { - success: true, data, totalCount: total, + currentPage: Math.floor(offset / limit) + 1, + totalPages: Math.ceil(total / limit), } } catch (error) { diff --git a/src/app/dashboard/components/edge.tsx b/src/app/dashboard/components/edge.tsx index c128390..21e6125 100644 --- a/src/app/dashboard/components/edge.tsx +++ b/src/app/dashboard/components/edge.tsx @@ -44,7 +44,7 @@ export default function Edge() { }) useEffect(() => { - fetchData({}, currentPage) + fetchData({}) }, [currentPage, itemsPerPage]) const fetchData = async (val: { @@ -52,11 +52,14 @@ export default function Edge() { public?: string city?: string isp?: string - }, page: number = 1) => { + }) => { try { setError(null) setLoading(true) - const result = await getEdgeNodes(val, page) + + // 计算偏移量 + const offset = (currentPage - 1) * itemsPerPage + const result = await getEdgeNodes(offset, itemsPerPage, val) const validatedData = (result.data).map(item => ({ id: item.id, macaddr: item.macaddr || '', @@ -90,7 +93,7 @@ export default function Edge() { isp: values.isp || undefined, } - fetchData(filters, 1) + fetchData(filters) } // 多IP节点格式化 @@ -172,7 +175,7 @@ export default function Edge() { city: formValues.city || '', isp: formValues.isp || '', } - fetchData(filters, page) + fetchData(filters) } // 处理每页显示数量变化 @@ -186,7 +189,7 @@ export default function Edge() { city: formValues.city || '', isp: formValues.isp || '', } - fetchData(filters, 1) + fetchData(filters) } if (loading) return ( @@ -201,7 +204,7 @@ export default function Edge() {