nodelist.js 2.5 KB

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