proportions.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. define(["chroma-js", "virtual-dom", "numeral-intl", "filters/genericnode", "vercomp" ],
  2. function (Chroma, V, numeral, Filter, vercomp) {
  3. return function (config, filterManager) {
  4. var self = this
  5. var scale = Chroma.scale("YlGnBu").mode("lab")
  6. var statusTable = document.createElement("table")
  7. statusTable.classList.add("proportion")
  8. var fwTable = document.createElement("table")
  9. fwTable.classList.add("proportion")
  10. var hwTable = document.createElement("table")
  11. hwTable.classList.add("proportion")
  12. var geoTable = document.createElement("table")
  13. geoTable.classList.add("proportion")
  14. var autoTable = document.createElement("table")
  15. autoTable.classList.add("proportion")
  16. function showStatGlobal(o) {
  17. var content, caption
  18. if (o.thumbnail) {
  19. content = document.createElement("img")
  20. content.src = o.thumbnail
  21. }
  22. if (o.caption) {
  23. caption = o.caption
  24. if (!content)
  25. content = document.createTextNode(caption)
  26. }
  27. var p = document.createElement("p")
  28. if (o.href) {
  29. var link = document.createElement("a")
  30. link.target = "_blank"
  31. link.href = o.href
  32. link.appendChild(content)
  33. if (caption && o.thumbnail)
  34. link.title = caption
  35. p.appendChild(link)
  36. } else
  37. p.appendChild(content)
  38. return p
  39. }
  40. function count(nodes, key, f) {
  41. var dict = {}
  42. nodes.forEach( function (d) {
  43. var v = dictGet(d, key.slice(0))
  44. if (f !== undefined)
  45. v = f(v)
  46. if (v === null)
  47. return
  48. dict[v] = 1 + (v in dict ? dict[v] : 0)
  49. })
  50. return Object.keys(dict).map(function (d) { return [d, dict[d], key, f] })
  51. }
  52. function addFilter(filter) {
  53. return function () {
  54. filterManager.addFilter(filter)
  55. return false
  56. }
  57. }
  58. function fillTable(name, table, data) {
  59. if (!table.last)
  60. table.last = V.h("table")
  61. var max = 0
  62. data.forEach(function (d) {
  63. if (d[1] > max)
  64. max = d[1]
  65. })
  66. var items = data.map(function (d) {
  67. var v = d[1] / max
  68. var c1 = Chroma.contrast(scale(v), "white")
  69. var c2 = Chroma.contrast(scale(v), "black")
  70. var filter = new Filter(name, d[2], d[0], d[3])
  71. var a = V.h("a", { href: "#", onclick: addFilter(filter) }, d[0])
  72. var th = V.h("th", a)
  73. var td = V.h("td", V.h("span", {style: {
  74. width: Math.round(v * 100) + "%",
  75. backgroundColor: scale(v).hex(),
  76. color: c1 > c2 ? "white" : "black"
  77. }}, numeral(d[1]).format("0,0")))
  78. return V.h("tr", [th, td])
  79. })
  80. var tableNew = V.h("table", items)
  81. table = V.patch(table, V.diff(table.last, tableNew))
  82. table.last = tableNew
  83. }
  84. self.setData = function (data) {
  85. var onlineNodes = data.nodes.all.filter(online)
  86. var nodes = onlineNodes.concat(data.nodes.lost)
  87. var nodeDict = {}
  88. data.nodes.all.forEach(function (d) {
  89. nodeDict[d.nodeinfo.node_id] = d
  90. })
  91. var statusDict = count(nodes, ["flags", "online"], function (d) {
  92. return d ? "online" : "offline"
  93. })
  94. var fwDict = count(nodes, ["nodeinfo", "software", "firmware", "release"])
  95. var hwDict = count(nodes, ["nodeinfo", "hardware", "model"])
  96. var geoDict = count(nodes, ["nodeinfo", "location"], function (d) {
  97. return d ? "ja" : "nein"
  98. })
  99. var autoDict = count(nodes, ["nodeinfo", "software", "autoupdater"], function (d) {
  100. if (d === null)
  101. return null
  102. else if (d.enabled)
  103. return d.branch
  104. else
  105. return "(deaktiviert)"
  106. })
  107. fillTable("Status", statusTable, statusDict.sort(function (a, b) { return b[1] - a[1] }))
  108. fillTable("Firmware", fwTable, fwDict.sort(function (a, b) { return vercomp(b[0], a[0]) }))
  109. fillTable("Hardware", hwTable, hwDict.sort(function (a, b) { return b[1] - a[1] }))
  110. fillTable("Koordinaten", geoTable, geoDict.sort(function (a, b) { return b[1] - a[1] }))
  111. fillTable("Autom. Updates", autoTable, autoDict.sort(function (a, b) { return b[1] - a[1] }))
  112. }
  113. self.render = function (el) {
  114. var h2
  115. h2 = document.createElement("h2")
  116. h2.textContent = "Status"
  117. el.appendChild(h2)
  118. el.appendChild(statusTable)
  119. h2 = document.createElement("h2")
  120. h2.textContent = "Firmwareversionen"
  121. el.appendChild(h2)
  122. el.appendChild(fwTable)
  123. h2 = document.createElement("h2")
  124. h2.textContent = "Hardwaremodelle"
  125. el.appendChild(h2)
  126. el.appendChild(hwTable)
  127. h2 = document.createElement("h2")
  128. h2.textContent = "Auf der Karte sichtbar"
  129. el.appendChild(h2)
  130. el.appendChild(geoTable)
  131. h2 = document.createElement("h2")
  132. h2.textContent = "Autoupdater"
  133. el.appendChild(h2)
  134. el.appendChild(autoTable)
  135. if (config.globalInfos)
  136. config.globalInfos.forEach( function (globalInfo) {
  137. h2 = document.createElement("h2")
  138. h2.textContent = globalInfo.name
  139. el.appendChild(h2)
  140. el.appendChild(showStatGlobal(globalInfo))
  141. })
  142. }
  143. return self
  144. }
  145. })