添加流量地图

This commit is contained in:
CN-JS-HuiBai
2026-04-05 01:17:45 +08:00
parent 2fc84f999c
commit af83f42d26
8 changed files with 281 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ const path = require('path');
const db = require('./db');
const prometheusService = require('./prometheus-service');
const cache = require('./cache');
const geoService = require('./geo-service');
const checkAndFixDatabase = require('./db-integrity-check');
const app = express();
@@ -548,7 +549,7 @@ app.get('/api/metrics/overview', async (req, res) => {
console.error('Error calculating 24h traffic from DB integration:', err);
}
res.json({
const overview = {
totalServers,
activeServers,
cpu: {
@@ -577,7 +578,35 @@ app.get('/api/metrics/overview', async (req, res) => {
total: traffic24hRx + traffic24hTx
},
servers: allServers
});
};
// --- Add Geo Information to Servers ---
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 cleanIp = realInstance.split(':')[0];
// Attempt to get location
const location = await geoService.getLocation(cleanIp);
if (location) {
return {
...server,
country: location.country,
countryName: location.country_name,
lat: location.latitude,
lng: location.longitude
};
}
return server;
}));
overview.servers = geoServers;
res.json(overview);
} catch (err) {
console.error('Error fetching overview metrics:', err);
res.status(500).json({ error: 'Failed to fetch metrics' });