| 1 |
/ |
| 2 |
|
| 3 |
function start(svgObject){ |
| 4 |
const texts = svgObject.getElementsByTagName('text') |
| 5 |
for (let text of texts) { |
| 6 |
text.setAttribute("style", text.getAttribute("style") + "; pointer-events:none") |
| 7 |
} |
| 8 |
} |
| 9 |
|
| 10 |
async function createProcesses(svgdoc, req, json = null){ |
| 11 |
await clear(svgdoc) |
| 12 |
clearTable() |
| 13 |
if(!json){ |
| 14 |
response = await fetch("/" + req) |
| 15 |
json = await response.json() |
| 16 |
} |
| 17 |
json.forEach(proc => { |
| 18 |
displayProcess(svgdoc, proc, req) |
| 19 |
}) |
| 20 |
createTooltip(svgdoc) |
| 21 |
if (/^proc\/\d+$/.test(req)) { |
| 22 |
|
| 23 |
displayInfo(svgdoc, json) |
| 24 |
|
| 25 |
document.getElementById('threads').checked = true |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
async function displayProcess(svgdoc, proc, req = null){ |
| 30 |
if(!proc.object) |
| 31 |
return |
| 32 |
|
| 33 |
if(!/proc\/\d+$/.test(req)) |
| 34 |
fillTable(svgdoc, proc) |
| 35 |
bullet(svgdoc, proc, "") |
| 36 |
text(svgdoc, proc, "") |
| 37 |
if(proc.threads){ |
| 38 |
proc.threads.forEach(async thread =>{ |
| 39 |
if(thread.object){ |
| 40 |
bullet(svgdoc, thread, "thread") |
| 41 |
text(svgdoc, thread, "thread") |
| 42 |
} |
| 43 |
}) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
function bullet(svgdoc, obj, objType){ |
| 48 |
let bulletCircle = svgdoc.getElementById("bulletCircle" + objType + "_" + obj.object.replace(':','_')) |
| 49 |
|
| 50 |
if(!bulletCircle){ |
| 51 |
let id = obj.object.replace(':','_')+"_rect" |
| 52 |
let rect = svgdoc.getElementById(id) |
| 53 |
|
| 54 |
bulletCircle = svgdoc.createElementNS("http://www.w3.org/2000/svg","rect") |
| 55 |
|
| 56 |
|
| 57 |
if(objType === "thread"){ |
| 58 |
bulletCircle.setAttribute("thread", true) |
| 59 |
bulletCircle.setAttribute("height", "15") |
| 60 |
bulletCircle.setAttribute("fill", "#7cacf9") |
| 61 |
bulletCircle.setAttribute("y", rect.y.animVal.value + 12) |
| 62 |
}else{ |
| 63 |
bulletCircle.setAttribute("y", rect.y.animVal.value - 5) |
| 64 |
bulletCircle.setAttribute("height", "15") |
| 65 |
bulletCircle.setAttribute("fill", "#FFFFFF") |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
if(id == "Machine_0_rect"){ |
| 70 |
const svg = svgdoc.getElementsByTagName('svg')[0] |
| 71 |
bulletCircle.setAttribute("x", svg.width.baseVal.value / 2 - 10) |
| 72 |
bulletCircle.setAttribute("y", 5) |
| 73 |
if(objType === "thread"){ |
| 74 |
bulletCircle.setAttribute("x", svg.width.baseVal.value / 2 + 45) |
| 75 |
bulletCircle.setAttribute("y", 5) |
| 76 |
} |
| 77 |
}else{ |
| 78 |
bulletCircle.setAttribute("x", rect.x.animVal.value-5) |
| 79 |
} |
| 80 |
bulletCircle.setAttribute("rx", 10) |
| 81 |
bulletCircle.setAttribute("ry", 10) |
| 82 |
bulletCircle.setAttribute("circle", true) |
| 83 |
bulletCircle.setAttribute("stroke-width", "1") |
| 84 |
bulletCircle.setAttribute("stroke", "#000000") |
| 85 |
bulletCircle.classList.add("tooltip-trigger") |
| 86 |
rect.parentNode.appendChild(bulletCircle) |
| 87 |
bulletCircle.id = "bulletCircle" + objType + "_" + obj.object.replace(':','_') |
| 88 |
let secondId |
| 89 |
if(objType === "thread") |
| 90 |
secondId = "thread id : " + obj.PID |
| 91 |
else |
| 92 |
secondId = "process id : " + obj.PID |
| 93 |
bulletCircle.setAttribute('secondId', secondId) |
| 94 |
}else { |
| 95 |
let secondId = bulletCircle.getAttribute('secondId') |
| 96 |
if(objType === "thread") |
| 97 |
secondId += ';' + "thread id : " + obj.PID |
| 98 |
else |
| 99 |
secondId += ';' + "process id : " + obj.PID |
| 100 |
bulletCircle.setAttribute('secondId', secondId) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
function text(svgdoc, obj, objType){ |
| 105 |
let id = obj.object.replace(':','_')+"_rect" |
| 106 |
let rect = svgdoc.getElementById(id) |
| 107 |
let bulletText = svgdoc.createElementNS("http://www.w3.org/2000/svg","text") |
| 108 |
let bulletCircle = svgdoc.getElementById("bulletCircle" + objType + "_" + obj.object.replace(':','_')) |
| 109 |
|
| 110 |
|
| 111 |
if(!svgdoc.getElementById("bulletText" + objType + "_" + obj.object.replace(':','_'))){ |
| 112 |
|
| 113 |
bulletText.setAttribute("circle", true) |
| 114 |
|
| 115 |
|
| 116 |
if(id == "Machine_0_rect"){ |
| 117 |
const svg = svgdoc.getElementsByTagName('svg')[0] |
| 118 |
bulletText.setAttribute("x", svg.width.baseVal.value / 2 - 3) |
| 119 |
bulletText.setAttribute("y", 15) |
| 120 |
if(objType === "thread"){ |
| 121 |
bulletText.setAttribute("x", svg.width.baseVal.value / 2 + 52) |
| 122 |
bulletText.setAttribute("y", 15) |
| 123 |
} |
| 124 |
}else{ |
| 125 |
bulletText.setAttribute("x", rect.x.animVal.value+2) |
| 126 |
bulletText.setAttribute("y", rect.y.animVal.value+6) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
if(objType === "thread" && id != "Machine_0_rect"){ |
| 131 |
bulletText.setAttribute("thread", true) |
| 132 |
bulletText.setAttribute("y", rect.y.animVal.value+22) |
| 133 |
} |
| 134 |
|
| 135 |
bulletText.setAttribute("style", bulletText.getAttribute("style") + "; pointer-events:none") |
| 136 |
bulletText.setAttribute("font-size", "10px") |
| 137 |
updateBulletText(bulletText, obj.PID, bulletCircle) |
| 138 |
bulletText.id = "bulletText" + objType + "_" + obj.object.replace(':','_') |
| 139 |
rect.parentNode.appendChild(bulletText) |
| 140 |
}else{ |
| 141 |
let text = svgdoc.getElementById("bulletText" + objType + "_" + obj.object.replace(':','_')) |
| 142 |
updateBulletText(text, findPid(text, '+'), bulletCircle) |
| 143 |
|
| 144 |
|
| 145 |
if(id == "Machine_0_rect"){ |
| 146 |
if (text.textContent == 10) |
| 147 |
text.setAttribute("x", parseInt(text.getAttribute('x')) - 3) |
| 148 |
else if(text.textContent == 100) |
| 149 |
text.setAttribute("x", parseInt(text.getAttribute('x')) - 4) |
| 150 |
return |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
async function updateBulletText(text, pid, bulletCircle){ |
| 156 |
text.textContent = await pid |
| 157 |
let length = text.getComputedTextLength() |
| 158 |
bulletCircle.setAttribute("width", length + 15) |
| 159 |
} |
| 160 |
|
| 161 |
function findPid(text, op){ |
| 162 |
let nb |
| 163 |
if(!text.getAttribute('nb')){ |
| 164 |
nb = "[2]" |
| 165 |
text.setAttribute('nb', nb) |
| 166 |
}else{ |
| 167 |
nb = text.getAttribute('nb') |
| 168 |
nb = nb.replace('[','') |
| 169 |
nb = nb.replace(']','') |
| 170 |
if(op == '+') |
| 171 |
nb = parseInt(nb) + 1 |
| 172 |
else{ |
| 173 |
nb = parseInt(nb) - 1 |
| 174 |
} |
| 175 |
nb = '[' + nb + ']' |
| 176 |
text.setAttribute('nb', nb) |
| 177 |
} |
| 178 |
return nb |
| 179 |
} |
| 180 |
|
| 181 |
function clear(svgdoc){ |
| 182 |
document.getElementById('searchId').value = '' |
| 183 |
const info = document.getElementById('info') |
| 184 |
info.classList = 'hidden' |
| 185 |
const infoNotFound = document.getElementById('info-not-found') |
| 186 |
infoNotFound.classList = 'hidden' |
| 187 |
svgElements = svgdoc.getElementsByTagName('svg')[0].children |
| 188 |
let i = 0; |
| 189 |
for(element of svgElements){ |
| 190 |
if(element.id.includes('bulletCircle')) |
| 191 |
i += 2 |
| 192 |
} |
| 193 |
if(i == 0) |
| 194 |
return |
| 195 |
for(i ; i >= 0 ; i--){ |
| 196 |
svgElements[svgElements.length - 1].remove() |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
function createTooltip(svgdoc){ |
| 201 |
if(svgdoc.getElementById('tooltip')) |
| 202 |
svgdoc.getElementById('tooltip').remove() |
| 203 |
|
| 204 |
const g = svgdoc.createElementNS("http://www.w3.org/2000/svg","g") |
| 205 |
const blackRect = svgdoc.createElementNS("http://www.w3.org/2000/svg",'rect') |
| 206 |
const whiteRect = svgdoc.createElementNS("http://www.w3.org/2000/svg",'rect') |
| 207 |
const tooltip = svgdoc.createElementNS("http://www.w3.org/2000/svg",'text') |
| 208 |
|
| 209 |
blackRect.setAttribute('fill','black') |
| 210 |
blackRect.setAttribute('opacity','0.4') |
| 211 |
blackRect.setAttribute('rx','2') |
| 212 |
blackRect.setAttribute('ry','2') |
| 213 |
blackRect.setAttribute('x','2') |
| 214 |
blackRect.setAttribute('y','-8') |
| 215 |
blackRect.setAttribute('height','11') |
| 216 |
blackRect.setAttribute('opacity','0.4') |
| 217 |
whiteRect.setAttribute('y','-10') |
| 218 |
whiteRect.setAttribute('fill','white') |
| 219 |
whiteRect.setAttribute('rx','2') |
| 220 |
whiteRect.setAttribute('ry','2') |
| 221 |
whiteRect.setAttribute('height','11') |
| 222 |
tooltip.setAttribute('x','4') |
| 223 |
tooltip.setAttribute('y','6') |
| 224 |
|
| 225 |
g.setAttribute('visibility', 'hidden') |
| 226 |
g.id = "tooltip" |
| 227 |
|
| 228 |
g.appendChild(blackRect) |
| 229 |
g.appendChild(whiteRect) |
| 230 |
g.appendChild(tooltip) |
| 231 |
svgdoc.getElementsByTagName('svg')[0].appendChild(g) |
| 232 |
|
| 233 |
let triggers = svgdoc.getElementsByClassName('tooltip-trigger') |
| 234 |
for (let i = 0; i < triggers.length; i++) { |
| 235 |
triggers[i].onmousemove = showTooltip |
| 236 |
triggers[i].onmouseout = hideTooltip |
| 237 |
triggers[i].onclick = async function(){ |
| 238 |
|
| 239 |
procs = triggers[i].getAttribute('secondId').split(";") |
| 240 |
let data = new Array() |
| 241 |
response = await fetch("/json") |
| 242 |
json = await response.json() |
| 243 |
await procs.forEach(proc => { |
| 244 |
json.forEach(jsonProcess => { |
| 245 |
if(proc.substring(13) == jsonProcess.PID) |
| 246 |
data.push(jsonProcess) |
| 247 |
}) |
| 248 |
}) |
| 249 |
let req = data.length == 1 ? "proc/" + data[0].PID : null |
| 250 |
createProcesses(svgdoc, req, data) |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
function showTooltip(evt) { |
| 255 |
const tooltip = svgdoc.getElementById('tooltip') |
| 256 |
let tooltipText = tooltip.getElementsByTagName('text')[0] |
| 257 |
let tooltipRects = tooltip.getElementsByTagName('rect') |
| 258 |
|
| 259 |
|
| 260 |
while(tooltipText.firstChild){ |
| 261 |
tooltipText.removeChild(tooltipText.firstChild) |
| 262 |
for (let i = 0; i < tooltipRects.length; i++) { |
| 263 |
tooltipRects[i].setAttribute("height", parseInt(tooltipRects[i].getAttribute("height")) - 15) |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
let text = evt.target.getAttribute('secondId') |
| 269 |
let tspan |
| 270 |
text = text.split(';') |
| 271 |
let length = 0 |
| 272 |
for (str of text ) { |
| 273 |
|
| 274 |
tspan = svgdoc.createElementNS("http://www.w3.org/2000/svg",'tspan') |
| 275 |
if(text.indexOf(str) !== 0) |
| 276 |
tspan.setAttribute('dy', '1em') |
| 277 |
tspan.setAttribute('x', '0') |
| 278 |
tspan.textContent = str |
| 279 |
tooltipText.appendChild(tspan) |
| 280 |
|
| 281 |
if(tspan.getComputedTextLength() > length) |
| 282 |
length = tspan.getComputedTextLength() |
| 283 |
|
| 284 |
for (let i = 0; i < tooltipRects.length; i++) { |
| 285 |
tooltipRects[i].setAttribute("height", parseInt(tooltipRects[i].getAttribute("height")) + 15) |
| 286 |
} |
| 287 |
|
| 288 |
if(text.indexOf(str) === 5){ |
| 289 |
tspan.textContent = '\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0...' |
| 290 |
tooltipText.appendChild(tspan) |
| 291 |
break |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
for (let i = 0; i < tooltipRects.length; i++) { |
| 296 |
tooltipRects[i].setAttributeNS(null, "width", length + 8) |
| 297 |
} |
| 298 |
|
| 299 |
let CTM = svgdoc.getElementsByTagName('svg')[0].getScreenCTM() |
| 300 |
let x = (evt.clientX - CTM.e + 6) / CTM.a; |
| 301 |
let y = (evt.clientY - CTM.f + 20) / CTM.d; |
| 302 |
tooltip.setAttributeNS(null, "transform", "translate(" + x + " " + y + ")"); |
| 303 |
tooltip.setAttributeNS(null, "visibility", "visible") |
| 304 |
} |
| 305 |
|
| 306 |
function hideTooltip() { |
| 307 |
const tooltip = svgdoc.getElementById('tooltip') |
| 308 |
tooltip.setAttributeNS(null, "visibility", "hidden") |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
async function displayInfo(svgdoc, data){ |
| 313 |
|
| 314 |
const info = document.getElementById('info') |
| 315 |
const infoNotFound = document.getElementById('info-not-found') |
| 316 |
info.classList = 'hidden' |
| 317 |
infoNotFound.classList = 'hidden' |
| 318 |
|
| 319 |
if(!data[0].object){ |
| 320 |
infoNotFound.classList.toggle('hidden') |
| 321 |
return |
| 322 |
} |
| 323 |
const infos = info.children[0].children |
| 324 |
infos[0].innerHTML = 'Process: ' + data[0].PID |
| 325 |
infos[1].innerHTML = 'Name: ' + data[0].name |
| 326 |
infos[2].innerHTML = 'Object: ' + data[0].object.replace(':',' L#') |
| 327 |
if(data[0].threads){ |
| 328 |
infos[3].innerHTML = 'Threads: ' + (data[0].threads.length - 1) |
| 329 |
}else{ |
| 330 |
infos[3].classList = 'hidden' |
| 331 |
} |
| 332 |
info.classList.toggle('hidden') |
| 333 |
} |
| 334 |
|
| 335 |
function fillTable(svgdoc, proc){ |
| 336 |
document.getElementById('processTable').classList = "table100 ver2 m-b-110" |
| 337 |
const table = document.getElementById('table') |
| 338 |
const tr = document.createElement('tr') |
| 339 |
const td = document.createElement('td') |
| 340 |
const td2 = document.createElement('td') |
| 341 |
const td3 = document.createElement('td') |
| 342 |
const checkbox = document.getElementById('mainCheck') |
| 343 |
|
| 344 |
tr.classList.add("row100", "head") |
| 345 |
td.classList.add("cell100", "column1") |
| 346 |
td2.classList.add("cell100", "column1") |
| 347 |
td3.classList.add("cell100", "column1") |
| 348 |
|
| 349 |
tr.setAttribute("object", proc.object) |
| 350 |
td.innerHTML = proc.PID |
| 351 |
td2.innerHTML = proc.name |
| 352 |
td3.innerHTML = "<input class='subCheck' checked='true' type='checkbox'>" |
| 353 |
td3.children[0].addEventListener('change', displayTableElement) |
| 354 |
checkbox.checked = true |
| 355 |
checkbox.onchange = changeAll |
| 356 |
|
| 357 |
tr.appendChild(td) |
| 358 |
tr.appendChild(td2) |
| 359 |
tr.appendChild(td3) |
| 360 |
table.appendChild(tr) |
| 361 |
|
| 362 |
|
| 363 |
function displayTableElement(event){ |
| 364 |
const tr = event.target.parentElement.parentElement |
| 365 |
const tds = tr.children |
| 366 |
const proc = { PID : tds[0].innerHTML, name : tds[1].innerHTML, object : tr.getAttribute('object') } |
| 367 |
if(event.target.checked == false){ |
| 368 |
const element = svgdoc.getElementById("bulletCircle" + "_" + proc.object.replace(':','_')) |
| 369 |
removeProc(element, proc) |
| 370 |
}else{ |
| 371 |
bullet(svgdoc, proc, "") |
| 372 |
text(svgdoc, proc, "") |
| 373 |
createTooltip(svgdoc) |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
function removeProc(element, proc){ |
| 378 |
let id = element.id |
| 379 |
let text = svgdoc.getElementById(id.replace('bulletCircle', 'bulletText')) |
| 380 |
let secondId = element.getAttribute('secondId').replace('process id : ' + proc.PID + ';', '') |
| 381 |
if(!text.getAttribute('nb') || text.getAttribute('nb') == '[1]'){ |
| 382 |
text.remove() |
| 383 |
element.remove() |
| 384 |
}else{ |
| 385 |
updateBulletText(text, findPid(text, '-'), element) |
| 386 |
} |
| 387 |
element.setAttribute('secondId', secondId) |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
function changeAll(event){ |
| 392 |
const checkboxes = document.getElementsByClassName('subCheck') |
| 393 |
size = checkboxes.length |
| 394 |
for(let i = 0 ; i < size ; i++ ){ |
| 395 |
if(checkboxes[i].checked != event.target.checked){ |
| 396 |
checkboxes[i].checked = event.target.checked |
| 397 |
displayTableElement({target: checkboxes[i]}) |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
function clearProc(){ |
| 404 |
document.getElementById('searchId').value = "" |
| 405 |
const checkboxes = document.getElementsByClassName('checkbox') |
| 406 |
for( let i = 0 ; i < checkboxes.length ; i++ ){ |
| 407 |
checkboxes[i].checked = false |
| 408 |
} |
| 409 |
|
| 410 |
let svgObject = document.getElementById('svg-object').contentDocument |
| 411 |
clear(svgObject) |
| 412 |
} |
| 413 |
|
| 414 |
function clearTable(){ |
| 415 |
const table = document.getElementById("table"); |
| 416 |
while (table.firstChild) { |
| 417 |
table.removeChild(table.firstChild); |
| 418 |
} |
| 419 |
document.getElementById("processTable").classList = "table100 ver2 m-b-110 hidden" |
| 420 |
} |
| 421 |
|
| 422 |
function handleProcButton(){ |
| 423 |
let functionIsRunning = false |
| 424 |
|
| 425 |
async function addProc(req){ |
| 426 |
if (!functionIsRunning) { |
| 427 |
functionIsRunning = true |
| 428 |
if(req == 'bound' || req == 'all'){ |
| 429 |
clearProc() |
| 430 |
clearTable() |
| 431 |
} |
| 432 |
let svgObject = document.getElementById('svg-object').contentDocument |
| 433 |
await createProcesses(svgObject, req) |
| 434 |
functionIsRunning = false |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
const procButtons = document.getElementsByClassName('procButton') |
| 439 |
for(let i = 0 ; i < procButtons.length ; i++){ |
| 440 |
procButtons[i].addEventListener('click', function(){ |
| 441 |
addProc(procButtons[i].type) |
| 442 |
}) |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
function handleCheckbox(){ |
| 447 |
const checkboxes = document.getElementsByClassName('checkbox') |
| 448 |
for( let i = 0 ; i < checkboxes.length ; i++ ){ |
| 449 |
checkboxes[i].checked = false |
| 450 |
checkboxes[i].addEventListener('change',function(e){ |
| 451 |
clearTable() |
| 452 |
let svgObject = document.getElementById('svg-object').contentDocument |
| 453 |
createProcesses(svgObject, e.target.id) |
| 454 |
}) |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
function procByid(){ |
| 459 |
let svgObject = document.getElementById('svg-object').contentDocument |
| 460 |
let id = document.getElementById('searchId').value |
| 461 |
clearProc() |
| 462 |
createProcesses(svgObject, 'proc/'+id) |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
setTimeout(function() { |
| 467 |
handleCheckbox() |
| 468 |
handleProcButton() |
| 469 |
const input = document.getElementById('searchId') |
| 470 |
input.value = '' |
| 471 |
input.addEventListener('keyup',function(event){ |
| 472 |
if(event.keyCode === 13){ |
| 473 |
event.preventDefault() |
| 474 |
procByid() |
| 475 |
} |
| 476 |
}) |
| 477 |
|
| 478 |
let svgObject = document.getElementById('svg-object').contentDocument |
| 479 |
let svgElement = svgObject.getElementById('Machine_0_rect') |
| 480 |
|
| 481 |
if(svgObject.getElementById('Machine_0_rect')){ |
| 482 |
createProcesses(svgObject, 'bound') |
| 483 |
start(svgObject) |
| 484 |
}else{ |
| 485 |
const h1 = document.createElement('h1') |
| 486 |
h1.innerHTML = "Your svg file doesn't have the good format to load javascript" |
| 487 |
document.body.appendChild(h1) |
| 488 |
} |
| 489 |
}, 200) |