From 60a1da6879a06301591fc46a9446c4e0ab4e4edb Mon Sep 17 00:00:00 2001 From: "Benjamin Arntzen (aider)" Date: Thu, 27 Jun 2024 06:59:19 +0100 Subject: [PATCH] Refactored log parsing and processing logic into separate functions. --- server.js | 121 +++++++++++++++++++++++++++++------------------------- 1 file changed, 66 insertions(+), 55 deletions(-) diff --git a/server.js b/server.js index 95c396b..c5f533d 100644 --- a/server.js +++ b/server.js @@ -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`); + } + }); +}