Verified Commit e88be150 authored by Éloïs's avatar Éloïs

[enh] add url csvCalculatorsRank

parent 7b1f06df
Pipeline #715 passed with stage
in 21 seconds
"use strict";
const co = require('co');
const os = require('os');
const fs = require('fs');
const webserver = require(__dirname + '/webserver.js');
const timestampToDatetime = require(__dirname + '/timestampToDatetime.js');
......@@ -14,6 +16,13 @@ module.exports = (duniterServer, host, port, appParente, program) => co(function
var x = new Date();
var offset = -x.getTimezoneOffset();
//timestampToDatetime(1000000, true, offset);
// Get monitDatasPath
const mdb = program.mdb || "duniter_default";
const monitDatasPath = os.homedir() + "/.config/duniter/" + mdb + "/currency-monit/";
// Create monitDatasPath
if (!fs.existsSync(monitDatasPath)) { fs.mkdirSync(monitDatasPath) }
// Define cache
var cache = {
......@@ -39,7 +48,7 @@ module.exports = (duniterServer, host, port, appParente, program) => co(function
console.log("module currency-monit started");
// Specialized node's UI
let httpServer = webserver(host, port, appParente, duniterServer, offset, cache);
let httpServer = webserver(host, port, appParente, duniterServer, monitDatasPath, offset, cache);
yield httpServer.openConnection();
})
......
......@@ -13,7 +13,7 @@ const bodyParser = require('body-parser');
const routes = require(__dirname + '/../routes');
const tpl = require(__dirname + '/tplit.js');
module.exports = (host, port, appParente, duniterServer, offset, cache) => {
module.exports = (host, port, appParente, duniterServer, monitDatasPath, offset, cache) => {
var app = express();
......@@ -31,6 +31,7 @@ module.exports = (host, port, appParente, duniterServer, offset, cache) => {
app.set('view engine', 'html') // register the template engine
app.locals.duniterServer = duniterServer
app.locals.monitDatasPath = monitDatasPath
app.locals.currencyName = duniterServer.conf.currency
app.locals.offset = offset
app.locals.cache = cache
......@@ -39,6 +40,22 @@ module.exports = (host, port, appParente, duniterServer, offset, cache) => {
app.locals.HTML_TOR_HEAD = fs.readFileSync(__dirname + '/../views/TOR_HEAD.html', 'utf-8')
app.use( routes )
/***************************************
* CSV des membres calculants
***************************************/
app.get('/csvCalculatorsRank', function(req, res) {
let files = fs.readdirSync(monitDatasPath + '/calculators_rank/')
let maxTimestamp = 0
for (let file of files) {
let fileTimestamp = parseInt(file.split('_')[2])
if (fileTimestamp > maxTimestamp) {
maxTimestamp = fileTimestamp
}
}
var file = monitDatasPath + '/calculators_rank/calculators_rank_' + maxTimestamp + '.csv';
res.download(file); // Set disposition and send it.
});
// Si l'on ne dispose pas d'un serveur web parent, lancer notre propre serveur web
if ( appParente == null )
......
"use strict";
const co = require('co')
const fs = require('fs')
const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime')
const colorScale = require(__dirname + '/../lib/colorScale')
const getLang = require(__dirname + '/../lib/getLang')
......@@ -12,7 +13,7 @@ var previousBlockchainTime= 0;
module.exports = (req, res, next) => co(function *() {
var { duniterServer } = req.app.locals
var { duniterServer, monitDatasPath } = req.app.locals
try {
// get GET parameters
......@@ -101,22 +102,41 @@ module.exports = (req, res, next) => co(function *() {
{
for (let m=0;m<tabBlockMembers.length;m++)
{
for (let n=0;n<9;n++)
{
tabCoreCountPerNode[m].push(0);
tabBlockCountPerNode[m].push(0);
if (data == 'meanNonce') { tabDataPerNode[m].push(0); }
}
for (let n=0;n<9;n++)
{
tabCoreCountPerNode[m].push(0);
tabBlockCountPerNode[m].push(0);
if (data == 'meanNonce') { tabDataPerNode[m].push(0); }
}
}
}
// Open a write stream into a new calculators_rank file
let pathToCalculatorsRankFile = monitDatasPath + 'calculators_rank/'
let calculatorsRankFilename = 'calculators_rank_' + Date.now() + '.csv'
if (!fs.existsSync(pathToCalculatorsRankFile)) { fs.mkdirSync(pathToCalculatorsRankFile) }
var ws = fs.createWriteStream(pathToCalculatorsRankFile + calculatorsRankFilename, {
flags: 'a',
encoding: 'utf8',
fd: null,
mode: 0o666,
autoClose: true
});
// Calculate the sum of blocks and their nonce and number of core
let calculatorsCount = 1;
for (let b=begin;b<blockchain.length;b++)
{
for (let m=0;m<tabBlockMembers.length;m++)
{
if (tabBlockMembers[m].pubkey == blockchain[b].issuer)
{
if (tabBlockMembers[m].blockCount == 0) {
//console.log("%s, %s, #%s, %s", calculatorsCount, tabBlockMembers[m].uid, b, timestampToDatetime(blockchain[b].medianTime));
ws.write(calculatorsCount + ', ' + tabBlockMembers[m].uid + ', #' + b + ', ' + timestampToDatetime(blockchain[b].medianTime) + '\r\n');
calculatorsCount++;
}
tabBlockMembers[m].blockCount++;
let nonce = parseInt((blockchain[b].nonce).toString().substr(3));
if (data == 'meanNonce') { tabBlockMembers[m].data += nonce; }
......@@ -135,6 +155,24 @@ module.exports = (req, res, next) => co(function *() {
}
}
}
// Close write stream into calculators_rank file
ws.end();
// Remove oldest calculators_rank file
let files = fs.readdirSync(pathToCalculatorsRankFile);
if (files.length > 10) {
let minTimestamp = parseInt(Date.now())
for (let file of files) {
let fileTimestamp = parseInt(file.split('_')[2])
if (fileTimestamp < minTimestamp) {
minTimestamp = fileTimestamp;
}
}
fs.unlink(pathToCalculatorsRankFile + 'calculators_rank_' + minTimestamp + '.csv',function(err){
if(err) return console.log(err);
});
}
// Delete non-significant nodes
// A node is considered as significant if its blockCount represents more than 3 % of the total member blockCount
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment