diff --git a/src/actions/stats.ts b/src/actions/stats.ts index 2cdb7d2..072679e 100644 --- a/src/actions/stats.ts +++ b/src/actions/stats.ts @@ -62,7 +62,7 @@ export async function getAllocationStatus(hours: number = 24) { export type GatewayInfo = { macaddr: string inner_ip: string - setid: string + setid: number enable: number } @@ -77,7 +77,7 @@ export async function getGatewayInfo() { enable: token.enable, }) .from(token) - .orderBy(token.macaddr) + .orderBy(sql`cast(regexp_replace(token.inner_ip, '192.168.50.', '') as unsigned)`) return { success: true, @@ -128,7 +128,7 @@ export async function getGatewayConfig(page?: number, mac?: string): Promise - 端口 - 线路 - 城市 - 节点MAC - 节点IP - 配置更新 - 在用状态 + 端口 + 线路 + 城市 + 节点MAC + 节点IP + 配置更新 + 在用状态 diff --git a/src/app/dashboard/components/gatewayinfo.tsx b/src/app/dashboard/components/gatewayinfo.tsx index c8564f8..1884192 100644 --- a/src/app/dashboard/components/gatewayinfo.tsx +++ b/src/app/dashboard/components/gatewayinfo.tsx @@ -16,16 +16,6 @@ const filterSchema = z.object({ type FilterSchema = z.infer -// IP地址排序函数 -const sortByIpAddress = (a: string, b: string): number => { - const ipToNumber = (ip: string): number => { - const parts = ip.split('.').map(part => parseInt(part, 10)) - return (parts[0] << 24) + (parts[1] << 16) + (parts[2] << 8) + parts[3] - } - - return ipToNumber(a) - ipToNumber(b) -} - export default function Gatewayinfo() { const [data, setData] = useState([]) const [filteredData, setFilteredData] = useState([]) @@ -71,11 +61,8 @@ export default function Gatewayinfo() { setError('') const result = await getGatewayInfo() - const sortedData = result.data.sort((a: GatewayInfo, b: GatewayInfo) => - sortByIpAddress(a.inner_ip, b.inner_ip), - ) - setData(sortedData) - setFilteredData(sortedData) // 初始化时设置filteredData + setData(result.data) + setFilteredData(result.data) // 初始化时设置filteredData } catch (error) { console.error('Failed to fetch gateway info:', error) diff --git a/src/lib/drizzle/index.ts b/src/lib/drizzle/index.ts index 75e0c38..39ac75b 100644 --- a/src/lib/drizzle/index.ts +++ b/src/lib/drizzle/index.ts @@ -1,20 +1,19 @@ import 'dotenv/config' -import { drizzle as client } from 'drizzle-orm/mysql2' +import { drizzle as client, MySql2Database } from 'drizzle-orm/mysql2' import * as schema from './schema' declare global { - var drizzle: ReturnType> | undefined + var drizzle: MySql2Database | undefined } -const { DATABASE_URL } = process.env -if (!DATABASE_URL) { - throw new Error('DATABASE_URL is not set') -} - -const drizzle = global.drizzle || client(DATABASE_URL, { mode: 'default', schema }) -if (process.env.NODE_ENV !== 'production') { - global.drizzle = drizzle -} +const drizzle = new Proxy({} as MySql2Database, { + get(_, prop) { + if (!global.drizzle && process.env.NODE_ENV !== 'production') { + global.drizzle = client(process.env.DATABASE_URL!, { mode: 'default', schema }) + } + return global.drizzle![prop as keyof typeof global.drizzle] + }, +}) export default drizzle export * from './schema' diff --git a/src/lib/redis.ts b/src/lib/redis.ts index 4db706c..b77f64f 100644 --- a/src/lib/redis.ts +++ b/src/lib/redis.ts @@ -1,9 +1,14 @@ import 'server-only' import { createClient } from 'redis' -const client = createClient({ - url: process.env.REDIS_URL, -}) +declare global { + var redis: ReturnType | undefined +} + +const client = global.redis || createClient({ url: process.env.REDIS_URL }) +if (process.env.NODE_ENV !== 'production') { + global.redis = client +} const redis = await client.connect() export default redis