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