nodelist.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. define(["virtual-dom"],
  2. function (V) {
  3. return function(router) {
  4. function showUptime(now, d) {
  5. var uptime
  6. if (d.flags.online && "uptime" in d.statistics)
  7. uptime = Math.round(d.statistics.uptime / 3600)
  8. else if (!d.flags.online && "lastseen" in d)
  9. uptime = Math.round(-(now - d.lastseen) / 3600000)
  10. var s = ""
  11. if (uptime !== undefined)
  12. if (Math.abs(uptime) >= 24)
  13. s = Math.round(uptime / 24) + "d"
  14. else
  15. s = uptime + "h"
  16. return {v: s, sort: uptime !== undefined ? -uptime : 0}
  17. }
  18. var self = this
  19. var el, tbody
  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. if (!tbody) {
  28. var h2 = document.createElement("h2")
  29. h2.textContent = "Alle Knoten"
  30. el.appendChild(h2)
  31. var table = document.createElement("table")
  32. el.appendChild(table)
  33. var thead = document.createElement("thead")
  34. var tr = document.createElement("tr")
  35. var th1 = document.createElement("th")
  36. th1.textContent = "Knoten"
  37. tr.appendChild(th1)
  38. var th2 = document.createElement("th")
  39. th2.textContent = "Uptime"
  40. tr.appendChild(th2)
  41. var th3 = document.createElement("th")
  42. th3.textContent = "Clients"
  43. tr.appendChild(th3)
  44. thead.appendChild(tr)
  45. table.appendChild(thead)
  46. tbody = document.createElement("tbody")
  47. tbody.last = V.h("tbody")
  48. table.appendChild(tbody)
  49. }
  50. var nodes = data.nodes.all.slice(0).sort( function (a, b) {
  51. return a.nodeinfo.hostname.localeCompare(b.nodeinfo.hostname)
  52. })
  53. var items = nodes.map( function (d) {
  54. var td1Content = []
  55. var aClass = ["hostname", d.flags.online ? "online" : "offline"]
  56. td1Content.push(V.h("a", { className: aClass.join(" "),
  57. onclick: router.node(d),
  58. href: "#"
  59. }, d.nodeinfo.hostname))
  60. if (has_location(d))
  61. td1Content.push(V.h("span", {className: "icon ion-location"}))
  62. var uptime = showUptime(data.now, d)
  63. var td1 = V.h("td", td1Content)
  64. var td2 = V.h("td", uptime.v)
  65. var td3 = V.h("td", "clients" in d.statistics ? d.statistics.clients : "")
  66. return V.h("tr", [td1, td2, td3])
  67. })
  68. var tbodyNew = V.h("tbody", items)
  69. tbody = V.patch(tbody, V.diff(tbody.last, tbodyNew))
  70. tbody.last = tbodyNew
  71. }
  72. }
  73. })