Refactored log parsing and processing logic into separate functions.

This commit is contained in:
Benjamin Arntzen (aider)
2024-06-27 06:59:19 +01:00
parent a2eff2745c
commit 60a1da6879
+66 -55
View File
@@ -77,17 +77,7 @@ setInterval(async () => {
const logLines = response.data.split('\n');
const validLogs = [];
logLines.forEach((line, index) => {
if (line.trim() !== '') {
try {
const unescapedLine = line.replace(/\\"/g, '"');
const parsedLog = JSON.parse(unescapedLine);
validLogs.push(parsedLog);
} catch (parseError) {
console.error(`Error parsing log line at index ${index}:`, line);
}
}
});
const validLogs = parseLogLines(response.data);
logs = validLogs;
} catch (error) {
console.error('Error fetching logs:', error);
@@ -98,50 +88,7 @@ setInterval(async () => {
if (Array.isArray(logs)) {
console.log('logs is an array, processing...');
logs.forEach((log, index) => {
console.log(`Processing log at index ${index}`);
const msgMatch = log._msg.match(/msg_hash=0x[0-9a-fA-F]+/);
const timeMatch = log._msg.match(/receivedTime=\d+/);
const nodeIdMatch = log.kubernetes_pod_name.match(/nodes-(\d+)/);
if (msgMatch && timeMatch && nodeIdMatch) {
const msg_hash = msgMatch[0].split('=')[1];
const receivedTime = timeMatch[0].split('=')[1];
const nodeId = parseInt(nodeIdMatch[1], 10);
const logIdentifier = `${msg_hash}-${receivedTime}`;
if (!logCache.has(logIdentifier)) {
logCache.add(logIdentifier);
const logData = {
msg_hash,
receivedTime,
nodeId,
newNode: !logCache.has(`node-${nodeId}`)
};
logCache.add(`node-${nodeId}`);
const eventData = {
msg_hash: logData.msg_hash,
receivedTime: logData.receivedTime,
nodeId: logData.nodeId,
newNode: logData.newNode
};
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
console.log(`Sending event to client ${client._socket.remoteAddress}:`, eventData);
client.send(JSON.stringify(eventData));
}
});
} else {
console.log(`Duplicate log found: ${logIdentifier}`);
}
} else {
console.warn('Log did not match expected format');
}
});
processLogs(logs);
} else {
console.error('Logs is not an array:', logs);
}
@@ -151,3 +98,67 @@ setInterval(async () => {
server.listen(3000, () => {
console.log('Server is listening on port 3000');
});
function parseLogLines(data) {
const logLines = data.split('\n');
const validLogs = [];
logLines.forEach((line, index) => {
if (line.trim() !== '') {
try {
const unescapedLine = line.replace(/\\"/g, '"');
const parsedLog = JSON.parse(unescapedLine);
validLogs.push(parsedLog);
} catch (parseError) {
console.error(`Error parsing log line at index ${index}: ${parseError.message}`);
}
}
});
return validLogs;
}
function processLogs(logs) {
logs.forEach((log, index) => {
console.log(`Processing log at index ${index}`);
const msgMatch = log._msg.match(/msg_hash=0x[0-9a-fA-F]+/);
const timeMatch = log._msg.match(/receivedTime=\d+/);
const nodeIdMatch = log.kubernetes_pod_name.match(/nodes-(\d+)/);
if (msgMatch && timeMatch && nodeIdMatch) {
const msg_hash = msgMatch[0].split('=')[1];
const receivedTime = timeMatch[0].split('=')[1];
const nodeId = parseInt(nodeIdMatch[1], 10);
const logIdentifier = `${msg_hash}-${receivedTime}`;
if (!logCache.has(logIdentifier)) {
logCache.add(logIdentifier);
const logData = {
msg_hash,
receivedTime,
nodeId,
newNode: !logCache.has(`node-${nodeId}`)
};
logCache.add(`node-${nodeId}`);
const eventData = {
msg_hash: logData.msg_hash,
receivedTime: logData.receivedTime,
nodeId: logData.nodeId,
newNode: logData.newNode
};
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
console.log(`Sending event to client ${client._socket.remoteAddress}:`, eventData);
client.send(JSON.stringify(eventData));
}
});
} else {
console.log(`Duplicate log found: ${logIdentifier}`);
}
} else {
console.warn(`Log at index ${index} did not match expected format`);
}
});
}