修改数据源查询逻辑
This commit is contained in:
@@ -820,8 +820,16 @@ app.post('/api/sources', requireAuth, async (req, res) => {
|
||||
if (!/^https?:\/\//i.test(url)) url = 'http://' + url;
|
||||
try {
|
||||
const [result] = await db.query(
|
||||
'INSERT INTO prometheus_sources (name, url, description, is_server_source, type) VALUES (?, ?, ?, ?, ?)',
|
||||
[name, url, description || '', is_server_source === undefined ? 1 : (is_server_source ? 1 : 0), type || 'prometheus']
|
||||
'INSERT INTO prometheus_sources (name, url, description, is_server_source, is_overview_source, is_detail_source, type) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
[
|
||||
name,
|
||||
url,
|
||||
description || '',
|
||||
is_server_source === undefined ? 1 : (is_server_source ? 1 : 0),
|
||||
req.body.is_overview_source === undefined ? 1 : (req.body.is_overview_source ? 1 : 0),
|
||||
req.body.is_detail_source === undefined ? 1 : (req.body.is_detail_source ? 1 : 0),
|
||||
type || 'prometheus'
|
||||
]
|
||||
);
|
||||
const [rows] = await db.query('SELECT * FROM prometheus_sources WHERE id = ?', [result.insertId]);
|
||||
|
||||
@@ -841,8 +849,17 @@ app.put('/api/sources/:id', requireAuth, async (req, res) => {
|
||||
if (url && !/^https?:\/\//i.test(url)) url = 'http://' + url;
|
||||
try {
|
||||
await db.query(
|
||||
'UPDATE prometheus_sources SET name = ?, url = ?, description = ?, is_server_source = ?, type = ? WHERE id = ?',
|
||||
[name, url, description || '', is_server_source ? 1 : 0, type || 'prometheus', req.params.id]
|
||||
'UPDATE prometheus_sources SET name = ?, url = ?, description = ?, is_server_source = ?, is_overview_source = ?, is_detail_source = ?, type = ? WHERE id = ?',
|
||||
[
|
||||
name,
|
||||
url,
|
||||
description || '',
|
||||
is_server_source ? 1 : 0,
|
||||
req.body.is_overview_source ? 1 : 0,
|
||||
req.body.is_detail_source ? 1 : 0,
|
||||
type || 'prometheus',
|
||||
req.params.id
|
||||
]
|
||||
);
|
||||
// Clear network history cache
|
||||
await cache.del('network_history_all');
|
||||
@@ -994,7 +1011,7 @@ 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 [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,
|
||||
@@ -1023,7 +1040,12 @@ async function getOverview(force = false) {
|
||||
|
||||
try {
|
||||
const metrics = await prometheusService.getOverviewMetrics(source.url, source.name);
|
||||
const enrichedMetrics = { ...metrics, sourceName: source.name };
|
||||
const enrichedMetrics = {
|
||||
...metrics,
|
||||
sourceName: source.name,
|
||||
isOverview: !!source.is_overview_source,
|
||||
isDetail: !!source.is_detail_source
|
||||
};
|
||||
|
||||
await cache.set(cacheKey, enrichedMetrics, 15); // Cache for 15s
|
||||
return enrichedMetrics;
|
||||
@@ -1046,14 +1068,16 @@ async function getOverview(force = false) {
|
||||
let allServers = [];
|
||||
|
||||
for (const m of validMetrics) {
|
||||
totalServers += m.totalServers;
|
||||
activeServers += (m.activeServers !== undefined ? m.activeServers : m.totalServers);
|
||||
cpuUsed += m.cpu.used;
|
||||
cpuTotal += m.cpu.total;
|
||||
memUsed += m.memory.used;
|
||||
memTotal += m.memory.total;
|
||||
diskUsed += m.disk.used;
|
||||
diskTotal += m.disk.total;
|
||||
if (m.isOverview) {
|
||||
totalServers += m.totalServers;
|
||||
activeServers += (m.activeServers !== undefined ? m.activeServers : m.totalServers);
|
||||
cpuUsed += m.cpu.used;
|
||||
cpuTotal += m.cpu.total;
|
||||
memUsed += m.memory.used;
|
||||
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)) {
|
||||
@@ -1063,7 +1087,9 @@ async function getOverview(force = false) {
|
||||
traffic24hTx += m.traffic24h.tx;
|
||||
}
|
||||
|
||||
allServers = allServers.concat(m.servers);
|
||||
if (m.isDetail) {
|
||||
allServers = allServers.concat(m.servers);
|
||||
}
|
||||
}
|
||||
|
||||
const overview = {
|
||||
@@ -1172,7 +1198,7 @@ app.get('/api/metrics/network-history', async (req, res) => {
|
||||
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 query = 'SELECT * FROM prometheus_sources WHERE is_overview_source = 1 AND type != "blackbox"';
|
||||
let params = [];
|
||||
|
||||
if (selectedSourcesStr) {
|
||||
@@ -1212,7 +1238,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 [sources] = await db.query('SELECT * FROM prometheus_sources WHERE is_server_source = 1 AND type != "blackbox"');
|
||||
const [sources] = await db.query('SELECT * FROM prometheus_sources WHERE is_overview_source = 1 AND type != "blackbox"');
|
||||
if (sources.length === 0) {
|
||||
return res.json({ timestamps: [], values: [] });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user