Files
log_analysis_web/site/scripts/init.js
T
2024-01-15 03:50:54 +03:00

87 lines
2.2 KiB
JavaScript

function InitSummaryChart() {
// Summary чарт
window.summary_chart = new Chart(
document.getElementById('summary-chart'), {
type: 'pie',
data: {
labels: [],
datasets: []
},
options: {
scales: {
x: {
title: {
display: true,
text: "Категория"
}
},
y: {
beginAtZero: true,
title: {
display: true,
text: "Количество"
}
}
}
}
}
);
window.summary_chart.render();
}
function InitDispatcher() {
window.dispatcher = new Dispatcher();
window.dispatcher.AddCommand("anomaly_logs", window.anomaly_logs);
window.dispatcher.AddCommand("statistics", window.statistics);
}
function InitSocket() {
// Создание сокета
window.is_socket_open = false;
window.socket = new WebSocket("ws://localhost:9555");
// При создании сокета
window.socket.onopen = function(event) {
console.log("connected to server");
window.is_socket_open = true;
};
// При получении ответа
window.socket.onmessage = function(event) {
console.log(`received: '${event.data}'`);
let response = JSON.parse(event.data);
window.dispatcher.Dispatch(response);
};
// При закрытии сокета
window.socket.onclose = function(event) {
if (event.wasClean)
console.log("connection closed");
else
console.log("failed to close connection");
};
// При ошибке
window.socket.onerror = function(error) {
alert(`Возникла ошибка при подключении к серверу. Пожалуйста, сообщите о ней разработчикам: ${error}`);
};
}
// Инициализация документа
document.addEventListener("DOMContentLoaded", function() {
InitSocket();
InitStatistics();
InitAnomalyLogs();
InitDispatcher();
var intervalId = setInterval(function() {
UpdateStatistics();
UpdateAnomalyLogs();
}, 2000);
});