Update format data gửi wiki

This commit is contained in:
nguyentrungthat 2025-12-02 11:19:24 +07:00
parent 1be935a805
commit ffad475c2b
3 changed files with 105 additions and 37 deletions

View File

@ -83,6 +83,17 @@ interface User {
userName: string
}
interface DataDPELP {
line: number
pid: any
vid: any
sn: any
mode: string
mac: string
license: any
issues: string[]
}
export default class LineConnection {
public client: net.Socket
public config: LineConfig
@ -94,7 +105,7 @@ export default class LineConnection {
private outputInventory: string
private outputScenario: string
private bufferCommand: string
public dataDPELP: string
public dataDPELP: DataDPELP | string
constructor(config: LineConfig, socketIO: any) {
this.config = config
@ -696,12 +707,12 @@ export default class LineConnection {
(Tóm tắt trạng thái tổng thể của hệ thống trong 24 ý)
issue:
(Tóm tắt cực ngắn gọn các lỗi/dấu hiệu bất thường, mỗi vấn đ 1 dòng, bỏ qua các vấn đ không quan trọng như về port up/down hay Invalid input, Incomplete command)
(Tóm tắt cực ngắn gọn các lỗi/dấu hiệu bất thường, mỗi vấn đ 1 dòng, bỏ qua các vấn đ không quan trọng như về port changed state to up/down hay Invalid input, Incomplete command)
Quy tắc:
Không giải thích dài dòng.
Chỉ tập trung vào lỗi, cảnh báo, thay đi trạng thái up/down bất thường.
Nếu log không lỗi ghi Không phát hiện vấn đ.
Nếu log không lỗi ghi "No issues detected.".
Ngắn gọn, dễ đc, đúng format
Return only json format with English`,

View File

@ -137,53 +137,59 @@ export function isValidJson(string: string) {
}
}
export function mapToLineFormat(input: InputData): string {
export function mapToLineFormat(input: InputData) {
const line = input.lineNumber
// Inventory info
const pid = input.inventory?.pid || ''
const vid = input.inventory?.vid || ''
const sn = input.inventory?.sn || ''
if (!pid || !sn) return `Line ${line}: No data`
if (!pid || !sn) {
return {
line,
pid: '',
vid: '',
sn: '',
mode: '',
mac: '',
license: [],
issues: ['No data'],
}
}
// Find MAC address from "show version" or other sources
// MAC
let mac = ''
const showVersion = input.data?.find((d) => d.command === 'show version')
if (showVersion?.textfsm?.[0]?.MAC_ADDRESS) {
mac = showVersion.textfsm[0].MAC_ADDRESS
}
// Get data license
const dataLicense = input.data?.find((comm: any) => comm.command?.trim() === 'show license')
const listLicense =
dataLicense?.textfsm && Array.isArray(dataLicense?.textfsm)
? dataLicense?.textfsm?.map((val: any) => val.FEATURE).join(', ')
// License
const dataLicense = input.data?.find((comm) => comm.command?.trim() === 'show license')
const license =
dataLicense?.textfsm && Array.isArray(dataLicense.textfsm)
? dataLicense.textfsm.map((v: any) => v.FEATURE)
: ''
// Mode (DPEL / DPELP)
const dataPlatform = input.data?.find((el) => el.command?.trim() === 'show platform')
const DPELP = dataPlatform && !dataPlatform?.output?.includes('Incomplete') ? true : false
const mode = dataPlatform && !dataPlatform.output?.includes('Incomplete') ? 'DPELP' : 'DPEL'
// Detect AI issues
const issues = input.latestScenario?.detectAI?.issue || []
// Issues
const issues = Array.isArray(input.latestScenario?.detectAI?.issue)
? input.latestScenario.detectAI.issue
: input.latestScenario?.detectAI?.issue
? [input.latestScenario.detectAI.issue]
: []
// Build output
let output = `Line ${line}: `
output += `PID: ${pid}, `
output += `VID: ${vid}, `
output += `SN: ${sn}, `
output += `Tested mode: ${DPELP ? 'DPELP' : 'DPEL'}, `
output += `${mac ? 'MAC Address:' + mac + `, ` : ''} `
output += `${listLicense ? 'License: ' + listLicense : ''}`
if (Array.isArray(issues) && issues.length > 0) {
output += `\n Issues:\n`
for (const issue of issues) {
output += ` - ${issue}\n`
}
} else if (typeof issues === 'string') {
output += `\n Issues: ${issues}`
return {
line,
pid,
vid,
sn,
mode,
mac,
license,
issues,
}
return output.trim()
}

View File

@ -538,6 +538,7 @@ export class WebSocketIo {
console.log('[DPELP] Received run all dpelp')
const results = await this.waitUntilAllReady(lineIds)
const tableHTML = this.generateTable(results)
const d = new Date(Date.now())
const pad = (n: number) => String(n).padStart(2, '0')
@ -548,7 +549,7 @@ export class WebSocketIo {
const linkWiki =
process.env.LINK_WIKI || 'https://logs.danielvu.com/api/wiki/page/insert?title=Dev_test'
await axios.post(linkWiki, {
data: `<pre>${results.join('\n\n')}\n</pre>`,
data: tableHTML,
titleAuto: 'AUTO - ' + dataFormat,
})
})
@ -1026,10 +1027,10 @@ export class WebSocketIo {
}
async waitUntilAllReady(lineIds: number[]) {
return new Promise<string[]>((resolve) => {
return new Promise<any[]>((resolve) => {
const interval = setInterval(() => {
let allReady = true
const results: string[] = []
const results = []
for (const lineId of lineIds) {
const line = this.lineMap.get(lineId)
if (!line || !line.dataDPELP) {
@ -1047,4 +1048,54 @@ export class WebSocketIo {
}, 5000) // check mỗi 5 giây
})
}
generateTable(results: any) {
let html = `<table border="1" cellpadding="6" style="border-collapse: collapse; font-size: 14px;">
<tr>
<th style="text-align:center; width:65px;">Line</th>
<th style="width:180px;">PID</th>
<th style="text-align:center; width:140px;">SN</th>
<th>MAC</th>
<th>Mode</th>
<th style="width:600px; word-wrap: break-word; white-space: normal;">License</th>
<th>Issues</th>
</tr>
`
for (const item of results) {
const licenses = Array.isArray(item.license)
? item.license
: item.license
? [item.license]
: []
// format license thành 3 cột
const licenseHTML = `
<div style="
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 4px;
width: 100%;
word-break: break-word;
">
${licenses.map((l: string) => `<div>${l}</div>`).join('')}
</div>
`
html += `
<tr>
<td style="text-align:center;">${item.line}</td>
<td><div style="display:flex;"><div style="width:135px; margin-right:4px;">${item.pid}</div><div>| ${item.vid}</div></div></td>
<td style="text-align:center;">${item.sn}</td>
<td>${item.mac}</td>
<td>${item.mode}</td>
<td style="width:600px;">${licenseHTML}</td>
<td>${item.issues?.length ? `- ` + item.issues.join(`<br>- `) : ''}</td>
</tr>
`
}
html += `</table>\n\n`
return html
}
}