4
0

nodelist.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. define(["sorttable", "virtual-dom", "numeral"], function (SortTable, V, numeral) {
  2. function getUptime(now, d) {
  3. if (d.flags.online && "uptime" in d.statistics)
  4. return Math.round(d.statistics.uptime)
  5. else if (!d.flags.online && "lastseen" in d)
  6. return Math.round(-(now - d.lastseen) / 3600)
  7. }
  8. function showUptime(uptime) {
  9. var s = ""
  10. uptime /= 3600
  11. if (uptime !== undefined)
  12. if (Math.abs(uptime) >= 24)
  13. s = Math.round(uptime / 24) + "d"
  14. else
  15. s = Math.round(uptime) + "h"
  16. return s
  17. }
  18. var headings = [{ name: "Knoten",
  19. sort: function (a, b) {
  20. return a.nodeinfo.hostname.localeCompare(b.nodeinfo.hostname)
  21. },
  22. reverse: false
  23. },
  24. { name: "Uptime",
  25. sort: function (a, b) {
  26. return a.uptime - b.uptime
  27. },
  28. reverse: true
  29. },
  30. { name: "Clients",
  31. sort: function (a, b) {
  32. return ("clients" in a.statistics ? a.statistics.clients : -1) -
  33. ("clients" in b.statistics ? b.statistics.clients : -1)
  34. },
  35. reverse: true
  36. }]
  37. return function(router) {
  38. function renderRow(d) {
  39. var td1Content = []
  40. var aClass = ["hostname", d.flags.online ? "online" : "offline"]
  41. td1Content.push(V.h("a", { className: aClass.join(" "),
  42. onclick: router.node(d),
  43. href: "#"
  44. }, d.nodeinfo.hostname))
  45. if (has_location(d))
  46. td1Content.push(V.h("span", {className: "icon ion-location"}))
  47. var td1 = V.h("td", td1Content)
  48. var td2 = V.h("td", showUptime(d.uptime))
  49. var td3 = V.h("td", numeral("clients" in d.statistics ? d.statistics.clients : "").format("0,0"))
  50. return V.h("tr", [td1, td2, td3])
  51. }
  52. var table = new SortTable(headings, 0, renderRow)
  53. this.render = function (d) {
  54. var el = document.createElement("div")
  55. d.appendChild(el)
  56. var h2 = document.createElement("h2")
  57. h2.textContent = "Alle Knoten"
  58. el.appendChild(h2)
  59. el.appendChild(table.el)
  60. }
  61. this.setData = function (d) {
  62. var data = d.nodes.all.map(function (e) {
  63. var n = Object.create(e)
  64. n.uptime = getUptime(d.now, e) || 0
  65. return n
  66. })
  67. table.setData(data)
  68. }
  69. }
  70. })