添加延迟显示支持
This commit is contained in:
@@ -264,6 +264,10 @@ app.post('/api/setup/init', async (req, res) => {
|
||||
default_theme VARCHAR(20) DEFAULT 'dark',
|
||||
show_95_bandwidth TINYINT(1) DEFAULT 0,
|
||||
p95_type VARCHAR(20) DEFAULT 'tx',
|
||||
blackbox_source_id INT,
|
||||
latency_source VARCHAR(100),
|
||||
latency_dest VARCHAR(100),
|
||||
latency_target VARCHAR(255),
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||
`);
|
||||
@@ -403,7 +407,11 @@ const serveIndex = async (req, res) => {
|
||||
page_name: '数据可视化展示大屏',
|
||||
title: '数据可视化展示大屏',
|
||||
logo_url: null,
|
||||
default_theme: 'dark'
|
||||
default_theme: 'dark',
|
||||
blackbox_source_id: null,
|
||||
latency_source: null,
|
||||
latency_dest: null,
|
||||
latency_target: null
|
||||
};
|
||||
|
||||
if (isDbInitialized) {
|
||||
@@ -439,7 +447,7 @@ app.use(express.static(path.join(__dirname, '..', 'public'), { index: false }));
|
||||
// Get all Prometheus sources
|
||||
app.get('/api/sources', async (req, res) => {
|
||||
try {
|
||||
const [rows] = await db.query('SELECT * FROM prometheus_sources ORDER BY created_at DESC');
|
||||
const [rows] = await db.query('SELECT * FROM prometheus_sources ORDER BY is_server_source DESC, created_at DESC');
|
||||
// Test connectivity for each source
|
||||
const sourcesWithStatus = await Promise.all(rows.map(async (source) => {
|
||||
try {
|
||||
@@ -458,15 +466,15 @@ app.get('/api/sources', async (req, res) => {
|
||||
|
||||
// Add a new Prometheus source
|
||||
app.post('/api/sources', requireAuth, async (req, res) => {
|
||||
let { name, url, description } = req.body;
|
||||
let { name, url, description, is_server_source } = req.body;
|
||||
if (!name || !url) {
|
||||
return res.status(400).json({ error: 'Name and URL are required' });
|
||||
}
|
||||
if (!/^https?:\/\//i.test(url)) url = 'http://' + url;
|
||||
try {
|
||||
const [result] = await db.query(
|
||||
'INSERT INTO prometheus_sources (name, url, description) VALUES (?, ?, ?)',
|
||||
[name, url, description || '']
|
||||
'INSERT INTO prometheus_sources (name, url, description, is_server_source) VALUES (?, ?, ?, ?)',
|
||||
[name, url, description || '', is_server_source === undefined ? 1 : (is_server_source ? 1 : 0)]
|
||||
);
|
||||
const [rows] = await db.query('SELECT * FROM prometheus_sources WHERE id = ?', [result.insertId]);
|
||||
|
||||
@@ -482,12 +490,12 @@ app.post('/api/sources', requireAuth, async (req, res) => {
|
||||
|
||||
// Update a Prometheus source
|
||||
app.put('/api/sources/:id', requireAuth, async (req, res) => {
|
||||
let { name, url, description } = req.body;
|
||||
let { name, url, description, is_server_source } = req.body;
|
||||
if (url && !/^https?:\/\//i.test(url)) url = 'http://' + url;
|
||||
try {
|
||||
await db.query(
|
||||
'UPDATE prometheus_sources SET name = ?, url = ?, description = ? WHERE id = ?',
|
||||
[name, url, description || '', req.params.id]
|
||||
'UPDATE prometheus_sources SET name = ?, url = ?, description = ?, is_server_source = ? WHERE id = ?',
|
||||
[name, url, description || '', is_server_source ? 1 : 0, req.params.id]
|
||||
);
|
||||
// Clear network history cache
|
||||
await cache.del('network_history_all');
|
||||
@@ -537,7 +545,11 @@ app.get('/api/settings', async (req, res) => {
|
||||
title: '数据可视化展示大屏',
|
||||
logo_url: null,
|
||||
show_95_bandwidth: 0,
|
||||
p95_type: 'tx'
|
||||
p95_type: 'tx',
|
||||
blackbox_source_id: null,
|
||||
latency_source: null,
|
||||
latency_dest: null,
|
||||
latency_target: null
|
||||
});
|
||||
}
|
||||
res.json(rows[0]);
|
||||
@@ -549,19 +561,28 @@ app.get('/api/settings', async (req, res) => {
|
||||
|
||||
// Update site settings
|
||||
app.post('/api/settings', requireAuth, async (req, res) => {
|
||||
const { page_name, title, logo_url, default_theme, show_95_bandwidth, p95_type } = req.body;
|
||||
const { page_name, title, logo_url, default_theme, show_95_bandwidth, p95_type, blackbox_source_id, latency_source, latency_dest, latency_target } = req.body;
|
||||
try {
|
||||
await db.query(
|
||||
`INSERT INTO site_settings (id, page_name, title, logo_url, default_theme, show_95_bandwidth, p95_type)
|
||||
VALUES (1, ?, ?, ?, ?, ?, ?)
|
||||
`INSERT INTO site_settings (id, page_name, title, logo_url, default_theme, show_95_bandwidth, p95_type, blackbox_source_id, latency_source, latency_dest, latency_target)
|
||||
VALUES (1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
page_name = VALUES(page_name),
|
||||
title = VALUES(title),
|
||||
logo_url = VALUES(logo_url),
|
||||
default_theme = VALUES(default_theme),
|
||||
show_95_bandwidth = VALUES(show_95_bandwidth),
|
||||
p95_type = VALUES(p95_type)`,
|
||||
[page_name, title, logo_url, default_theme, show_95_bandwidth ? 1 : 0, p95_type || 'tx']
|
||||
p95_type = VALUES(p95_type),
|
||||
blackbox_source_id = VALUES(blackbox_source_id),
|
||||
latency_source = VALUES(latency_source),
|
||||
latency_dest = VALUES(latency_dest),
|
||||
latency_target = VALUES(latency_target)`,
|
||||
[
|
||||
page_name, title, logo_url, default_theme,
|
||||
show_95_bandwidth ? 1 : 0, p95_type || 'tx',
|
||||
blackbox_source_id || null, latency_source || null,
|
||||
latency_dest || null, latency_target || null
|
||||
]
|
||||
);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
@@ -574,7 +595,7 @@ app.post('/api/settings', requireAuth, async (req, res) => {
|
||||
|
||||
// Reusable function to get overview metrics
|
||||
async function getOverview() {
|
||||
const [sources] = await db.query('SELECT * FROM prometheus_sources');
|
||||
const [sources] = await db.query('SELECT * FROM prometheus_sources WHERE is_server_source = 1');
|
||||
if (sources.length === 0) {
|
||||
return {
|
||||
totalServers: 0,
|
||||
@@ -715,7 +736,7 @@ app.get('/api/metrics/network-history', async (req, res) => {
|
||||
const cached = await cache.get(cacheKey);
|
||||
if (cached) return res.json(cached);
|
||||
|
||||
const [sources] = await db.query('SELECT * FROM prometheus_sources');
|
||||
const [sources] = await db.query('SELECT * FROM prometheus_sources WHERE is_server_source = 1');
|
||||
if (sources.length === 0) {
|
||||
return res.json({ timestamps: [], rx: [], tx: [] });
|
||||
}
|
||||
@@ -744,7 +765,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');
|
||||
const [sources] = await db.query('SELECT * FROM prometheus_sources WHERE is_server_source = 1');
|
||||
if (sources.length === 0) {
|
||||
return res.json({ timestamps: [], values: [] });
|
||||
}
|
||||
@@ -821,6 +842,28 @@ app.get('*', (req, res, next) => {
|
||||
});
|
||||
|
||||
|
||||
// Get latency for A-B connection
|
||||
app.get('/api/metrics/latency', async (req, res) => {
|
||||
try {
|
||||
const [settings] = await db.query('SELECT blackbox_source_id, latency_target FROM site_settings WHERE id = 1');
|
||||
if (settings.length === 0 || !settings[0].blackbox_source_id || !settings[0].latency_target) {
|
||||
return res.json({ latency: null });
|
||||
}
|
||||
|
||||
// Lookup source URL from the source ID
|
||||
const [sources] = await db.query('SELECT url FROM prometheus_sources WHERE id = ?', [settings[0].blackbox_source_id]);
|
||||
if (sources.length === 0) {
|
||||
return res.json({ latency: null });
|
||||
}
|
||||
|
||||
const latency = await prometheusService.getLatency(sources[0].url, settings[0].latency_target);
|
||||
res.json({ latency });
|
||||
} catch (err) {
|
||||
console.error('Error fetching latency:', err);
|
||||
res.status(500).json({ error: 'Failed to fetch latency' });
|
||||
}
|
||||
});
|
||||
|
||||
// ==================== WebSocket Server ====================
|
||||
|
||||
const server = http.createServer(app);
|
||||
|
||||
Reference in New Issue
Block a user