4
0

proportions.js 4.6 KB

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