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

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
+1
View File
@@ -10,6 +10,7 @@
<script src="scripts/common.js"></script> <script src="scripts/common.js"></script>
<script src="scripts/dispatcher.js"></script> <script src="scripts/dispatcher.js"></script>
<script src="scripts/init.js"></script> <script src="scripts/init.js"></script>
<script src="scripts/base/controller_base.js"></script>
<script src="scripts/anomaly_logs/init.js"></script> <script src="scripts/anomaly_logs/init.js"></script>
<script src="scripts/anomaly_logs/anomaly_logs.js"></script> <script src="scripts/anomaly_logs/anomaly_logs.js"></script>
<script src="scripts/statistics/init.js"></script> <script src="scripts/statistics/init.js"></script>
+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; this.last_index = -1;
} }
// Отправка запроса на сервер // Отправка запроса на сервер
SendRequest() { SendRequest() {
var request = {}; var request = {};
request.command = 'anomaly_logs';
request.parameters = { request.parameters = {
'last_index': this.last_index 'last_index': this.last_index
}; };
let request_data = JSON.stringify(request); let request_data = JSON.stringify(request);
socket.send(request_data); super.SendRequestImpl(request_data);
} }
// Обработка ответа от сервера // Обработка ответа от сервера
@@ -30,12 +31,12 @@ class AnomalyLogs {
return; return;
var last_index = this.last_index; var last_index = this.last_index;
for (const key in logs) { for (const item of logs) {
var row = window.anomaly_logs_table.insertRow(); var row = window.anomaly_logs_table.insertRow();
var cell = row.insertCell(0); 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) if (index > last_index)
last_index = index; last_index = index;
} }
+2 -2
View File
@@ -1,4 +1,4 @@
function InitAnomalyLogs() { function InitAnomalyLogs(url) {
window.anomaly_logs = new AnomalyLogs(); window.anomaly_logs = new AnomalyLogs(url);
window.anomaly_logs_table = document.getElementById("anomaly-logs-table"); 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); 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() { document.addEventListener("DOMContentLoaded", function() {
InitSocket(); let url = "ws://localhost:9555";
InitStatistics(); InitStatistics(url);
InitAnomalyLogs(); InitAnomalyLogs(url);
InitDispatcher(); InitDispatcher();
+2 -2
View File
@@ -27,7 +27,7 @@ function InitStatisticsChart(labels, colors) {
return chart; return chart;
} }
function InitStatistics() { function InitStatistics(url) {
window.statistics = new Statistics(); window.statistics = new Statistics(url);
window.total_logs_amount = document.getElementById("total-logs-amount"); 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.labels = [ "Аномальные", "Не аномальные" ];
this.colors = [ "#E96479", "#7DB9B6" ]; this.colors = [ "#E96479", "#7DB9B6" ];
this.keys = [ "anomaly", "not anomaly" ]; this.keys = [ "anomaly", "not anomaly" ];
@@ -20,11 +22,10 @@ class Statistics {
// Отправка запроса на сервер // Отправка запроса на сервер
SendRequest() { SendRequest() {
var request = {}; var request = {};
request.command = 'statistics';
request.parameters = {}; request.parameters = {};
let request_data = JSON.stringify(request); let request_data = JSON.stringify(request);
socket.send(request_data); super.SendRequestImpl(request_data);
} }
// Обработка ответа от сервера // Обработка ответа от сервера