63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import XRegExp from 'xregexp'
|
|
|
|
// Patterns for each field
|
|
|
|
// Parser function
|
|
const parseLog = (data: string) => {
|
|
const patterns = [
|
|
XRegExp('^Index\\s+\\d+\\s+Feature:\\s+(?<FEATURE>\\S+)'),
|
|
XRegExp('^Period\\s+left:\\s+(?<PERIOD_LEFT>.+)'),
|
|
XRegExp('^Period\\s+Used:\\s+(?<PERIOD_USED>.+)'),
|
|
XRegExp('^License\\s+Type:\\s+(?<LICENSE_TYPE>.+)'),
|
|
XRegExp('^License\\s+State:\\s+(?<LICENSE_STATE>.+)'),
|
|
XRegExp('^License\\s+Count:\\s+(?<LICENSE_COUNT>.+)'),
|
|
XRegExp('^License\\s+Priority:\\s+(?<LICENSE_PRIORITY>.+)'),
|
|
]
|
|
|
|
const lines = data.split('\n')
|
|
const records = []
|
|
let currentRecord: any = null
|
|
|
|
for (const line of lines) {
|
|
if (XRegExp.test(line, XRegExp('^Index\\s+\\d+\\s+Feature:'))) {
|
|
// Start a new record
|
|
if (currentRecord) {
|
|
records.push(currentRecord)
|
|
}
|
|
currentRecord = {
|
|
FEATURE: '',
|
|
PERIOD_LEFT: '',
|
|
PERIOD_USED: '',
|
|
LICENSE_TYPE: '',
|
|
LICENSE_STATE: '',
|
|
LICENSE_COUNT: '',
|
|
LICENSE_PRIORITY: '',
|
|
}
|
|
}
|
|
|
|
if (currentRecord) {
|
|
for (const pattern of patterns) {
|
|
const match = XRegExp.exec(line, pattern)
|
|
if (match) {
|
|
const item = match?.groups || {}
|
|
Object.keys(item).forEach((key) => {
|
|
if (item && item[key] !== undefined) {
|
|
currentRecord[key] = item[key]
|
|
}
|
|
})
|
|
break // Stop processing this line once a pattern matches
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Push the last record if it exists
|
|
if (currentRecord) {
|
|
records.push(currentRecord)
|
|
}
|
|
|
|
return records
|
|
}
|
|
|
|
export default parseLog
|