Improve log handling and email reporting

Refactored log buffer management to use 'allBuffer' for complete log storage and updated email content to include raw logs. Enhanced log table formatting and removed status from email header. Commented out interface up/down rules in helper.ts to adjust log rule detection.
This commit is contained in:
nguyentrungthat 2025-12-23 09:36:46 +07:00
parent 0af1cd8747
commit 48a50b5628
2 changed files with 41 additions and 40 deletions

View File

@ -169,15 +169,15 @@ export default class LineConnection {
lineNumber, lineNumber,
status: 'connected', status: 'connected',
}) })
// this.checkLog() this.checkLog()
resolve() resolve()
}, 1000) }, 1000)
}) })
this.client.on('data', (data) => { this.client.on('data', (data) => {
let message = this.connecting ? cleanData(data.toString()) : data.toString() let message = this.connecting ? cleanData(data.toString()) : data.toString()
// const lines = this.bufferLog.push(data) const lines = this.bufferLog.push(data)
// lines.forEach(this.handleLogLine) lines.forEach(this.handleLogLine)
let rawData = '' let rawData = ''
if (this.isRunningScript) { if (this.isRunningScript) {
this.waitingScenario = true this.waitingScenario = true
@ -806,31 +806,31 @@ export default class LineConnection {
try { try {
if (this.config.status !== 'connected') { if (this.config.status !== 'connected') {
clearInterval(interval) clearInterval(interval)
this.session.clear()
this.bufferLog.clear()
return return
} }
const result = this.session.finalize() const result = this.session.finalize()
if (result.errors.length === 0) return if (result.errors.length === 0) {
this.session.clear()
this.bufferLog.clear()
return
}
// console.log('===== TEST RESULT =====') const detectLog = await this.detectLogWithAI(this.bufferLog.allBuffer)
// console.log('STATUS:', result.status)
// console.log('SUMMARY:', result.summary)
// result.errors.forEach((err, idx) => {
// console.log(`\n[${idx + 1}] ${err.level} - ${err.ruleId}`)
// console.log('Message:', err.message)
// console.log('Log:', err.evidence.raw)
// })
const detectLog = await this.detectLogWithAI(this.bufferLog.buffer)
// console.log(detectLog) // console.log(detectLog)
const tableHTML = this.buildEmailContent(result, detectLog) const tableHTML = this.buildEmailContent(result, detectLog)
await sendMessageToMail( await sendMessageToMail(
'andrew.ng@apactech.io', 'andrew.ng@apactech.io',
`[${result.status}] - [${this.config.stationName} - Line: ${this.config.lineNumber}] - Cisco Device Log Result`, `[ATC] - [${this.config.stationName} - Line: ${this.config.lineNumber}] - Raw log issue`,
tableHTML tableHTML +
// , `${`
// ['ips@ipsupply.com.au', 'kay@ipsupply.com.au', 'joseph@apactech.io'] <hr />
<p>Logs:</p>
<div style="white-space: break-spaces; background-color: #f5f5f5; color: black; padding: 8px; max-height: 500px; overflow-y: scroll; border: 1px #ccc solid;"><span style="color: black;">
${this.bufferLog.allBuffer}</span></div>`}`,
['ips@ipsupply.com.au', 'kay@ipsupply.com.au', 'joseph@apactech.io']
) )
this.session.clear() this.session.clear()
this.bufferLog.clear() this.bufferLog.clear()
@ -847,8 +847,8 @@ export default class LineConnection {
const header = ` const header = `
<tr> <tr>
<th style="padding:6px;">Level</th> <th style="padding:6px; text-align:center;">Level</th>
<th style="padding:6px;">Rule</th> <th style="padding:6px; text-align:center;">Rule</th>
<th style="padding:6px; text-align:center;">Message</th> <th style="padding:6px; text-align:center;">Message</th>
<th style="padding:6px; width:1000px;">Log Evidence</th> <th style="padding:6px; width:1000px;">Log Evidence</th>
</tr> </tr>
@ -881,7 +881,7 @@ export default class LineConnection {
renderAIDetectTable(row: any): string { renderAIDetectTable(row: any): string {
return ` return `
<table border="1" cellpadding="6" style="border-collapse: collapse; width:100%;"> <table border="1" cellpadding="6" style="border-collapse: collapse; width:100%; margin-bottom: 15px;">
<tr> <tr>
<th style="padding:6px;">Summary</th> <th style="padding:6px;">Summary</th>
<th style="padding:6px;">Issues</th> <th style="padding:6px;">Issues</th>
@ -902,7 +902,6 @@ export default class LineConnection {
return ` return `
<h3>Cisco Device Log Result</h3> <h3>Cisco Device Log Result</h3>
<p>Line: <b>${this.config.lineNumber}</b> - Station: <b>${this.config.stationName}</b></p> <p>Line: <b>${this.config.lineNumber}</b> - Station: <b>${this.config.stationName}</b></p>
<p><b>Status:</b> ${result.status}</p>
<p><b>Summary:</b> ${result.summary}</p> <p><b>Summary:</b> ${result.summary}</p>
<hr /> <hr />
${table} ${table}

View File

@ -410,20 +410,20 @@ export const RULES: LogRule[] = [
message: 'Critical features disabled by license', message: 'Critical features disabled by license',
}, },
// INTERFACE // INTERFACE
{ // {
id: 'INTERFACE_UP', // id: 'INTERFACE_UP',
category: 'INTERFACE', // category: 'INTERFACE',
match: /LINK-3-UPDOWN: Interface .* up/i, // match: /LINK-3-UPDOWN: Interface .* up/i,
level: 'PASS', // level: 'PASS',
message: 'Interface up', // message: 'Interface up',
}, // },
{ // {
id: 'INTERFACE_FLAP', // id: 'INTERFACE_FLAP',
category: 'INTERFACE', // category: 'INTERFACE',
match: /LINK-3-UPDOWN: Interface .* down/i, // match: /LINK-3-UPDOWN: Interface .* down/i,
level: 'WARN', // level: 'WARN',
message: 'Interface flapping detected', // message: 'Interface flapping detected',
}, // },
{ {
id: 'INTERFACE_ERROR', id: 'INTERFACE_ERROR',
category: 'INTERFACE', category: 'INTERFACE',
@ -576,15 +576,17 @@ export class TestSession {
} }
export class LogStreamBuffer { export class LogStreamBuffer {
public buffer = '' public allBuffer = ''
private buffer = ''
public push(chunk: Buffer): string[] { public push(chunk: Buffer): string[] {
this.buffer += chunk.toString('utf8').replace('--More--', '').trim() this.buffer += chunk.toString('utf8').replace('--More--', '').trim()
this.allBuffer += cleanData(chunk.toString())
const lines = this.buffer.split(/\r?\n/) const lines = this.buffer.split(/\r?\n/)
this.buffer = lines.pop() || '' this.buffer = lines.pop() || ''
return lines.map((l) => l.trim()).filter(Boolean) return lines.map((l) => l.replaceAll('--More--', '').trim()).filter(Boolean)
} }
public flush(): string | null { public flush(): string | null {
@ -595,7 +597,7 @@ export class LogStreamBuffer {
} }
public clear() { public clear() {
this.buffer = '' this.allBuffer = ''
} }
} }