完成 APP 建立流程和使用分析、增加主機備機的備援機制、管理者後臺增加資料庫監控

This commit is contained in:
2025-09-12 18:22:30 +08:00
parent 9c5dceb001
commit b85a9ce95e
19 changed files with 2982 additions and 757 deletions

View File

@@ -0,0 +1,92 @@
// =====================================================
// 資料庫狀態監控 API
// =====================================================
import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/database';
export async function GET(request: NextRequest) {
try {
// 獲取備援狀態
const failoverStatus = db.getFailoverStatus();
if (!failoverStatus) {
return NextResponse.json({
success: false,
message: '備援功能未啟用',
data: null
});
}
// 獲取詳細狀態信息
const status = {
isEnabled: failoverStatus.isEnabled,
currentDatabase: failoverStatus.currentDatabase,
masterHealthy: failoverStatus.masterHealthy,
slaveHealthy: failoverStatus.slaveHealthy,
lastHealthCheck: failoverStatus.lastHealthCheck && failoverStatus.lastHealthCheck > 0
? new Date(failoverStatus.lastHealthCheck).toISOString()
: new Date().toISOString(), // 如果沒有健康檢查記錄,使用當前時間
consecutiveFailures: failoverStatus.consecutiveFailures,
uptime: process.uptime(),
timestamp: new Date().toISOString()
};
return NextResponse.json({
success: true,
message: '資料庫狀態獲取成功',
data: status
});
} catch (error) {
console.error('獲取資料庫狀態失敗:', error);
return NextResponse.json({
success: false,
message: '獲取資料庫狀態失敗',
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { action, database } = body;
if (action === 'switch') {
if (!database || !['master', 'slave'].includes(database)) {
return NextResponse.json({
success: false,
message: '無效的資料庫參數'
}, { status: 400 });
}
const success = await db.switchDatabase(database as 'master' | 'slave');
if (success) {
return NextResponse.json({
success: true,
message: `已切換到 ${database} 資料庫`
});
} else {
return NextResponse.json({
success: false,
message: `切換到 ${database} 資料庫失敗`
}, { status: 400 });
}
}
return NextResponse.json({
success: false,
message: '無效的操作'
}, { status: 400 });
} catch (error) {
console.error('執行資料庫操作失敗:', error);
return NextResponse.json({
success: false,
message: '執行資料庫操作失敗',
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}

View File

@@ -0,0 +1,73 @@
// =====================================================
// 資料庫同步管理 API
// =====================================================
import { NextRequest, NextResponse } from 'next/server';
import { dbSync } from '@/lib/database-sync';
// 獲取同步狀態
export async function GET(request: NextRequest) {
try {
const status = await dbSync.getSyncStatus();
return NextResponse.json({
success: true,
message: '同步狀態獲取成功',
data: status
});
} catch (error) {
console.error('獲取同步狀態失敗:', error);
return NextResponse.json({
success: false,
message: '獲取同步狀態失敗',
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}
// 執行同步操作
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { action, tableName, condition } = body;
switch (action) {
case 'sync_table':
if (!tableName) {
return NextResponse.json({
success: false,
message: '缺少表名參數'
}, { status: 400 });
}
const syncResult = await dbSync.syncFromMasterToSlave(tableName, condition);
if (syncResult) {
return NextResponse.json({
success: true,
message: `成功同步表 ${tableName} 到備機`
});
} else {
return NextResponse.json({
success: false,
message: `同步表 ${tableName} 失敗`
}, { status: 500 });
}
default:
return NextResponse.json({
success: false,
message: '無效的操作'
}, { status: 400 });
}
} catch (error) {
console.error('執行同步操作失敗:', error);
return NextResponse.json({
success: false,
message: '執行同步操作失敗',
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}

View File

@@ -14,24 +14,19 @@ export async function GET(request: NextRequest) {
const users = await userService.query(sql);
// 為每個用戶獲取統計數據
const usersWithStats = await Promise.all(
users.map(async (user) => {
const stats = await userService.getUserAppAndReviewStats(user.id);
return {
id: user.id,
name: user.name,
email: user.email,
department: user.department,
role: user.role,
status: user.status,
createdAt: user.created_at,
lastLogin: user.last_login,
appCount: stats.appCount,
reviewCount: stats.reviewCount
};
})
);
// 格式化用戶數據
const usersWithStats = users.map((user) => {
return {
id: user.id,
name: user.name,
email: user.email,
department: user.department,
role: user.role,
status: user.status,
createdAt: user.created_at,
lastLogin: user.last_login
};
});
return NextResponse.json({
success: true,

View File

@@ -8,9 +8,10 @@ export async function GET(request: NextRequest, { params }: { params: { id: stri
const { id: appId } = await params
const { searchParams } = new URL(request.url)
// 獲取日期範圍參數
// 獲取日期範圍和部門過濾參數
const startDate = searchParams.get('startDate')
const endDate = searchParams.get('endDate')
const department = searchParams.get('department')
// 獲取應用基本統計
const app = await appService.getAppById(appId)
@@ -24,8 +25,8 @@ export async function GET(request: NextRequest, { params }: { params: { id: stri
// 獲取評分統計
const ratingStats = await appService.getAppRatingStats(appId)
// 獲取使用趨勢數據(支援日期範圍)
const usageStats = await appService.getAppUsageStats(appId, startDate || undefined, endDate || undefined)
// 獲取使用趨勢數據(支援日期範圍和部門過濾
const usageStats = await appService.getAppUsageStats(appId, startDate || undefined, endDate || undefined, department || undefined)
// 獲取收藏數量
const favoritesCount = await appService.getAppFavoritesCount(appId)