4
0

nodelist.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. define(["tablesort", "tablesort.numeric"], function (Tablesort) {
  2. return function(router) {
  3. function showUptime(el, now, d) {
  4. var uptime
  5. if (d.flags.online && "uptime" in d.statistics)
  6. uptime = Math.round(d.statistics.uptime / 3600)
  7. else if (!d.flags.online && "lastseen" in d)
  8. uptime = Math.round(-(now - d.lastseen) / 3600000)
  9. var s = ""
  10. if (uptime !== undefined)
  11. if (Math.abs(uptime) >= 24)
  12. s = Math.round(uptime / 24) + "d"
  13. else
  14. s = uptime + "h"
  15. el.textContent = s
  16. el.setAttribute("data-sort", uptime !== undefined ? -uptime : 0)
  17. }
  18. var self = this
  19. var el
  20. self.render = function (d) {
  21. el = document.createElement("div")
  22. d.appendChild(el)
  23. }
  24. self.setData = function (data) {
  25. if (data.nodes.all.length === 0)
  26. return
  27. var h2 = document.createElement("h2")
  28. h2.textContent = "Alle Knoten"
  29. el.appendChild(h2)
  30. var table = document.createElement("table")
  31. var thead = document.createElement("thead")
  32. var tr = document.createElement("tr")
  33. var th1 = document.createElement("th")
  34. th1.textContent = "Knoten"
  35. th1.classList.add("sort-default")
  36. tr.appendChild(th1)
  37. var th2 = document.createElement("th")
  38. th2.textContent = "Uptime"
  39. tr.appendChild(th2)
  40. var th3 = document.createElement("th")
  41. th3.textContent = "Clients"
  42. tr.appendChild(th3)
  43. thead.appendChild(tr)
  44. table.appendChild(thead)
  45. var tbody = document.createElement("tbody")
  46. data.nodes.all.forEach( function (d) {
  47. var row = document.createElement("tr")
  48. var td1 = document.createElement("td")
  49. var a = document.createElement("a")
  50. a.textContent = d.nodeinfo.hostname
  51. a.href = "#"
  52. a.onclick = router.node(d)
  53. a.classList.add("hostname")
  54. a.classList.add(d.flags.online ? "online" : "offline")
  55. td1.appendChild(a)
  56. row.appendChild(td1)
  57. if (has_location(d)) {
  58. var span = document.createElement("span")
  59. span.classList.add("icon")
  60. span.classList.add("ion-location")
  61. td1.appendChild(span)
  62. }
  63. var td2 = document.createElement("td")
  64. showUptime(td2, data.now, d)
  65. row.appendChild(td2)
  66. var td3 = document.createElement("td")
  67. td3.textContent = "clients" in d.statistics ? d.statistics.clients : ""
  68. row.appendChild(td3)
  69. tbody.appendChild(row)
  70. })
  71. table.appendChild(tbody)
  72. new Tablesort(table)
  73. el.appendChild(table)
  74. }
  75. }
  76. })