proportions.js 3.6 KB

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