fix: preserve advanced snmp oid json fields

This commit is contained in:
zxr
2026-07-05 18:31:47 +08:00
parent c3f38f5e2a
commit 83b713890b

View File

@@ -314,6 +314,8 @@ const createEmptySnmpOidRow = (): SnmpOidRow => ({
type: '',
})
const snmpOidTableFields = ['oid', 'metric_name', 'metric_unit', 'type']
const normalizeSnmpOidRows = (rows: SnmpOidRow[]) =>
rows
.map((row) => ({
@@ -337,6 +339,28 @@ const findIncompleteSnmpOidRowIndex = (rows: SnmpOidRow[]) =>
return Boolean(hasAnyValue && (!normalizedRow.oid || !normalizedRow.metric_name))
})
const parseSnmpOidJsonArray = (raw: string) => {
const text = raw.trim()
if (!text) {
return []
}
const parsed = JSON.parse(text)
if (!Array.isArray(parsed)) {
throw new Error('SNMP OID 配置必须是 JSON 数组')
}
return parsed
}
const hasSnmpOidTableUnsupportedFields = (raw: string) => {
const rows = parseSnmpOidJsonArray(raw)
return rows.some((item) => {
if (!item || typeof item !== 'object' || Array.isArray(item)) {
return Boolean(item)
}
return Object.keys(item).some((key) => !snmpOidTableFields.includes(key))
})
}
const stringifySnmpOidRows = (rows: SnmpOidRow[]) => {
const normalizedRows = normalizeSnmpOidRows(rows)
return normalizedRows.length ? JSON.stringify(normalizedRows, null, 2) : ''
@@ -347,10 +371,7 @@ const parseSnmpOidRows = (raw: string): SnmpOidRow[] => {
if (!text) {
return []
}
const parsed = JSON.parse(text)
if (!Array.isArray(parsed)) {
throw new Error('SNMP OID 配置必须是 JSON 数组')
}
const parsed = parseSnmpOidJsonArray(text)
return parsed.map((item) => ({
oid: String(item?.oid ?? '').trim(),
metric_name: String(item?.metric_name ?? '').trim(),
@@ -432,7 +453,12 @@ watch(
})
snmpOidEditMode.value = 'table'
try {
syncSnmpOidRowsFromJson()
if (hasSnmpOidTableUnsupportedFields(formData.snmp_oids || '')) {
snmpOidEditMode.value = 'json'
snmpOidRows.value = []
} else {
syncSnmpOidRowsFromJson()
}
} catch {
snmpOidEditMode.value = 'json'
snmpOidRows.value = []
@@ -495,6 +521,10 @@ const switchSnmpOidEditMode = () => {
return
}
try {
if (hasSnmpOidTableUnsupportedFields(formData.snmp_oids)) {
Message.warning('当前 SNMP OID JSON 包含表格不支持的字段,请在 JSON 模式下编辑')
return
}
syncSnmpOidRowsFromJson()
snmpOidEditMode.value = 'table'
} catch {
@@ -521,6 +551,7 @@ const validateSnmpOidConfigBeforeSubmit = () => {
}
try {
const parsedRows = parseSnmpOidJsonArray(formData.snmp_oids)
const rows = parseSnmpOidRows(formData.snmp_oids)
const invalidIndex = findIncompleteSnmpOidRowIndex(rows)
if (invalidIndex >= 0) {
@@ -529,7 +560,7 @@ const validateSnmpOidConfigBeforeSubmit = () => {
}
const normalizedRows = normalizeSnmpOidRows(rows)
snmpOidRows.value = normalizedRows
formData.snmp_oids = normalizedRows.length ? JSON.stringify(normalizedRows, null, 2) : ''
formData.snmp_oids = JSON.stringify(parsedRows, null, 2)
return true
} catch {
Message.warning('SNMP OID 配置必须是合法 JSON 数组')