proportions.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. define(["chroma-js"], function (Chroma) {
  2. return function () {
  3. var self = this
  4. var fwTable, hwTable, autoTable, gwTable
  5. var scale = Chroma.scale("YlGnBu").mode("lab")
  6. function count(nodes, key, def, f) {
  7. var dict = {}
  8. nodes.forEach( function (d) {
  9. var v = dictGet(d, key.slice(0))
  10. if (f !== undefined)
  11. v = f(v)
  12. if (v === null)
  13. v = def
  14. dict[v] = 1 + (v in dict ? dict[v] : 0)
  15. })
  16. return Object.keys(dict).map(function (d) { return [d, dict[d]] })
  17. }
  18. function fillTable(table, data) {
  19. var max = 0
  20. data.forEach(function (d) {
  21. if (d[1] > max)
  22. max = d[1]
  23. })
  24. data.forEach(function (d) {
  25. var v = d[1] / max
  26. var row = document.createElement("tr")
  27. var th = document.createElement("th")
  28. var td = document.createElement("td")
  29. var span = document.createElement("span")
  30. th.textContent = d[0]
  31. span.style.width = Math.round(v * 100) + "%"
  32. span.style.backgroundColor = scale(v).hex()
  33. var c1 = Chroma.contrast(scale(v), "white")
  34. var c2 = Chroma.contrast(scale(v), "black")
  35. span.style.color = c1 > c2 ? "white" : "black"
  36. span.textContent = d[1]
  37. td.appendChild(span)
  38. row.appendChild(th)
  39. row.appendChild(td)
  40. table.appendChild(row)
  41. })
  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. })