diff --git a/site/index.html b/site/index.html index 56d5631..e1aab66 100644 --- a/site/index.html +++ b/site/index.html @@ -10,6 +10,7 @@ + diff --git a/site/scripts/anomaly_logs/anomaly_logs.js b/site/scripts/anomaly_logs/anomaly_logs.js index c902f92..8d76ea9 100644 --- a/site/scripts/anomaly_logs/anomaly_logs.js +++ b/site/scripts/anomaly_logs/anomaly_logs.js @@ -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; } diff --git a/site/scripts/anomaly_logs/init.js b/site/scripts/anomaly_logs/init.js index d1cfd59..4c30471 100644 --- a/site/scripts/anomaly_logs/init.js +++ b/site/scripts/anomaly_logs/init.js @@ -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"); } diff --git a/site/scripts/base/controller_base.js b/site/scripts/base/controller_base.js new file mode 100644 index 0000000..2f2085f --- /dev/null +++ b/site/scripts/base/controller_base.js @@ -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); + } +} diff --git a/site/scripts/init.js b/site/scripts/init.js index 4d6e6fd..b81d1ad 100644 --- a/site/scripts/init.js +++ b/site/scripts/init.js @@ -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(); diff --git a/site/scripts/statistics/init.js b/site/scripts/statistics/init.js index 80dfe23..34597dd 100644 --- a/site/scripts/statistics/init.js +++ b/site/scripts/statistics/init.js @@ -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"); } diff --git a/site/scripts/statistics/statistics.js b/site/scripts/statistics/statistics.js index 826b16f..c1f7bdf 100644 --- a/site/scripts/statistics/statistics.js +++ b/site/scripts/statistics/statistics.js @@ -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); } // Обработка ответа от сервера