Добавил сайт
This commit is contained in:
@@ -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()
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
function InitAnomalyLogs() {
|
||||
window.anomaly_logs = new AnomalyLogs();
|
||||
window.anomaly_logs_table = document.getElementById("anomaly-logs-table");
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user