Improve logs ()

This commit is contained in:
Antonio Navarro Perez 2021-11-20 11:57:25 -08:00 committed by GitHub
parent 66eadf35dc
commit 5d4e48f0f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 343 additions and 33 deletions

69
assets/js/logs.js Normal file
View file

@ -0,0 +1,69 @@
Distribyted.logs = {
loadView: function () {
fetch("/api/log")
.then(response => {
if (response.ok) {
return response.body.getReader();
} else {
response.json().then(json => {
Distribyted.message.error('Error getting logs from server. Error: ' + json.error);
}).catch(error => {
Distribyted.message.error('Error getting logs from server. Error: ' + error);
})
}
})
.then(reader => {
var decoder = new TextDecoder()
var lastString = ''
reader.read().then(function processText({ done, value }) {
if (done) {
return;
}
const string = `${lastString}${decoder.decode(value)}`
const lines = string.split(/\r\n|[\r\n]/g)
this.lastString = lines.pop() || ''
lines.forEach(element => {
try {
var json = JSON.parse(element)
var properties = ""
for (let [key, value] of Object.entries(json)) {
if (key == "level" || key == "component" || key == "message" || key == "time") {
continue
}
properties += `<b>${key}</b>=${value} `
}
var tableClass = "table-primary"
switch (json.level) {
case "info":
tableClass = ""
break;
case "error":
tableClass = "table-danger"
break;
case "warn":
tableClass = "table-warning"
break;
case "debug":
tableClass = "table-info"
break;
default:
break;
}
template = `<tr class="${tableClass}"><td>${new Date(json.time*1000).toLocaleString()}</td><td>${json.level}</td><td>${json.component}</td><td>${json.message}</td><td>${properties}</td></tr>`;
document.getElementById("log_table").innerHTML += template;
} catch (err) {
// server can send some corrupted json line
console.log(err);
}
});
return reader.read().then(processText);
}).catch(err => console.log(err));
}).catch(err => console.log(err));
}
}

View file

@ -129,6 +129,7 @@ Distribyted.routes = {
.then(function (response) {
if (response.ok) {
Distribyted.message.info('Torrent deleted.')
Distribyted.routes.loadView();
} else {
response.json().then(json => {
Distribyted.message.error('Error deletting torrent. Response: ' + json.error)
@ -159,7 +160,8 @@ $("#new-magnet").submit(function (event) {
let url = '/api/routes/' + route + '/torrent'
let body = JSON.stringify({ magnet: magnet })
console.log("LOG", url, body)
document.getElementById("submit_magnet_loading").style = "display:block"
fetch(url, {
method: 'POST',
body: body
@ -167,6 +169,7 @@ $("#new-magnet").submit(function (event) {
.then(function (response) {
if (response.ok) {
Distribyted.message.info('New magnet added.')
Distribyted.routes.loadView();
} else {
response.json().then(json => {
Distribyted.message.error('Error adding new magnet. Response: ' + json.error)
@ -176,6 +179,8 @@ $("#new-magnet").submit(function (event) {
}
})
.catch(function (error) {
Distribyted.message.error('Error deletting torrent: ' + error.message)
Distribyted.message.error('Error adding torrent: ' + error.message)
}).then(function () {
document.getElementById("submit_magnet_loading").style = "display:none"
});
});