2024-06-27 05:57:10 +01:00
|
|
|
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 {
|
2024-06-27 06:14:59 +01:00
|
|
|
const response = await axios.post('https://vmselect.riff.cc/select/logsql/query', 'query=_time:2024-06-27T05:13 relay received');
|
2024-06-27 06:02:12 +01:00
|
|
|
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);
|
2024-06-27 06:07:30 +01:00
|
|
|
const logLines = response.data.split('\n');
|
2024-06-27 06:10:25 +01:00
|
|
|
const validLogs = [];
|
2024-06-27 06:09:38 +01:00
|
|
|
logLines.forEach((line, index) => {
|
2024-06-27 06:07:09 +01:00
|
|
|
try {
|
2024-06-27 06:10:25 +01:00
|
|
|
const parsedLog = JSON.parse(line);
|
|
|
|
|
validLogs.push(parsedLog);
|
2024-06-27 06:07:09 +01:00
|
|
|
} catch (parseError) {
|
|
|
|
|
console.error(`Error parsing log line at index ${index}:`, line);
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-06-27 06:10:25 +01:00
|
|
|
const logs = validLogs;
|
2024-06-27 06:02:12 +01:00
|
|
|
}
|
2024-06-27 05:58:10 +01:00
|
|
|
console.log('Logs response:', logs);
|
2024-06-27 05:57:10 +01:00
|
|
|
|
2024-06-27 05:58:10 +01:00
|
|
|
if (Array.isArray(logs)) {
|
|
|
|
|
logs.forEach(log => {
|
2024-06-27 05:59:54 +01:00
|
|
|
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}`;
|
2024-06-27 06:00:24 +01:00
|
|
|
if (!logCache.has(logIdentifier)) {
|
|
|
|
|
logCache.add(logIdentifier);
|
|
|
|
|
const logData = {
|
|
|
|
|
msg_hash,
|
|
|
|
|
receivedTime,
|
|
|
|
|
nodeId
|
|
|
|
|
};
|
2024-06-27 05:59:54 +01:00
|
|
|
|
2024-06-27 06:00:24 +01:00
|
|
|
wss.clients.forEach(client => {
|
|
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
|
|
|
client.send(JSON.stringify(logData));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-06-27 05:58:10 +01:00
|
|
|
}
|
|
|
|
|
});
|
2024-06-27 06:00:05 +01:00
|
|
|
} else {
|
|
|
|
|
console.error('Logs response is not an array:', logs);
|
2024-06-27 05:58:10 +01:00
|
|
|
}
|
2024-06-27 06:00:05 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error querying logs:', error);
|
2024-06-27 05:57:10 +01:00
|
|
|
}
|
|
|
|
|
}, 3000);
|
|
|
|
|
|
|
|
|
|
server.listen(3000, () => {
|
|
|
|
|
console.log('Server is listening on port 3000');
|
|
|
|
|
});
|