diff --git a/public/js/app.js b/public/js/app.js index ba8c938..be39672 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -2070,6 +2070,18 @@ const savedTheme = localStorage.getItem('theme'); const themeToApply = savedTheme || settings.default_theme || 'dark'; applyTheme(themeToApply); + + // Apply settings to UI (logo, name, etc.) + applySiteSettings(window.SITE_SETTINGS); + + // Refresh overview and historical charts to reflect new source selections + fetchNetworkHistory(true); + // We can't force the WS broadcast easily from client, + // but we can fetch the overview via REST API once to update UI + fetch('/api/metrics/overview?force=true') + .then(res => res.json()) + .then(data => updateDashboard(data)) + .catch(() => {}); } else { const err = await response.json(); showSiteMessage(`保存失败: ${err.error || '未知错误'}`, 'error'); diff --git a/server/index.js b/server/index.js index cc6f49f..f9633a8 100644 --- a/server/index.js +++ b/server/index.js @@ -964,7 +964,21 @@ app.post('/api/settings', requireAuth, async (req, res) => { // Reusable function to get overview metrics async function getOverview(force = false) { - const [sources] = await db.query('SELECT * FROM prometheus_sources WHERE is_server_source = 1 AND type != "blackbox"'); + 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); if (sources.length === 0) { return { totalServers: 0, @@ -1159,7 +1173,21 @@ 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 [sources] = await db.query('SELECT * FROM prometheus_sources WHERE is_server_source = 1 AND type != "blackbox"'); + 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); if (sources.length === 0) { return res.json({ timestamps: [], values: [] }); }