ATC_SIMPLE/BACKEND/app/ultils/templates/show_env.ts

64 lines
1.9 KiB
TypeScript

import XRegExp from 'xregexp'
const parseShowEnvironment = (data: string) => {
const patterns = [
// 1. Dạng Stack hiện đại: "1 1 OK" (Ưu tiên cao nhất để tránh nhầm với số)
XRegExp(
'^\\s*(?<SWITCH_ID>\\d+)\\s+(?<NAME>\\d+)\\s+(?:\\w+\\s+)?(?<STATE>OK|Faulty|Normal|Failure).*?$'
),
// 2. Dạng Sensor/Table phức tạp (ISR 4k/ASR): "Sensor Value State"
// VD: "Temp: CPU Die Temperature 47 Celsius Normal"
XRegExp(
'^(?<TYPE>Temp|Fan|Voltage|Power|Sensor):?\\s*(?<NAME>.+?)\\s+(?<VALUE>\\d+(?:\\s+Deg\\s+C|\\s*Celsius|\\s*V|\\s*W)?)\\s+(?<STATE>Normal|Critical|Shutdown|Warning|OK)$',
'i'
),
// 3. Dạng: FAN is OK / POWER is OK / RPS is NOT PRESENT
XRegExp('^(?<NAME>[A-Z ]+?)\\s+is\\s+(?<STATE>.+)$'),
// 4. Dạng: FAN 1 is OK / POWER SUPPLY A is NOT PRESENT
XRegExp('^(?<TYPE>[A-Z ]+?)\\s+(?<NAME>[A-Z0-9]+)\\s+is\\s+(?<STATE>.+)$'),
// 5. Dạng bảng: Temp: Inlet Front Normal
XRegExp(
'^(?<TYPE>Temp|Fan|Voltage|Power):?\\s*(?<NAME>[^\\s]+)\\s+(?<LOCATION>[^\\s]+)\\s+(?<STATE>.+)$'
),
// 6. Dạng: Inlet Temperature Value: 27 Degree Celsius
XRegExp('^(?<TYPE>.*?Temperature).*?:\\s*(?<VALUE>.+)$'),
]
const lines = data.split('\n')
const records: any[] = []
for (let line of lines) {
line = line.trim()
if (!line) continue
// Bỏ prompt
if (line.startsWith('Switch#')) continue
for (const p of patterns) {
const m = XRegExp.exec(line, p)
if (m?.groups) {
const record: any = {
TYPE: m.groups.TYPE?.trim() || '',
NAME: m.groups.NAME?.trim() || '',
LOCATION: m.groups.LOCATION?.trim() || '',
STATE: m.groups.STATE?.trim() || '',
VALUE: m.groups.VALUE?.trim() || '',
RAW: line,
}
records.push(record)
break
}
}
}
return records
}
export default parseShowEnvironment