Files
shensheshou/app/api/system-status/route.ts

79 lines
1.9 KiB
TypeScript

/**
* 系统状态 API 路由
* 返回 MongoDB 数据库真实状态
*/
import { NextResponse } from "next/server"
import { getDatabaseStats, healthCheck } from "@/lib/mongodb"
/**
* GET /api/system-status
* 获取系统状态
*/
export async function GET() {
try {
// 健康检查
const health = await healthCheck()
if (!health.mongodb) {
return NextResponse.json({
status: 'error',
connected: false,
latencyMs: health.latencyMs,
error: health.error || 'MongoDB 连接失败',
databases: [],
totalDocuments: 0,
totalSizeGB: 0,
lastCheck: new Date().toISOString()
}, { status: 503 })
}
// 获取数据库统计
const stats = await getDatabaseStats()
return NextResponse.json({
status: 'healthy',
connected: stats.connected,
latencyMs: health.latencyMs,
databases: stats.databases,
totalDocuments: stats.totalDocuments,
totalSizeGB: stats.totalSizeGB,
lastCheck: new Date().toISOString(),
// 格式化显示
summary: {
userCount: formatNumber(stats.totalDocuments),
dataSize: `${stats.totalSizeGB} GB`,
dbCount: stats.databases.length,
responseTime: `${health.latencyMs}ms`
}
}, {
headers: { 'Cache-Control': 'no-store, max-age=0' }
})
} catch (error) {
console.error('System status error:', error)
return NextResponse.json({
status: 'error',
connected: false,
error: error instanceof Error ? error.message : 'Unknown error',
lastCheck: new Date().toISOString()
}, { status: 500 })
}
}
/**
* 格式化数字显示
*/
function formatNumber(num: number): string {
if (num >= 1000000000) {
return `${(num / 1000000000).toFixed(2)}B`
}
if (num >= 1000000) {
return `${(num / 1000000).toFixed(1)}M`
}
if (num >= 1000) {
return `${(num / 1000).toFixed(1)}K`
}
return num.toString()
}