4
0

proportions.js 5.7 KB

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