diff --git a/site/css/style.css b/site/css/style.css new file mode 100644 index 0000000..bc2639b --- /dev/null +++ b/site/css/style.css @@ -0,0 +1,68 @@ +.main { + display: flex; + flex-direction: row; + height: 100%; +} + +.round-borders { + box-sizing: border-box; + border-radius: 10px; + border-style: solid; + border-width: thin; + border-color: #000000; +} + +.left-side-block { + width: 70%; + height: 100%; + margin-right: 1%; +} + +.right-side-block { + flex-grow: 1; + flex-direction: column; + height: 100%; + display: flex; +} + +.table-block { + height: 100%; + width: 100%; + padding: 1%; +} + +table { + border-collapse: collapse; +} + +td { + text-align: center; + padding: 1px; +} + +td:first-child { + text-align: left; + border-right: 1px solid; + width: 40%; + padding: 5px; +} + +tr:nth-child(even) { + background-color: #eeeeee; +} + +.logs-block { + height: 100%; +} + +.statistics-chart-block { + height: 100%; + display: flex; + justify-content: center; + align-items: center; + flex-direction: column; +} + +.statistics-chart { + padding: 2%; +} diff --git a/site/images/favicon.ico b/site/images/favicon.ico new file mode 100644 index 0000000..513a0c1 Binary files /dev/null and b/site/images/favicon.ico differ diff --git a/site/index.html b/site/index.html new file mode 100644 index 0000000..56d5631 --- /dev/null +++ b/site/index.html @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + +
+ +
+
+ + + + +
Аномальные логи
+
+
+ +
+
+

Общее количество: -

+ +
+ +
+
+
+
+ diff --git a/site/scripts/anomaly_logs/anomaly_logs.js b/site/scripts/anomaly_logs/anomaly_logs.js new file mode 100644 index 0000000..c902f92 --- /dev/null +++ b/site/scripts/anomaly_logs/anomaly_logs.js @@ -0,0 +1,49 @@ +class AnomalyLogs { + // Конструктор + constructor() { + 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); + } + + // Обработка ответа от сервера + // respone -- ответ сервера + ProcessResponse(response) { + this.UpdateData(response); + } + + // Обновление данных + // respone -- ответ от сервера + UpdateData(response) { + let logs = response["result"]; + if (logs == null) + return; + + var last_index = this.last_index; + for (const key in logs) { + var row = window.anomaly_logs_table.insertRow(); + var cell = row.insertCell(0); + cell.innerHTML = logs[key]; + + let index = Number(key); + if (index > last_index) + last_index = index; + } + + this.last_index = last_index + } +} + +function UpdateAnomalyLogs() { + window.anomaly_logs.SendRequest() +} diff --git a/site/scripts/anomaly_logs/init.js b/site/scripts/anomaly_logs/init.js new file mode 100644 index 0000000..d1cfd59 --- /dev/null +++ b/site/scripts/anomaly_logs/init.js @@ -0,0 +1,4 @@ +function InitAnomalyLogs() { + window.anomaly_logs = new AnomalyLogs(); + window.anomaly_logs_table = document.getElementById("anomaly-logs-table"); +} diff --git a/site/scripts/dispatcher.js b/site/scripts/dispatcher.js new file mode 100644 index 0000000..549a1e6 --- /dev/null +++ b/site/scripts/dispatcher.js @@ -0,0 +1,22 @@ +class Dispatcher { + constructor() { + this.commands = {}; + } + + AddCommand(command_name, command) { + this.commands[command_name] = command; + } + + Dispatch(response) { + if (response["status"] != "ok") { + alert(`Возникла ошибка. Пожалуйста, сообщите о ней разработчикам: ${response["error_message"]}`); + return; + } + + let command = response["command"]; + console.log("dispatching " + command); + this.commands[command].ProcessResponse(response); + } + + commands; +} \ No newline at end of file diff --git a/site/scripts/init.js b/site/scripts/init.js new file mode 100644 index 0000000..4d6e6fd --- /dev/null +++ b/site/scripts/init.js @@ -0,0 +1,86 @@ +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); +}); diff --git a/site/scripts/statistics/init.js b/site/scripts/statistics/init.js new file mode 100644 index 0000000..80dfe23 --- /dev/null +++ b/site/scripts/statistics/init.js @@ -0,0 +1,33 @@ +function InitStatisticsChart(labels, colors) { + // Создаём чарт + let chart = new Chart( + document.getElementById("statistics-chart"), { + type: 'pie', + data: { + labels: labels, + datasets: [{ + label: "Количество", + data: [0, 0], + backgroundColor: colors + }] + }, + options: { + plugins: { + legend: { + display: false + } + } + } + } + ); + + // Отображаем чарт + chart.update(); + + return chart; +} + +function InitStatistics() { + window.statistics = new Statistics(); + window.total_logs_amount = document.getElementById("total-logs-amount"); +} diff --git a/site/scripts/statistics/statistics.js b/site/scripts/statistics/statistics.js new file mode 100644 index 0000000..826b16f --- /dev/null +++ b/site/scripts/statistics/statistics.js @@ -0,0 +1,51 @@ +class Statistics { + // Конструктор + constructor() { + this.labels = [ "Аномальные", "Не аномальные" ]; + this.colors = [ "#E96479", "#7DB9B6" ]; + this.keys = [ "anomaly", "not anomaly" ]; + this.total_amount_key = "total"; + this.is_dataset_visible = [true, true]; + this.chart = InitStatisticsChart(this.labels, this.colors); + this.context = { + keys: [], + datasets: {} + }; + } + + // Поля + chart; + context; + + // Отправка запроса на сервер + SendRequest() { + var request = {}; + request.command = 'statistics'; + request.parameters = {}; + + let request_data = JSON.stringify(request); + socket.send(request_data); + } + + // Обработка ответа от сервера + // respone -- ответ сервера + ProcessResponse(response) { + this.UpdateChart(response); + } + + // Обновление чарта + // respone -- ответ от сервера + UpdateChart(response) { + let statistics = response["result"]; + let statistics_data = [ statistics[this.keys[0]], statistics[this.keys[1]] ]; + + window.total_logs_amount.innerHTML = "Общее количество: " + statistics[this.total_amount_key]; + + this.chart.data.datasets[0].data = statistics_data; + this.chart.update(); + } +} + +function UpdateStatistics() { + window.statistics.SendRequest() +}