支持数据源编辑
This commit is contained in:
@@ -2121,7 +2121,8 @@
|
|||||||
${source.description ? `<div class="source-item-desc">${escapeHtml(source.description)}</div>` : ''}
|
${source.description ? `<div class="source-item-desc">${escapeHtml(source.description)}</div>` : ''}
|
||||||
</div>
|
</div>
|
||||||
<div class="source-item-actions">
|
<div class="source-item-actions">
|
||||||
<button class="btn btn-delete" onclick="deleteSource(${source.id})">删除</button>
|
<button class="btn btn-secondary btn-sm" onclick="editSource(${JSON.stringify(source).replace(/"/g, '"')})">编辑</button>
|
||||||
|
<button class="btn btn-delete btn-sm" onclick="deleteSource(${source.id})">删除</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`).join('');
|
`).join('');
|
||||||
@@ -2166,6 +2167,55 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---- Add Source ----
|
// ---- Add Source ----
|
||||||
|
let editingSourceId = null;
|
||||||
|
|
||||||
|
window.editSource = function(source) {
|
||||||
|
editingSourceId = source.id;
|
||||||
|
dom.sourceName.value = source.name || '';
|
||||||
|
dom.sourceUrl.value = source.url || '';
|
||||||
|
dom.sourceType.value = source.type || 'prometheus';
|
||||||
|
dom.sourceDesc.value = source.description || '';
|
||||||
|
dom.isServerSource.checked = !!source.is_server_source;
|
||||||
|
|
||||||
|
// Toggle Blackbox UI
|
||||||
|
if (source.type === 'blackbox') {
|
||||||
|
dom.serverSourceOption.style.display = 'none';
|
||||||
|
} else {
|
||||||
|
dom.serverSourceOption.style.display = 'flex';
|
||||||
|
}
|
||||||
|
|
||||||
|
dom.btnAdd.textContent = '保存修改';
|
||||||
|
|
||||||
|
// Add cancel button if not already there
|
||||||
|
if (!document.getElementById('btnCancelEditSource')) {
|
||||||
|
const cancelBtn = document.createElement('button');
|
||||||
|
cancelBtn.id = 'btnCancelEditSource';
|
||||||
|
cancelBtn.className = 'btn btn-secondary';
|
||||||
|
cancelBtn.style.marginLeft = '8px';
|
||||||
|
cancelBtn.textContent = '取消';
|
||||||
|
cancelBtn.onclick = cancelEditSource;
|
||||||
|
dom.btnAdd.parentNode.appendChild(cancelBtn);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scroll to form
|
||||||
|
dom.sourceName.focus();
|
||||||
|
};
|
||||||
|
|
||||||
|
function cancelEditSource() {
|
||||||
|
editingSourceId = null;
|
||||||
|
dom.sourceName.value = '';
|
||||||
|
dom.sourceUrl.value = '';
|
||||||
|
dom.sourceType.value = 'prometheus';
|
||||||
|
dom.sourceDesc.value = '';
|
||||||
|
dom.isServerSource.checked = true;
|
||||||
|
dom.serverSourceOption.style.display = 'flex';
|
||||||
|
dom.btnAdd.textContent = '添加';
|
||||||
|
|
||||||
|
const cancelBtn = document.getElementById('btnCancelEditSource');
|
||||||
|
if (cancelBtn) cancelBtn.remove();
|
||||||
|
hideMessage();
|
||||||
|
}
|
||||||
|
|
||||||
async function addSource() {
|
async function addSource() {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
showMessage('请先登录后操作', 'error');
|
showMessage('请先登录后操作', 'error');
|
||||||
@@ -2185,21 +2235,29 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
dom.btnAdd.textContent = '添加中...';
|
const isEditing = editingSourceId !== null;
|
||||||
|
dom.btnAdd.textContent = isEditing ? '保存中...' : '添加中...';
|
||||||
dom.btnAdd.disabled = true;
|
dom.btnAdd.disabled = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/sources', {
|
const urlPath = isEditing ? `/api/sources/${editingSourceId}` : '/api/sources';
|
||||||
method: 'POST',
|
const method = isEditing ? 'PUT' : 'POST';
|
||||||
|
|
||||||
|
const response = await fetch(urlPath, {
|
||||||
|
method: method,
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ name, url, description, is_server_source, type })
|
body: JSON.stringify({ name, url, description, is_server_source, type })
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
showMessage('数据源添加成功', 'success');
|
showMessage(isEditing ? '数据源更新成功' : '数据源添加成功', 'success');
|
||||||
dom.sourceName.value = '';
|
if (isEditing) {
|
||||||
dom.sourceUrl.value = '';
|
cancelEditSource();
|
||||||
dom.sourceDesc.value = '';
|
} else {
|
||||||
|
dom.sourceName.value = '';
|
||||||
|
dom.sourceUrl.value = '';
|
||||||
|
dom.sourceDesc.value = '';
|
||||||
|
}
|
||||||
loadSources();
|
loadSources();
|
||||||
fetchMetrics();
|
fetchMetrics();
|
||||||
fetchNetworkHistory();
|
fetchNetworkHistory();
|
||||||
|
|||||||
@@ -507,7 +507,7 @@ app.post('/api/sources', requireAuth, async (req, res) => {
|
|||||||
|
|
||||||
// Update a Prometheus source
|
// Update a Prometheus source
|
||||||
app.put('/api/sources/:id', requireAuth, async (req, res) => {
|
app.put('/api/sources/:id', requireAuth, async (req, res) => {
|
||||||
let { name, url, description, is_server_source } = req.body;
|
let { name, url, description, is_server_source, type } = req.body;
|
||||||
if (url && !/^https?:\/\//i.test(url)) url = 'http://' + url;
|
if (url && !/^https?:\/\//i.test(url)) url = 'http://' + url;
|
||||||
try {
|
try {
|
||||||
await db.query(
|
await db.query(
|
||||||
|
|||||||
Reference in New Issue
Block a user