309 lines
8.3 KiB
JavaScript
Raw Normal View History

2019-06-15 11:54:30 +03:00
import "bootstrap/dist/css/bootstrap.min.css";
2019-06-15 17:53:21 +03:00
let state = {};
2019-06-16 14:01:00 +03:00
let url = "http://localhost:8090/";
2019-06-15 11:54:30 +03:00
window.onload = function () {
2019-06-15 13:13:56 +03:00
let list = document.getElementById("zoneList");
let coolZone = {
name: "Cool Zone",
nodesNumber: 12,
2019-06-15 17:11:40 +03:00
lastHeight: 49771,
2019-06-15 13:13:56 +03:00
binaryLink: "#"
};
2019-06-15 17:11:40 +03:00
let coolStatus = {
status: "in-progress",
height: 3545
};
2019-06-15 23:10:31 +03:00
appendNewZone("cool-zone", coolZone, coolStatus);
2019-06-15 13:13:56 +03:00
let iris = {
name: "Iris",
nodesNumber: 57,
lastHeight: 12567,
binaryLink: "#"
};
2019-06-15 17:11:40 +03:00
let irisStatus = {
status: "success"
};
2019-06-15 23:10:31 +03:00
appendNewZone("iris", iris, irisStatus);
2019-06-15 13:13:56 +03:00
2019-06-16 14:01:00 +03:00
let badZone = {
name: "Bad Zone",
2019-06-15 13:13:56 +03:00
nodesNumber: 4,
lastHeight: 2566,
binaryLink: "#"
};
2019-06-15 17:11:40 +03:00
let nameStatus = {
status: "error"
};
2019-06-16 14:01:00 +03:00
let bzId = "bad-zone";
state[bzId] = {};
state[bzId].errorInfo = JSON.parse(`{
"height": "10",
"txs": null,
"signatures": [
{
"validator_address": "7869301611744D2F6A5578EB001DB18BCC0AC705",
"signature": "ruuvN8mPiKXniLc0JkblK4ol1oUHx3RuLDykik/DN8aiWXnWzPOPiLZrOLE0XbranEJpdErGalZnzShblUixAw=="
}
]
}`);
console.log("bzid === " + bzId);
console.log("sdfsfsdf === " + state[bzId].errorInfo);
appendNewZone(bzId, badZone, nameStatus);
2019-06-15 17:11:40 +03:00
let buttonEl = document.getElementById("start-check");
2019-06-15 23:10:31 +03:00
buttonEl.addEventListener("click", function() {
startCheckEvent()
});
2019-06-15 11:54:30 +03:00
};
2019-06-15 17:11:40 +03:00
function startCheckEvent() {
2019-06-16 14:01:00 +03:00
let nodeIp = document.getElementById("node-ip").value;
let port = document.getElementById("node-port").value;
let binaryUrl = document.getElementById("binary-url").value;
let name = document.getElementById("node-name").value;
let params = {
nodeIp: nodeIp,
port: port,
binaryUrl: binaryUrl,
name: name
};
2019-06-15 17:53:21 +03:00
let someInfo = {
2019-06-16 14:01:00 +03:00
name: name,
2019-06-15 17:53:21 +03:00
nodesNumber: 34,
2019-06-16 14:01:00 +03:00
lastHeight: 48,
2019-06-15 17:53:21 +03:00
binaryLink: "#"
};
2019-06-16 11:11:50 +03:00
2019-06-16 14:01:00 +03:00
startCheck(params, someInfo.name, someInfo);
2019-06-15 17:11:40 +03:00
}
2019-06-15 13:13:56 +03:00
2019-06-15 23:10:31 +03:00
function appendNewZone(id, info, status) {
2019-06-16 14:01:00 +03:00
if (state[id]) {
state[id].info = info;
state[id].status = status;
} else {
state[id] = {
info: info,
status: status
};
}
2019-06-15 17:11:40 +03:00
let list = document.getElementById("zoneList");
2019-06-15 23:10:31 +03:00
if (!status) {
status = {
status: "in-progress",
height: 0
};
}
2019-06-15 17:11:40 +03:00
let zoneInfo = zoneElement(id, info, status);
2019-06-15 23:10:31 +03:00
list.innerHTML = zoneInfo + list.innerHTML;
if (status.status === "error") {
2019-06-16 14:01:00 +03:00
setError(id, status.height)
2019-06-15 23:10:31 +03:00
}
2019-06-15 17:11:40 +03:00
}
function setHeightAndWidth(el, currentHeight, lastHeight) {
let percent = currentHeight / lastHeight * 100;
el.style.width = `${percent}%`;
el.innerHTML = `Height: ${currentHeight}`;
}
2019-06-15 13:13:56 +03:00
2019-06-15 17:11:40 +03:00
function changeHeight(el, currentHeight) {
el.innerHTML = `Height: ${currentHeight}`;
}
2019-06-15 23:10:31 +03:00
function getInfo(id) {
let el = document.getElementById(id);
return el.getElementsByClassName("bg-white")[0];
}
function showError(id, btn) {
let infoDiv = getInfo(id);
2019-06-16 14:01:00 +03:00
let jsonInfo;
console.log("ID === " + state[id].errorInfo);
if (state[id].errorInfo) {
jsonInfo = state[id].errorInfo;
} else {
let block = state[id].block;
let signatures = block.last_commit.precommits.map((p) => {
return {
validator_address: p.validator_address,
signature: p.signature
}
});
jsonInfo = {
height: block.header.height,
txs: block.data.txs,
signatures: signatures
};
}
infoDiv.innerHTML = `<pre>${JSON.stringify(jsonInfo, null, 2)}</pre>`;
2019-06-15 19:25:55 +03:00
btn.innerHTML = "Hide Error";
}
2019-06-15 23:10:31 +03:00
function hideError(id, btn) {
let infoDiv = getInfo(id);
2019-06-15 19:25:55 +03:00
infoDiv.innerHTML = "";
btn.innerHTML = "Show Error";
}
2019-06-15 23:10:31 +03:00
function errorAction(btn, id) {
showError(id, btn);
}
function unerrorAction(btn, id) {
hideError(id, btn);
2019-06-15 19:25:55 +03:00
}
2019-06-15 23:10:31 +03:00
function genErrorButton(id) {
let newBtn = document.createElement("button");
newBtn.type = "button";
newBtn.id = id + "-btn";
newBtn.style.width = "60%";
newBtn.classList.add("btn", "btn-block", "btn-danger");
newBtn.innerHTML = "Get Error Info";
return newBtn;
2019-06-15 19:25:55 +03:00
}
2019-06-16 14:01:00 +03:00
function setError(id, erroredHeight) {
2019-06-15 23:10:31 +03:00
let el = getProgressBar(id);
2019-06-16 14:01:00 +03:00
console.log("ERROR!!!!!!!!!!! " + erroredHeight);
getBlock(id, erroredHeight).then((resp) => {
return resp.text()
}).then((r) => {
console.log(JSON.parse(r));
state[id].block = JSON.parse(r).result.block;
});
2019-06-15 17:53:21 +03:00
el.classList.add("progress-error");
2019-06-15 23:10:31 +03:00
let btn = genErrorButton(id);
state[id].swap = false;
state[id].errorButton = btn;
state[id].errorFunc = function(e) {
console.log("error button clicked = " + id);
if (state[id].swap) {
unerrorAction(btn, id);
state[id].swap = false;
2019-06-15 19:25:55 +03:00
} else {
2019-06-15 23:10:31 +03:00
errorAction(btn, id);
state[id].swap = true;
2019-06-15 19:25:55 +03:00
}
};
2019-06-15 23:10:31 +03:00
el.parentElement.appendChild(btn);
// to avoid `onclick` erasure for some reasons
Object.keys(state).forEach(function(key) {
if (state[key].errorButton) {
document.getElementById(key).onclick = state[key].errorFunc
}
});
2019-06-15 17:11:40 +03:00
}
function zoneElement(id, info, status) {
2019-06-15 13:13:56 +03:00
let barStatus;
2019-06-15 17:11:40 +03:00
let percent = "100";
let currentHeight = info.lastHeight;
if (status.status === "success") {
2019-06-15 13:13:56 +03:00
barStatus = "progress-bar"
2019-06-15 17:11:40 +03:00
} else if (status.status === "error") {
2019-06-15 13:13:56 +03:00
barStatus = "progress-bar progress-error"
2019-06-15 17:11:40 +03:00
} else if (status.status === "in-progress") {
barStatus = "progress-bar";
currentHeight = status.height;
percent = currentHeight / info.lastHeight * 100;
console.log("last height: " + info.lastHeight);
console.log("cur height: " + currentHeight);
console.log("percent: " + percent);
2019-06-15 13:13:56 +03:00
}
console.log("hi " + id);
2019-06-15 23:10:31 +03:00
2019-06-16 14:01:00 +03:00
return `<div class="row border border-dark p-2" id="${id}">
<div class="col-12 list-info">
<p>Name: ${info.name}</p><p>Validator Number: ${info.nodesNumber}</p><p>Last Height: ${info.lastHeight}</p><p><a href="${info.binaryLink}">Binary link</a></p>
</div>
2019-06-15 13:13:56 +03:00
<div class="col-12">
<div class="progress">
2019-06-15 23:10:31 +03:00
<div class="${barStatus}" role="progressbar" style="width: ${percent}%;" aria-valuenow="${percent}" aria-valuemin="0" aria-valuemax="100">Height: ${currentHeight}</div>
2019-06-15 13:13:56 +03:00
</div>
2019-06-15 19:25:55 +03:00
<div class="bg-white"></div>
2019-06-15 13:13:56 +03:00
</div>
2019-06-15 23:10:31 +03:00
</div>`;
}
function getProgressBar(id) {
return document.getElementById(id).getElementsByClassName("progress-bar")[0];
2019-06-15 13:13:56 +03:00
}
2019-06-16 14:01:00 +03:00
function startCheck(params, zoneId, info) {
2019-06-15 11:54:30 +03:00
2019-06-15 23:10:31 +03:00
if (state[zoneId]) {
return;
}
2019-06-16 14:01:00 +03:00
createCheck(params).then((sa) => {
let socket = new WebSocket("ws://127.0.0.1:8090/file");
2019-06-15 17:11:40 +03:00
2019-06-16 11:11:50 +03:00
socket.onopen = function(e) {
2019-06-15 17:11:40 +03:00
2019-06-16 11:11:50 +03:00
console.log(e);
state[zoneId] = {
info: info
};
appendNewZone(zoneId, info);
2019-06-15 23:10:31 +03:00
};
2019-06-15 17:11:40 +03:00
2019-06-16 11:11:50 +03:00
let k = 0;
socket.onmessage = function(event) {
let st = state[zoneId];
let data = JSON.parse(event.data);
2019-06-16 14:01:00 +03:00
if (data.height) {
console.log("current height = " + data.height);
state[zoneId].currentHeight = data.height
}
2019-06-16 11:11:50 +03:00
let el = getProgressBar(zoneId);
k++;
if (data.correct) {
if (st.info.lastHeight < 1000 || k % 100 === 0) {
setHeightAndWidth(el, data.height, st.info.lastHeight);
} else {
changeHeight(el, data.height)
}
} else {
2019-06-16 14:01:00 +03:00
let erroredHeight = state[zoneId].currentHeight;
setError(zoneId, erroredHeight);
2019-06-16 11:11:50 +03:00
socket.close()
}
};
2019-06-15 17:11:40 +03:00
2019-06-16 11:11:50 +03:00
socket.onclose = function(event) {
console.log("Websocket was closed. Cause: " + JSON.stringify(event))
};
2019-06-15 17:11:40 +03:00
2019-06-16 11:11:50 +03:00
socket.onerror = function(error) {
console.log("Error: " + JSON.stringify(error))
};
});
2019-06-15 17:11:40 +03:00
}
2019-06-16 14:01:00 +03:00
async function getApps(id, height) {
return fetch(`${url}/apps`)
}
async function getBlock(id, height) {
return fetch(`${url}/block/${id}/${height}`)
}
async function createCheck(params) {
let createUrl = `${url}/create/${params.name}/${params.nodeIp}/${params.port}/${params.binaryUrl}`;
fetch(createUrl);
return Promise.resolve()
}