Обновление веб-интерфейса

This commit is contained in:
2024-02-21 11:45:53 +03:00
parent 4043250ca5
commit 20b3cea0b0
7 changed files with 61 additions and 52 deletions
+8 -7
View File
@@ -1,19 +1,20 @@
class AnomalyLogs {
class AnomalyLogs extends ControllerBase {
// Конструктор
constructor() {
constructor(url) {
super(url, "anomaly_logs/get");
this.last_index = -1;
}
// Отправка запроса на сервер
SendRequest() {
var request = {};
request.command = 'anomaly_logs';
request.parameters = {
'last_index': this.last_index
};
let request_data = JSON.stringify(request);
socket.send(request_data);
super.SendRequestImpl(request_data);
}
// Обработка ответа от сервера
@@ -30,12 +31,12 @@ class AnomalyLogs {
return;
var last_index = this.last_index;
for (const key in logs) {
for (const item of logs) {
var row = window.anomaly_logs_table.insertRow();
var cell = row.insertCell(0);
cell.innerHTML = logs[key];
cell.innerHTML = item["data"];
let index = Number(key);
let index = Number(item["id"]);
if (index > last_index)
last_index = index;
}
+2 -2
View File
@@ -1,4 +1,4 @@
function InitAnomalyLogs() {
window.anomaly_logs = new AnomalyLogs();
function InitAnomalyLogs(url) {
window.anomaly_logs = new AnomalyLogs(url);
window.anomaly_logs_table = document.getElementById("anomaly-logs-table");
}
+40
View File
@@ -0,0 +1,40 @@
class ControllerBase {
ProcessResponse(response) {
alert("Вызов нереализованного метода. Сообщите об этой ошибке разработчикам");
}
constructor(url, path) {
this.socket_ = new WebSocket(url + "/" + path);
// При создании сокета
this.socket_.onopen = function(event) {
console.log("connected to server");
};
// При получении ответа
var self = this;
this.socket_.onmessage = function(event) {
console.log(`received: '${event.data}'`);
let response = JSON.parse(event.data);
self.ProcessResponse(response);
};
// При закрытии сокета
this.socket_.onclose = function(event) {
if (event.wasClean)
console.log("connection closed");
else
console.log("failed to close connection");
};
// При ошибке
this.socket_.onerror = function(error) {
alert(`Возникла ошибка при подключении к серверу. Пожалуйста, сообщите о ней разработчикам: ${error}`);
};
}
SendRequestImpl(request) {
this.socket_.send(request);
}
}
+3 -37
View File
@@ -36,46 +36,12 @@ function InitDispatcher() {
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();
let url = "ws://localhost:9555";
InitStatistics();
InitAnomalyLogs();
InitStatistics(url);
InitAnomalyLogs(url);
InitDispatcher();
+2 -2
View File
@@ -27,7 +27,7 @@ function InitStatisticsChart(labels, colors) {
return chart;
}
function InitStatistics() {
window.statistics = new Statistics();
function InitStatistics(url) {
window.statistics = new Statistics(url);
window.total_logs_amount = document.getElementById("total-logs-amount");
}
+5 -4
View File
@@ -1,6 +1,8 @@
class Statistics {
class Statistics extends ControllerBase {
// Конструктор
constructor() {
constructor(url) {
super(url, "statistics/get");
this.labels = [ "Аномальные", "Не аномальные" ];
this.colors = [ "#E96479", "#7DB9B6" ];
this.keys = [ "anomaly", "not anomaly" ];
@@ -20,11 +22,10 @@ class Statistics {
// Отправка запроса на сервер
SendRequest() {
var request = {};
request.command = 'statistics';
request.parameters = {};
let request_data = JSON.stringify(request);
socket.send(request_data);
super.SendRequestImpl(request_data);
}
// Обработка ответа от сервера