Files
nodejs-visualiser/server.js
T

78 lines
2.3 KiB
JavaScript

const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const axios = require('axios');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
let logCache = new Set();
app.use(express.static('public'));
wss.on('connection', (ws) => {
console.log('Client connected');
});
setInterval(async () => {
try {
const response = await axios.post('https://vmselect.riff.cc/select/logsql/query', 'query=_time:5s relay received');
try {
const responseData = JSON.parse(response.data);
console.log('Parsed response data:', responseData);
const logs = responseData.data; // Assuming the logs are in responseData.data
} catch (parseError) {
console.error('Error parsing response data:', parseError);
const logLines = response.data.split('\n');
logLines.forEach((line, index) => {
try {
JSON.parse(line);
} catch (parseError) {
console.error(`Error parsing log line at index ${index}:`, line);
}
});
return;
}
console.log('Logs response:', logs);
if (Array.isArray(logs)) {
logs.forEach(log => {
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
};
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(logData));
}
});
}
}
});
} else {
console.error('Logs response is not an array:', logs);
}
} catch (error) {
console.error('Error querying logs:', error);
}
}, 3000);
server.listen(3000, () => {
console.log('Server is listening on port 3000');
});