Добавил сайт

This commit is contained in:
2024-01-15 03:50:54 +03:00
parent 68e421b336
commit 3f65dd50d3
9 changed files with 355 additions and 0 deletions
+68
View File
@@ -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%;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

+42
View File
@@ -0,0 +1,42 @@
<head>
<meta charset="UTF-8">
<link rel="icon" type="image/x-icon" href="/images/favicon.ico">
<link rel="stylesheet" href="css/style.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.socket.io/4.6.0/socket.io.min.js"></script>
<script src="scripts/common.js"></script>
<script src="scripts/dispatcher.js"></script>
<script src="scripts/init.js"></script>
<script src="scripts/anomaly_logs/init.js"></script>
<script src="scripts/anomaly_logs/anomaly_logs.js"></script>
<script src="scripts/statistics/init.js"></script>
<script src="scripts/statistics/statistics.js"></script>
</head>
<body>
<div class="main">
<div class="left-side-block">
<div class="round-borders table-block">
<table id="anomaly-logs-table">
<tr>
<td>Аномальные логи</td>
</tr>
</table>
</div>
</div>
<div class="right-side-block">
<div class="round-borders statistics-chart-block">
<p id="total-logs-amount">Общее количество: -</p>
<div class="statistics-chart">
<canvas id="statistics-chart"></canvas>
</div>
</div>
</div>
</div>
</body>
+49
View File
@@ -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()
}
+4
View File
@@ -0,0 +1,4 @@
function InitAnomalyLogs() {
window.anomaly_logs = new AnomalyLogs();
window.anomaly_logs_table = document.getElementById("anomaly-logs-table");
}
+22
View File
@@ -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;
}
+86
View File
@@ -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);
});
+33
View File
@@ -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");
}
+51
View File
@@ -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()
}