236 lines
6.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-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
let nameService = {
name: "Name Service",
nodesNumber: 4,
lastHeight: 2566,
binaryLink: "#"
};
2019-06-15 17:11:40 +03:00
let nameStatus = {
status: "error"
};
2019-06-15 23:10:31 +03:00
appendNewZone("name service", nameService, 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 11:11:50 +03:00
let nodeIpEl = document.getElementById("node-ip").value;
let portEl = document.getElementById("node-port").value;
let binaryUrlEl = document.getElementById("binary-url").value;
let nameEl = document.getElementById("node-name").value;
2019-06-15 17:53:21 +03:00
let someInfo = {
2019-06-16 11:11:50 +03:00
name: nameEl,
2019-06-15 17:53:21 +03:00
nodesNumber: 34,
2019-06-15 19:25:55 +03:00
lastHeight: 13,
2019-06-15 17:53:21 +03:00
binaryLink: "#"
};
2019-06-16 11:11:50 +03:00
let createUrl = `http://localhost:8090/create/${nameEl}/${nodeIpEl}/${portEl}/${binaryUrlEl}`;
startCheck(createUrl, 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) {
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") {
setError(id)
}
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-15 19:25:55 +03:00
infoDiv.innerHTML = "some error about";
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-15 23:10:31 +03:00
function setError(id) {
let el = getProgressBar(id);
console.log("ERROR!!!!!!!!!!! ");
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
return `<div class="row border border-dark" id="${id}">
2019-06-15 13:13:56 +03:00
<div class="row"><div class="col-12">${info.name}, ${info.nodesNumber} nodes, ${info.lastHeight}</div></div>
<div class="row"><div class="col-12"><a href="${info.binaryLink}">Binary link</a></div></div>
<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 11:11:50 +03:00
function startCheck(createUrl, 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 11:11:50 +03:00
fetch(createUrl).then((sa) => {
let socket = new WebSocket("ws://127.0.0.1:8090/websocket/" + zoneId);
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);
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 {
setError(zoneId);
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
}