修复无法正常登录的问题
This commit is contained in:
@@ -362,6 +362,17 @@
|
|||||||
function initGlobe() {
|
function initGlobe() {
|
||||||
if (!dom.globeContainer) return;
|
if (!dom.globeContainer) return;
|
||||||
|
|
||||||
|
if (typeof Globe !== 'function') {
|
||||||
|
console.warn('[Globe] Globe.gl library not loaded. 3D visualization will be disabled.');
|
||||||
|
dom.globeContainer.innerHTML = `
|
||||||
|
<div style="height: 100%; display: flex; align-items: center; justify-content: center; color: var(--text-muted); font-size: 0.8rem; text-align: center; padding: 20px;">
|
||||||
|
Globe.gl 库加载失败<br>请检查网络连接或刷新页面
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
myGlobe = Globe()
|
myGlobe = Globe()
|
||||||
(dom.globeContainer)
|
(dom.globeContainer)
|
||||||
.globeImageUrl('//unpkg.com/three-globe/example/img/earth-dark.jpg')
|
.globeImageUrl('//unpkg.com/three-globe/example/img/earth-dark.jpg')
|
||||||
@@ -399,6 +410,9 @@
|
|||||||
myGlobe.controls().autoRotate = true;
|
myGlobe.controls().autoRotate = true;
|
||||||
myGlobe.controls().autoRotateSpeed = 0.5;
|
myGlobe.controls().autoRotateSpeed = 0.5;
|
||||||
myGlobe.controls().enableZoom = false; // Disable zoom to maintain dashboard feel
|
myGlobe.controls().enableZoom = false; // Disable zoom to maintain dashboard feel
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[Globe] Initialization failed:', err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateGlobe(servers) {
|
function updateGlobe(servers) {
|
||||||
|
|||||||
@@ -18,16 +18,9 @@ const REQUIRED_TABLES = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
async function checkAndFixDatabase() {
|
async function checkAndFixDatabase() {
|
||||||
// Only run if .env is already configured
|
|
||||||
const envPath = path.join(__dirname, '..', '.env');
|
const envPath = path.join(__dirname, '..', '.env');
|
||||||
if (!fs.existsSync(envPath)) return;
|
if (!fs.existsSync(envPath)) return;
|
||||||
|
|
||||||
const dbHost = process.env.MYSQL_HOST || 'localhost';
|
|
||||||
const dbUser = process.env.MYSQL_USER || 'root';
|
|
||||||
const dbPass = process.env.MYSQL_PASSWORD || '';
|
|
||||||
const dbPort = parseInt(process.env.MYSQL_PORT) || 3306;
|
|
||||||
const dbName = process.env.MYSQL_DATABASE || 'display_wall';
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Check tables
|
// Check tables
|
||||||
const [rows] = await db.query("SHOW TABLES");
|
const [rows] = await db.query("SHOW TABLES");
|
||||||
@@ -36,36 +29,23 @@ async function checkAndFixDatabase() {
|
|||||||
const missingTables = REQUIRED_TABLES.filter(t => !existingTables.includes(t));
|
const missingTables = REQUIRED_TABLES.filter(t => !existingTables.includes(t));
|
||||||
|
|
||||||
if (missingTables.length > 0) {
|
if (missingTables.length > 0) {
|
||||||
console.log(`[Database Integrity] ⚠️ Missing tables: ${missingTables.join(', ')}`);
|
console.log(`[Database Integrity] ⚠️ Missing tables: ${missingTables.join(', ')}. Creating them...`);
|
||||||
await recreateDatabase(dbHost, dbPort, dbUser, dbPass, dbName);
|
|
||||||
} else {
|
for (const table of missingTables) {
|
||||||
// console.log(`[Database Integrity] ✅ All tables accounted for.`);
|
await createTable(table);
|
||||||
|
}
|
||||||
|
console.log(`[Database Integrity] ✅ Missing tables created.`);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code === 'ER_BAD_DB_ERROR') {
|
|
||||||
console.log(`[Database Integrity] ⚠️ Database "${dbName}" does not exist.`);
|
|
||||||
await recreateDatabase(dbHost, dbPort, dbUser, dbPass, dbName);
|
|
||||||
} else {
|
|
||||||
console.error('[Database Integrity] ❌ Error checking integrity:', err.message);
|
console.error('[Database Integrity] ❌ Error checking integrity:', err.message);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function recreateDatabase(host, port, user, password, dbName) {
|
async function createTable(tableName) {
|
||||||
console.log(`[Database Integrity] 🔄 Re-initializing database "${dbName}"...`);
|
console.log(` - Creating table "${tableName}"...`);
|
||||||
|
switch (tableName) {
|
||||||
let connection;
|
case 'users':
|
||||||
try {
|
await db.query(`
|
||||||
connection = await mysql.createConnection({ host, port, user, password });
|
|
||||||
|
|
||||||
// Drop and create database
|
|
||||||
await connection.query(`DROP DATABASE IF EXISTS \`${dbName}\``);
|
|
||||||
await connection.query(`CREATE DATABASE \`${dbName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`);
|
|
||||||
await connection.query(`USE \`${dbName}\``);
|
|
||||||
|
|
||||||
// Recreate all tables
|
|
||||||
console.log(' - Creating table "users"...');
|
|
||||||
await connection.query(`
|
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
username VARCHAR(255) NOT NULL UNIQUE,
|
username VARCHAR(255) NOT NULL UNIQUE,
|
||||||
@@ -74,9 +54,9 @@ async function recreateDatabase(host, port, user, password, dbName) {
|
|||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||||
`);
|
`);
|
||||||
|
break;
|
||||||
console.log(' - Creating table "prometheus_sources"...');
|
case 'prometheus_sources':
|
||||||
await connection.query(`
|
await db.query(`
|
||||||
CREATE TABLE IF NOT EXISTS prometheus_sources (
|
CREATE TABLE IF NOT EXISTS prometheus_sources (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
name VARCHAR(255) NOT NULL,
|
name VARCHAR(255) NOT NULL,
|
||||||
@@ -86,9 +66,9 @@ async function recreateDatabase(host, port, user, password, dbName) {
|
|||||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||||
`);
|
`);
|
||||||
|
break;
|
||||||
console.log(' - Creating table "site_settings"...');
|
case 'site_settings':
|
||||||
await connection.query(`
|
await db.query(`
|
||||||
CREATE TABLE IF NOT EXISTS site_settings (
|
CREATE TABLE IF NOT EXISTS site_settings (
|
||||||
id INT PRIMARY KEY DEFAULT 1,
|
id INT PRIMARY KEY DEFAULT 1,
|
||||||
page_name VARCHAR(255) DEFAULT '数据可视化展示大屏',
|
page_name VARCHAR(255) DEFAULT '数据可视化展示大屏',
|
||||||
@@ -98,13 +78,13 @@ async function recreateDatabase(host, port, user, password, dbName) {
|
|||||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||||
`);
|
`);
|
||||||
await connection.query(`
|
await db.query(`
|
||||||
INSERT INTO site_settings (id, page_name, title, default_theme)
|
INSERT IGNORE INTO site_settings (id, page_name, title, default_theme)
|
||||||
VALUES (1, '数据可视化展示大屏', '数据可视化展示大屏', 'dark')
|
VALUES (1, '数据可视化展示大屏', '数据可视化展示大屏', 'dark')
|
||||||
`);
|
`);
|
||||||
|
break;
|
||||||
console.log(' - Creating table "traffic_stats"...');
|
case 'traffic_stats':
|
||||||
await connection.query(`
|
await db.query(`
|
||||||
CREATE TABLE IF NOT EXISTS traffic_stats (
|
CREATE TABLE IF NOT EXISTS traffic_stats (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
rx_bytes BIGINT UNSIGNED DEFAULT 0,
|
rx_bytes BIGINT UNSIGNED DEFAULT 0,
|
||||||
@@ -115,9 +95,9 @@ async function recreateDatabase(host, port, user, password, dbName) {
|
|||||||
UNIQUE INDEX (timestamp)
|
UNIQUE INDEX (timestamp)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||||
`);
|
`);
|
||||||
|
break;
|
||||||
console.log(' - Creating table "server_locations"...');
|
case 'server_locations':
|
||||||
await connection.query(`
|
await db.query(`
|
||||||
CREATE TABLE IF NOT EXISTS server_locations (
|
CREATE TABLE IF NOT EXISTS server_locations (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
ip VARCHAR(255) NOT NULL UNIQUE,
|
ip VARCHAR(255) NOT NULL UNIQUE,
|
||||||
@@ -130,16 +110,7 @@ async function recreateDatabase(host, port, user, password, dbName) {
|
|||||||
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||||
`);
|
`);
|
||||||
|
break;
|
||||||
console.log(`[Database Integrity] ✅ Re-initialization complete.`);
|
|
||||||
|
|
||||||
// Refresh db pool in the main app context
|
|
||||||
db.initPool();
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
console.error('[Database Integrity] ❌ Critical failure during re-initialization:', err.message);
|
|
||||||
} finally {
|
|
||||||
if (connection) await connection.end();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -582,25 +582,28 @@ app.get('/api/metrics/overview', async (req, res) => {
|
|||||||
|
|
||||||
// --- Add Geo Information to Servers ---
|
// --- Add Geo Information to Servers ---
|
||||||
const geoServers = await Promise.all(overview.servers.map(async (server) => {
|
const geoServers = await Promise.all(overview.servers.map(async (server) => {
|
||||||
// The original IP is masked in the response from prometheusService.getOverviewMetrics
|
|
||||||
// But we can get it back from serverIdMap in prometheusService if we are on the same process
|
|
||||||
// Actually, prometheusService needs to provide a way to get the real IP back.
|
|
||||||
// Or we can just modify getOverviewMetrics to return the real IP for internal use.
|
|
||||||
|
|
||||||
// Let's look at prometheusService.js's getServerIdMap logic
|
|
||||||
const realInstance = prometheusService.resolveToken(server.instance);
|
const realInstance = prometheusService.resolveToken(server.instance);
|
||||||
const cleanIp = realInstance.split(':')[0];
|
const cleanIp = realInstance.split(':')[0];
|
||||||
|
|
||||||
// Attempt to get location
|
// Try to get from DB cache only (fast)
|
||||||
const location = await geoService.getLocation(cleanIp);
|
try {
|
||||||
if (location) {
|
const [rows] = await db.query('SELECT * FROM server_locations WHERE ip = ?', [cleanIp]);
|
||||||
|
if (rows.length > 0) {
|
||||||
|
const location = rows[0];
|
||||||
return {
|
return {
|
||||||
...server,
|
...server,
|
||||||
country: location.country,
|
country: location.country,
|
||||||
countryName: location.country_name,
|
countryName: location.country_name,
|
||||||
|
city: location.city,
|
||||||
lat: location.latitude,
|
lat: location.latitude,
|
||||||
lng: location.longitude
|
lng: location.longitude
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
// Trigger background resolution for future requests
|
||||||
|
geoService.getLocation(cleanIp).catch(() => {});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// DB error, skip geo for now
|
||||||
}
|
}
|
||||||
return server;
|
return server;
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user