IMPROVEMENT: improve stats to not fill up memory over time 🎉

This commit is contained in:
Christian Kellner
2018-02-05 11:26:19 +01:00
parent 60f02f4d7a
commit 97d7ba7f9a

View File

@@ -1,25 +1,36 @@
const config = require('../../conf/config.json');
let lastScrape = {};
let stats = {
lastScrape: {},
foundScrapes: {}
};
if (config.enableStats) {
const http = require('http');
http
.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
config,
lastScrape
const http = require('http');
http
.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(
JSON.stringify({
config,
stats
})
);
})
);
})
.listen(config.statsPort, '127.0.0.1');
.listen(config.statsPort, '127.0.0.1');
}
exports.setLastScrape = (serviceName, numberOfNewListsings) => {
lastScrape[serviceName] = lastScrape[serviceName] || [];
lastScrape[serviceName].push({
scapeTime: new Date().toString(),
listingsFound: numberOfNewListsings
});
const datetime = date => {
return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()} ${date.getHours()}:${date.getMinutes()}`;
};
exports.setLastScrape = (serviceName, numberOfNewListsings) => {
const d = new Date();
const dt = datetime(d);
stats.lastScrape[serviceName] = d.toString();
if (numberOfNewListsings > 0) {
stats.foundScrapes[dt] = stats.foundScrapes[dt] || {};
stats.foundScrapes[dt][serviceName] = numberOfNewListsings;
}
};