hopglass/lib/proportions.js

131 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-04-02 22:07:00 +02:00
define(["chroma-js", "virtual-dom", "numeral-intl"],
function (Chroma, V, numeral) {
2015-03-30 02:58:47 +02:00
return function () {
var self = this
2015-03-30 03:12:30 +02:00
var fwTable, hwTable, autoTable, gwTable
2015-03-30 02:58:47 +02:00
var scale = Chroma.scale("YlGnBu").mode("lab")
function count(nodes, key, f) {
2015-03-30 02:58:47 +02:00
var dict = {}
nodes.forEach( function (d) {
var v = dictGet(d, key.slice(0))
if (f !== undefined)
v = f(v)
if (v === null)
return
2015-03-30 02:58:47 +02:00
dict[v] = 1 + (v in dict ? dict[v] : 0)
})
return Object.keys(dict).map(function (d) { return [d, dict[d]] })
}
function fillTable(table, data) {
2015-04-02 22:07:00 +02:00
if (!table.last)
table.last = V.h("table")
2015-03-30 02:58:47 +02:00
var max = 0
data.forEach(function (d) {
if (d[1] > max)
max = d[1]
})
2015-04-02 22:07:00 +02:00
var items = data.map(function (d) {
2015-03-30 02:58:47 +02:00
var v = d[1] / max
var c1 = Chroma.contrast(scale(v), "white")
var c2 = Chroma.contrast(scale(v), "black")
2015-04-02 22:07:00 +02:00
var th = V.h("th", d[0])
var td = V.h("td", V.h("span", {style: {
width: Math.round(v * 100) + "%",
backgroundColor: scale(v).hex(),
color: c1 > c2 ? "white" : "black"
}}, numeral(d[1]).format("0,0")))
return V.h("tr", [th, td])
2015-03-30 02:58:47 +02:00
})
2015-04-02 22:07:00 +02:00
var tableNew = V.h("table", items)
table = V.patch(table, V.diff(table.last, tableNew))
table.last = tableNew
2015-03-30 02:58:47 +02:00
}
self.setData = function (data) {
2015-03-30 03:12:30 +02:00
var onlineNodes = data.nodes.all.filter(online)
var nodes = onlineNodes.concat(data.nodes.lost)
var nodeDict = {}
data.nodes.all.forEach(function (d) {
nodeDict[d.nodeinfo.node_id] = d
})
2015-03-30 02:58:47 +02:00
var fwDict = count(nodes, ["nodeinfo", "software", "firmware", "release"])
var hwDict = count(nodes, ["nodeinfo", "hardware", "model"])
var autoDict = count(nodes, ["nodeinfo", "software", "autoupdater"], function (d) {
if (d === null)
2015-03-30 02:58:47 +02:00
return null
else if (d.enabled)
2015-03-30 02:58:47 +02:00
return d.branch
else
return "(deaktiviert)"
2015-03-30 02:58:47 +02:00
})
var gwDict = count(onlineNodes, ["statistics", "gateway"], function (d) {
2015-03-30 03:12:30 +02:00
if (d === null)
return null
if (d in nodeDict)
return nodeDict[d].nodeinfo.hostname
return d
})
2015-03-30 02:58:47 +02:00
fillTable(fwTable, fwDict.sort(function (a, b) { return b[1] - a[1] }))
fillTable(hwTable, hwDict.sort(function (a, b) { return b[1] - a[1] }))
fillTable(autoTable, autoDict.sort(function (a, b) { return b[1] - a[1] }))
2015-03-30 03:12:30 +02:00
fillTable(gwTable, gwDict.sort(function (a, b) { return b[1] - a[1] }))
2015-03-30 02:58:47 +02:00
}
self.render = function (el) {
var h2
h2 = document.createElement("h2")
h2.textContent = "Firmwareversionen"
el.appendChild(h2)
fwTable = document.createElement("table")
fwTable.classList.add("proportion")
el.appendChild(fwTable)
h2 = document.createElement("h2")
h2.textContent = "Hardwaremodelle"
el.appendChild(h2)
hwTable = document.createElement("table")
hwTable.classList.add("proportion")
el.appendChild(hwTable)
h2 = document.createElement("h2")
h2.textContent = "Autoupdater"
el.appendChild(h2)
autoTable = document.createElement("table")
autoTable.classList.add("proportion")
el.appendChild(autoTable)
2015-03-30 03:12:30 +02:00
h2 = document.createElement("h2")
h2.textContent = "Gewählter Gateway"
el.appendChild(h2)
gwTable = document.createElement("table")
gwTable.classList.add("proportion")
el.appendChild(gwTable)
2015-03-30 02:58:47 +02:00
}
return self
}
})