Merge pull request #55 from DDR0/dev

ui improvements for the web interface storage tab
This commit is contained in:
Daniel authored and GitHub committed 2017-08-30 11:12:01 +02:00
commit c52fd3f966
6 files changed
+128 -47

No files matched your search

+43
View File
@@ -307,6 +307,49 @@ app.post("/api/getStats", function(req,res) {
}); });
} }
}); });
// Modified version of getStats which feeds the web interface production graphs
// at /nodes. It is like getStats but it does not report items which were not
// produced at the moment of recording. (This is to save space, the 0-items were
// making up about 92% of the response body weight.)
app.post("/api/getTimelineStats", function(req,res) {
console.log(req.body);
if(typeof req.body == "object" && req.body.slaveID) {
// if not specified, get stats for last 24 hours
if(!req.body.fromTime) {
req.body.fromTime = Date.now() - 86400000 // 24 hours in MS
}
if(!req.body.toTime) {
req.body.toTime = Date.now();
}
console.log("Looking... " + JSON.stringify(req.body));
db.flows.find({
slaveID: req.body.slaveID,
}, function(err, docs) {
if(err) {
console.error(err);
return;
}
//Filter out all the entries outside the time range.
docs = docs.filter(function (el) {
return el.timestamp <= req.body.toTime && el.timestamp >= req.body.fromTime;
});
//Filter out all the elements that weren't produced.
docs.forEach(el => {
for(let key in el.data) {
if(el.data[key] === '0') {
delete el.data[key];
}
}
});
res.send(docs);
});
}
});
app.get("/api/getFactorioLocale", function(req,res){ app.get("/api/getFactorioLocale", function(req,res){
getFactorioLocale.asObject(config.factorioDirectory, "en", (err, factorioLocale) => res.send(factorioLocale)); getFactorioLocale.asObject(config.factorioDirectory, "en", (err, factorioLocale) => res.send(factorioLocale));
}); });
+2 -1
View File
@@ -1,7 +1,7 @@
<!doctype HTML> <!doctype HTML>
<head> <head>
<%- include("head.html") %> <%- include("head.html") %>
<script src="storage.js"></script> <script defer src="storage.js"></script>
</head> </head>
<body> <body>
@@ -11,6 +11,7 @@
<h2>Storage</h2> <h2>Storage</h2>
<input type="text" placeholder="search" id="search"></input> <input type="text" placeholder="search" id="search"></input>
<div id="contents"> <div id="contents">
<table><tbody><!-- populated by js --></tbody></table>
</div> </div>
<canvas id="contentGraph" width="200" height="200"></canvas> <canvas id="contentGraph" width="200" height="200"></canvas>
</div> </div>
+1 -1
View File
@@ -78,7 +78,7 @@ function getImageFromName(name){
return "https://wiki.factorio.com/images/" + capitalizeFirstLetter(replaceAll(factorioLocale["fluid-name"][name], ' ', '_').toLowerCase()) + ".png"; return "https://wiki.factorio.com/images/" + capitalizeFirstLetter(replaceAll(factorioLocale["fluid-name"][name], ' ', '_').toLowerCase()) + ".png";
} else if(factorioLocale["equipment-name"] && factorioLocale["equipment-name"][name]){ } else if(factorioLocale["equipment-name"] && factorioLocale["equipment-name"][name]){
return "https://wiki.factorio.com/images/" + capitalizeFirstLetter(replaceAll(factorioLocale["equipment-name"][name], ' ', '_').toLowerCase()) + ".png"; return "https://wiki.factorio.com/images/" + capitalizeFirstLetter(replaceAll(factorioLocale["equipment-name"][name], ' ', '_').toLowerCase()) + ".png";
} else return ""; } else return "/pictures/unknown-item.png";
} }
window.factorioLocale = {}; window.factorioLocale = {};
$.getJSON("/api/getFactorioLocale", (locale) => { $.getJSON("/api/getFactorioLocale", (locale) => {
Binary file not shown.

After

Width:  |  Height:  |  Size: 126 B

+33 -32
View File
@@ -44,32 +44,34 @@ let chartIgnoreList = [
// ID of slave, ID of canvasjs div without # // ID of slave, ID of canvasjs div without #
function makeGraph(slaveID, selector) { function makeGraph(slaveID, selector) {
post("api/getStats", {slaveID: slaveID}, function(data){ post("api/getTimelineStats", {slaveID: slaveID}, function(pointsInTime) {
//console.log("Building chart " + slaveID + " with this data:") //console.log("Building chart " + slaveID + " with this data:")
//console.log(data)
if(data.length > 0) { //Find all keys at all points in time.
// find keys let itemNames = {};
let itemNames = []; pointsInTime.forEach(function(point) {
for(let key in data[data.length - 1].data) { for(let key in point.data) {
itemNames[itemNames.length] = key itemNames[key] = true;
} }
let chartData = []; });
for(let o = 0; o < itemNames.length; o++) { itemNames = Object.keys(itemNames);
if(!chartIgnoreList.includes(itemNames[o])){
chartData[chartData.length] = generateLineChartArray(data, itemNames[o]); //Generate a chart list for each item.
} const displayNames = itemNames.filter(function(name) {
} return !chartIgnoreList.includes(name);
//console.log(chartData) });
drawChart(selector, chartData) const chartData = displayNames.map(function(name) {
} return generateLineChartArray(pointsInTime, name)
// callback(data); });
chartData.length && drawChart(selector, chartData)
}) })
} }
// production info // production info
// string, object, function(object) // string, object, function(object)
function post(url, data, callback) { function post(url, data, callback) {
console.log("POST " + url + JSON.stringify(data)) console.log("POST " + url, data);
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open("POST", url, true); xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json"); xhr.setRequestHeader("Content-type", "application/json");
@@ -83,27 +85,26 @@ function post(url, data, callback) {
} }
function generateLineChartArray(data, nameKey) { function generateLineChartArray(data, nameKey) {
const legendKeys = ["copper-cable", "iron-plate", "copper-plate", "electronic-circuit", "steel-plate", "advanced-circuit", "crude-oil", "petroleum-gas"];
let chartData = []; let chartData = [];
for(let i = 0; i < data.length; i++) { for(let i = 0; i < data.length; i++) {
// only show recent data let y = data[i].data[nameKey]
if(data[i].timestamp > Date.now() - (24*60*60*1000)){ if(!data[i].data[nameKey]) {
let y = data[i].data[nameKey] y = 0;
if(!data[i].data[nameKey]) { } else if(y < 0) {
y = 0; y = 0;
} else if(y < 0) { }
y = 0; chartData[chartData.length] = {
} x: new Date(data[i].timestamp),
chartData[chartData.length] = { y: Number(y)
x: new Date(data[i].timestamp),
y: Number(y)
}
} }
} }
chartData = sortByKey(chartData, "x"); chartData = sortByKey(chartData, "x");
let xyz = {}; let xyz = {};
xyz.name = nameKey; xyz.name = nameKey;
xyz.type = "line"; xyz.type = "line";
if(nameKey == "copper-wire"||nameKey == "iron-plate"||nameKey == "copper-plate"||nameKey == "electronic-circuit"||nameKey == "steel-plate"||nameKey == "advanced-circuit"||nameKey == "crude-oil"||nameKey == "petroleum-gas"){
if(legendKeys.indexOf(nameKey) >= 0){
xyz.showInLegend = true; xyz.showInLegend = true;
} }
xyz.dataPoints = chartData; xyz.dataPoints = chartData;
@@ -113,7 +114,7 @@ function generateLineChartArray(data, nameKey) {
var chartsByID = {}; var chartsByID = {};
function drawChart(selector, chartData) { function drawChart(selector, chartData) {
// selector is ID of element, ex "chartContainer" or "-123199123" // selector is ID of element, ex "chartContainer" or "-123199123"
console.log(chartData) // console.log(chartData)
chartsByID[selector] = new CanvasJS.Chart(selector, { chartsByID[selector] = new CanvasJS.Chart(selector, {
title:{ title:{
text: "Production graph" text: "Production graph"
+49 -13
View File
@@ -1,16 +1,49 @@
// function to draw data we recieve from ajax requests // function to draw data we recieve from ajax requests
_lastData = [];
function drawcontents(data) { function drawcontents(data) {
data = sortByKey(data, "count"); data = data || _lastData; //Cache data so we can drawcontents without waiting for the server, for the search box.
let result = "<table>"; _lastData = data;
for(i = 0;i < data.length; i++) {
var img = getImageFromName(data[i].name); const search = new RegExp(document.querySelector("#search").value, 'i');
var searchField = document.querySelector("#search"); data = data.filter(function(item) {
if(!searchField.value || data[i].name.includes(searchField.value)) { return search.test(item.name);
result = result + "<tr><td><image src='" + img + "' onerror='hideThis(this);'></td><td>" + data[i].name + "</td><td>" + data[i].count + "</td></tr>"; })
sortByKey(data, "count");
const table = document.querySelector("#contents tbody"); //tables have tbody inserted automatically
const rows = table.children;
//update existing rows or create new ones
data.forEach(function(item, i) {
let row = rows[i];
if(!row) {
row = document.createElement('tr');
row.innerHTML = "<td><img width=32 height=32></td><td class=name></td><td class=count></td>";
table.appendChild(row);
} }
const img = row.querySelector('img');
const imgName = getImageFromName(item.name);
if(img.getAttribute('src') !== imgName) {
img.setAttribute('src',imgName);
}
const name = row.querySelector('.name');
if(name.textContent !== item.name) {
name.textContent = item.name;
}
const count = row.querySelector('.count');
if(count.textContent !== ''+item.count) {
count.textContent = item.count;
}
});
//remove excess rows, for example, after filtering
while (data.length < rows.length) {
table.removeChild(rows[data.length]);
} }
result = result + "</table>"
document.getElementById("contents").innerHTML = result;
} }
// get cluster inventory from master // get cluster inventory from master
@@ -33,8 +66,11 @@ if(JSON.parse(localStorage.settings)["Periodically update storage screen"]) {
// function to sort arrays of objects after a keys value // function to sort arrays of objects after a keys value
function sortByKey(array, key) { function sortByKey(array, key) {
return array.sort(function(a, b) { array.sort(function(a, b) {
var x = Number(a[key]); var y = Number(b[key]); return b[key] - a[key];
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}); });
} }
document.querySelector("#search").addEventListener('input', function() {
drawcontents();
})