修复数据源统计的选择影响服务器详情的BUG
This commit is contained in:
@@ -964,21 +964,7 @@ app.post('/api/settings', requireAuth, async (req, res) => {
|
||||
|
||||
// Reusable function to get overview metrics
|
||||
async function getOverview(force = false) {
|
||||
const [settingsRows] = await db.query('SELECT network_data_sources FROM site_settings WHERE id = 1');
|
||||
const selectedSourcesStr = settingsRows.length > 0 ? settingsRows[0].network_data_sources : null;
|
||||
|
||||
let query = 'SELECT * FROM prometheus_sources WHERE is_server_source = 1 AND type != "blackbox"';
|
||||
let params = [];
|
||||
|
||||
if (selectedSourcesStr) {
|
||||
const selectedSourceNames = selectedSourcesStr.split(',').map(s => s.trim()).filter(s => s);
|
||||
if (selectedSourceNames.length > 0) {
|
||||
query += ' AND name IN (?)';
|
||||
params.push(selectedSourceNames);
|
||||
}
|
||||
}
|
||||
|
||||
const [sources] = await db.query(query, params);
|
||||
const [sources] = await db.query('SELECT * FROM prometheus_sources WHERE is_server_source = 1 AND type != "blackbox"');
|
||||
if (sources.length === 0) {
|
||||
return {
|
||||
totalServers: 0,
|
||||
@@ -992,6 +978,10 @@ async function getOverview(force = false) {
|
||||
};
|
||||
}
|
||||
|
||||
const [settingsRows] = await db.query('SELECT network_data_sources FROM site_settings WHERE id = 1');
|
||||
const selectedSourcesStr = settingsRows.length > 0 ? settingsRows[0].network_data_sources : null;
|
||||
const selectedSourceNames = selectedSourcesStr ? selectedSourcesStr.split(',').map(s => s.trim()).filter(s => s) : [];
|
||||
|
||||
const allMetrics = await Promise.all(sources.map(async (source) => {
|
||||
const cacheKey = `source_metrics:${source.url}:${source.name}`;
|
||||
if (force) {
|
||||
@@ -1003,10 +993,10 @@ async function getOverview(force = false) {
|
||||
|
||||
try {
|
||||
const metrics = await prometheusService.getOverviewMetrics(source.url, source.name);
|
||||
// Don't set cache here if we want real-time WS push to be fresh,
|
||||
// but keeping it for REST API performance is fine.
|
||||
await cache.set(cacheKey, metrics, 15); // Cache for 15s
|
||||
return metrics;
|
||||
const enrichedMetrics = { ...metrics, sourceName: source.name };
|
||||
|
||||
await cache.set(cacheKey, enrichedMetrics, 15); // Cache for 15s
|
||||
return enrichedMetrics;
|
||||
} catch (err) {
|
||||
console.error(`Error fetching metrics from ${source.name}:`, err.message);
|
||||
return null;
|
||||
@@ -1034,10 +1024,15 @@ async function getOverview(force = false) {
|
||||
memTotal += m.memory.total;
|
||||
diskUsed += m.disk.used;
|
||||
diskTotal += m.disk.total;
|
||||
|
||||
// Aggregates ONLY for selected network sources
|
||||
if (selectedSourceNames.length === 0 || selectedSourceNames.includes(m.sourceName)) {
|
||||
netRx += m.network.rx;
|
||||
netTx += m.network.tx;
|
||||
traffic24hRx += m.traffic24h.rx;
|
||||
traffic24hTx += m.traffic24h.tx;
|
||||
}
|
||||
|
||||
allServers = allServers.concat(m.servers);
|
||||
}
|
||||
|
||||
@@ -1173,21 +1168,7 @@ app.get('/api/metrics/network-history', async (req, res) => {
|
||||
// Get CPU usage history for sparklines
|
||||
app.get('/api/metrics/cpu-history', async (req, res) => {
|
||||
try {
|
||||
const [settingsRows] = await db.query('SELECT network_data_sources FROM site_settings WHERE id = 1');
|
||||
const selectedSourcesStr = settingsRows.length > 0 ? settingsRows[0].network_data_sources : null;
|
||||
|
||||
let query = 'SELECT * FROM prometheus_sources WHERE is_server_source = 1 AND type != "blackbox"';
|
||||
let params = [];
|
||||
|
||||
if (selectedSourcesStr) {
|
||||
const selectedSourceNames = selectedSourcesStr.split(',').map(s => s.trim()).filter(s => s);
|
||||
if (selectedSourceNames.length > 0) {
|
||||
query += ' AND name IN (?)';
|
||||
params.push(selectedSourceNames);
|
||||
}
|
||||
}
|
||||
|
||||
const [sources] = await db.query(query, params);
|
||||
const [sources] = await db.query('SELECT * FROM prometheus_sources WHERE is_server_source = 1 AND type != "blackbox"');
|
||||
if (sources.length === 0) {
|
||||
return res.json({ timestamps: [], values: [] });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user