优化数据库查询布局

This commit is contained in:
CN-JS-HuiBai
2026-04-12 17:48:43 +08:00
parent b79cb09987
commit e60fa2b982
5 changed files with 136 additions and 94 deletions

View File

@@ -797,7 +797,8 @@ app.get('/api/sources', requireAuth, async (req, res) => {
const res = await fetch(`${source.url.replace(/\/+$/, '')}/metrics`, { timeout: 3000 }).catch(() => null);
response = (res && res.ok) ? 'Blackbox Exporter Ready' : 'Connection Error';
} else {
response = await prometheusService.testConnection(source.url);
// Use a shorter timeout for list view to prevent blocking UI
response = await prometheusService.testConnection(source.url, 2500);
}
return { ...source, status: 'online', version: response };
} catch (e) {
@@ -954,7 +955,6 @@ app.post('/api/settings', requireAuth, async (req, res) => {
latency_target: current.latency_target || null,
icp_filing: icp_filing !== undefined ? icp_filing : (current.icp_filing || null),
ps_filing: ps_filing !== undefined ? ps_filing : (current.ps_filing || null),
network_data_sources: network_data_sources !== undefined ? network_data_sources : (current.network_data_sources || null),
show_server_ip: show_server_ip !== undefined ? (show_server_ip ? 1 : 0) : (current.show_server_ip || 0),
ip_metric_name: ip_metric_name !== undefined ? ip_metric_name : (current.ip_metric_name || null),
ip_label_name: ip_label_name !== undefined ? ip_label_name : (current.ip_label_name || 'address'),
@@ -995,7 +995,7 @@ app.post('/api/settings', requireAuth, async (req, res) => {
settings.page_name, settings.show_page_name, settings.title, settings.logo_url, settings.logo_url_dark, settings.favicon_url,
settings.default_theme, settings.show_95_bandwidth, settings.p95_type, settings.require_login_for_server_details,
settings.blackbox_source_id, settings.latency_source, settings.latency_dest, settings.latency_target,
settings.icp_filing, settings.ps_filing, settings.network_data_sources, settings.show_server_ip,
settings.icp_filing, settings.ps_filing, settings.show_server_ip,
settings.ip_metric_name, settings.ip_label_name, settings.custom_metrics
]
);
@@ -1011,7 +1011,9 @@ app.post('/api/settings', requireAuth, async (req, res) => {
// Reusable function to get overview metrics
async function getOverview(force = false) {
// Fetch sources: overview OR detail
const [sources] = await db.query('SELECT * FROM prometheus_sources WHERE (is_overview_source = 1 OR is_detail_source = 1) AND type != "blackbox"');
if (sources.length === 0) {
return {
totalServers: 0,
@@ -1024,11 +1026,6 @@ async function getOverview(force = false) {
servers: []
};
}
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) {
@@ -1077,10 +1074,6 @@ 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;
@@ -1195,21 +1188,8 @@ app.get('/api/metrics/network-history', async (req, res) => {
if (cached) return res.json(cached);
}
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_overview_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 query = 'SELECT * FROM prometheus_sources WHERE is_overview_source = 1 AND type != "blackbox"';
const [sources] = await db.query(query);
if (sources.length === 0) {
return res.json({ timestamps: [], rx: [], tx: [] });
}

View File

@@ -64,12 +64,12 @@ function createClient(baseUrl) {
/**
* Test Prometheus connection
*/
async function testConnection(url) {
async function testConnection(url, customTimeout = null) {
const normalized = normalizeUrl(url);
try {
// Using native fetch to avoid follow-redirects/axios "protocol mismatch" issues in some Node environments
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), QUERY_TIMEOUT);
const timer = setTimeout(() => controller.abort(), customTimeout || QUERY_TIMEOUT);
// Node native fetch - handles http/https automatically
const res = await fetch(`${normalized}/api/v1/status/buildinfo`, {